Skip to content

Commit

Permalink
simplify v3 sync presence collecting
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Volk <[email protected]>
  • Loading branch information
jevolk committed Jan 31, 2025
1 parent c5926c9 commit 3987e2f
Showing 1 changed file with 10 additions and 45 deletions.
55 changes: 10 additions & 45 deletions src/api/client/sync/v3.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
cmp::{self},
collections::{hash_map::Entry, BTreeMap, HashMap, HashSet},
collections::{BTreeMap, HashMap, HashSet},
time::Duration,
};

Expand Down Expand Up @@ -45,7 +45,7 @@ use ruma::{
uiaa::UiaaResponse,
},
events::{
presence::PresenceEvent,
presence::{PresenceEvent, PresenceEventContent},
room::member::{MembershipState, RoomMemberEventContent},
AnyRawAccountDataEvent, AnySyncEphemeralRoomEvent, StateEventType,
TimelineEventType::*,
Expand All @@ -68,7 +68,7 @@ struct StateChanges {
left_encrypted_users: HashSet<OwnedUserId>,
}

type PresenceUpdates = HashMap<OwnedUserId, PresenceEvent>;
type PresenceUpdates = HashMap<OwnedUserId, PresenceEventContent>;

/// # `GET /_matrix/client/r0/sync`
///
Expand Down Expand Up @@ -353,9 +353,11 @@ pub(crate) async fn build_sync_events(
next_batch: next_batch.to_string(),
presence: Presence {
events: presence_updates
.unwrap_or_default()
.into_values()
.map(|v| Raw::new(&v).expect("PresenceEvent always serializes successfully"))
.into_iter()
.flat_map(IntoIterator::into_iter)
.map(|(sender, content)| PresenceEvent { content, sender })
.map(|ref event| Raw::new(event))
.filter_map(Result::ok)
.collect(),
},
rooms: Rooms {
Expand Down Expand Up @@ -392,45 +394,8 @@ async fn process_presence_updates(
.map_ok(move |event| (user_id, event))
.ok()
})
.ready_fold(PresenceUpdates::new(), |mut updates, (user_id, event)| {
match updates.entry(user_id.into()) {
| Entry::Vacant(slot) => {
let mut new_event = event;
new_event.content.last_active_ago = match new_event.content.currently_active {
| Some(true) => None,
| _ => new_event.content.last_active_ago,
};

slot.insert(new_event);
},
| Entry::Occupied(mut slot) => {
let curr_event = slot.get_mut();
let curr_content = &mut curr_event.content;
let new_content = event.content;

// Update existing presence event with more info
curr_content.presence = new_content.presence;
curr_content.status_msg = new_content
.status_msg
.or_else(|| curr_content.status_msg.take());
curr_content.displayname = new_content
.displayname
.or_else(|| curr_content.displayname.take());
curr_content.avatar_url = new_content
.avatar_url
.or_else(|| curr_content.avatar_url.take());
curr_content.currently_active = new_content
.currently_active
.or(curr_content.currently_active);
curr_content.last_active_ago = match curr_content.currently_active {
| Some(true) => None,
| _ => new_content.last_active_ago.or(curr_content.last_active_ago),
};
},
};

updates
})
.map(|(user_id, event)| (user_id.to_owned(), event.content))
.collect()
.await
}

Expand Down

0 comments on commit 3987e2f

Please sign in to comment.