2025-10-07 21:35:42 +00:00
|
|
|
use std::{cmp::Ordering, collections::HashSet};
|
2025-10-05 19:48:07 +00:00
|
|
|
|
|
|
|
|
use futures::{
|
2025-10-07 10:57:16 +00:00
|
|
|
FutureExt, StreamExt, TryFutureExt, TryStreamExt,
|
2025-10-05 19:48:07 +00:00
|
|
|
future::{OptionFuture, join, join3, join4},
|
|
|
|
|
};
|
|
|
|
|
use ruma::{
|
|
|
|
|
JsOption, MxcUri, OwnedMxcUri, RoomId, UInt, UserId,
|
2025-10-07 21:35:42 +00:00
|
|
|
api::client::sync::sync_events::{
|
|
|
|
|
UnreadNotificationsCount,
|
|
|
|
|
v5::{DisplayName, response},
|
|
|
|
|
},
|
|
|
|
|
events::{
|
|
|
|
|
StateEventType,
|
|
|
|
|
TimelineEventType::{
|
|
|
|
|
self, Beacon, CallInvite, PollStart, RoomEncrypted, RoomMessage, Sticker,
|
|
|
|
|
},
|
|
|
|
|
room::member::MembershipState,
|
|
|
|
|
},
|
2025-10-05 19:48:07 +00:00
|
|
|
};
|
|
|
|
|
use tuwunel_core::{
|
2025-10-07 21:35:42 +00:00
|
|
|
Result, at, debug_error, err, is_equal_to,
|
2025-10-05 19:48:07 +00:00
|
|
|
matrix::{Event, StateKey, pdu::PduCount},
|
|
|
|
|
ref_at,
|
2025-10-07 21:35:42 +00:00
|
|
|
utils::{
|
|
|
|
|
BoolExt, IterStream, ReadyExt, TryFutureExtExt, math::usize_from_ruma, result::FlatOk,
|
|
|
|
|
stream::BroadbandExt,
|
|
|
|
|
},
|
2025-10-05 19:48:07 +00:00
|
|
|
};
|
2025-10-07 21:35:42 +00:00
|
|
|
use tuwunel_service::{Services, sync::Room};
|
|
|
|
|
|
|
|
|
|
use super::{super::load_timeline, Connection, SyncInfo, WindowRoom};
|
|
|
|
|
use crate::client::ignored_filter;
|
2025-10-05 19:48:07 +00:00
|
|
|
|
2025-10-07 21:35:42 +00:00
|
|
|
static DEFAULT_BUMP_TYPES: [TimelineEventType; 6] =
|
|
|
|
|
[CallInvite, PollStart, Beacon, RoomEncrypted, RoomMessage, Sticker];
|
2025-10-05 19:48:07 +00:00
|
|
|
|
2025-10-07 21:35:42 +00:00
|
|
|
#[tracing::instrument(
|
|
|
|
|
name = "room",
|
|
|
|
|
level = "debug",
|
|
|
|
|
skip_all,
|
|
|
|
|
fields(room_id, roomsince)
|
|
|
|
|
)]
|
2025-10-05 19:48:07 +00:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
|
|
|
pub(super) async fn handle(
|
2025-10-07 21:35:42 +00:00
|
|
|
SyncInfo { services, sender_user, .. }: SyncInfo<'_>,
|
|
|
|
|
conn: &Connection,
|
|
|
|
|
WindowRoom { lists, membership, room_id, .. }: &WindowRoom,
|
2025-10-05 19:48:07 +00:00
|
|
|
) -> Result<Option<response::Room>> {
|
2025-10-07 21:35:42 +00:00
|
|
|
debug_assert!(DEFAULT_BUMP_TYPES.is_sorted(), "DEFAULT_BUMP_TYPES is not sorted");
|
|
|
|
|
|
2025-10-21 05:25:27 +00:00
|
|
|
let &Room { roomsince, .. } = conn
|
2025-10-07 21:35:42 +00:00
|
|
|
.rooms
|
|
|
|
|
.get(room_id)
|
|
|
|
|
.ok_or_else(|| err!("Missing connection state for {room_id}"))?;
|
|
|
|
|
|
|
|
|
|
let is_invite = *membership == Some(MembershipState::Invite);
|
|
|
|
|
let default_details = (0_usize, HashSet::new());
|
|
|
|
|
let (timeline_limit, required_state) = lists
|
|
|
|
|
.iter()
|
|
|
|
|
.filter_map(|list_id| conn.lists.get(list_id))
|
|
|
|
|
.map(|list| &list.room_details)
|
|
|
|
|
.chain(conn.subscriptions.get(room_id).into_iter())
|
|
|
|
|
.fold(default_details, |(mut timeline_limit, mut required_state), config| {
|
|
|
|
|
let limit = usize_from_ruma(config.timeline_limit);
|
|
|
|
|
|
|
|
|
|
timeline_limit = timeline_limit.max(limit);
|
|
|
|
|
required_state.extend(config.required_state.clone());
|
|
|
|
|
|
|
|
|
|
(timeline_limit, required_state)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let timeline: OptionFuture<_> = is_invite
|
|
|
|
|
.is_false()
|
2025-10-05 19:48:07 +00:00
|
|
|
.then(|| {
|
|
|
|
|
load_timeline(
|
|
|
|
|
services,
|
|
|
|
|
sender_user,
|
|
|
|
|
room_id,
|
2025-10-06 01:12:01 +00:00
|
|
|
PduCount::Normal(roomsince),
|
2025-10-07 21:35:42 +00:00
|
|
|
Some(PduCount::from(conn.next_batch)),
|
2025-10-06 01:12:01 +00:00
|
|
|
timeline_limit,
|
2025-10-05 19:48:07 +00:00
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
.into();
|
|
|
|
|
|
|
|
|
|
let Ok(timeline) = timeline.await.transpose() else {
|
|
|
|
|
debug_error!(?room_id, "Missing timeline.");
|
|
|
|
|
return Ok(None);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let (timeline_pdus, limited, _lastcount) =
|
|
|
|
|
timeline.unwrap_or_else(|| (Vec::new(), true, PduCount::default()));
|
|
|
|
|
|
2025-10-07 21:35:42 +00:00
|
|
|
if roomsince != 0 && timeline_pdus.is_empty() && !is_invite {
|
2025-10-05 19:48:07 +00:00
|
|
|
return Ok(None);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let prev_batch = timeline_pdus
|
|
|
|
|
.first()
|
|
|
|
|
.map(at!(0))
|
|
|
|
|
.map(PduCount::into_unsigned)
|
2025-10-06 01:12:01 +00:00
|
|
|
.or_else(|| roomsince.ne(&0).then_some(roomsince))
|
2025-10-05 19:48:07 +00:00
|
|
|
.as_ref()
|
|
|
|
|
.map(ToString::to_string);
|
|
|
|
|
|
|
|
|
|
let bump_stamp = timeline_pdus
|
|
|
|
|
.iter()
|
|
|
|
|
.filter(|(_, pdu)| {
|
|
|
|
|
DEFAULT_BUMP_TYPES
|
|
|
|
|
.binary_search(pdu.event_type())
|
|
|
|
|
.is_ok()
|
|
|
|
|
})
|
|
|
|
|
.fold(Option::<UInt>::None, |mut bump_stamp, (_, pdu)| {
|
2025-10-07 21:35:42 +00:00
|
|
|
let ts = pdu.origin_server_ts().0;
|
2025-10-05 19:48:07 +00:00
|
|
|
if bump_stamp.is_none_or(|bump_stamp| bump_stamp < ts) {
|
|
|
|
|
bump_stamp.replace(ts);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bump_stamp
|
|
|
|
|
});
|
|
|
|
|
|
2025-10-07 21:35:42 +00:00
|
|
|
let lazy = required_state
|
2025-10-05 19:48:07 +00:00
|
|
|
.iter()
|
|
|
|
|
.any(is_equal_to!(&(StateEventType::RoomMember, "$LAZY".into())));
|
|
|
|
|
|
|
|
|
|
let mut timeline_senders: Vec<_> = timeline_pdus
|
|
|
|
|
.iter()
|
|
|
|
|
.filter(|_| lazy)
|
|
|
|
|
.map(ref_at!(1))
|
|
|
|
|
.map(Event::sender)
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
timeline_senders.sort();
|
|
|
|
|
timeline_senders.dedup();
|
|
|
|
|
let timeline_senders = timeline_senders
|
|
|
|
|
.iter()
|
2025-10-07 10:57:16 +00:00
|
|
|
.map(|sender| (StateEventType::RoomMember, StateKey::from_str(sender.as_str())))
|
|
|
|
|
.stream();
|
|
|
|
|
|
2025-10-07 21:35:42 +00:00
|
|
|
let wildcard_state = required_state
|
2025-10-07 10:57:16 +00:00
|
|
|
.iter()
|
|
|
|
|
.filter(|(_, state_key)| state_key == "*")
|
|
|
|
|
.map(|(event_type, _)| {
|
|
|
|
|
services
|
|
|
|
|
.state_accessor
|
|
|
|
|
.room_state_keys(room_id, event_type)
|
|
|
|
|
.map_ok(|state_key| (event_type.clone(), state_key))
|
|
|
|
|
.ready_filter_map(Result::ok)
|
|
|
|
|
})
|
|
|
|
|
.stream()
|
|
|
|
|
.flatten();
|
2025-10-05 19:48:07 +00:00
|
|
|
|
2025-10-07 21:35:42 +00:00
|
|
|
let required_state = required_state
|
2025-10-05 19:48:07 +00:00
|
|
|
.iter()
|
|
|
|
|
.cloned()
|
|
|
|
|
.stream()
|
2025-10-07 10:57:16 +00:00
|
|
|
.chain(wildcard_state)
|
|
|
|
|
.chain(timeline_senders)
|
2025-10-05 19:48:07 +00:00
|
|
|
.broad_filter_map(async |state| {
|
|
|
|
|
let state_key: StateKey = match state.1.as_str() {
|
2025-10-07 10:57:16 +00:00
|
|
|
| "$LAZY" | "*" => return None,
|
2025-10-05 19:48:07 +00:00
|
|
|
| "$ME" => sender_user.as_str().into(),
|
|
|
|
|
| _ => state.1.clone(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
services
|
|
|
|
|
.state_accessor
|
|
|
|
|
.room_state_get(room_id, &state.0, &state_key)
|
|
|
|
|
.map_ok(Event::into_format)
|
|
|
|
|
.ok()
|
|
|
|
|
.await
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
// TODO: figure out a timestamp we can use for remote invites
|
2025-10-07 21:35:42 +00:00
|
|
|
let invite_state: OptionFuture<_> = is_invite
|
2025-10-05 19:48:07 +00:00
|
|
|
.then(|| {
|
|
|
|
|
services
|
|
|
|
|
.state_cache
|
|
|
|
|
.invite_state(sender_user, room_id)
|
|
|
|
|
.ok()
|
|
|
|
|
})
|
|
|
|
|
.into();
|
|
|
|
|
|
|
|
|
|
let timeline = timeline_pdus
|
|
|
|
|
.iter()
|
|
|
|
|
.stream()
|
|
|
|
|
.filter_map(|item| ignored_filter(services, item.clone(), sender_user))
|
|
|
|
|
.map(at!(1))
|
|
|
|
|
.map(Event::into_format)
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
let room_name = services
|
|
|
|
|
.state_accessor
|
|
|
|
|
.get_name(room_id)
|
2025-10-07 21:35:42 +00:00
|
|
|
.map_ok(Into::into)
|
2025-10-05 19:48:07 +00:00
|
|
|
.map(Result::ok);
|
|
|
|
|
|
|
|
|
|
let room_avatar = services
|
|
|
|
|
.state_accessor
|
|
|
|
|
.get_avatar(room_id)
|
|
|
|
|
.map_ok(|content| content.url)
|
|
|
|
|
.ok()
|
|
|
|
|
.map(Option::flatten);
|
|
|
|
|
|
|
|
|
|
let highlight_count = services
|
|
|
|
|
.user
|
|
|
|
|
.highlight_count(sender_user, room_id)
|
|
|
|
|
.map(TryInto::try_into)
|
|
|
|
|
.map(Result::ok);
|
|
|
|
|
|
|
|
|
|
let notification_count = services
|
|
|
|
|
.user
|
|
|
|
|
.notification_count(sender_user, room_id)
|
|
|
|
|
.map(TryInto::try_into)
|
|
|
|
|
.map(Result::ok);
|
|
|
|
|
|
|
|
|
|
let joined_count = services
|
|
|
|
|
.state_cache
|
|
|
|
|
.room_joined_count(room_id)
|
|
|
|
|
.map_ok(TryInto::try_into)
|
|
|
|
|
.map_ok(Result::ok)
|
|
|
|
|
.map(FlatOk::flat_ok);
|
|
|
|
|
|
|
|
|
|
let invited_count = services
|
|
|
|
|
.state_cache
|
|
|
|
|
.room_invited_count(room_id)
|
|
|
|
|
.map_ok(TryInto::try_into)
|
|
|
|
|
.map_ok(Result::ok)
|
|
|
|
|
.map(FlatOk::flat_ok);
|
|
|
|
|
|
2025-10-07 21:35:42 +00:00
|
|
|
let is_dm = services
|
|
|
|
|
.state_accessor
|
|
|
|
|
.is_direct(room_id, sender_user)
|
|
|
|
|
.map(|is_dm| is_dm.then_some(is_dm));
|
|
|
|
|
|
|
|
|
|
let meta = join3(room_name, room_avatar, is_dm);
|
2025-10-05 19:48:07 +00:00
|
|
|
let events = join3(timeline, required_state, invite_state);
|
|
|
|
|
let member_counts = join(joined_count, invited_count);
|
|
|
|
|
let notification_counts = join(highlight_count, notification_count);
|
|
|
|
|
let (
|
2025-10-07 21:35:42 +00:00
|
|
|
(room_name, room_avatar, is_dm),
|
2025-10-05 19:48:07 +00:00
|
|
|
(timeline, required_state, invite_state),
|
|
|
|
|
(joined_count, invited_count),
|
|
|
|
|
(highlight_count, notification_count),
|
|
|
|
|
) = join4(meta, events, member_counts, notification_counts)
|
|
|
|
|
.boxed()
|
|
|
|
|
.await;
|
|
|
|
|
|
|
|
|
|
let (heroes, hero_name, heroes_avatar) = calculate_heroes(
|
|
|
|
|
services,
|
|
|
|
|
sender_user,
|
|
|
|
|
room_id,
|
2025-10-07 21:35:42 +00:00
|
|
|
room_name.as_ref(),
|
2025-10-05 19:48:07 +00:00
|
|
|
room_avatar.as_deref(),
|
|
|
|
|
)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
let num_live = None; // Count events in timeline greater than global sync counter
|
|
|
|
|
|
|
|
|
|
Ok(Some(response::Room {
|
2025-10-06 01:12:01 +00:00
|
|
|
initial: Some(roomsince == 0),
|
2025-10-07 21:35:42 +00:00
|
|
|
lists: lists.clone(),
|
|
|
|
|
membership: membership.clone(),
|
2025-10-05 19:48:07 +00:00
|
|
|
name: room_name.or(hero_name),
|
|
|
|
|
avatar: JsOption::from_option(room_avatar.or(heroes_avatar)),
|
2025-10-07 21:35:42 +00:00
|
|
|
is_dm,
|
2025-10-05 19:48:07 +00:00
|
|
|
required_state,
|
2025-10-07 21:35:42 +00:00
|
|
|
invite_state: invite_state.flatten(),
|
|
|
|
|
prev_batch: prev_batch.as_deref().map(Into::into),
|
2025-10-05 19:48:07 +00:00
|
|
|
limited,
|
2025-10-07 21:35:42 +00:00
|
|
|
timeline,
|
2025-10-05 19:48:07 +00:00
|
|
|
bump_stamp,
|
|
|
|
|
heroes,
|
|
|
|
|
num_live,
|
|
|
|
|
joined_count,
|
|
|
|
|
invited_count,
|
|
|
|
|
unread_notifications: UnreadNotificationsCount { highlight_count, notification_count },
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-07 21:35:42 +00:00
|
|
|
#[tracing::instrument(name = "heroes", level = "trace", skip_all)]
|
2025-10-05 19:48:07 +00:00
|
|
|
#[allow(clippy::type_complexity)]
|
|
|
|
|
async fn calculate_heroes(
|
|
|
|
|
services: &Services,
|
|
|
|
|
sender_user: &UserId,
|
|
|
|
|
room_id: &RoomId,
|
2025-10-07 21:35:42 +00:00
|
|
|
room_name: Option<&DisplayName>,
|
2025-10-05 19:48:07 +00:00
|
|
|
room_avatar: Option<&MxcUri>,
|
2025-10-07 21:35:42 +00:00
|
|
|
) -> Result<(Option<Vec<response::Hero>>, Option<DisplayName>, Option<OwnedMxcUri>)> {
|
2025-10-05 19:48:07 +00:00
|
|
|
const MAX_HEROES: usize = 5;
|
|
|
|
|
let heroes: Vec<_> = services
|
|
|
|
|
.state_cache
|
|
|
|
|
.room_members(room_id)
|
|
|
|
|
.ready_filter(|&member| member != sender_user)
|
|
|
|
|
.ready_filter_map(|member| room_name.is_none().then_some(member))
|
|
|
|
|
.map(ToOwned::to_owned)
|
|
|
|
|
.broadn_filter_map(MAX_HEROES, async |user_id| {
|
|
|
|
|
let content = services
|
|
|
|
|
.state_accessor
|
|
|
|
|
.get_member(room_id, &user_id)
|
|
|
|
|
.await
|
|
|
|
|
.ok()?;
|
|
|
|
|
|
|
|
|
|
let name: OptionFuture<_> = content
|
|
|
|
|
.displayname
|
|
|
|
|
.is_none()
|
|
|
|
|
.then(|| services.users.displayname(&user_id).ok())
|
|
|
|
|
.into();
|
|
|
|
|
|
|
|
|
|
let avatar: OptionFuture<_> = content
|
|
|
|
|
.avatar_url
|
|
|
|
|
.is_none()
|
|
|
|
|
.then(|| services.users.avatar_url(&user_id).ok())
|
|
|
|
|
.into();
|
|
|
|
|
|
|
|
|
|
let (name, avatar) = join(name, avatar).await;
|
|
|
|
|
let hero = response::Hero {
|
|
|
|
|
user_id,
|
|
|
|
|
avatar: avatar.unwrap_or(content.avatar_url),
|
2025-10-07 21:35:42 +00:00
|
|
|
name: name
|
|
|
|
|
.unwrap_or(content.displayname)
|
|
|
|
|
.map(Into::into),
|
2025-10-05 19:48:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Some(hero)
|
|
|
|
|
})
|
|
|
|
|
.take(MAX_HEROES)
|
|
|
|
|
.collect()
|
|
|
|
|
.await;
|
|
|
|
|
|
|
|
|
|
let hero_name = match heroes.len().cmp(&(1_usize)) {
|
|
|
|
|
| Ordering::Less => None,
|
|
|
|
|
| Ordering::Equal => Some(
|
|
|
|
|
heroes[0]
|
|
|
|
|
.name
|
|
|
|
|
.clone()
|
2025-10-07 21:35:42 +00:00
|
|
|
.unwrap_or_else(|| heroes[0].user_id.as_str().into()),
|
2025-10-05 19:48:07 +00:00
|
|
|
),
|
|
|
|
|
| Ordering::Greater => {
|
|
|
|
|
let firsts = heroes[1..]
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|h| {
|
|
|
|
|
h.name
|
|
|
|
|
.clone()
|
2025-10-07 21:35:42 +00:00
|
|
|
.unwrap_or_else(|| h.user_id.as_str().into())
|
2025-10-05 19:48:07 +00:00
|
|
|
})
|
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
|
.join(", ");
|
|
|
|
|
|
|
|
|
|
let last = heroes[0]
|
|
|
|
|
.name
|
|
|
|
|
.clone()
|
2025-10-07 21:35:42 +00:00
|
|
|
.unwrap_or_else(|| heroes[0].user_id.as_str().into());
|
2025-10-05 19:48:07 +00:00
|
|
|
|
2025-10-07 21:35:42 +00:00
|
|
|
Some(format!("{firsts} and {last}")).map(Into::into)
|
2025-10-05 19:48:07 +00:00
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let heroes_avatar = (room_avatar.is_none() && room_name.is_none())
|
|
|
|
|
.then(|| {
|
|
|
|
|
heroes
|
|
|
|
|
.first()
|
|
|
|
|
.and_then(|hero| hero.avatar.clone())
|
|
|
|
|
})
|
|
|
|
|
.flatten();
|
|
|
|
|
|
|
|
|
|
Ok((Some(heroes), hero_name, heroes_avatar))
|
|
|
|
|
}
|