2025-10-27 17:24:02 -04:00
|
|
|
use std::collections::{BTreeMap, HashMap};
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-12-14 21:58:01 -05:00
|
|
|
use conduwuit::{
|
2025-10-22 09:31:08 -04:00
|
|
|
Result, at, err, extract_variant, is_equal_to,
|
2025-04-04 03:30:13 +00:00
|
|
|
matrix::{
|
|
|
|
|
Event,
|
2025-10-22 09:31:08 -04:00
|
|
|
pdu::{PduCount, PduEvent},
|
2025-04-04 03:30:13 +00:00
|
|
|
},
|
2025-01-25 07:18:33 +00:00
|
|
|
result::FlatOk,
|
2024-12-06 12:45:20 +00:00
|
|
|
utils::{
|
2025-10-22 09:31:08 -04:00
|
|
|
BoolExt, IterStream, ReadyExt, TryFutureExtExt,
|
2024-12-06 12:45:20 +00:00
|
|
|
math::ruma_from_u64,
|
2025-10-27 17:24:02 -04:00
|
|
|
stream::{Tools, WidebandExt},
|
2025-01-25 07:18:33 +00:00
|
|
|
},
|
2024-12-06 12:45:20 +00:00
|
|
|
};
|
2025-10-28 11:32:12 -04:00
|
|
|
use conduwuit_service::Services;
|
2024-12-06 12:45:20 +00:00
|
|
|
use futures::{
|
2025-10-20 17:59:51 -04:00
|
|
|
FutureExt, StreamExt, TryFutureExt,
|
2025-10-24 14:57:31 -04:00
|
|
|
future::{OptionFuture, join, join3, join4, try_join},
|
2024-07-07 06:17:58 +00:00
|
|
|
};
|
2020-07-30 18:14:47 +02:00
|
|
|
use ruma::{
|
2025-10-27 17:24:02 -04:00
|
|
|
OwnedRoomId, OwnedUserId, RoomId, UserId,
|
2025-10-22 09:31:08 -04:00
|
|
|
api::client::sync::sync_events::{
|
|
|
|
|
UnreadNotificationsCount,
|
|
|
|
|
v3::{Ephemeral, JoinedRoom, RoomAccountData, RoomSummary, State as RoomState, Timeline},
|
2024-08-30 10:31:08 +02:00
|
|
|
},
|
|
|
|
|
events::{
|
2024-11-04 22:38:12 +00:00
|
|
|
AnyRawAccountDataEvent, AnySyncEphemeralRoomEvent, StateEventType,
|
2024-10-16 05:32:27 +00:00
|
|
|
TimelineEventType::*,
|
2025-02-23 01:17:45 -05:00
|
|
|
room::member::{MembershipState, RoomMemberEventContent},
|
2024-08-30 10:31:08 +02:00
|
|
|
},
|
|
|
|
|
serde::Raw,
|
2025-02-23 01:17:45 -05:00
|
|
|
uint,
|
2020-07-30 18:14:47 +02:00
|
|
|
};
|
2021-06-30 09:52:01 +02:00
|
|
|
|
2024-10-16 05:32:27 +00:00
|
|
|
use super::{load_timeline, share_encrypted_room};
|
2025-10-22 09:31:08 -04:00
|
|
|
use crate::client::{
|
2025-10-27 17:24:02 -04:00
|
|
|
ignored_filter,
|
|
|
|
|
sync::v3::{
|
2025-10-28 11:32:12 -04:00
|
|
|
DeviceListUpdates, SyncContext, prepare_lazily_loaded_members,
|
2025-11-05 10:04:43 -05:00
|
|
|
state::{build_state_incremental, build_state_initial},
|
2025-10-27 17:24:02 -04:00
|
|
|
},
|
2025-09-21 17:03:40 +00:00
|
|
|
};
|
2024-12-06 12:45:20 +00:00
|
|
|
|
2025-10-24 14:57:31 -04:00
|
|
|
/// Generate the sync response for a room the user is joined to.
|
2024-12-08 03:00:09 +00:00
|
|
|
#[tracing::instrument(
|
|
|
|
|
name = "joined",
|
|
|
|
|
level = "debug",
|
|
|
|
|
skip_all,
|
|
|
|
|
fields(
|
|
|
|
|
room_id = ?room_id,
|
|
|
|
|
),
|
|
|
|
|
)]
|
2023-11-27 00:39:50 -05:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2025-10-22 09:31:08 -04:00
|
|
|
pub(super) async fn load_joined_room(
|
2024-12-15 00:05:47 -05:00
|
|
|
services: &Services,
|
2025-10-24 14:57:31 -04:00
|
|
|
sync_context: SyncContext<'_>,
|
|
|
|
|
ref room_id: OwnedRoomId,
|
|
|
|
|
) -> Result<(JoinedRoom, DeviceListUpdates)> {
|
|
|
|
|
let SyncContext {
|
2025-11-05 10:01:12 -05:00
|
|
|
syncing_user,
|
|
|
|
|
last_sync_end_count,
|
|
|
|
|
current_count,
|
2025-10-20 17:59:51 -04:00
|
|
|
full_state,
|
2025-10-27 17:24:02 -04:00
|
|
|
..
|
2025-10-24 14:57:31 -04:00
|
|
|
} = sync_context;
|
2025-10-27 09:32:28 -04:00
|
|
|
let mut device_list_updates = DeviceListUpdates::new();
|
2024-12-08 03:00:09 +00:00
|
|
|
|
2025-11-05 10:01:12 -05:00
|
|
|
// the room state as of `next_batch`.
|
2024-12-08 03:00:09 +00:00
|
|
|
let current_shortstatehash = services
|
|
|
|
|
.rooms
|
|
|
|
|
.state
|
|
|
|
|
.get_room_shortstatehash(room_id)
|
|
|
|
|
.map_err(|_| err!(Database(error!("Room {room_id} has no state"))));
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2025-10-27 09:32:28 -04:00
|
|
|
// the room state as of the end of the last sync.
|
|
|
|
|
// this will be None if we are doing an initial sync or if we just joined this
|
|
|
|
|
// room.
|
2025-11-05 10:01:12 -05:00
|
|
|
let last_sync_end_shortstatehash =
|
|
|
|
|
OptionFuture::from(last_sync_end_count.map(|last_sync_end_count| {
|
|
|
|
|
services
|
|
|
|
|
.rooms
|
|
|
|
|
.user
|
|
|
|
|
.get_token_shortstatehash(room_id, last_sync_end_count)
|
|
|
|
|
.ok()
|
|
|
|
|
}))
|
|
|
|
|
.map(Option::flatten)
|
|
|
|
|
.map(Ok);
|
2025-10-24 14:57:31 -04:00
|
|
|
|
2025-11-05 10:01:12 -05:00
|
|
|
let (current_shortstatehash, last_sync_end_shortstatehash) =
|
|
|
|
|
try_join(current_shortstatehash, last_sync_end_shortstatehash).await?;
|
|
|
|
|
|
|
|
|
|
// load recent timeline events.
|
2024-12-08 03:00:09 +00:00
|
|
|
|
2024-12-15 00:05:47 -05:00
|
|
|
let timeline = load_timeline(
|
|
|
|
|
services,
|
2025-11-05 10:01:12 -05:00
|
|
|
syncing_user,
|
2024-12-15 00:05:47 -05:00
|
|
|
room_id,
|
2025-11-05 10:01:12 -05:00
|
|
|
last_sync_end_count.map(PduCount::Normal),
|
|
|
|
|
Some(PduCount::Normal(current_count)),
|
2025-10-28 09:15:49 -04:00
|
|
|
services.config.incremental_sync_max_timeline_size,
|
2024-12-15 00:05:47 -05:00
|
|
|
);
|
2024-12-08 03:00:09 +00:00
|
|
|
|
2025-01-25 07:18:33 +00:00
|
|
|
let receipt_events = services
|
|
|
|
|
.rooms
|
|
|
|
|
.read_receipt
|
2025-11-05 10:01:12 -05:00
|
|
|
.readreceipts_since(room_id, last_sync_end_count)
|
2025-01-25 07:18:33 +00:00
|
|
|
.filter_map(|(read_user, _, edu)| async move {
|
|
|
|
|
services
|
|
|
|
|
.users
|
2025-11-05 10:01:12 -05:00
|
|
|
.user_is_ignored(read_user, syncing_user)
|
2025-01-25 07:18:33 +00:00
|
|
|
.await
|
|
|
|
|
.or_some((read_user.to_owned(), edu))
|
|
|
|
|
})
|
|
|
|
|
.collect::<HashMap<OwnedUserId, Raw<AnySyncEphemeralRoomEvent>>>()
|
|
|
|
|
.map(Ok);
|
|
|
|
|
|
2025-10-24 14:57:31 -04:00
|
|
|
let (timeline, receipt_events) = try_join(timeline, receipt_events).boxed().await?;
|
2025-02-02 07:40:08 +00:00
|
|
|
|
2025-10-24 14:57:31 -04:00
|
|
|
// the state at the beginning of the timeline
|
2025-10-20 17:59:51 -04:00
|
|
|
let timeline_start_shortstatehash = async {
|
2025-10-24 14:57:31 -04:00
|
|
|
if let Some((_, pdu)) = timeline.pdus.front() {
|
2025-10-20 17:59:51 -04:00
|
|
|
if let Ok(shortstatehash) = services
|
2025-02-02 07:40:08 +00:00
|
|
|
.rooms
|
2025-10-20 17:59:51 -04:00
|
|
|
.state_accessor
|
|
|
|
|
.pdu_shortstatehash(&pdu.event_id)
|
|
|
|
|
.await
|
|
|
|
|
{
|
|
|
|
|
return shortstatehash;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-22 09:03:25 -04:00
|
|
|
current_shortstatehash
|
2025-10-20 17:59:51 -04:00
|
|
|
};
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2025-10-24 14:57:31 -04:00
|
|
|
let last_notification_read: OptionFuture<_> = timeline
|
|
|
|
|
.pdus
|
2024-12-08 03:00:09 +00:00
|
|
|
.is_empty()
|
|
|
|
|
.then(|| {
|
|
|
|
|
services
|
|
|
|
|
.rooms
|
|
|
|
|
.user
|
2025-11-05 10:01:12 -05:00
|
|
|
.last_notification_read(syncing_user, room_id)
|
2024-12-08 03:00:09 +00:00
|
|
|
})
|
|
|
|
|
.into();
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2025-10-27 09:32:28 -04:00
|
|
|
// the syncing user's membership event during the last sync.
|
|
|
|
|
// this will be None if `previous_sync_end_shortstatehash` is None.
|
2025-11-05 10:01:12 -05:00
|
|
|
let membership_during_previous_sync: OptionFuture<_> = last_sync_end_shortstatehash
|
2025-10-24 14:57:31 -04:00
|
|
|
.map(|shortstatehash| {
|
2025-01-25 07:18:33 +00:00
|
|
|
services
|
|
|
|
|
.rooms
|
|
|
|
|
.state_accessor
|
2025-10-24 14:57:31 -04:00
|
|
|
.state_get_content(
|
|
|
|
|
shortstatehash,
|
|
|
|
|
&StateEventType::RoomMember,
|
2025-11-05 10:01:12 -05:00
|
|
|
syncing_user.as_str(),
|
2025-10-24 14:57:31 -04:00
|
|
|
)
|
2025-01-25 07:18:33 +00:00
|
|
|
.ok()
|
|
|
|
|
})
|
|
|
|
|
.into();
|
|
|
|
|
|
2025-10-20 17:59:51 -04:00
|
|
|
let is_encrypted_room = services
|
|
|
|
|
.rooms
|
|
|
|
|
.state_accessor
|
|
|
|
|
.state_get(current_shortstatehash, &StateEventType::RoomEncryption, "")
|
|
|
|
|
.is_ok();
|
|
|
|
|
|
|
|
|
|
let (
|
|
|
|
|
last_notification_read,
|
2025-10-24 14:57:31 -04:00
|
|
|
membership_during_previous_sync,
|
2025-10-20 17:59:51 -04:00
|
|
|
timeline_start_shortstatehash,
|
|
|
|
|
is_encrypted_room,
|
|
|
|
|
) = join4(
|
|
|
|
|
last_notification_read,
|
2025-10-24 14:57:31 -04:00
|
|
|
membership_during_previous_sync,
|
2025-10-20 17:59:51 -04:00
|
|
|
timeline_start_shortstatehash,
|
|
|
|
|
is_encrypted_room,
|
|
|
|
|
)
|
|
|
|
|
.await;
|
2025-02-02 07:40:08 +00:00
|
|
|
|
2025-10-24 14:57:31 -04:00
|
|
|
// TODO: If the requesting user got state-reset out of the room, this
|
|
|
|
|
// will be `true` when it shouldn't be. this function should never be called
|
|
|
|
|
// in that situation, but it may be if the membership cache didn't get updated.
|
|
|
|
|
// the root cause of this needs to be addressed
|
|
|
|
|
let joined_since_last_sync = membership_during_previous_sync.flatten().is_none_or(
|
|
|
|
|
|content: RoomMemberEventContent| content.membership != MembershipState::Join,
|
|
|
|
|
);
|
2025-01-25 07:18:33 +00:00
|
|
|
|
2025-10-24 14:57:31 -04:00
|
|
|
// the user IDs of members whose membership needs to be sent to the client, if
|
|
|
|
|
// lazy-loading is enabled.
|
2025-10-27 17:24:02 -04:00
|
|
|
let lazily_loaded_members =
|
2025-10-28 11:32:12 -04:00
|
|
|
prepare_lazily_loaded_members(services, sync_context, room_id, timeline.senders()).await;
|
2025-10-24 14:57:31 -04:00
|
|
|
|
2025-10-27 09:32:28 -04:00
|
|
|
/*
|
|
|
|
|
compute the state delta between the previous sync and this sync. if this is an initial sync
|
2025-11-05 10:04:43 -05:00
|
|
|
*or* we just joined this room, `build_state_initial` will be used, otherwise `build_state_incremental`
|
2025-10-27 09:32:28 -04:00
|
|
|
will be used.
|
|
|
|
|
*/
|
2025-11-05 10:01:12 -05:00
|
|
|
let mut state_events = if let Some(last_sync_end_count) = last_sync_end_count
|
|
|
|
|
&& let Some(last_sync_end_shortstatehash) = last_sync_end_shortstatehash
|
2025-10-27 09:32:28 -04:00
|
|
|
&& !full_state
|
|
|
|
|
{
|
2025-11-05 10:04:43 -05:00
|
|
|
build_state_incremental(
|
2025-10-27 09:32:28 -04:00
|
|
|
services,
|
2025-11-05 10:01:12 -05:00
|
|
|
syncing_user,
|
2025-10-27 09:32:28 -04:00
|
|
|
room_id,
|
2025-11-05 10:01:12 -05:00
|
|
|
PduCount::Normal(last_sync_end_count),
|
|
|
|
|
last_sync_end_shortstatehash,
|
2025-10-27 09:32:28 -04:00
|
|
|
timeline_start_shortstatehash,
|
|
|
|
|
current_shortstatehash,
|
|
|
|
|
&timeline,
|
|
|
|
|
lazily_loaded_members.as_ref(),
|
|
|
|
|
)
|
|
|
|
|
.boxed()
|
|
|
|
|
.await?
|
|
|
|
|
} else {
|
2025-11-05 10:04:43 -05:00
|
|
|
build_state_initial(
|
2025-10-27 09:32:28 -04:00
|
|
|
services,
|
2025-11-05 10:01:12 -05:00
|
|
|
syncing_user,
|
2025-10-27 09:32:28 -04:00
|
|
|
timeline_start_shortstatehash,
|
|
|
|
|
lazily_loaded_members.as_ref(),
|
|
|
|
|
)
|
|
|
|
|
.boxed()
|
|
|
|
|
.await?
|
|
|
|
|
};
|
2025-10-20 17:59:51 -04:00
|
|
|
|
2025-10-27 09:32:28 -04:00
|
|
|
// for incremental syncs, calculate updates to E2EE device lists
|
2025-11-05 10:01:12 -05:00
|
|
|
if last_sync_end_count.is_some() && is_encrypted_room {
|
2025-11-05 10:04:43 -05:00
|
|
|
extend_device_list_updates(
|
2025-10-27 09:32:28 -04:00
|
|
|
services,
|
|
|
|
|
sync_context,
|
|
|
|
|
room_id,
|
|
|
|
|
&mut device_list_updates,
|
|
|
|
|
&state_events,
|
|
|
|
|
joined_since_last_sync,
|
|
|
|
|
)
|
|
|
|
|
.await;
|
|
|
|
|
}
|
2025-10-20 17:59:51 -04:00
|
|
|
|
|
|
|
|
// only compute room counts and heroes (aka the summary) if the room's members
|
|
|
|
|
// changed since the last sync
|
|
|
|
|
let (joined_member_count, invited_member_count, heroes) =
|
2025-11-05 10:01:12 -05:00
|
|
|
// calculate counts if any of the state events we're syncing are of type `m.room.member`
|
2025-10-20 17:59:51 -04:00
|
|
|
if state_events.iter().any(|event| event.kind == RoomMember) {
|
2025-11-05 10:04:43 -05:00
|
|
|
build_room_summary(services, room_id, syncing_user).await?
|
2025-10-20 17:59:51 -04:00
|
|
|
} else {
|
|
|
|
|
(None, None, None)
|
|
|
|
|
};
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2025-02-05 05:10:30 +00:00
|
|
|
let is_sender_membership = |pdu: &PduEvent| {
|
|
|
|
|
pdu.kind == StateEventType::RoomMember.into()
|
|
|
|
|
&& pdu
|
|
|
|
|
.state_key
|
|
|
|
|
.as_deref()
|
2025-11-05 10:01:12 -05:00
|
|
|
.is_some_and(is_equal_to!(syncing_user.as_str()))
|
2025-02-05 05:10:30 +00:00
|
|
|
};
|
|
|
|
|
|
2025-10-24 14:57:31 -04:00
|
|
|
// the membership event of the syncing user, if they joined since the last sync
|
|
|
|
|
let sender_join_membership_event: Option<_> = (joined_since_last_sync
|
|
|
|
|
&& timeline.pdus.is_empty())
|
|
|
|
|
.then(|| {
|
|
|
|
|
state_events
|
|
|
|
|
.iter()
|
|
|
|
|
.position(is_sender_membership)
|
|
|
|
|
.map(|pos| state_events.swap_remove(pos))
|
|
|
|
|
})
|
|
|
|
|
.flatten();
|
|
|
|
|
|
|
|
|
|
// the prev_batch token for the response
|
|
|
|
|
let prev_batch = timeline.pdus.front().map(at!(0)).or_else(|| {
|
|
|
|
|
sender_join_membership_event
|
|
|
|
|
.is_some()
|
2025-11-05 10:01:12 -05:00
|
|
|
.and(last_sync_end_count)
|
2025-10-24 14:57:31 -04:00
|
|
|
.map(Into::into)
|
|
|
|
|
});
|
2025-02-05 05:10:30 +00:00
|
|
|
|
2025-10-24 14:57:31 -04:00
|
|
|
let timeline_pdus = timeline
|
|
|
|
|
.pdus
|
2025-02-05 05:10:30 +00:00
|
|
|
.into_iter()
|
|
|
|
|
.stream()
|
2025-10-24 14:57:31 -04:00
|
|
|
// filter out ignored events from the timeline
|
2025-11-05 10:01:12 -05:00
|
|
|
.wide_filter_map(|item| ignored_filter(services, item, syncing_user))
|
2025-02-05 05:10:30 +00:00
|
|
|
.map(at!(1))
|
2025-10-24 14:57:31 -04:00
|
|
|
// if the syncing user just joined, add their membership event to the timeline
|
|
|
|
|
.chain(sender_join_membership_event.into_iter().stream())
|
2025-04-26 08:24:47 +00:00
|
|
|
.map(Event::into_format)
|
2025-02-05 05:10:30 +00:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
2024-12-08 03:00:09 +00:00
|
|
|
let account_data_events = services
|
|
|
|
|
.account_data
|
2025-11-05 10:01:12 -05:00
|
|
|
.changes_since(Some(room_id), syncing_user, last_sync_end_count, Some(current_count))
|
2024-12-08 03:00:09 +00:00
|
|
|
.ready_filter_map(|e| extract_variant!(e, AnyRawAccountDataEvent::Room))
|
|
|
|
|
.collect();
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2025-10-24 14:57:31 -04:00
|
|
|
/*
|
|
|
|
|
send notification counts if:
|
|
|
|
|
1. this is an initial sync
|
|
|
|
|
2. the user hasn't seen any notifications
|
|
|
|
|
3. the last notification the user saw has changed since the last sync
|
|
|
|
|
*/
|
|
|
|
|
let send_notification_counts = last_notification_read.is_none_or(|last_notification_read| {
|
2025-11-05 10:01:12 -05:00
|
|
|
last_sync_end_count
|
|
|
|
|
.is_none_or(|last_sync_end_count| last_notification_read > last_sync_end_count)
|
2025-10-24 14:57:31 -04:00
|
|
|
});
|
2025-01-25 07:18:33 +00:00
|
|
|
|
2024-12-08 03:00:09 +00:00
|
|
|
let notification_count: OptionFuture<_> = send_notification_counts
|
|
|
|
|
.then(|| {
|
|
|
|
|
services
|
|
|
|
|
.rooms
|
|
|
|
|
.user
|
2025-11-05 10:01:12 -05:00
|
|
|
.notification_count(syncing_user, room_id)
|
2024-12-08 03:00:09 +00:00
|
|
|
.map(TryInto::try_into)
|
|
|
|
|
.unwrap_or(uint!(0))
|
|
|
|
|
})
|
|
|
|
|
.into();
|
|
|
|
|
|
|
|
|
|
let highlight_count: OptionFuture<_> = send_notification_counts
|
|
|
|
|
.then(|| {
|
|
|
|
|
services
|
|
|
|
|
.rooms
|
|
|
|
|
.user
|
2025-11-05 10:01:12 -05:00
|
|
|
.highlight_count(syncing_user, room_id)
|
2024-12-08 03:00:09 +00:00
|
|
|
.map(TryInto::try_into)
|
|
|
|
|
.unwrap_or(uint!(0))
|
|
|
|
|
})
|
|
|
|
|
.into();
|
|
|
|
|
|
2025-02-02 07:40:08 +00:00
|
|
|
let typing_events = services
|
|
|
|
|
.rooms
|
|
|
|
|
.typing
|
|
|
|
|
.last_typing_update(room_id)
|
|
|
|
|
.and_then(|count| async move {
|
2025-11-05 10:01:12 -05:00
|
|
|
if last_sync_end_count.is_some_and(|last_sync_end_count| count <= last_sync_end_count)
|
|
|
|
|
{
|
2025-02-02 07:40:08 +00:00
|
|
|
return Ok(Vec::<Raw<AnySyncEphemeralRoomEvent>>::new());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let typings = services
|
|
|
|
|
.rooms
|
|
|
|
|
.typing
|
2025-11-05 10:01:12 -05:00
|
|
|
.typings_event_for_user(room_id, syncing_user)
|
2025-02-02 07:40:08 +00:00
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
Ok(vec![serde_json::from_str(&serde_json::to_string(&typings)?)?])
|
|
|
|
|
})
|
|
|
|
|
.unwrap_or(Vec::new());
|
|
|
|
|
|
2024-12-06 12:45:20 +00:00
|
|
|
let unread_notifications = join(notification_count, highlight_count);
|
2025-10-20 17:59:51 -04:00
|
|
|
let events = join3(timeline_pdus, account_data_events, typing_events);
|
|
|
|
|
let (unread_notifications, events) = join(unread_notifications, events).boxed().await;
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2025-01-25 07:18:33 +00:00
|
|
|
let (room_events, account_data_events, typing_events) = events;
|
2024-12-06 12:45:20 +00:00
|
|
|
let (notification_count, highlight_count) = unread_notifications;
|
2024-11-04 22:38:12 +00:00
|
|
|
|
2025-11-05 10:01:12 -05:00
|
|
|
let last_privateread_update = if let Some(last_sync_end_count) = last_sync_end_count {
|
2025-10-20 17:59:51 -04:00
|
|
|
services
|
|
|
|
|
.rooms
|
|
|
|
|
.read_receipt
|
2025-11-05 10:01:12 -05:00
|
|
|
.last_privateread_update(syncing_user, room_id)
|
|
|
|
|
.await > last_sync_end_count
|
2025-10-20 17:59:51 -04:00
|
|
|
} else {
|
|
|
|
|
true
|
|
|
|
|
};
|
2024-12-10 22:54:19 -05:00
|
|
|
|
|
|
|
|
let private_read_event = if last_privateread_update {
|
|
|
|
|
services
|
|
|
|
|
.rooms
|
|
|
|
|
.read_receipt
|
2025-11-05 10:01:12 -05:00
|
|
|
.private_read_get(room_id, syncing_user)
|
2024-12-10 22:54:19 -05:00
|
|
|
.await
|
|
|
|
|
.ok()
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
|
|
|
|
|
2024-12-08 03:00:09 +00:00
|
|
|
let edus: Vec<Raw<AnySyncEphemeralRoomEvent>> = receipt_events
|
|
|
|
|
.into_values()
|
|
|
|
|
.chain(typing_events.into_iter())
|
2024-12-10 22:54:19 -05:00
|
|
|
.chain(private_read_event.into_iter())
|
2024-12-08 03:00:09 +00:00
|
|
|
.collect();
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2025-10-24 14:57:31 -04:00
|
|
|
// save the room state at this sync to use during the next sync
|
2024-07-16 08:05:25 +00:00
|
|
|
services
|
2024-03-25 17:05:11 -04:00
|
|
|
.rooms
|
|
|
|
|
.user
|
2025-11-05 10:01:12 -05:00
|
|
|
.associate_token_shortstatehash(room_id, current_count, current_shortstatehash)
|
2024-08-08 17:18:30 +00:00
|
|
|
.await;
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-12-06 12:45:20 +00:00
|
|
|
let joined_room = JoinedRoom {
|
2024-12-15 00:05:47 -05:00
|
|
|
account_data: RoomAccountData { events: account_data_events },
|
2023-03-13 10:39:02 +01:00
|
|
|
summary: RoomSummary {
|
2024-07-07 06:17:58 +00:00
|
|
|
joined_member_count: joined_member_count.map(ruma_from_u64),
|
|
|
|
|
invited_member_count: invited_member_count.map(ruma_from_u64),
|
2024-12-06 12:45:20 +00:00
|
|
|
heroes: heroes
|
|
|
|
|
.into_iter()
|
|
|
|
|
.flatten()
|
|
|
|
|
.map(TryInto::try_into)
|
|
|
|
|
.filter_map(Result::ok)
|
|
|
|
|
.collect(),
|
2020-07-30 18:14:47 +02:00
|
|
|
},
|
2024-12-15 00:05:47 -05:00
|
|
|
unread_notifications: UnreadNotificationsCount { highlight_count, notification_count },
|
2023-03-13 10:39:02 +01:00
|
|
|
timeline: Timeline {
|
2025-10-27 17:25:21 -04:00
|
|
|
// mirror Synapse behavior by setting `limited` if the user joined since the last sync
|
|
|
|
|
limited: timeline.limited || joined_since_last_sync,
|
2025-02-05 05:10:30 +00:00
|
|
|
prev_batch: prev_batch.as_ref().map(ToString::to_string),
|
2023-03-13 10:39:02 +01:00
|
|
|
events: room_events,
|
|
|
|
|
},
|
2024-07-16 08:05:25 +00:00
|
|
|
state: RoomState {
|
2025-04-26 08:24:47 +00:00
|
|
|
events: state_events.into_iter().map(Event::into_format).collect(),
|
2023-03-13 10:39:02 +01:00
|
|
|
},
|
2024-12-15 00:05:47 -05:00
|
|
|
ephemeral: Ephemeral { events: edus },
|
2023-03-13 10:39:02 +01:00
|
|
|
unread_thread_notifications: BTreeMap::new(),
|
2024-12-06 12:45:20 +00:00
|
|
|
};
|
|
|
|
|
|
2025-10-20 17:59:51 -04:00
|
|
|
Ok((joined_room, device_list_updates))
|
2024-12-06 12:45:20 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-05 10:04:43 -05:00
|
|
|
async fn extend_device_list_updates(
|
2025-02-02 07:40:08 +00:00
|
|
|
services: &Services,
|
2025-11-05 10:01:12 -05:00
|
|
|
SyncContext {
|
|
|
|
|
syncing_user,
|
|
|
|
|
last_sync_end_count: since,
|
|
|
|
|
current_count,
|
|
|
|
|
..
|
|
|
|
|
}: SyncContext<'_>,
|
2025-10-24 14:57:31 -04:00
|
|
|
room_id: &RoomId,
|
|
|
|
|
device_list_updates: &mut DeviceListUpdates,
|
|
|
|
|
state_events: &Vec<PduEvent>,
|
|
|
|
|
joined_since_last_sync: bool,
|
|
|
|
|
) {
|
|
|
|
|
// add users with changed keys to the `changed` list
|
|
|
|
|
services
|
|
|
|
|
.users
|
2025-11-05 10:01:12 -05:00
|
|
|
.room_keys_changed(room_id, since, Some(current_count))
|
2025-10-24 14:57:31 -04:00
|
|
|
.map(at!(0))
|
|
|
|
|
.map(ToOwned::to_owned)
|
|
|
|
|
.ready_for_each(|user_id| {
|
|
|
|
|
device_list_updates.changed.insert(user_id);
|
|
|
|
|
})
|
|
|
|
|
.await;
|
2025-02-02 07:40:08 +00:00
|
|
|
|
2025-10-24 14:57:31 -04:00
|
|
|
// add users who now share encrypted rooms to `changed` and
|
|
|
|
|
// users who no longer share encrypted rooms to `left`
|
|
|
|
|
for state_event in state_events {
|
|
|
|
|
if state_event.kind == RoomMember {
|
|
|
|
|
let Some(content): Option<RoomMemberEventContent> = state_event.get_content().ok()
|
|
|
|
|
else {
|
|
|
|
|
continue;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let Some(user_id): Option<OwnedUserId> = state_event
|
|
|
|
|
.state_key
|
|
|
|
|
.as_ref()
|
|
|
|
|
.and_then(|key| key.parse().ok())
|
|
|
|
|
else {
|
|
|
|
|
continue;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
use MembershipState::*;
|
|
|
|
|
|
|
|
|
|
if matches!(content.membership, Leave | Join) {
|
|
|
|
|
let shares_encrypted_room =
|
2025-11-05 10:01:12 -05:00
|
|
|
share_encrypted_room(services, syncing_user, &user_id, Some(room_id))
|
2025-10-24 14:57:31 -04:00
|
|
|
.await;
|
|
|
|
|
match content.membership {
|
|
|
|
|
| Leave if !shares_encrypted_room => {
|
|
|
|
|
device_list_updates.left.insert(user_id);
|
|
|
|
|
},
|
|
|
|
|
| Join if joined_since_last_sync || shares_encrypted_room => {
|
|
|
|
|
device_list_updates.changed.insert(user_id);
|
|
|
|
|
},
|
|
|
|
|
| _ => (),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-02-02 07:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-05 10:04:43 -05:00
|
|
|
async fn build_room_summary(
|
2024-12-15 00:05:47 -05:00
|
|
|
services: &Services,
|
|
|
|
|
room_id: &RoomId,
|
2025-11-05 10:01:12 -05:00
|
|
|
syncing_user: &UserId,
|
2024-12-06 12:45:20 +00:00
|
|
|
) -> Result<(Option<u64>, Option<u64>, Option<Vec<OwnedUserId>>)> {
|
|
|
|
|
let joined_member_count = services
|
|
|
|
|
.rooms
|
|
|
|
|
.state_cache
|
|
|
|
|
.room_joined_count(room_id)
|
|
|
|
|
.unwrap_or(0);
|
|
|
|
|
|
|
|
|
|
let invited_member_count = services
|
|
|
|
|
.rooms
|
|
|
|
|
.state_cache
|
|
|
|
|
.room_invited_count(room_id)
|
|
|
|
|
.unwrap_or(0);
|
|
|
|
|
|
2024-12-15 00:05:47 -05:00
|
|
|
let (joined_member_count, invited_member_count) =
|
|
|
|
|
join(joined_member_count, invited_member_count).await;
|
2024-12-06 12:45:20 +00:00
|
|
|
|
2025-01-08 01:20:42 +00:00
|
|
|
let small_room = joined_member_count.saturating_add(invited_member_count) <= 5;
|
2024-12-06 12:45:20 +00:00
|
|
|
|
|
|
|
|
let heroes: OptionFuture<_> = small_room
|
2025-11-05 10:04:43 -05:00
|
|
|
.then(|| build_heroes(services, room_id, syncing_user))
|
2024-12-06 12:45:20 +00:00
|
|
|
.into();
|
|
|
|
|
|
|
|
|
|
Ok((Some(joined_member_count), Some(invited_member_count), heroes.await))
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-05 10:04:43 -05:00
|
|
|
async fn build_heroes(
|
2024-12-15 00:05:47 -05:00
|
|
|
services: &Services,
|
|
|
|
|
room_id: &RoomId,
|
2025-11-05 10:01:12 -05:00
|
|
|
syncing_user: &UserId,
|
2024-12-15 00:05:47 -05:00
|
|
|
) -> Vec<OwnedUserId> {
|
2024-12-06 12:45:20 +00:00
|
|
|
services
|
|
|
|
|
.rooms
|
|
|
|
|
.timeline
|
2025-11-05 10:01:12 -05:00
|
|
|
.all_pdus(syncing_user, room_id)
|
2024-12-06 12:45:20 +00:00
|
|
|
.ready_filter(|(_, pdu)| pdu.kind == RoomMember)
|
2024-12-15 00:05:47 -05:00
|
|
|
.fold_default(|heroes: Vec<_>, (_, pdu)| {
|
2025-11-05 10:01:12 -05:00
|
|
|
fold_hero(heroes, services, room_id, syncing_user, pdu)
|
2024-12-15 00:05:47 -05:00
|
|
|
})
|
2024-12-06 12:45:20 +00:00
|
|
|
.await
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn fold_hero(
|
2024-12-15 00:05:47 -05:00
|
|
|
mut heroes: Vec<OwnedUserId>,
|
|
|
|
|
services: &Services,
|
|
|
|
|
room_id: &RoomId,
|
2025-11-05 10:01:12 -05:00
|
|
|
syncing_user: &UserId,
|
2024-12-15 00:05:47 -05:00
|
|
|
pdu: PduEvent,
|
2024-12-06 12:45:20 +00:00
|
|
|
) -> Vec<OwnedUserId> {
|
2024-12-15 00:05:47 -05:00
|
|
|
let Some(user_id): Option<&UserId> =
|
|
|
|
|
pdu.state_key.as_deref().map(TryInto::try_into).flat_ok()
|
|
|
|
|
else {
|
2024-12-06 12:45:20 +00:00
|
|
|
return heroes;
|
|
|
|
|
};
|
|
|
|
|
|
2025-11-05 10:01:12 -05:00
|
|
|
if user_id == syncing_user {
|
2024-12-06 12:45:20 +00:00
|
|
|
return heroes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let Ok(content): Result<RoomMemberEventContent, _> = pdu.get_content() else {
|
|
|
|
|
return heroes;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// The membership was and still is invite or join
|
|
|
|
|
if !matches!(content.membership, MembershipState::Join | MembershipState::Invite) {
|
|
|
|
|
return heroes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if heroes.iter().any(is_equal_to!(user_id)) {
|
|
|
|
|
return heroes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let (is_invited, is_joined) = join(
|
|
|
|
|
services.rooms.state_cache.is_invited(user_id, room_id),
|
|
|
|
|
services.rooms.state_cache.is_joined(user_id, room_id),
|
|
|
|
|
)
|
|
|
|
|
.await;
|
|
|
|
|
|
|
|
|
|
if !is_joined && is_invited {
|
|
|
|
|
return heroes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
heroes.push(user_id.to_owned());
|
|
|
|
|
heroes
|
|
|
|
|
}
|