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

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

77 lines
1.6 KiB
Rust
Raw Normal View History

2024-07-16 08:05:25 +00:00
use axum::extract::State;
use conduwuit::{
2025-06-04 00:11:09 +01:00
Result, at, debug_warn,
2025-04-26 08:24:47 +00:00
matrix::{
Event,
pdu::{PduCount, PduEvent},
},
};
2024-08-08 17:18:30 +00:00
use futures::StreamExt;
2024-11-02 06:12:54 +00:00
use ruma::{api::client::threads::get_threads, uint};
2023-06-25 19:31:40 +02:00
2025-09-07 18:06:11 -04:00
use crate::Ruma;
2023-06-25 19:31:40 +02:00
/// # `GET /_matrix/client/r0/rooms/{roomId}/threads`
2024-07-16 08:05:25 +00:00
pub(crate) async fn get_threads_route(
State(services): State<crate::State>,
ref body: Ruma<get_threads::v1::Request>,
2024-07-16 08:05:25 +00:00
) -> Result<get_threads::v1::Response> {
2023-06-25 19:31:40 +02:00
// Use limit or else 10, with maximum 100
2024-03-25 17:05:11 -04:00
let limit = body
.limit
2024-05-04 09:45:37 -04:00
.unwrap_or_else(|| uint!(10))
.try_into()
2024-03-25 17:05:11 -04:00
.unwrap_or(10)
.min(100);
2023-06-25 19:31:40 +02:00
2024-11-02 06:12:54 +00:00
let from: PduCount = body
.from
.as_deref()
2025-09-07 18:06:11 -04:00
.map(str::parse)
2024-11-02 06:12:54 +00:00
.transpose()?
.unwrap_or_else(PduCount::max);
2023-06-25 19:31:40 +02:00
2024-11-02 06:12:54 +00:00
let threads: Vec<(PduCount, PduEvent)> = services
2023-06-25 19:31:40 +02:00
.rooms
.threads
2024-11-02 06:12:54 +00:00
.threads_until(body.sender_user(), &body.room_id, from, &body.include)
2024-08-08 17:18:30 +00:00
.await?
2023-06-25 19:31:40 +02:00
.take(limit)
2024-08-08 17:18:30 +00:00
.filter_map(|(count, pdu)| async move {
2024-07-16 08:05:25 +00:00
services
2023-06-25 19:31:40 +02:00
.rooms
.state_accessor
2024-11-02 06:12:54 +00:00
.user_can_see_event(body.sender_user(), &body.room_id, &pdu.event_id)
2024-08-08 17:18:30 +00:00
.await
.then_some((count, pdu))
2023-06-25 19:31:40 +02:00
})
2025-06-04 00:11:09 +01:00
.then(|(count, mut pdu)| async move {
if let Err(e) = services
.rooms
.pdu_metadata
.add_bundled_aggregations_to_pdu(body.sender_user(), &mut pdu)
.await
{
debug_warn!("Failed to add bundled aggregations to thread: {e}");
}
(count, pdu)
})
2024-08-08 17:18:30 +00:00
.collect()
.await;
2023-06-25 19:31:40 +02:00
Ok(get_threads::v1::Response {
2024-11-02 06:12:54 +00:00
next_batch: threads
.last()
2024-11-06 21:02:23 +00:00
.filter(|_| threads.len() >= limit)
.map(at!(0))
.as_ref()
2024-11-02 06:12:54 +00:00
.map(ToString::to_string),
2024-03-25 17:05:11 -04:00
chunk: threads
.into_iter()
2024-11-06 21:02:23 +00:00
.map(at!(1))
2025-04-26 08:24:47 +00:00
.map(Event::into_format)
2024-03-25 17:05:11 -04:00
.collect(),
2023-06-25 19:31:40 +02:00
})
}