summaryrefslogtreecommitdiff
path: root/src/api/client_server/threads.rs
blob: a095b4209842c73c1bfac16f01c2a1e4ebd608cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use ruma::api::client::{error::ErrorKind, threads::get_threads};

use crate::{services, Error, Result, Ruma};

/// # `GET /_matrix/client/r0/rooms/{roomId}/threads`
pub async fn get_threads_route(
    body: Ruma<get_threads::v1::Request>,
) -> Result<get_threads::v1::Response> {
    let sender_user = body.sender_user.as_ref().expect("user is authenticated");

    // Use limit or else 10, with maximum 100
    let limit = body
        .limit
        .and_then(|l| l.try_into().ok())
        .unwrap_or(10)
        .min(100);

    let from = if let Some(from) = &body.from {
        from.parse()
            .map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, ""))?
    } else {
        u64::MAX
    };

    let threads = services()
        .rooms
        .threads
        .threads_until(sender_user, &body.room_id, from, &body.include)?
        .take(limit)
        .filter_map(|r| r.ok())
        .filter(|(_, pdu)| {
            services()
                .rooms
                .state_accessor
                .user_can_see_event(sender_user, &body.room_id, &pdu.event_id)
                .unwrap_or(false)
        })
        .collect::<Vec<_>>();

    let next_batch = threads.last().map(|(count, _)| count.to_string());

    Ok(get_threads::v1::Response {
        chunk: threads
            .into_iter()
            .map(|(_, pdu)| pdu.to_room_event())
            .collect(),
        next_batch,
    })
}