session: Move room encryption event handler to Room

This commit is contained in:
Kévin Commaille 2023-12-21 23:54:06 +01:00
parent ad9deced91
commit deaa6e6166
No known key found for this signature in database
GPG Key ID: 29A48C1F03620416
2 changed files with 30 additions and 36 deletions

View File

@ -26,6 +26,7 @@ use ruma::{
receipt::{ReceiptEventContent, ReceiptType},
relation::Annotation,
room::{
encryption::SyncRoomEncryptionEvent,
join_rules::JoinRule,
power_levels::{PowerLevelAction, RoomPowerLevelsEventContent},
},
@ -296,6 +297,7 @@ impl Room {
self.set_up_receipts();
self.set_up_typing();
self.init_timeline();
self.set_up_is_encrypted();
spawn!(
glib::Priority::DEFAULT_IDLE,
@ -338,13 +340,6 @@ impl Room {
obj.init_power_levels().await;
})
);
spawn!(
glib::Priority::DEFAULT_IDLE,
clone!(@weak self as obj => async move {
obj.load_is_encrypted().await;
})
);
}
fn init_timeline(&self) {
@ -1500,7 +1495,7 @@ impl Room {
}
/// Set whether this room is encrypted.
pub fn set_is_encrypted(&self, is_encrypted: bool) {
fn set_is_encrypted(&self, is_encrypted: bool) {
let was_encrypted = self.encrypted();
if was_encrypted == is_encrypted {
return;
@ -1520,6 +1515,31 @@ impl Room {
}));
}
/// Listen to changes in room encryption.
fn set_up_is_encrypted(&self) {
let matrix_room = self.matrix_room();
let obj_weak = glib::SendWeakRef::from(self.downgrade());
matrix_room.add_event_handler(move |_: SyncRoomEncryptionEvent| {
let obj_weak = obj_weak.clone();
async move {
let ctx = glib::MainContext::default();
ctx.spawn(async move {
if let Some(obj) = obj_weak.upgrade() {
obj.set_is_encrypted(true);
}
});
}
});
spawn!(
glib::Priority::DEFAULT_IDLE,
clone!(@weak self as obj => async move {
obj.load_is_encrypted().await;
})
);
}
/// Load whether the room is encrypted from the SDK.
async fn load_is_encrypted(&self) {
let matrix_room = self.matrix_room().clone();

View File

@ -8,10 +8,7 @@ use gtk::{
prelude::*,
subclass::prelude::*,
};
use matrix_sdk::{
config::SyncSettings, matrix_auth::MatrixSession, room::Room as MatrixRoom, sync::SyncResponse,
Client,
};
use matrix_sdk::{config::SyncSettings, matrix_auth::MatrixSession, sync::SyncResponse, Client};
use ruma::{
api::client::{
error::ErrorKind,
@ -19,10 +16,7 @@ use ruma::{
session::logout,
},
assign,
events::{
direct::DirectEventContent, room::encryption::SyncRoomEncryptionEvent,
GlobalAccountDataEvent,
},
events::{direct::DirectEventContent, GlobalAccountDataEvent},
};
use tokio::task::JoinHandle;
use tracing::{debug, error};
@ -219,7 +213,6 @@ impl Session {
self.room_list().load().await;
self.setup_direct_room_handler();
self.setup_room_encrypted_changes();
self.set_state(SessionState::InitialSync);
self.sync();
@ -512,23 +505,4 @@ impl Session {
},
);
}
fn setup_room_encrypted_changes(&self) {
let session_weak = glib::SendWeakRef::from(self.downgrade());
self.client().add_event_handler(
move |_: SyncRoomEncryptionEvent, matrix_room: MatrixRoom| {
let session_weak = session_weak.clone();
async move {
let ctx = glib::MainContext::default();
ctx.spawn(async move {
if let Some(session) = session_weak.upgrade() {
if let Some(room) = session.room_list().get(matrix_room.room_id()) {
room.set_is_encrypted(true);
}
}
});
}
},
);
}
}