2024-10-16 05:32:27 +00:00
|
|
|
mod v3;
|
|
|
|
mod v4;
|
|
|
|
|
2024-12-14 21:58:01 -05:00
|
|
|
use conduwuit::{
|
2025-01-04 04:12:50 +00:00
|
|
|
utils::stream::{BroadbandExt, ReadyExt, TryIgnore},
|
2024-12-04 00:00:40 +00:00
|
|
|
PduCount,
|
|
|
|
};
|
2025-01-04 04:12:50 +00:00
|
|
|
use futures::{pin_mut, StreamExt};
|
2024-10-16 05:32:27 +00:00
|
|
|
use ruma::{RoomId, UserId};
|
|
|
|
|
|
|
|
pub(crate) use self::{v3::sync_events_route, v4::sync_events_v4_route};
|
|
|
|
use crate::{service::Services, Error, PduEvent, Result};
|
|
|
|
|
|
|
|
async fn load_timeline(
|
2024-12-15 00:05:47 -05:00
|
|
|
services: &Services,
|
|
|
|
sender_user: &UserId,
|
|
|
|
room_id: &RoomId,
|
|
|
|
roomsincecount: PduCount,
|
|
|
|
next_batch: Option<PduCount>,
|
|
|
|
limit: usize,
|
2024-10-16 05:32:27 +00:00
|
|
|
) -> Result<(Vec<(PduCount, PduEvent)>, bool), Error> {
|
2024-10-16 06:58:37 +00:00
|
|
|
let last_timeline_count = services
|
2024-10-16 05:32:27 +00:00
|
|
|
.rooms
|
|
|
|
.timeline
|
2024-11-07 04:49:01 +00:00
|
|
|
.last_timeline_count(Some(sender_user), room_id)
|
2024-10-16 06:58:37 +00:00
|
|
|
.await?;
|
|
|
|
|
|
|
|
if last_timeline_count <= roomsincecount {
|
|
|
|
return Ok((Vec::new(), false));
|
|
|
|
}
|
|
|
|
|
2025-01-04 04:12:50 +00:00
|
|
|
let non_timeline_pdus = services
|
2024-10-16 06:58:37 +00:00
|
|
|
.rooms
|
|
|
|
.timeline
|
2024-11-07 04:49:01 +00:00
|
|
|
.pdus_rev(Some(sender_user), room_id, None)
|
2025-01-04 04:12:50 +00:00
|
|
|
.ignore_err()
|
2024-11-20 19:42:34 +00:00
|
|
|
.ready_skip_while(|&(pducount, _)| pducount > next_batch.unwrap_or_else(PduCount::max))
|
|
|
|
.ready_take_while(|&(pducount, _)| pducount > roomsincecount);
|
2024-10-16 06:58:37 +00:00
|
|
|
|
|
|
|
// Take the last events for the timeline
|
2025-01-04 04:12:50 +00:00
|
|
|
pin_mut!(non_timeline_pdus);
|
|
|
|
let timeline_pdus: Vec<_> = non_timeline_pdus.by_ref().take(limit).collect().await;
|
|
|
|
|
|
|
|
let timeline_pdus: Vec<_> = timeline_pdus.into_iter().rev().collect();
|
2024-10-16 05:32:27 +00:00
|
|
|
|
2024-10-16 06:58:37 +00:00
|
|
|
// They /sync response doesn't always return all messages, so we say the output
|
|
|
|
// is limited unless there are events in non_timeline_pdus
|
|
|
|
let limited = non_timeline_pdus.next().await.is_some();
|
2024-10-16 05:32:27 +00:00
|
|
|
|
|
|
|
Ok((timeline_pdus, limited))
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn share_encrypted_room(
|
2024-12-15 00:05:47 -05:00
|
|
|
services: &Services,
|
|
|
|
sender_user: &UserId,
|
|
|
|
user_id: &UserId,
|
|
|
|
ignore_room: Option<&RoomId>,
|
2024-10-16 05:32:27 +00:00
|
|
|
) -> bool {
|
|
|
|
services
|
|
|
|
.rooms
|
2024-11-20 20:21:31 +00:00
|
|
|
.state_cache
|
2024-10-16 05:32:27 +00:00
|
|
|
.get_shared_rooms(sender_user, user_id)
|
|
|
|
.ready_filter(|&room_id| Some(room_id) != ignore_room)
|
2024-12-04 00:00:40 +00:00
|
|
|
.broad_any(|other_room_id| {
|
2024-10-16 05:32:27 +00:00
|
|
|
services
|
|
|
|
.rooms
|
|
|
|
.state_accessor
|
|
|
|
.is_encrypted_room(other_room_id)
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
}
|