matrix_sdk_base: save room states after successfully parsed account events

master
Dominique Martinet 2020-10-08 09:06:27 +02:00
parent d858940342
commit 2602c36ad0
1 changed files with 37 additions and 17 deletions

View File

@ -698,19 +698,20 @@ impl BaseClient {
/// Handle a m.direct event, updating rooms states if necessary.
///
/// Returns true if any room changed, false otherwise.
pub(crate) async fn handle_direct(&self, event: &DirectEvent) -> bool {
let mut update = false;
pub(crate) async fn handle_direct(&self, event: &DirectEvent) -> Vec<Arc<RwLock<Room>>> {
let mut updated_rooms = vec![];
for (user_id, rooms) in event.content.iter() {
for room_id in rooms.iter() {
if let Some(room) = self.get_joined_room(room_id).await {
let mut room = room.write().await;
if room.handle_direct(user_id) {
update = true;
if let Some(room) = &self.get_joined_room(room_id).await {
let mut room_locked = room.write().await;
if room_locked.handle_direct(user_id) {
updated_rooms.push(room.to_owned());
}
}
}
}
update
updated_rooms
}
/// Receive a timeline event for a joined room and update the client state.
@ -893,7 +894,8 @@ impl BaseClient {
}
}
/// Receive an account data event from a sync response and updates the client state.
/// Receive an account data event associated to a room from a sync
/// response and updates the client state.
///
/// Returns true if the state of the `Room` has changed, false otherwise.
///
@ -902,19 +904,32 @@ impl BaseClient {
/// * `room_id` - The unique id of the room the event belongs to.
///
/// * `event` - The presence event for a specified room member.
pub async fn receive_account_data_event(
&self,
_: Option<&RoomId>,
event: &AnyBasicEvent,
) -> bool {
pub async fn receive_room_account_data_event(&self, _: &RoomId, event: &AnyBasicEvent) -> bool {
match event {
AnyBasicEvent::IgnoredUserList(event) => self.handle_ignored_users(event).await,
AnyBasicEvent::PushRules(event) => self.handle_push_rules(event).await,
AnyBasicEvent::Direct(event) => self.handle_direct(event).await,
_ => false,
}
}
/// Receive an account data event from a sync response and updates
/// the client state.
///
/// Returns true if the state of any room has changed, false otherwise.
///
/// # Arguments
///
/// * `event` - The presence event for a specified room member.
pub async fn receive_account_data_event(
&self,
event: &AnyBasicEvent,
) -> Vec<Arc<RwLock<Room>>> {
match event {
AnyBasicEvent::Direct(event) => self.handle_direct(event).await,
_ => vec![],
}
}
/// Receive an ephemeral event from a sync response and updates the client state.
///
/// Returns true if the state of the `Room` has changed, false otherwise.
@ -1076,7 +1091,7 @@ impl BaseClient {
// FIXME: receive_* and emit_* methods shouldn't be called in parallel. We
// should only pass events to receive_* methods and then let *them* emit.
if let Ok(e) = account_data.deserialize() {
if self.receive_account_data_event(Some(&room_id), &e).await {
if self.receive_room_account_data_event(&room_id, &e).await {
updated = true;
}
self.emit_account_data_event(room_id, &e, RoomStateType::Joined)
@ -1201,13 +1216,18 @@ impl BaseClient {
{
// FIXME: emit_account_data_event assumes a room is given
if let Ok(e) = account_data.deserialize() {
if self.receive_account_data_event(None, &e).await {
for room in self.receive_account_data_event(&e).await {
if let Some(store) = self.state_store.read().await.as_ref() {
// FIXME: currently only operate on Joined rooms
store
.store_room_state(RoomState::Joined(room.read().await.deref()))
.await?;
}
updated = true;
}
}
}
}
// FIXME store all rooms if updated?
Ok(updated)
}