Files
continuwuity/src/api/client/sync/v3.rs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

1333 lines
33 KiB
Rust
Raw Normal View History

2020-07-30 18:14:47 +02:00
use std::{
2025-01-31 05:05:32 +00:00
collections::{BTreeMap, HashMap, HashSet},
2020-07-30 18:14:47 +02:00
time::Duration,
2024-03-05 19:48:54 -05:00
};
2024-07-16 08:05:25 +00:00
use axum::extract::State;
2024-12-06 12:45:20 +00:00
use futures::{
2025-01-29 01:04:02 +00:00
FutureExt, StreamExt, TryFutureExt, TryStreamExt,
2025-06-29 01:21:21 +00:00
future::{OptionFuture, join, join3, join4, join5, try_join4},
2025-04-06 21:59:18 +00:00
pin_mut,
2024-07-07 06:17:58 +00:00
};
2020-07-30 18:14:47 +02:00
use ruma::{
DeviceId, EventId, OwnedEventId, OwnedRoomId, OwnedUserId, RoomId, UserId,
2022-02-18 15:33:14 +01:00
api::client::{
2025-01-25 07:18:33 +00:00
filter::FilterDefinition,
2023-03-13 10:39:02 +01:00
sync::sync_events::{
2025-06-29 03:33:29 +00:00
self, DeviceLists, StrippedState, UnreadNotificationsCount,
2024-03-05 19:48:54 -05:00
v3::{
Ephemeral, Filter, GlobalAccountData, InviteState, InvitedRoom, JoinedRoom,
2025-01-11 18:43:54 -05:00
KnockState, KnockedRoom, LeftRoom, Presence, RoomAccountData, RoomSummary, Rooms,
2025-06-29 03:33:29 +00:00
State as RoomState, StateEvents, Timeline, ToDevice,
2024-03-05 19:48:54 -05:00
},
},
2022-01-05 18:15:00 +01:00
uiaa::UiaaResponse,
2024-08-30 10:31:08 +02:00
},
events::{
2024-11-04 22:38:12 +00:00
AnyRawAccountDataEvent, AnySyncEphemeralRoomEvent, StateEventType,
2025-07-27 01:43:59 +00:00
SyncEphemeralRoomEvent,
2024-10-16 05:32:27 +00:00
TimelineEventType::*,
presence::{PresenceEvent, PresenceEventContent},
room::member::{MembershipState, RoomMemberEventContent},
2025-07-27 01:43:59 +00:00
typing::TypingEventContent,
2024-08-30 10:31:08 +02:00
},
serde::Raw,
uint,
2020-07-30 18:14:47 +02:00
};
use tokio::time;
2025-04-22 01:41:02 +00:00
use tuwunel_core::{
Result, at, err, error, extract_variant, is_equal_to,
matrix::{
Event,
2025-08-03 22:06:05 +00:00
event::Matches,
2025-04-22 01:41:02 +00:00
pdu::{EventHash, PduCount, PduEvent},
},
pair_of, ref_at,
result::FlatOk,
trace,
2025-04-22 01:41:02 +00:00
utils::{
self, BoolExt, FutureBoolExt, IterStream, ReadyExt, TryFutureExtExt,
future::{OptionStream, ReadyEqExt},
math::ruma_from_u64,
result::LogErr,
2025-04-22 01:41:02 +00:00
stream::{BroadbandExt, Tools, TryExpect, WidebandExt},
},
warn,
};
use tuwunel_service::{
Services,
rooms::{
lazy_loading,
lazy_loading::{Options, Witness},
short::{ShortEventId, ShortStateHash, ShortStateKey},
},
};
2021-06-30 09:52:01 +02:00
2024-10-16 05:32:27 +00:00
use super::{load_timeline, share_encrypted_room};
use crate::{Ruma, RumaResponse, client::ignored_filter};
2024-12-06 12:45:20 +00:00
#[derive(Default)]
struct StateChanges {
heroes: Option<Vec<OwnedUserId>>,
joined_member_count: Option<u64>,
invited_member_count: Option<u64>,
state_events: Vec<PduEvent>,
}
2025-01-31 05:05:32 +00:00
type PresenceUpdates = HashMap<OwnedUserId, PresenceEventContent>;
2024-03-05 19:48:54 -05:00
2020-08-22 21:38:01 +02:00
/// # `GET /_matrix/client/r0/sync`
///
/// Synchronize the client's state with the latest state on the server.
///
/// - This endpoint takes a `since` parameter which should be the `next_batch`
2024-06-16 00:36:49 +00:00
/// value from a previous request for incremental syncs.
2021-08-31 19:14:37 +02:00
///
/// Calling this endpoint without a `since` parameter returns:
/// - Some of the most recent events of each timeline
/// - Notification counts for each room
/// - Joined and invited member counts, heroes
/// - All state events
///
/// Calling this endpoint with a `since` parameter from a previous `next_batch`
/// returns: For joined rooms:
/// - Some of the most recent events of each timeline that happened after since
2022-01-09 13:42:25 +01:00
/// - If user joined the room after since: All state events (unless lazy loading
2024-06-16 00:36:49 +00:00
/// is activated) and all device list updates in that room
2021-08-31 19:14:37 +02:00
/// - If the user was already in the room: A list of all events that are in the
2024-06-16 00:36:49 +00:00
/// state now, but were not in the state at `since`
2021-08-31 19:14:37 +02:00
/// - If the state we send contains a member event: Joined and invited member
/// counts, heroes
/// - Device list updates that happened after `since`
/// - If there are events in the timeline we send or the user send updated his
/// read mark: Notification counts
/// - EDUs that are active now (read receipts, typing updates, presence)
2022-01-09 13:42:25 +01:00
/// - TODO: Allow multiple sync streams to support Pantalaimon
2021-08-31 19:14:37 +02:00
///
/// For invited rooms:
/// - If the user was invited after `since`: A subset of the state of the room
/// at the point of the invite
///
/// For left rooms:
/// - If the user left after `since`: `prev_batch` token, empty state (TODO:
2021-08-31 19:14:37 +02:00
/// subset of the state at the point of the leave)
2024-12-08 20:31:16 +00:00
#[tracing::instrument(
name = "sync",
level = "debug",
skip_all,
fields(
user_id = %body.sender_user(),
2024-12-08 20:31:16 +00:00
)
)]
2024-04-22 23:48:57 -04:00
pub(crate) async fn sync_events_route(
State(services): State<crate::State>,
body: Ruma<sync_events::v3::Request>,
2022-02-18 15:33:14 +01:00
) -> Result<sync_events::v3::Response, RumaResponse<UiaaResponse>> {
2024-12-06 12:45:20 +00:00
let (sender_user, sender_device) = body.sender();
2020-07-30 18:14:47 +02:00
// Presence update
if services.config.allow_local_presence {
2024-07-16 08:05:25 +00:00
services
2024-04-01 20:48:40 -07:00
.presence
2024-12-06 12:45:20 +00:00
.ping_presence(sender_user, &body.body.set_presence)
.await
.log_err()
.ok();
2024-04-01 20:48:40 -07:00
}
2020-07-30 18:14:47 +02:00
let mut since = body
.body
.since
.as_deref()
.map(str::parse)
.flat_ok()
.unwrap_or(0);
let timeout = body
.body
.timeout
.as_ref()
.map(Duration::as_millis)
.map(TryInto::try_into)
.flat_ok()
.unwrap_or(services.config.client_sync_timeout_default)
.max(services.config.client_sync_timeout_min)
.min(services.config.client_sync_timeout_max);
let stop_at = time::Instant::now()
.checked_add(Duration::from_millis(timeout))
.expect("configuration must limit maximum timeout");
loop {
let watchers = services.sync.watch(sender_user, sender_device);
2025-08-05 09:28:02 +00:00
let next_batch = services.globals.wait_pending().await?;
debug_assert!(since <= next_batch, "next_batch is monotonic");
if since < next_batch || body.body.full_state {
let response = build_sync_events(&services, &body, since, next_batch).await?;
let empty = response.rooms.is_empty()
&& response.presence.is_empty()
&& response.account_data.is_empty()
&& response.device_lists.is_empty()
&& response.to_device.is_empty();
if !empty || body.body.full_state {
return Ok(response);
}
}
// Wait for activity
if time::timeout_at(stop_at, watchers).await.is_err() {
trace!(since, next_batch, "empty response");
2025-08-05 09:28:02 +00:00
return Ok(sync_events::v3::Response::new(next_batch.to_string()));
}
trace!(
since,
last_batch = ?next_batch,
count = ?services.globals.pending_count(),
stop_at = ?stop_at,
2025-08-05 09:28:02 +00:00
"notified by watcher"
);
since = next_batch;
}
}
#[tracing::instrument(
name = "build",
level = "debug",
ret(level = "trace"),
skip_all,
fields(
%since,
%next_batch,
count = ?services.globals.pending_count(),
)
)]
async fn build_sync_events(
services: &Services,
body: &Ruma<sync_events::v3::Request>,
since: u64,
next_batch: u64,
) -> Result<sync_events::v3::Response> {
let (sender_user, sender_device) = body.sender();
2025-01-25 07:18:33 +00:00
let full_state = body.body.full_state;
2024-12-06 12:45:20 +00:00
let filter = match body.body.filter.as_ref() {
| None => FilterDefinition::default(),
| Some(Filter::FilterDefinition(filter)) => filter.clone(),
| Some(Filter::FilterId(filter_id)) => services
2024-03-25 17:05:11 -04:00
.users
2024-12-06 12:45:20 +00:00
.get_filter(sender_user, filter_id)
2024-08-08 17:18:30 +00:00
.await
2024-03-25 17:05:11 -04:00
.unwrap_or_default(),
2022-01-05 18:15:00 +01:00
};
let joined_rooms = services
2024-03-25 17:05:11 -04:00
.rooms
.state_cache
2024-12-06 12:45:20 +00:00
.rooms_joined(sender_user)
2024-08-08 17:18:30 +00:00
.map(ToOwned::to_owned)
2024-12-06 12:45:20 +00:00
.broad_filter_map(|room_id| {
load_joined_room(
services,
2024-12-06 12:45:20 +00:00
sender_user,
sender_device,
room_id.clone(),
since,
next_batch,
full_state,
2025-01-25 07:18:33 +00:00
&filter,
2024-12-06 12:45:20 +00:00
)
.map_ok(move |(joined_room, dlu, jeu)| (room_id, joined_room, dlu, jeu))
.ok()
})
.ready_fold(
(BTreeMap::new(), HashSet::new(), HashSet::new()),
|(mut joined_rooms, mut device_list_updates, mut left_encrypted_users),
(room_id, joined_room, dlu, leu)| {
device_list_updates.extend(dlu);
left_encrypted_users.extend(leu);
if !joined_room.is_empty() {
joined_rooms.insert(room_id, joined_room);
}
2024-12-06 12:45:20 +00:00
(joined_rooms, device_list_updates, left_encrypted_users)
2020-07-30 18:14:47 +02:00
},
2021-04-11 21:01:27 +02:00
);
2020-07-30 18:14:47 +02:00
2024-12-06 12:45:20 +00:00
let left_rooms = services
.rooms
.state_cache
.rooms_left(sender_user)
.broad_filter_map(|(room_id, _)| {
handle_left_room(
services,
2024-12-06 12:45:20 +00:00
since,
room_id.clone(),
sender_user,
2025-01-25 07:18:33 +00:00
next_batch,
2024-12-06 12:45:20 +00:00
full_state,
2025-01-25 07:18:33 +00:00
&filter,
2024-12-06 12:45:20 +00:00
)
.map_ok(move |left_room| (room_id, left_room))
.ok()
})
.ready_filter_map(|(room_id, left_room)| left_room.map(|left_room| (room_id, left_room)))
.collect();
2024-08-08 17:18:30 +00:00
let invited_rooms = services
.rooms
.state_cache
.rooms_invited(sender_user)
2025-07-08 11:08:58 +00:00
.fold_default(async |mut invited_rooms: BTreeMap<_, _>, (room_id, invite_state)| {
2024-12-06 12:45:20 +00:00
let invite_count = services
.rooms
.state_cache
.get_invite_count(&room_id, sender_user)
.await
.ok();
// Invited before last sync
if Some(since) >= invite_count || Some(next_batch) < invite_count {
2024-12-06 12:45:20 +00:00
return invited_rooms;
}
let invited_room = InvitedRoom {
2025-06-29 03:33:29 +00:00
invite_state: InviteState {
events: invite_state
.into_iter()
.map(Raw::cast::<StrippedState>)
.collect(),
},
2024-12-06 12:45:20 +00:00
};
invited_rooms.insert(room_id, invited_room);
invited_rooms
});
2024-12-06 12:45:20 +00:00
2025-01-11 18:43:54 -05:00
let knocked_rooms = services
.rooms
.state_cache
.rooms_knocked(sender_user)
2025-07-08 11:08:58 +00:00
.fold_default(async |mut knocked_rooms: BTreeMap<_, _>, (room_id, knock_state)| {
2025-01-11 18:43:54 -05:00
let knock_count = services
.rooms
.state_cache
.get_knock_count(&room_id, sender_user)
.await
.ok();
// Knocked before last sync; or after the cutoff for this sync
if Some(since) >= knock_count || Some(next_batch) < knock_count {
2025-01-11 18:43:54 -05:00
return knocked_rooms;
}
let knocked_room = KnockedRoom {
2025-06-29 03:33:29 +00:00
knock_state: KnockState {
events: knock_state
.into_iter()
.map(Raw::cast::<StrippedState>)
.collect(),
},
2025-01-11 18:43:54 -05:00
};
knocked_rooms.insert(room_id, knocked_room);
knocked_rooms
});
2024-12-06 12:45:20 +00:00
let presence_updates: OptionFuture<_> = services
.config
.allow_local_presence
2025-07-26 04:59:00 +00:00
.then(|| process_presence_updates(services, since, next_batch, sender_user))
2024-12-06 12:45:20 +00:00
.into();
let account_data = services
.account_data
.changes_since(None, sender_user, since, Some(next_batch))
2024-12-06 12:45:20 +00:00
.ready_filter_map(|e| extract_variant!(e, AnyRawAccountDataEvent::Global))
.collect();
// Look for device list updates of this account
let keys_changed = services
.users
.keys_changed(sender_user, since, Some(next_batch))
2024-12-06 12:45:20 +00:00
.map(ToOwned::to_owned)
.collect::<HashSet<_>>();
let to_device_events = services
.users
.get_to_device_events(sender_user, sender_device, Some(since), Some(next_batch))
2024-12-06 12:45:20 +00:00
.collect::<Vec<_>>();
let device_one_time_keys_count = services
.users
.count_one_time_keys(sender_user, sender_device);
2020-08-21 21:22:59 +02:00
2020-07-30 18:14:47 +02:00
// Remove all to-device events the device received *last time*
let remove_to_device_events =
services
.users
.remove_to_device_events(sender_user, sender_device, since);
2024-12-06 12:45:20 +00:00
let (
account_data,
keys_changed,
device_one_time_keys_count,
((), to_device_events, presence_updates),
(
(joined_rooms, mut device_list_updates, left_encrypted_users),
left_rooms,
invited_rooms,
knocked_rooms,
),
) = join5(
account_data,
keys_changed,
device_one_time_keys_count,
join3(remove_to_device_events, to_device_events, presence_updates),
join4(joined_rooms, left_rooms, invited_rooms, knocked_rooms),
)
.boxed()
.await;
2024-12-06 12:45:20 +00:00
device_list_updates.extend(keys_changed);
// If the user doesn't share an encrypted room with the target anymore, we need
// to tell them
let device_list_left: HashSet<_> = left_encrypted_users
2024-12-06 12:45:20 +00:00
.into_iter()
.stream()
2025-07-08 11:08:58 +00:00
.broad_filter_map(async |user_id: OwnedUserId| {
share_encrypted_room(services, sender_user, &user_id, None)
.await
.eq(&false)
.then_some(user_id)
2024-12-06 12:45:20 +00:00
})
.collect()
2024-08-08 17:18:30 +00:00
.await;
2020-07-30 18:14:47 +02:00
2025-08-03 22:06:05 +00:00
let presence_events = presence_updates
.into_iter()
.flat_map(IntoIterator::into_iter)
.map(|(sender, content)| PresenceEvent { content, sender })
.map(|ref event| Raw::new(event))
.filter_map(Result::ok)
.collect();
Ok(sync_events::v3::Response {
account_data: GlobalAccountData { events: account_data },
2024-12-06 12:45:20 +00:00
device_lists: DeviceLists {
changed: device_list_updates.into_iter().collect(),
left: device_list_left.into_iter().collect(),
},
device_one_time_keys_count,
// Fallback keys are not yet supported
device_unused_fallback_key_types: None,
2025-01-25 07:18:33 +00:00
next_batch: next_batch.to_string(),
2025-08-03 22:06:05 +00:00
presence: Presence { events: presence_events },
2022-02-18 15:33:14 +01:00
rooms: Rooms {
2020-07-30 18:14:47 +02:00
leave: left_rooms,
join: joined_rooms,
invite: invited_rooms,
2025-01-11 18:43:54 -05:00
knock: knocked_rooms,
2020-07-30 18:14:47 +02:00
},
to_device: ToDevice { events: to_device_events },
})
2020-07-30 18:14:47 +02:00
}
2020-08-21 21:22:59 +02:00
#[tracing::instrument(name = "presence", level = "debug", skip_all)]
async fn process_presence_updates(
services: &Services,
since: u64,
2025-07-26 04:59:00 +00:00
next_batch: u64,
syncing_user: &UserId,
) -> PresenceUpdates {
2024-12-06 12:45:20 +00:00
services
.presence
2025-07-26 04:59:00 +00:00
.presence_since(since, Some(next_batch))
2024-12-06 12:45:20 +00:00
.filter(|(user_id, ..)| {
services
.rooms
.state_cache
.user_sees_user(syncing_user, user_id)
})
.filter_map(|(user_id, _, presence_bytes)| {
services
.presence
.from_json_bytes_to_event(presence_bytes, user_id)
.map_ok(move |event| (user_id, event))
.ok()
})
2025-01-31 05:05:32 +00:00
.map(|(user_id, event)| (user_id.to_owned(), event.content))
.collect()
2024-12-06 12:45:20 +00:00
.await
}
2024-12-08 20:31:16 +00:00
#[tracing::instrument(
name = "left",
level = "debug",
skip_all,
fields(
room_id = %room_id,
full = %full_state,
),
)]
2024-07-16 08:05:25 +00:00
#[allow(clippy::too_many_arguments)]
2024-05-01 12:39:59 -04:00
async fn handle_left_room(
services: &Services,
since: u64,
ref room_id: OwnedRoomId,
sender_user: &UserId,
2025-01-25 07:18:33 +00:00
next_batch: u64,
full_state: bool,
2025-01-25 07:18:33 +00:00
filter: &FilterDefinition,
2024-12-06 12:45:20 +00:00
) -> Result<Option<LeftRoom>> {
2024-07-16 08:05:25 +00:00
let left_count = services
2024-05-01 12:39:59 -04:00
.rooms
.state_cache
.get_left_count(room_id, sender_user)
2024-08-08 17:18:30 +00:00
.await
.ok();
2024-05-01 12:39:59 -04:00
let filter_exclude = filter
.room
.not_rooms
.iter()
.any(is_equal_to!(room_id));
let filter_include = filter
.room
.rooms
.as_ref()
.is_some_and(|rooms| rooms.iter().any(is_equal_to!(room_id)));
let too_soon = Some(next_batch) < left_count;
let too_late = Some(since) >= left_count;
let initial_sync = since == 0;
let include_leave =
filter.room.include_leave && !filter_exclude && (filter_include || initial_sync);
// Left before last sync or after cutoff for next sync
if (too_late && !include_leave) || too_soon {
2024-12-06 12:45:20 +00:00
return Ok(None);
2024-05-01 12:39:59 -04:00
}
2025-04-06 21:59:18 +00:00
let is_not_found = services.rooms.metadata.exists(room_id).eq(&false);
let is_disabled = services.rooms.metadata.is_disabled(room_id);
let is_banned = services.rooms.metadata.is_banned(room_id);
pin_mut!(is_not_found, is_disabled, is_banned);
if is_not_found.or(is_disabled).or(is_banned).await {
2024-05-01 12:39:59 -04:00
// This is just a rejected invite, not a room we know
// Insert a leave event anyways for the client
2024-05-01 12:39:59 -04:00
let event = PduEvent {
event_id: EventId::new(services.globals.server_name()),
2024-05-01 12:39:59 -04:00
sender: sender_user.to_owned(),
origin: None,
origin_server_ts: utils::millis_since_unix_epoch().try_into()?,
2024-09-29 20:57:33 -04:00
kind: RoomMember,
content: serde_json::from_str(r#"{"membership":"leave"}"#)?,
state_key: Some(sender_user.as_str().into()),
2024-05-01 12:39:59 -04:00
unsigned: None,
// The following keys are dropped on conversion
2024-12-06 12:45:20 +00:00
room_id: room_id.clone(),
2024-05-01 12:39:59 -04:00
prev_events: vec![],
depth: uint!(1),
auth_events: vec![],
redacts: None,
2025-07-08 11:55:52 +00:00
hashes: EventHash::default(),
2024-05-01 12:39:59 -04:00
signatures: None,
};
2024-12-06 12:45:20 +00:00
return Ok(Some(LeftRoom {
account_data: RoomAccountData { events: Vec::new() },
2024-12-06 12:45:20 +00:00
timeline: Timeline {
limited: false,
2025-01-25 07:18:33 +00:00
prev_batch: Some(next_batch.to_string()),
2024-12-06 12:45:20 +00:00
events: Vec::new(),
},
2025-06-29 03:33:29 +00:00
state: RoomState::Before(StateEvents { events: vec![event.into_format()] }),
2024-12-06 12:45:20 +00:00
}));
2024-05-01 12:39:59 -04:00
}
let mut left_state_events = Vec::new();
2025-04-22 04:42:26 +00:00
let since_shortstatehash = services
.rooms
.user
.get_token_shortstatehash(room_id, since);
2024-05-01 12:39:59 -04:00
2025-01-29 01:04:02 +00:00
let since_state_ids: HashMap<_, OwnedEventId> = since_shortstatehash
.map_ok(|since_shortstatehash| {
services
.rooms
.state_accessor
.state_full_ids(since_shortstatehash)
.map(Ok)
})
.try_flatten_stream()
.try_collect()
.await
.unwrap_or_default();
2024-05-01 12:39:59 -04:00
2024-11-30 08:09:51 +00:00
let Ok(left_event_id): Result<OwnedEventId> = services
2024-08-08 17:18:30 +00:00
.rooms
.state_accessor
.room_state_get_id(room_id, &StateEventType::RoomMember, sender_user.as_str())
2024-08-08 17:18:30 +00:00
.await
2024-05-01 12:39:59 -04:00
else {
warn!("Left {room_id} but no left state event");
2024-12-06 12:45:20 +00:00
return Ok(None);
2024-05-01 12:39:59 -04:00
};
2024-08-08 17:18:30 +00:00
let Ok(left_shortstatehash) = services
2024-05-01 12:39:59 -04:00
.rooms
.state_accessor
2024-08-08 17:18:30 +00:00
.pdu_shortstatehash(&left_event_id)
.await
2024-05-01 12:39:59 -04:00
else {
warn!(event_id = %left_event_id, "Leave event has no state in {room_id}");
2024-12-06 12:45:20 +00:00
return Ok(None);
2024-05-01 12:39:59 -04:00
};
2025-01-29 01:04:02 +00:00
let mut left_state_ids: HashMap<_, _> = services
2024-05-01 12:39:59 -04:00
.rooms
.state_accessor
.state_full_ids(left_shortstatehash)
2025-01-29 01:04:02 +00:00
.collect()
.await;
2024-05-01 12:39:59 -04:00
2024-07-16 08:05:25 +00:00
let leave_shortstatekey = services
2024-05-01 12:39:59 -04:00
.rooms
.short
2024-08-08 17:18:30 +00:00
.get_or_create_shortstatekey(&StateEventType::RoomMember, sender_user.as_str())
.await;
2024-05-01 12:39:59 -04:00
left_state_ids.insert(leave_shortstatekey, left_event_id);
2024-11-02 06:12:54 +00:00
for (shortstatekey, event_id) in left_state_ids {
if full_state || since_state_ids.get(&shortstatekey) != Some(&event_id) {
let (event_type, state_key) = services
.rooms
.short
.get_statekey_from_short(shortstatekey)
.await?;
2024-05-01 12:39:59 -04:00
2025-01-25 07:18:33 +00:00
if filter.room.state.lazy_load_options.is_enabled()
&& event_type == StateEventType::RoomMember
&& !full_state
&& state_key
.as_str()
.try_into()
.is_ok_and(|user_id: &UserId| sender_user != user_id)
2024-05-01 12:39:59 -04:00
{
2025-01-25 07:18:33 +00:00
continue;
2024-05-01 12:39:59 -04:00
}
2025-01-25 07:18:33 +00:00
let Ok(pdu) = services.rooms.timeline.get_pdu(&event_id).await else {
error!("Pdu in state not found: {event_id}");
continue;
};
2025-04-26 08:24:47 +00:00
left_state_events.push(pdu.into_format());
2024-05-01 12:39:59 -04:00
}
}
2024-12-06 12:45:20 +00:00
Ok(Some(LeftRoom {
account_data: RoomAccountData { events: Vec::new() },
2024-12-06 12:45:20 +00:00
timeline: Timeline {
2025-01-25 07:18:33 +00:00
// TODO: support left timeline events so we dont need to set limited to true
limited: true,
prev_batch: Some(next_batch.to_string()),
events: Vec::new(), // and so we dont need to set this to empty vec
2024-12-06 12:45:20 +00:00
},
2025-06-29 03:33:29 +00:00
state: RoomState::Before(StateEvents { events: left_state_events }),
2024-12-06 12:45:20 +00:00
}))
}
#[tracing::instrument(
name = "joined",
level = "debug",
skip_all,
fields(
room_id = ?room_id,
),
)]
#[allow(clippy::too_many_arguments)]
2023-03-13 10:39:02 +01:00
async fn load_joined_room(
services: &Services,
sender_user: &UserId,
sender_device: &DeviceId,
ref room_id: OwnedRoomId,
since: u64,
next_batch: u64,
full_state: bool,
2025-01-25 07:18:33 +00:00
filter: &FilterDefinition,
2024-12-06 12:45:20 +00:00
) -> Result<(JoinedRoom, HashSet<OwnedUserId>, HashSet<OwnedUserId>)> {
let sincecount = PduCount::Normal(since);
let next_batchcount = PduCount::Normal(next_batch);
2023-03-13 10:39:02 +01:00
let current_shortstatehash = services
.rooms
.state
.get_room_shortstatehash(room_id)
.map_err(|_| err!(Database(error!("Room {room_id} has no state"))));
2023-03-13 10:39:02 +01:00
let since_shortstatehash = services
.rooms
.user
.get_token_shortstatehash(room_id, since)
.ok()
.map(Ok);
2025-08-03 22:06:05 +00:00
let timeline_limit: usize = filter
.room
.timeline
.limit
.unwrap_or_else(|| uint!(10))
.try_into()?;
let timeline = load_timeline(
services,
sender_user,
room_id,
sincecount,
Some(next_batchcount),
2025-08-03 22:06:05 +00:00
timeline_limit,
);
2025-01-25 07:18:33 +00:00
let receipt_events = services
.rooms
.read_receipt
.readreceipts_since(room_id, since, Some(next_batch))
2025-07-08 11:08:58 +00:00
.filter_map(async |(read_user, _, edu)| {
2025-01-25 07:18:33 +00:00
services
.users
.user_is_ignored(read_user, sender_user)
.await
.or_some((read_user.to_owned(), edu))
})
.collect::<HashMap<OwnedUserId, Raw<AnySyncEphemeralRoomEvent>>>()
.map(Ok);
let (current_shortstatehash, since_shortstatehash, (timeline_pdus, limited), receipt_events) =
2025-01-25 07:18:33 +00:00
try_join4(current_shortstatehash, since_shortstatehash, timeline, receipt_events)
.boxed()
.await?;
// State was changed after the cutoff for this sync; similar to other handlers.
if current_shortstatehash > next_batch {
// Transfer the since_shortstatehash not the current over to the next sync.
if let Some(since_shortstatehash) = since_shortstatehash {
services
.rooms
.user
.associate_token_shortstatehash(room_id, next_batch, since_shortstatehash)
.await;
}
return Ok((JoinedRoom::default(), HashSet::new(), HashSet::new()));
}
let associate_token = services
.rooms
.user
.associate_token_shortstatehash(room_id, next_batch, current_shortstatehash);
2025-02-02 07:40:08 +00:00
let lazy_loading_enabled = filter.room.state.lazy_load_options.is_enabled()
2025-04-22 04:42:26 +00:00
|| filter
.room
.timeline
.lazy_load_options
.is_enabled();
2025-02-02 07:40:08 +00:00
let lazy_loading_context = &lazy_loading::Context {
user_id: sender_user,
device_id: Some(sender_device),
2025-02-02 07:40:08 +00:00
room_id,
token: Some(since),
options: Some(&filter.room.state.lazy_load_options),
};
2025-08-03 22:06:05 +00:00
let initial = since == 0 || since_shortstatehash.is_none();
2025-02-02 07:40:08 +00:00
// Reset lazy loading because this is an initial sync
let lazy_load_reset: OptionFuture<_> = initial
2025-04-22 04:42:26 +00:00
.then(|| {
services
.rooms
.lazy_loading
.reset(lazy_loading_context)
})
2025-02-02 07:40:08 +00:00
.into();
lazy_load_reset.await;
let witness: OptionFuture<_> = lazy_loading_enabled
.then(|| {
let witness: Witness = timeline_pdus
.iter()
.map(ref_at!(1))
.map(Event::sender)
.map(Into::into)
.chain(receipt_events.keys().map(Into::into))
.collect();
services
.rooms
.lazy_loading
.witness_retain(witness, lazy_loading_context)
})
.into();
2023-03-13 10:39:02 +01:00
2025-08-03 22:06:05 +00:00
let horizon_shortstatehash: OptionFuture<_> = timeline_pdus
.iter()
.map(at!(0))
.map(PduCount::into_unsigned)
.map(|shorteventid| {
services
.rooms
.state_accessor
.get_shortstatehash(shorteventid)
})
.next()
.into();
let last_notification_read: OptionFuture<_> = timeline_pdus
.is_empty()
.then(|| {
services
.rooms
.user
.last_notification_read(sender_user, room_id)
})
.into();
2025-01-25 07:18:33 +00:00
let since_sender_member: OptionFuture<_> = since_shortstatehash
.map(|short| {
services
.rooms
.state_accessor
.state_get_content(short, &StateEventType::RoomMember, sender_user.as_str())
.ok()
})
.into();
2025-08-03 22:06:05 +00:00
let encrypted_room = services
.rooms
.state_accessor
.state_get(current_shortstatehash, &StateEventType::RoomEncryption, "")
.is_ok();
let last_privateread_update = services
.rooms
.read_receipt
.last_privateread_update(sender_user, room_id);
2025-08-03 22:06:05 +00:00
let (
witness,
(encrypted_room, last_privateread_update, last_notification_read),
(since_sender_member, horizon_shortstatehash, ()),
) = join3(
witness,
join3(encrypted_room, last_privateread_update, last_notification_read),
join3(since_sender_member, horizon_shortstatehash, associate_token),
)
.boxed()
.await;
2025-02-02 07:40:08 +00:00
2025-01-25 07:18:33 +00:00
let joined_since_last_sync =
since_sender_member
.flatten()
.is_none_or(|content: RoomMemberEventContent| {
content.membership != MembershipState::Join
});
2024-12-06 12:45:20 +00:00
let StateChanges {
heroes,
joined_member_count,
invited_member_count,
2025-02-05 05:10:30 +00:00
mut state_events,
2025-01-31 08:34:32 +00:00
} = calculate_state_changes(
services,
sender_user,
room_id,
full_state,
2025-08-03 22:06:05 +00:00
encrypted_room,
2025-01-31 08:34:32 +00:00
since_shortstatehash,
2025-08-03 22:06:05 +00:00
horizon_shortstatehash.flat_ok(),
2025-01-31 08:34:32 +00:00
current_shortstatehash,
joined_since_last_sync,
2025-02-02 07:40:08 +00:00
witness.as_ref(),
2025-01-31 08:34:32 +00:00
)
.await?;
2023-03-13 10:39:02 +01:00
2025-08-03 22:06:05 +00:00
let send_notification_counts =
last_notification_read.is_none_or(|last_count| last_count.gt(&since));
let is_sender_membership = |event: &PduEvent| {
*event.event_type() == StateEventType::RoomMember.into()
&& event
.state_key()
2025-02-05 05:10:30 +00:00
.is_some_and(is_equal_to!(sender_user.as_str()))
};
let joined_sender_member: 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();
2024-12-06 12:45:20 +00:00
let notification_count: OptionFuture<_> = send_notification_counts
.then(|| {
services
.rooms
.user
.notification_count(sender_user, room_id)
2024-12-06 12:45:20 +00:00
.map(TryInto::try_into)
.unwrap_or(uint!(0))
})
.into();
let highlight_count: OptionFuture<_> = send_notification_counts
.then(|| {
services
.rooms
.user
.highlight_count(sender_user, room_id)
2024-12-06 12:45:20 +00:00
.map(TryInto::try_into)
.unwrap_or(uint!(0))
})
.into();
let private_read_event: OptionFuture<_> = last_privateread_update
.gt(&since)
.then(|| {
services
.rooms
.read_receipt
.private_read_get(room_id, sender_user)
.map(Result::ok)
})
.into();
2025-02-02 07:40:08 +00:00
let typing_events = services
.rooms
.typing
.last_typing_update(room_id)
2025-07-08 11:08:58 +00:00
.and_then(async |count| {
2025-02-02 07:40:08 +00:00
if count <= since {
return Ok(Vec::<Raw<AnySyncEphemeralRoomEvent>>::new());
}
2025-07-27 01:43:59 +00:00
let typings = typings_event_for_user(services, room_id, sender_user).await?;
2025-02-02 07:40:08 +00:00
Ok(vec![serde_json::from_str(&serde_json::to_string(&typings)?)?])
})
.unwrap_or(Vec::new());
2025-08-03 22:06:05 +00:00
let extract_membership = |event: &PduEvent| {
let content: RoomMemberEventContent = event.get_content().ok()?;
let user_id: OwnedUserId = event.state_key()?.parse().ok()?;
Some((content, user_id))
};
let timeline_membership_changes: Vec<_> = timeline_pdus
.iter()
.map(ref_at!(1))
.filter(|_| !initial)
.filter_map(extract_membership)
.collect();
let device_list_updates = state_events
.iter()
.stream()
.ready_filter(|_| !initial)
.ready_filter(|state_event| *state_event.event_type() == RoomMember)
.ready_filter_map(extract_membership)
.chain(timeline_membership_changes.into_iter().stream())
.fold_default(async |(mut dlu, mut leu): pair_of!(HashSet<_>), (content, user_id)| {
use MembershipState::*;
let shares_encrypted_room = async |user_id| {
share_encrypted_room(services, sender_user, user_id, Some(room_id)).await
};
match content.membership {
| Leave => leu.insert(user_id),
| Join if joined_since_last_sync || !shares_encrypted_room(&user_id).await =>
dlu.insert(user_id),
| _ => false,
};
(dlu, leu)
});
let prev_batch = timeline_pdus.first().map(at!(0)).or_else(|| {
joined_sender_member
.is_some()
.then_some(since)
.map(Into::into)
});
let include_in_timeline = |event: &PduEvent| {
let filter = &filter.room.timeline;
filter.matches(event)
};
let room_events = timeline_pdus
.into_iter()
.stream()
.wide_filter_map(|item| ignored_filter(services, item, sender_user))
.map(at!(1))
.chain(joined_sender_member.into_iter().stream())
.ready_filter(include_in_timeline)
.collect::<Vec<_>>();
let device_updates = services
.users
.room_keys_changed(room_id, since, Some(next_batch))
.map(|(user_id, _)| user_id)
.map(ToOwned::to_owned)
.collect::<Vec<_>>();
let account_data_events = services
.account_data
.changes_since(Some(room_id), sender_user, since, Some(next_batch))
.ready_filter_map(|e| extract_variant!(e, AnyRawAccountDataEvent::Room))
.collect();
let (
(notification_count, highlight_count),
2025-08-03 22:06:05 +00:00
((mut device_list_updates, left_encrypted_users), device_updates),
(room_events, account_data_events, typing_events, private_read_event),
) = join3(
join(notification_count, highlight_count),
2025-08-03 22:06:05 +00:00
join(device_list_updates, device_updates),
join4(room_events, account_data_events, typing_events, private_read_event),
)
.boxed()
.await;
2024-11-04 22:38:12 +00:00
device_list_updates.extend(device_updates);
2024-12-10 22:54:19 -05:00
2025-08-03 22:06:05 +00:00
let is_in_timeline = |event: &PduEvent| {
room_events
.iter()
.map(Event::event_id)
.any(is_equal_to!(event.event_id()))
};
let include_in_state = |event: &PduEvent| {
let filter = &filter.room.state;
filter.matches(event) && (full_state || !is_in_timeline(event))
};
let state_events = state_events
.into_iter()
.filter(include_in_state)
.map(Event::into_format)
.collect();
let heroes = heroes
.into_iter()
.flatten()
.map(TryInto::try_into)
.filter_map(Result::ok)
.collect();
let edus: Vec<Raw<AnySyncEphemeralRoomEvent>> = receipt_events
.into_values()
.chain(typing_events.into_iter())
.chain(private_read_event.flatten().into_iter())
.collect();
2023-03-13 10:39:02 +01:00
2024-12-06 12:45:20 +00:00
let joined_room = JoinedRoom {
account_data: RoomAccountData { events: account_data_events },
2025-08-03 22:06:05 +00:00
ephemeral: Ephemeral { events: edus },
2025-06-29 03:33:29 +00:00
state: RoomState::Before(StateEvents { events: state_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),
2025-08-03 22:06:05 +00:00
heroes,
2023-03-13 10:39:02 +01:00
},
timeline: Timeline {
limited: limited || joined_since_last_sync,
prev_batch: prev_batch.as_ref().map(ToString::to_string),
2025-08-03 22:06:05 +00:00
events: room_events
.into_iter()
.map(Event::into_format)
.collect(),
},
unread_notifications: UnreadNotificationsCount { highlight_count, notification_count },
2023-03-13 10:39:02 +01:00
unread_thread_notifications: BTreeMap::new(),
2024-12-06 12:45:20 +00:00
};
Ok((joined_room, device_list_updates, left_encrypted_users))
}
#[tracing::instrument(
name = "state",
level = "trace",
skip_all,
fields(
full = %full_state,
cs = %current_shortstatehash,
ss = ?since_shortstatehash,
)
)]
2024-12-06 12:45:20 +00:00
#[allow(clippy::too_many_arguments)]
2025-06-29 01:21:21 +00:00
async fn calculate_state_changes<'a>(
services: &Services,
sender_user: &UserId,
room_id: &RoomId,
full_state: bool,
2025-08-03 22:06:05 +00:00
encrypted_room: bool,
since_shortstatehash: Option<ShortStateHash>,
2025-08-03 22:06:05 +00:00
horizon_shortstatehash: Option<ShortStateHash>,
current_shortstatehash: ShortStateHash,
joined_since_last_sync: bool,
2025-01-31 08:34:32 +00:00
witness: Option<&'a Witness>,
2024-12-06 12:45:20 +00:00
) -> Result<StateChanges> {
2025-06-29 01:21:21 +00:00
let initial = full_state || since_shortstatehash.is_none() || joined_since_last_sync;
2024-12-06 12:45:20 +00:00
2025-06-29 01:21:21 +00:00
let incremental = !initial && since_shortstatehash != Some(current_shortstatehash);
2025-08-03 22:06:05 +00:00
let horizon_shortstatehash = horizon_shortstatehash.unwrap_or(current_shortstatehash);
2024-12-06 12:45:20 +00:00
2025-08-03 22:06:05 +00:00
let since_shortstatehash = since_shortstatehash.unwrap_or(horizon_shortstatehash);
2025-01-31 08:34:32 +00:00
2025-02-02 07:40:08 +00:00
let state_get_shorteventid = |user_id: &'a UserId| {
services
.rooms
.state_accessor
.state_get_shortid(
2025-08-03 22:06:05 +00:00
horizon_shortstatehash,
2025-02-02 07:40:08 +00:00
&StateEventType::RoomMember,
user_id.as_str(),
)
.ok()
};
2024-12-06 12:45:20 +00:00
2025-02-02 07:40:08 +00:00
let lazy_state_ids: OptionFuture<_> = witness
2025-06-29 01:21:21 +00:00
.filter(|_| !encrypted_room)
2025-02-02 07:40:08 +00:00
.map(|witness| {
StreamExt::into_future(
witness
.iter()
.stream()
.broad_filter_map(|user_id| state_get_shorteventid(user_id)),
)
2025-02-02 07:40:08 +00:00
})
.into();
2025-06-29 01:21:21 +00:00
let state_diff_ids: OptionFuture<_> = incremental
2025-02-02 07:40:08 +00:00
.then(|| {
StreamExt::into_future(
services
.rooms
.state_accessor
2025-08-03 22:06:05 +00:00
.state_added((since_shortstatehash, horizon_shortstatehash))
.boxed(),
)
2025-02-02 07:40:08 +00:00
})
.into();
2025-06-29 01:21:21 +00:00
let current_state_ids: OptionFuture<_> = initial
2025-02-02 07:40:08 +00:00
.then(|| {
StreamExt::into_future(
services
.rooms
.state_accessor
2025-08-03 22:06:05 +00:00
.state_full_shortids(horizon_shortstatehash)
2025-07-08 11:48:45 +00:00
.expect_ok(),
)
2025-02-02 07:40:08 +00:00
})
.into();
let state_events = current_state_ids
.stream()
.chain(state_diff_ids.stream())
2025-06-29 01:21:21 +00:00
.broad_filter_map(async |(shortstatekey, shorteventid)| {
2025-02-02 07:40:08 +00:00
if witness.is_none() || encrypted_room {
return Some(shorteventid);
}
lazy_filter(services, sender_user, shortstatekey, shorteventid).await
})
.chain(lazy_state_ids.stream())
2025-02-02 07:40:08 +00:00
.broad_filter_map(|shorteventid| {
services
.rooms
.short
.get_eventid_from_short(shorteventid)
.ok()
})
2025-06-29 01:21:21 +00:00
.broad_filter_map(async |event_id: OwnedEventId| {
2025-04-22 04:42:26 +00:00
services
.rooms
.timeline
.get_pdu(&event_id)
.ok()
2025-06-29 01:21:21 +00:00
.await
2025-02-02 07:40:08 +00:00
})
.collect::<Vec<_>>()
2025-07-08 11:48:45 +00:00
.boxed()
2025-02-02 07:40:08 +00:00
.await;
2025-06-29 01:21:21 +00:00
let send_member_counts = state_events
2025-04-22 04:42:26 +00:00
.iter()
.any(|event| event.kind == RoomMember);
2024-12-06 12:45:20 +00:00
2025-06-29 01:21:21 +00:00
let member_counts: OptionFuture<_> = send_member_counts
.then(|| calculate_counts(services, room_id, sender_user))
.into();
let (joined_member_count, invited_member_count, heroes) =
2025-08-03 22:06:05 +00:00
member_counts.await.unwrap_or((None, None, None));
2024-12-06 12:45:20 +00:00
Ok(StateChanges {
heroes,
joined_member_count,
invited_member_count,
2025-02-02 07:40:08 +00:00
state_events,
2024-12-06 12:45:20 +00:00
})
}
2025-02-02 07:40:08 +00:00
async fn lazy_filter(
services: &Services,
sender_user: &UserId,
shortstatekey: ShortStateKey,
shorteventid: ShortEventId,
) -> Option<ShortEventId> {
let (event_type, state_key) = services
.rooms
.short
.get_statekey_from_short(shortstatekey)
.await
.ok()?;
(event_type != StateEventType::RoomMember || state_key == sender_user.as_str())
.then_some(shorteventid)
}
2024-12-06 12:45:20 +00:00
async fn calculate_counts(
services: &Services,
room_id: &RoomId,
sender_user: &UserId,
2025-06-29 01:21:21 +00:00
) -> (Option<u64>, Option<u64>, Option<Vec<OwnedUserId>>) {
2024-12-06 12:45:20 +00:00
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);
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
.then(|| calculate_heroes(services, room_id, sender_user))
.into();
2025-06-29 01:21:21 +00:00
(Some(joined_member_count), Some(invited_member_count), heroes.await)
2024-12-06 12:45:20 +00:00
}
async fn calculate_heroes(
services: &Services,
room_id: &RoomId,
sender_user: &UserId,
) -> Vec<OwnedUserId> {
2024-12-06 12:45:20 +00:00
services
.rooms
.timeline
.all_pdus(sender_user, room_id)
.ready_filter(|(_, pdu)| pdu.kind == RoomMember)
.fold_default(|heroes: Vec<_>, (_, pdu)| {
fold_hero(heroes, services, room_id, sender_user, pdu)
})
2024-12-06 12:45:20 +00:00
.await
}
async fn fold_hero(
mut heroes: Vec<OwnedUserId>,
services: &Services,
room_id: &RoomId,
sender_user: &UserId,
pdu: PduEvent,
2024-12-06 12:45:20 +00:00
) -> Vec<OwnedUserId> {
2025-04-22 04:42:26 +00: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;
};
if user_id == sender_user {
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(
2025-04-22 04:42:26 +00:00
services
.rooms
.state_cache
.is_invited(user_id, room_id),
services
.rooms
.state_cache
.is_joined(user_id, room_id),
2024-12-06 12:45:20 +00:00
)
.await;
if !is_joined && is_invited {
return heroes;
}
heroes.push(user_id.to_owned());
heroes
}
2025-07-27 01:43:59 +00:00
async fn typings_event_for_user(
services: &Services,
room_id: &RoomId,
sender_user: &UserId,
) -> Result<SyncEphemeralRoomEvent<TypingEventContent>> {
Ok(SyncEphemeralRoomEvent {
content: TypingEventContent {
user_ids: services
.rooms
.typing
.typing_users_for_user(room_id, sender_user)
.await?,
},
})
}