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.

56 lines
1.2 KiB
Rust
Raw Normal View History

2024-07-16 08:05:25 +00:00
use axum::extract::State;
2024-11-02 06:12:54 +00:00
use conduit::{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
2024-11-02 06:12:54 +00:00
use crate::{Result, 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(
2024-11-02 06:12:54 +00:00
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()
.map(str::parse)
.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
})
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()
.map(|(count, _)| count)
.map(ToString::to_string),
2024-03-25 17:05:11 -04:00
chunk: threads
.into_iter()
.map(|(_, pdu)| pdu.to_room_event())
.collect(),
2023-06-25 19:31:40 +02:00
})
}