matrix_sdk_base: room: add direct_target field

Rooms marked as "direct" are associated a user_id in "m.direct" events.
Clients could want to handle these separately
master
Dominique Martinet 2020-10-07 19:02:13 +02:00
parent 7d023ebdb9
commit 883183324f
4 changed files with 41 additions and 1 deletions

View File

@ -27,7 +27,7 @@ use matrix_sdk_common::locks::Mutex;
use matrix_sdk_common::{
api::r0 as api,
events::{
ignored_user_list::IgnoredUserListEvent, push_rules::PushRulesEvent,
direct::DirectEvent, ignored_user_list::IgnoredUserListEvent, push_rules::PushRulesEvent,
room::member::MemberEventContent, AnyBasicEvent, AnyStrippedStateEvent,
AnySyncEphemeralRoomEvent, AnySyncMessageEvent, AnySyncRoomEvent, AnySyncStateEvent,
},
@ -695,6 +695,24 @@ 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;
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;
}
}
}
}
update
}
/// Receive a timeline event for a joined room and update the client state.
///
/// Returns a bool, true when the `Room` state has been updated.
@ -888,6 +906,7 @@ impl BaseClient {
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,
}
}

View File

@ -206,6 +206,7 @@ mod test {
},
"own_user_id": "@example:example.com",
"creator": null,
"direct_target": null,
"joined_members": {},
"invited_members": {},
"messages": [ msg ],
@ -253,6 +254,7 @@ mod test {
},
"own_user_id": "@example:example.com",
"creator": null,
"direct_target": null,
"joined_members": {},
"invited_members": {},
"messages": [ msg ],

View File

@ -190,6 +190,8 @@ pub struct Room {
pub own_user_id: UserId,
/// The mxid of the room creator.
pub creator: Option<UserId>,
/// The mxid of the "direct" target if any
pub direct_target: Option<UserId>,
// TODO: Track banned members, e.g. for /unban support?
/// The map of invited room members.
pub invited_members: HashMap<UserId, RoomMember>,
@ -312,6 +314,7 @@ impl Room {
room_name: RoomName::default(),
own_user_id: own_user_id.clone(),
creator: None,
direct_target: None,
invited_members: HashMap::new(),
joined_members: HashMap::new(),
#[cfg(feature = "messages")]
@ -799,6 +802,20 @@ impl Room {
true
}
/// Handle setting direct attribute as part of a m.direct event,
/// updating the room if necessary
///
/// Returns true if the direct_target changed, false otherwise.
pub fn handle_direct(&mut self, user_id: &UserId) -> bool {
if let Some(old_target) = &self.direct_target {
if old_target == user_id {
return false;
}
}
self.direct_target = Some(user_id.clone());
true
}
fn handle_encryption_event(&mut self, event: &SyncStateEvent<EncryptionEventContent>) -> bool {
self.encrypted = Some(event.into());
true

View File

@ -160,6 +160,7 @@ mod test {
},
"own_user_id": "@example:example.com",
"creator": null,
"direct_target": null,
"joined_members": {},
"invited_members": {},
"typing_users": [],
@ -188,6 +189,7 @@ mod test {
},
"own_user_id": "@example:example.com",
"creator": null,
"direct_target": null,
"joined_members": {},
"invited_members": {},
"messages": [],