summaryrefslogtreecommitdiff
path: root/src/api/client_server/relations.rs
blob: a7cea7860ea4f4e9c667edf47b28154b5284804d (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use ruma::api::client::relations::{
    get_relating_events, get_relating_events_with_rel_type,
    get_relating_events_with_rel_type_and_event_type,
};

use crate::{service::rooms::timeline::PduCount, services, Result, Ruma};

/// # `GET /_matrix/client/r0/rooms/{roomId}/relations/{eventId}/{relType}/{eventType}`
pub async fn get_relating_events_with_rel_type_and_event_type_route(
    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> {
    let sender_user = body.sender_user.as_ref().expect("user is authenticated");

    let from = match body.from.clone() {
        Some(from) => PduCount::try_from_string(&from)?,
        None => match ruma::api::Direction::Backward {
            // TODO: fix ruma so `body.dir` exists
            ruma::api::Direction::Forward => PduCount::min(),
            ruma::api::Direction::Backward => PduCount::max(),
        },
    };

    let to = body
        .to
        .as_ref()
        .and_then(|t| PduCount::try_from_string(&t).ok());

    // Use limit or else 10, with maximum 100
    let limit = body
        .limit
        .and_then(|u| u32::try_from(u).ok())
        .map_or(10_usize, |u| u as usize)
        .min(100);

    let res = services()
        .rooms
        .pdu_metadata
        .paginate_relations_with_filter(
            sender_user,
            &body.room_id,
            &body.event_id,
            Some(body.event_type.clone()),
            Some(body.rel_type.clone()),
            from,
            to,
            limit,
        )?;

    Ok(
        get_relating_events_with_rel_type_and_event_type::v1::Response {
            chunk: res.chunk,
            next_batch: res.next_batch,
            prev_batch: res.prev_batch,
        },
    )
}

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

    let from = match body.from.clone() {
        Some(from) => PduCount::try_from_string(&from)?,
        None => match ruma::api::Direction::Backward {
            // TODO: fix ruma so `body.dir` exists
            ruma::api::Direction::Forward => PduCount::min(),
            ruma::api::Direction::Backward => PduCount::max(),
        },
    };

    let to = body
        .to
        .as_ref()
        .and_then(|t| PduCount::try_from_string(&t).ok());

    // Use limit or else 10, with maximum 100
    let limit = body
        .limit
        .and_then(|u| u32::try_from(u).ok())
        .map_or(10_usize, |u| u as usize)
        .min(100);

    let res = services()
        .rooms
        .pdu_metadata
        .paginate_relations_with_filter(
            sender_user,
            &body.room_id,
            &body.event_id,
            None,
            Some(body.rel_type.clone()),
            from,
            to,
            limit,
        )?;

    Ok(get_relating_events_with_rel_type::v1::Response {
        chunk: res.chunk,
        next_batch: res.next_batch,
        prev_batch: res.prev_batch,
    })
}

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

    let from = match body.from.clone() {
        Some(from) => PduCount::try_from_string(&from)?,
        None => match ruma::api::Direction::Backward {
            // TODO: fix ruma so `body.dir` exists
            ruma::api::Direction::Forward => PduCount::min(),
            ruma::api::Direction::Backward => PduCount::max(),
        },
    };

    let to = body
        .to
        .as_ref()
        .and_then(|t| PduCount::try_from_string(&t).ok());

    // Use limit or else 10, with maximum 100
    let limit = body
        .limit
        .and_then(|u| u32::try_from(u).ok())
        .map_or(10_usize, |u| u as usize)
        .min(100);

    services()
        .rooms
        .pdu_metadata
        .paginate_relations_with_filter(
            sender_user,
            &body.room_id,
            &body.event_id,
            None,
            None,
            from,
            to,
            limit,
        )
}