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

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

707 lines
17 KiB
Rust
Raw Normal View History

2025-10-22 09:31:08 -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,
matrix::{
Event,
2025-10-22 09:31:08 -04:00
pdu::{PduCount, PduEvent},
},
ref_at,
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,
future::OptionStream,
2024-12-06 12:45:20 +00:00
math::ruma_from_u64,
stream::{BroadbandExt, Tools, WidebandExt},
2024-12-06 12:45:20 +00:00
},
};
2024-12-14 21:58:01 -05:00
use conduwuit_service::{
Services,
2025-01-25 07:18:33 +00:00
rooms::{
lazy_loading,
lazy_loading::{Options, Witness},
short::ShortStateHash,
},
2024-12-06 12:45:20 +00:00
};
use futures::{
FutureExt, StreamExt, TryFutureExt,
2025-10-22 09:31:08 -04:00
future::{OptionFuture, join, join3, join4, try_join4},
2024-07-07 06:17:58 +00:00
};
2020-07-30 18:14:47 +02:00
use ruma::{
2025-10-22 09:31:08 -04:00
OwnedEventId, OwnedRoomId, OwnedUserId, RoomId, UserId,
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::*,
room::member::{MembershipState, RoomMemberEventContent},
2024-08-30 10:31:08 +02:00
},
serde::Raw,
uint,
2020-07-30 18:14:47 +02:00
};
2025-02-02 07:40:08 +00:00
use service::rooms::short::{ShortEventId, ShortStateKey};
use tracing::trace;
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::{
TimelinePdus, ignored_filter,
sync::v3::{DeviceListUpdates, SyncContext},
2025-09-21 17:03:40 +00:00
};
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)]
2025-10-22 09:31:08 -04:00
pub(super) async fn load_joined_room(
services: &Services,
SyncContext {
sender_user,
sender_device,
since,
next_batch,
full_state,
filter,
}: SyncContext<'_>,
ref room_id: OwnedRoomId,
) -> Result<(JoinedRoom, DeviceListUpdates)> {
let mut device_list_updates = DeviceListUpdates::new();
let sincecount = since.map(PduCount::Normal);
let next_batchcount = PduCount::Normal(next_batch);
// the shortstatehash of the room's state right now
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
// the shortstatehash of what the room's state was when the `since` token was
// issued
let since_shortstatehash = OptionFuture::from(since.map(|since| {
services
.rooms
.user
.get_token_shortstatehash(room_id, since)
.ok()
}))
.map(|v| Ok(v.flatten()));
let timeline = load_timeline(
services,
sender_user,
room_id,
sincecount,
Some(next_batchcount),
10_usize,
);
2025-01-25 07:18:33 +00:00
let receipt_events = services
.rooms
.read_receipt
.readreceipts_since(room_id, since)
.filter_map(|(read_user, _, edu)| async move {
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, receipt_events) =
try_join4(current_shortstatehash, since_shortstatehash, timeline, receipt_events)
.boxed()
.await?;
2024-03-05 19:48:54 -05:00
let TimelinePdus { pdus: timeline_pdus, limited } = timeline;
let is_initial_sync = since_shortstatehash.is_none();
2025-02-02 07:40:08 +00:00
let timeline_start_shortstatehash = async {
if let Some((_, pdu)) = timeline_pdus.first() {
if let Ok(shortstatehash) = services
2025-02-02 07:40:08 +00:00
.rooms
.state_accessor
.pdu_shortstatehash(&pdu.event_id)
.await
{
return shortstatehash;
}
}
2025-10-22 09:03:25 -04:00
current_shortstatehash
};
2024-03-05 19:48:54 -05:00
let last_notification_read: OptionFuture<_> = timeline_pdus
.is_empty()
.then(|| {
services
.rooms
.user
.last_notification_read(sender_user, room_id)
})
.into();
2024-03-05 19:48:54 -05:00
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();
let is_encrypted_room = services
.rooms
.state_accessor
.state_get(current_shortstatehash, &StateEventType::RoomEncryption, "")
.is_ok();
let (
last_notification_read,
since_sender_member,
timeline_start_shortstatehash,
is_encrypted_room,
) = join4(
last_notification_read,
since_sender_member,
timeline_start_shortstatehash,
is_encrypted_room,
)
.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
});
let lazy_loading_enabled = (filter.room.state.lazy_load_options.is_enabled()
|| filter.room.timeline.lazy_load_options.is_enabled())
&& !full_state;
let lazy_loading_context = &lazy_loading::Context {
user_id: sender_user,
device_id: Some(sender_device),
2025-01-31 08:34:32 +00:00
room_id,
token: since,
options: Some(&filter.room.state.lazy_load_options),
};
let lazy_loading_witness = OptionFuture::from(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)
}))
.await;
/* replace with multiple steps
glossary for my own sanity:
- full state: every state event from the start of the room to the start of the timeline
- incremental state: state events from `since` to the start of the timeline
- state_events: the `state` key on the JSON object we return
if initial sync or full_state:
get full state
use full state as state_events
else if TL is limited:
get incremental state
use incremental state as state_events
if encryption is enabled:
use incremental state to extend device list
else:
state_events is empty
compute counts and heroes from state_events
*/
let mut state_events = if is_initial_sync || full_state {
// reset lazy loading state on initial sync
if is_initial_sync {
services
.rooms
.lazy_loading
.reset(lazy_loading_context)
.await;
2025-10-22 09:03:25 -04:00
}
2025-10-22 09:03:25 -04:00
calculate_state_initial(
services,
sender_user,
timeline_start_shortstatehash,
lazy_loading_witness.as_ref(),
)
.boxed()
2025-10-22 09:03:25 -04:00
.await?
} else if limited {
let state_incremental = calculate_state_incremental(
services,
sender_user,
since_shortstatehash,
timeline_start_shortstatehash,
lazy_loading_witness.as_ref(),
)
.boxed()
.await?;
// calculate device list updates for E2EE
if is_encrypted_room {
// add users with changed keys to the `changed` list
services
.users
.room_keys_changed(room_id, since, Some(next_batch))
.map(at!(0))
.map(ToOwned::to_owned)
.ready_for_each(|user_id| {
device_list_updates.changed.insert(user_id);
})
.await;
// add users who now share encrypted rooms to `changed` and
// users who no longer share encrypted rooms to `left`
for state_event in &state_incremental {
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;
};
2025-10-22 09:03:25 -04:00
{
use MembershipState::*;
if matches!(content.membership, Leave | Join) {
let shares_encrypted_room = share_encrypted_room(
services,
sender_user,
&user_id,
Some(room_id),
)
.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);
},
| _ => (),
}
}
}
}
}
}
state_incremental
} else {
vec![]
};
// 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) =
if state_events.iter().any(|event| event.kind == RoomMember) {
calculate_counts(services, room_id, sender_user).await?
} 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()
.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();
2025-10-22 09:03:25 -04:00
let prev_batch = timeline_pdus
.first()
.map(at!(0))
.or_else(|| joined_sender_member.is_some().and(since).map(Into::into));
2025-02-05 05:10:30 +00:00
let timeline_pdus = timeline_pdus
2025-02-05 05:10:30 +00:00
.into_iter()
.stream()
.wide_filter_map(|item| ignored_filter(services, item, sender_user))
.map(at!(1))
.chain(joined_sender_member.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<_>>();
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();
2024-03-05 19:48:54 -05:00
let send_notification_counts =
last_notification_read.is_none_or(|count| since.is_none_or(|since| count > since));
2025-01-25 07:18:33 +00:00
let notification_count: OptionFuture<_> = send_notification_counts
.then(|| {
services
.rooms
.user
.notification_count(sender_user, room_id)
.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)
.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 {
if since.is_some_and(|since| count <= since) {
2025-02-02 07:40:08 +00:00
return Ok(Vec::<Raw<AnySyncEphemeralRoomEvent>>::new());
}
let typings = services
.rooms
.typing
.typings_all(room_id, sender_user)
.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);
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
let last_privateread_update = if let Some(since) = since {
services
.rooms
.read_receipt
.last_privateread_update(sender_user, room_id)
.await > since
} else {
true
};
2024-12-10 22:54:19 -05:00
let private_read_event = if last_privateread_update {
services
.rooms
.read_receipt
.private_read_get(room_id, sender_user)
.await
.ok()
} else {
None
};
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())
.collect();
2024-03-05 19:48:54 -05:00
2023-03-13 10:39:02 +01:00
// Save the state after this sync so we can send the correct state diff next
// sync
2024-07-16 08:05:25 +00:00
services
2024-03-25 17:05:11 -04:00
.rooms
.user
.associate_token_shortstatehash(room_id, next_batch, 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 {
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
},
unread_notifications: UnreadNotificationsCount { highlight_count, notification_count },
2023-03-13 10:39:02 +01:00
timeline: Timeline {
limited,
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
},
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
};
Ok((joined_room, device_list_updates))
2024-12-06 12:45:20 +00:00
}
/// Calculate the "initial state", or all events from the start of the room up
/// to (but not including) the `current_shortstatehash`. If
/// `lazy_loading_witness` is `None`, lazy loading will be disabled.
#[tracing::instrument(
name = "initial",
level = "trace",
skip_all,
fields(current_shortstatehash)
)]
2024-12-06 12:45:20 +00:00
#[allow(clippy::too_many_arguments)]
async fn calculate_state_initial(
services: &Services,
sender_user: &UserId,
current_shortstatehash: ShortStateHash,
lazy_loading_witness: Option<&Witness>,
) -> Result<Vec<PduEvent>> {
2025-01-29 01:04:02 +00:00
let (shortstatekeys, event_ids): (Vec<_>, Vec<_>) = services
2024-12-06 12:45:20 +00:00
.rooms
.state_accessor
.state_full_ids(current_shortstatehash)
2025-01-29 01:04:02 +00:00
.unzip()
.await;
2024-12-06 12:45:20 +00:00
trace!("event ids for initial sync @ {:?}: {:?}", current_shortstatehash, event_ids);
services
2025-01-25 07:18:33 +00:00
.rooms
.short
2025-01-29 01:04:02 +00:00
.multi_get_statekey_from_short(shortstatekeys.into_iter().stream())
.zip(event_ids.into_iter().stream())
2025-01-25 07:18:33 +00:00
.ready_filter_map(|item| Some((item.0.ok()?, item.1)))
.ready_filter_map(|((event_type, state_key), event_id)| {
let lazy = lazy_loading_witness.is_some_and(|witness| {
event_type == StateEventType::RoomMember
&& state_key.as_str().try_into().is_ok_and(|user_id: &UserId| {
sender_user != user_id && !witness.contains(user_id)
})
});
2024-12-06 12:45:20 +00:00
2025-01-31 08:34:32 +00:00
lazy.or_some(event_id)
2025-01-25 07:18:33 +00:00
})
.broad_filter_map(|event_id: OwnedEventId| async move {
services.rooms.timeline.get_pdu(&event_id).await.ok()
2024-12-06 12:45:20 +00:00
})
2025-01-25 07:18:33 +00:00
.collect()
.map(Ok)
.await
2020-07-30 18:14:47 +02:00
}
2024-12-06 12:45:20 +00:00
/// Calculate the "incremental state", or all events from the
/// `since_shortstatehash` up to (but not including)
/// the `current_shortstatehash`. If `lazy_loading_witness` is `None`, lazy
/// loading will be disabled.
#[tracing::instrument(name = "incremental", level = "trace", skip_all)]
2024-12-06 12:45:20 +00:00
#[allow(clippy::too_many_arguments)]
2025-01-31 08:34:32 +00:00
async fn calculate_state_incremental<'a>(
services: &Services,
2025-02-02 07:40:08 +00:00
sender_user: &'a UserId,
since_shortstatehash: Option<ShortStateHash>,
current_shortstatehash: ShortStateHash,
lazy_loading_witness: Option<&'a Witness>,
) -> Result<Vec<PduEvent>> {
2025-01-31 08:34:32 +00:00
let since_shortstatehash = since_shortstatehash.unwrap_or(current_shortstatehash);
2024-12-06 12:45:20 +00:00
2025-02-02 07:40:08 +00:00
let state_get_shorteventid = |user_id: &'a UserId| {
2025-01-31 08:34:32 +00:00
services
2024-12-06 12:45:20 +00:00
.rooms
.state_accessor
2025-02-02 07:40:08 +00:00
.state_get_shortid(
current_shortstatehash,
&StateEventType::RoomMember,
user_id.as_str(),
)
2025-01-31 08:34:32 +00:00
.ok()
};
2024-12-06 12:45:20 +00:00
let lazy_state_ids: OptionFuture<_> = lazy_loading_witness
2025-01-31 08:34:32 +00:00
.map(|witness| {
StreamExt::into_future(
witness
.iter()
.stream()
.broad_filter_map(|user_id| state_get_shorteventid(user_id)),
)
2025-01-31 08:34:32 +00:00
})
.into();
let state_diff_shortids = services
.rooms
.state_accessor
.state_added((since_shortstatehash, current_shortstatehash))
.boxed();
2025-01-31 08:34:32 +00:00
state_diff_shortids
2025-02-02 07:40:08 +00:00
.broad_filter_map(|(shortstatekey, shorteventid)| async move {
if lazy_loading_witness.is_none() {
2025-02-02 07:40:08 +00:00
return Some(shorteventid);
}
lazy_filter(services, sender_user, shortstatekey, shorteventid).await
2025-01-31 08:34:32 +00:00
})
.chain(lazy_state_ids.stream())
2025-02-02 07:40:08 +00:00
.broad_filter_map(|shorteventid| {
2025-01-31 08:34:32 +00:00
services
.rooms
2025-02-02 07:40:08 +00:00
.short
.get_eventid_from_short(shorteventid)
2025-01-31 08:34:32 +00:00
.ok()
})
2025-02-02 07:40:08 +00:00
.broad_filter_map(|event_id: OwnedEventId| async move {
services.rooms.timeline.get_pdu(&event_id).await.ok()
})
.collect::<Vec<_>>()
.map(Ok)
.await
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,
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);
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();
Ok((Some(joined_member_count), Some(invited_member_count), heroes.await))
}
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> {
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(
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
}