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

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

267 lines
6.3 KiB
Rust
Raw Normal View History

2024-07-16 08:05:25 +00:00
use axum::extract::State;
use futures::{Stream, StreamExt, TryFutureExt, future::try_join};
2024-11-02 06:12:54 +00:00
use ruma::{
EventId, RoomId, UInt, UserId,
2024-11-02 06:12:54 +00:00
api::{
Direction,
2024-11-02 06:12:54 +00:00
client::relations::{
get_relating_events, get_relating_events_with_rel_type,
get_relating_events_with_rel_type_and_event_type,
2024-11-02 06:12:54 +00:00
},
},
events::{TimelineEventType, relation::RelationType},
2024-11-02 06:12:54 +00:00
};
2025-04-22 01:41:02 +00:00
use tuwunel_core::{
Result, at,
matrix::{
event::{Event, RelationTypeEqual},
pdu::{Pdu, PduCount},
},
utils::{
result::FlatOk,
stream::{IterStream, ReadyExt, WidebandExt},
},
2025-04-22 01:41:02 +00:00
};
use tuwunel_service::Services;
2023-06-25 19:31:40 +02:00
2024-11-02 06:12:54 +00:00
use crate::Ruma;
/// # `GET /_matrix/client/r0/rooms/{roomId}/relations/{eventId}/{relType}/{eventType}`
2024-04-22 23:48:57 -04:00
pub(crate) async fn get_relating_events_with_rel_type_and_event_type_route(
State(services): State<crate::State>,
body: Ruma<get_relating_events_with_rel_type_and_event_type::v1::Request>,
) -> Result<get_relating_events_with_rel_type_and_event_type::v1::Response> {
2024-11-02 06:12:54 +00:00
paginate_relations_with_filter(
&services,
body.sender_user(),
&body.room_id,
&body.event_id,
body.event_type.clone().into(),
body.rel_type.clone().into(),
body.from.as_deref(),
body.to.as_deref(),
body.limit,
body.recurse,
body.dir,
)
.await
.map(|res| get_relating_events_with_rel_type_and_event_type::v1::Response {
chunk: res.chunk,
next_batch: res.next_batch,
prev_batch: res.prev_batch,
recursion_depth: res.recursion_depth,
})
}
/// # `GET /_matrix/client/r0/rooms/{roomId}/relations/{eventId}/{relType}`
2024-04-22 23:48:57 -04:00
pub(crate) async fn get_relating_events_with_rel_type_route(
State(services): State<crate::State>,
body: Ruma<get_relating_events_with_rel_type::v1::Request>,
) -> Result<get_relating_events_with_rel_type::v1::Response> {
2024-11-02 06:12:54 +00:00
paginate_relations_with_filter(
&services,
body.sender_user(),
&body.room_id,
&body.event_id,
None,
body.rel_type.clone().into(),
body.from.as_deref(),
body.to.as_deref(),
body.limit,
body.recurse,
body.dir,
)
.await
.map(|res| get_relating_events_with_rel_type::v1::Response {
chunk: res.chunk,
next_batch: res.next_batch,
prev_batch: res.prev_batch,
recursion_depth: res.recursion_depth,
})
}
/// # `GET /_matrix/client/r0/rooms/{roomId}/relations/{eventId}`
2024-04-22 23:48:57 -04:00
pub(crate) async fn get_relating_events_route(
State(services): State<crate::State>,
body: Ruma<get_relating_events::v1::Request>,
) -> Result<get_relating_events::v1::Response> {
2024-11-02 06:12:54 +00:00
paginate_relations_with_filter(
&services,
body.sender_user(),
&body.room_id,
&body.event_id,
None,
None,
body.from.as_deref(),
body.to.as_deref(),
body.limit,
body.recurse,
body.dir,
)
.await
}
#[allow(clippy::too_many_arguments)]
#[tracing::instrument(
name = "relations",
level = "debug",
skip_all,
fields(room_id, target, from, to, dir, limit, recurse),
ret(level = "trace")
)]
2024-11-02 06:12:54 +00:00
async fn paginate_relations_with_filter(
services: &Services,
sender_user: &UserId,
room_id: &RoomId,
target: &EventId,
filter_event_type: Option<TimelineEventType>,
filter_rel_type: Option<RelationType>,
from: Option<&str>,
to: Option<&str>,
limit: Option<UInt>,
recurse: bool,
dir: Direction,
2024-11-02 06:12:54 +00:00
) -> Result<get_relating_events::v1::Response> {
2024-11-06 21:02:23 +00:00
let start: PduCount = from
2024-11-02 06:12:54 +00:00
.map(str::parse)
.transpose()?
.unwrap_or_else(|| match dir {
| Direction::Forward => PduCount::min(),
| Direction::Backward => PduCount::max(),
2024-11-02 06:12:54 +00:00
});
let to: Option<PduCount> = to.map(str::parse).flat_ok();
// Use limit or else 30, with maximum 100
let limit: usize = limit
.map(TryInto::try_into)
.flat_ok()
.unwrap_or(30)
.min(100);
// Spec (v1.10) recommends depth of at least 3
let depth: u8 = if recurse { 3 } else { 1 };
2024-11-02 06:12:54 +00:00
let events: Vec<_> =
get_relations(services, sender_user, room_id, target, start, limit, depth, dir)
.await
.ready_filter(|(_, pdu)| {
filter_event_type
.as_ref()
.is_none_or(|kind| kind == pdu.kind())
})
.ready_filter(|(_, pdu)| {
filter_rel_type
.as_ref()
.is_none_or(|rel_type| rel_type.relation_type_equal(pdu))
})
.ready_take_while(|(count, _)| Some(*count) != to)
.wide_filter_map(|item| visibility_filter(services, sender_user, item))
.take(limit)
.collect()
.await;
2024-11-02 06:12:54 +00:00
let next_batch = events
.last()
.map(at!(0))
.as_ref()
.map(ToString::to_string);
2024-11-02 06:12:54 +00:00
Ok(get_relating_events::v1::Response {
next_batch,
2024-11-06 21:02:23 +00:00
prev_batch: from.map(Into::into),
2024-11-02 06:12:54 +00:00
recursion_depth: recurse.then_some(depth.into()),
chunk: events
.into_iter()
.map(at!(1))
2025-04-26 08:24:47 +00:00
.map(Event::into_format)
2024-11-02 06:12:54 +00:00
.collect(),
})
}
#[allow(clippy::too_many_arguments)]
async fn get_relations(
services: &Services,
sender_user: &UserId,
room_id: &RoomId,
target: &EventId,
from: PduCount,
limit: usize,
max_depth: u8,
dir: Direction,
) -> impl Stream<Item = (PduCount, Pdu)> + Send {
let room_id = services.short.get_shortroomid(room_id);
let target = services
.timeline
.get_pdu_count(target)
.map_ok(|target| {
match target {
| PduCount::Normal(count) => count,
| _ => {
// TODO: Support backfilled relations
0 // This will result in an empty iterator
},
}
});
let Ok((room_id, target)) = try_join(room_id, target).await else {
return Vec::new().into_iter().stream();
};
let mut pdus: Vec<_> = services
.pdu_metadata
.get_relations(room_id, target.into(), from, dir, Some(sender_user))
.collect()
.await;
let mut stack: Vec<_> = pdus
.iter()
.filter(|_| max_depth > 0)
.map(|(count, _)| (*count, 1))
.collect();
'limit: while let Some((count, depth)) = stack.pop() {
let target = match count {
| PduCount::Normal(count) => count,
| PduCount::Backfilled(_) => {
// TODO: Support backfilled relations
0 // This will result in an empty iterator
},
};
let relations: Vec<_> = services
.pdu_metadata
.get_relations(room_id, target.into(), from, dir, Some(sender_user))
.collect()
.await;
for (count, pdu) in relations {
if depth < max_depth {
stack.push((count, depth.saturating_add(1)));
}
if pdus.len() < limit {
pdus.push((count, pdu));
} else {
break 'limit;
}
}
}
pdus.into_iter().stream()
}
2025-07-08 11:48:45 +00:00
async fn visibility_filter<Pdu: Event>(
services: &Services,
sender_user: &UserId,
item: (PduCount, Pdu),
) -> Option<(PduCount, Pdu)> {
2024-11-02 06:12:54 +00:00
let (_, pdu) = &item;
2024-08-08 17:18:30 +00:00
services
2024-11-02 06:12:54 +00:00
.state_accessor
.user_can_see_event(sender_user, pdu.room_id(), pdu.event_id())
2024-08-08 17:18:30 +00:00
.await
2024-11-02 06:12:54 +00:00
.then_some(item)
2023-06-25 19:31:40 +02:00
}