Skip to content

Commit

Permalink
feat(base): Consider knocked members to be part of the room for displ…
Browse files Browse the repository at this point in the history
…ay name disambiguation
  • Loading branch information
poljar committed Nov 7, 2024
1 parent 90d1b68 commit 3932a26
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions crates/matrix-sdk-base/src/store/ambiguity_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ use crate::{
store::StateStoreExt,
};

#[derive(Debug)]
pub(crate) struct AmbiguityCache {
pub store: Arc<DynStateStore>,
pub cache: BTreeMap<OwnedRoomId, BTreeMap<String, BTreeSet<OwnedUserId>>>,
pub changes: BTreeMap<OwnedRoomId, BTreeMap<OwnedEventId, AmbiguityChange>>,
}

/// A map of users that use a certain display name.
#[derive(Debug, Clone)]
struct DisplayNameUsers {
Expand Down Expand Up @@ -81,6 +74,18 @@ impl DisplayNameUsers {
}
}

fn is_member_active(membership: &MembershipState) -> bool {
use MembershipState::*;
matches!(membership, Join | Invite | Knock)
}

#[derive(Debug)]
pub(crate) struct AmbiguityCache {
pub store: Arc<DynStateStore>,
pub cache: BTreeMap<OwnedRoomId, BTreeMap<String, BTreeSet<OwnedUserId>>>,
pub changes: BTreeMap<OwnedRoomId, BTreeMap<OwnedEventId, AmbiguityChange>>,
}

impl AmbiguityCache {
/// Create a new [`AmbiguityCache`] backed by the given state store.
pub fn new(store: Arc<DynStateStore>) -> Self {
Expand Down Expand Up @@ -172,8 +177,6 @@ impl AmbiguityCache {
room_id: &RoomId,
new_event: &SyncRoomMemberEvent,
) -> Result<Option<String>> {
use MembershipState::*;

let user_id = new_event.state_key();

let old_event = if let Some(m) = changes
Expand All @@ -188,7 +191,7 @@ impl AmbiguityCache {

let Some(Ok(old_event)) = old_event.map(|r| r.deserialize()) else { return Ok(None) };

if matches!(old_event.membership(), Join | Invite) {
if is_member_active(old_event.membership()) {
let display_name = if let Some(d) = changes
.profiles
.get(room_id)
Expand Down Expand Up @@ -248,8 +251,6 @@ impl AmbiguityCache {
room_id: &RoomId,
member_event: &SyncRoomMemberEvent,
) -> Result<(Option<DisplayNameUsers>, Option<DisplayNameUsers>)> {
use MembershipState::*;

let old_display_name = self.get_old_display_name(changes, room_id, member_event).await?;

let old_map = if let Some(old_name) = old_display_name.as_deref() {
Expand All @@ -258,7 +259,7 @@ impl AmbiguityCache {
None
};

let new_map = if matches!(member_event.membership(), Join | Invite) {
let new_map = if is_member_active(member_event.membership()) {
let new = member_event
.as_original()
.and_then(|ev| ev.content.displayname.as_deref())
Expand Down

0 comments on commit 3932a26

Please sign in to comment.