Files
continuwuity/src/api/client/context.rs
T

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

244 lines
5.9 KiB
Rust
Raw Normal View History

2022-10-05 20:34:31 +02:00
use std::collections::HashSet;
2024-03-05 19:48:54 -05:00
2024-07-16 08:05:25 +00:00
use axum::extract::State;
2024-08-08 17:18:30 +00:00
use conduit::{err, error, Err};
use futures::StreamExt;
2022-01-04 14:30:13 +01:00
use ruma::{
2024-08-08 17:18:30 +00:00
api::client::{context::get_context, filter::LazyLoadOptions},
events::{StateEventType, TimelineEventType::*},
2022-01-04 14:30:13 +01:00
};
2020-07-30 18:14:47 +02:00
2024-08-08 17:18:30 +00:00
use crate::{Result, Ruma};
2024-03-05 19:48:54 -05:00
/// # `GET /_matrix/client/r0/rooms/{roomId}/context/{eventId}`
2021-08-31 19:14:37 +02:00
///
/// Allows loading room history around an event.
///
/// - Only works if the user is joined (TODO: always allow, but only show events
2024-06-16 00:36:49 +00:00
/// if the user was joined, depending on history_visibility)
2024-07-16 08:05:25 +00:00
pub(crate) async fn get_context_route(
State(services): State<crate::State>, body: Ruma<get_context::v3::Request>,
) -> Result<get_context::v3::Response> {
2020-10-18 20:33:12 +02:00
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
2022-01-04 14:30:13 +01:00
let sender_device = body.sender_device.as_ref().expect("user is authenticated");
2024-03-05 19:48:54 -05:00
// some clients, at least element, seem to require knowledge of redundant
// members for "inline" profiles on the timeline to work properly
2022-02-12 02:06:30 +01:00
let (lazy_load_enabled, lazy_load_send_redundant) = match &body.filter.lazy_load_options {
2022-02-04 13:30:42 +01:00
LazyLoadOptions::Enabled {
2022-02-12 02:06:30 +01:00
include_redundant_members,
} => (true, *include_redundant_members),
LazyLoadOptions::Disabled => (false, cfg!(feature = "element_hacks")),
2022-02-04 13:30:42 +01:00
};
2024-03-05 19:48:54 -05:00
let mut lazy_loaded = HashSet::with_capacity(100);
2024-03-05 19:48:54 -05:00
2024-07-16 08:05:25 +00:00
let base_token = services
2021-03-23 12:59:27 +01:00
.rooms
2022-09-07 13:25:51 +02:00
.timeline
2024-08-08 17:18:30 +00:00
.get_pdu_count(&body.event_id)
.await
.map_err(|_| err!(Request(NotFound("Base event id not found."))))?;
2024-03-05 19:48:54 -05:00
2024-07-16 08:05:25 +00:00
let base_event = services
2023-02-20 22:59:45 +01:00
.rooms
.timeline
2024-08-08 17:18:30 +00:00
.get_pdu(&body.event_id)
.await
.map_err(|_| err!(Request(NotFound("Base event not found."))))?;
2024-03-05 19:48:54 -05:00
2024-08-08 17:18:30 +00:00
let room_id = &base_event.room_id;
2024-03-05 19:48:54 -05:00
2024-07-16 08:05:25 +00:00
if !services
2024-03-25 17:05:11 -04:00
.rooms
.state_accessor
2024-08-08 17:18:30 +00:00
.user_can_see_event(sender_user, room_id, &body.event_id)
.await
2024-03-25 17:05:11 -04:00
{
2024-08-08 17:18:30 +00:00
return Err!(Request(Forbidden("You don't have permission to view this event.")));
2022-02-04 13:30:42 +01:00
}
2024-03-05 19:48:54 -05:00
2024-08-08 17:18:30 +00:00
if !services
.rooms
.lazy_loading
.lazy_load_was_sent_before(sender_user, sender_device, room_id, &base_event.sender)
.await || lazy_load_send_redundant
2022-02-04 13:30:42 +01:00
{
lazy_loaded.insert(base_event.sender.as_str().to_owned());
2022-01-04 14:30:13 +01:00
}
2024-03-05 19:48:54 -05:00
2024-05-04 09:45:37 -04:00
// Use limit or else 10, with maximum 100
let limit = usize::try_from(body.limit).unwrap_or(10).min(100);
2024-03-05 19:48:54 -05:00
2022-01-04 14:30:13 +01:00
let base_event = base_event.to_room_event();
2024-03-05 19:48:54 -05:00
2024-07-16 08:05:25 +00:00
let events_before: Vec<_> = services
2020-07-30 18:14:47 +02:00
.rooms
2022-09-07 13:25:51 +02:00
.timeline
2024-08-08 17:18:30 +00:00
.pdus_until(sender_user, room_id, base_token)
.await?
2023-06-25 19:31:40 +02:00
.take(limit / 2)
2024-08-08 17:18:30 +00:00
.filter_map(|(count, pdu)| async move {
// list of safe and common non-state events to ignore
if matches!(
&pdu.kind,
RoomMessage
| Sticker | CallInvite
| CallNotify | RoomEncrypted
| Image | File | Audio
| Voice | Video | UnstablePollStart
| PollStart | KeyVerificationStart
| Reaction | Emote
| Location
) && services
.users
.user_is_ignored(&pdu.sender, sender_user)
.await
{
return None;
}
2024-07-16 08:05:25 +00:00
services
2023-02-22 15:49:55 +01:00
.rooms
.state_accessor
2024-08-08 17:18:30 +00:00
.user_can_see_event(sender_user, room_id, &pdu.event_id)
.await
.then_some((count, pdu))
2023-02-22 15:49:55 +01:00
})
2024-08-08 17:18:30 +00:00
.collect()
.await;
2024-03-05 19:48:54 -05:00
2022-01-04 14:30:13 +01:00
for (_, event) in &events_before {
2024-08-08 17:18:30 +00:00
if !services
.rooms
.lazy_loading
.lazy_load_was_sent_before(sender_user, sender_device, room_id, &event.sender)
.await || lazy_load_send_redundant
2022-02-04 13:30:42 +01:00
{
lazy_loaded.insert(event.sender.as_str().to_owned());
2022-01-04 14:30:13 +01:00
}
2024-03-05 19:48:54 -05:00
}
2024-03-25 17:05:11 -04:00
let start_token = events_before
.last()
.map_or_else(|| base_token.stringify(), |(count, _)| count.stringify());
2024-03-05 19:48:54 -05:00
2024-07-16 08:05:25 +00:00
let events_after: Vec<_> = services
2020-07-30 18:14:47 +02:00
.rooms
2022-09-07 13:25:51 +02:00
.timeline
2024-08-08 17:18:30 +00:00
.pdus_after(sender_user, room_id, base_token)
.await?
2023-06-25 19:31:40 +02:00
.take(limit / 2)
2024-08-08 17:18:30 +00:00
.filter_map(|(count, pdu)| async move {
// list of safe and common non-state events to ignore
if matches!(
&pdu.kind,
RoomMessage
| Sticker | CallInvite
| CallNotify | RoomEncrypted
| Image | File | Audio
| Voice | Video | UnstablePollStart
| PollStart | KeyVerificationStart
| Reaction | Emote
| Location
) && services
.users
.user_is_ignored(&pdu.sender, sender_user)
.await
{
return None;
}
2024-07-16 08:05:25 +00:00
services
2023-02-22 15:49:55 +01:00
.rooms
.state_accessor
2024-08-08 17:18:30 +00:00
.user_can_see_event(sender_user, room_id, &pdu.event_id)
.await
.then_some((count, pdu))
2023-02-22 15:49:55 +01:00
})
2024-08-08 17:18:30 +00:00
.collect()
.await;
2024-03-05 19:48:54 -05:00
2022-01-04 14:30:13 +01:00
for (_, event) in &events_after {
2024-08-08 17:18:30 +00:00
if !services
.rooms
.lazy_loading
.lazy_load_was_sent_before(sender_user, sender_device, room_id, &event.sender)
.await || lazy_load_send_redundant
2022-02-04 13:30:42 +01:00
{
lazy_loaded.insert(event.sender.as_str().to_owned());
2022-01-04 14:30:13 +01:00
}
2024-03-05 19:48:54 -05:00
}
2024-07-16 08:05:25 +00:00
let shortstatehash = services
2024-04-12 19:50:30 -04:00
.rooms
.state_accessor
.pdu_shortstatehash(
events_after
.last()
.map_or(&*body.event_id, |(_, e)| &*e.event_id),
2024-08-08 17:18:30 +00:00
)
.await
2024-04-12 19:50:30 -04:00
.map_or(
2024-07-16 08:05:25 +00:00
services
2024-04-12 19:50:30 -04:00
.rooms
.state
2024-08-08 17:18:30 +00:00
.get_room_shortstatehash(room_id)
.await
2024-04-12 19:50:30 -04:00
.expect("All rooms have state"),
|hash| hash,
);
2024-03-05 19:48:54 -05:00
2024-07-16 08:05:25 +00:00
let state_ids = services
2024-03-25 17:05:11 -04:00
.rooms
.state_accessor
.state_full_ids(shortstatehash)
2024-08-08 17:18:30 +00:00
.await
.map_err(|e| err!(Database("State not found: {e}")))?;
2024-03-05 19:48:54 -05:00
2024-03-25 17:05:11 -04:00
let end_token = events_after
.last()
.map_or_else(|| base_token.stringify(), |(count, _)| count.stringify());
2024-03-05 19:48:54 -05:00
let mut state = Vec::with_capacity(state_ids.len());
2024-03-05 19:48:54 -05:00
2022-02-04 13:30:42 +01:00
for (shortstatekey, id) in state_ids {
2024-07-16 08:05:25 +00:00
let (event_type, state_key) = services
2024-03-25 17:05:11 -04:00
.rooms
.short
2024-08-08 17:18:30 +00:00
.get_statekey_from_short(shortstatekey)
.await?;
2024-03-05 19:48:54 -05:00
2022-04-06 21:31:29 +02:00
if event_type != StateEventType::RoomMember {
2024-08-08 17:18:30 +00:00
let Ok(pdu) = services.rooms.timeline.get_pdu(&id).await else {
error!("Pdu in state not found: {id}");
2024-03-23 14:38:15 -04:00
continue;
2022-02-04 13:30:42 +01:00
};
2024-03-23 14:38:15 -04:00
2022-02-04 13:30:42 +01:00
state.push(pdu.to_state_event());
} else if !lazy_load_enabled || lazy_loaded.contains(&state_key) {
2024-08-08 17:18:30 +00:00
let Ok(pdu) = services.rooms.timeline.get_pdu(&id).await else {
error!("Pdu in state not found: {id}");
2024-03-23 14:38:15 -04:00
continue;
2022-02-04 13:30:42 +01:00
};
2024-03-23 14:38:15 -04:00
2022-02-04 13:30:42 +01:00
state.push(pdu.to_state_event());
2024-03-05 19:48:54 -05:00
}
2022-01-04 14:30:13 +01:00
}
2024-03-05 19:48:54 -05:00
Ok(get_context::v3::Response {
start: Some(start_token),
end: Some(end_token),
events_before: events_before
.iter()
.map(|(_, pdu)| pdu.to_room_event())
.collect(),
2022-01-13 11:48:18 +01:00
event: Some(base_event),
events_after: events_after
.iter()
.map(|(_, pdu)| pdu.to_room_event())
.collect(),
2022-01-04 14:30:13 +01:00
state,
})
2020-07-30 18:14:47 +02:00
}