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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//! [GET /_matrix/client/r0/rooms/{roomId}/messages](https://matrix.org/docs/spec/client_server/r0.6.0#get-matrix-client-r0-rooms-roomid-messages)

use js_int::UInt;
use ruma_api::ruma_api;
use ruma_events::{
    collections::all::{RoomEvent, StateEvent},
    EventJson,
};
use ruma_identifiers::RoomId;
use serde::{Deserialize, Serialize};

use crate::r0::filter::RoomEventFilter;

ruma_api! {
    metadata {
        description: "Get message events for a room.",
        method: GET,
        name: "get_message_events",
        path: "/_matrix/client/r0/rooms/:room_id/messages",
        rate_limited: false,
        requires_authentication: true,
    }

    request {
        /// The room to get events from.
        #[ruma_api(path)]
        pub room_id: RoomId,
        /// The token to start returning events from.
        ///
        /// This token can be obtained from a
        /// prev_batch token returned for each room by the sync API, or from a start or end token
        /// returned by a previous request to this endpoint.
        #[ruma_api(query)]
        pub from: String,
        /// The token to stop returning events at.
        ///
        /// This token can be obtained from a prev_batch
        /// token returned for each room by the sync endpoint, or from a start or end token returned
        /// by a previous request to this endpoint.
        #[serde(skip_serializing_if = "Option::is_none")]
        #[ruma_api(query)]
        pub to: Option<String>,
        /// The direction to return events from.
        #[ruma_api(query)]
        pub dir: Direction,
        /// The maximum number of events to return.
        ///
        /// Default: 10.
        #[serde(skip_serializing_if = "Option::is_none")]
        #[ruma_api(query)]
        pub limit: Option<UInt>,
        /// A RoomEventFilter to filter returned events with.
        #[ruma_api(query)]
        #[serde(
            with = "ruma_serde::json_string",
            default,
            skip_serializing_if = "Option::is_none"
        )]
        pub filter: Option<RoomEventFilter>,
    }

    response {
        /// The token the pagination starts from.
        #[serde(skip_serializing_if = "Option::is_none")]
        pub start: Option<String>,
        /// The token the pagination ends at.
        #[serde(skip_serializing_if = "Option::is_none")]
        pub end: Option<String>,
        /// A list of room events.
        #[serde(default, skip_serializing_if = "Vec::is_empty")]
        pub chunk: Vec<EventJson<RoomEvent>>,
        /// A list of state events relevant to showing the `chunk`.
        #[serde(default, skip_serializing_if = "Vec::is_empty")]
        pub state: Vec<EventJson<StateEvent>>,
    }

    error: crate::Error
}

/// The direction to return events from.
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
pub enum Direction {
    /// Return events backwards in time from the requested `from` token.
    #[serde(rename = "b")]
    Backward,
    /// Return events forwards in time from the requested `from` token.
    #[serde(rename = "f")]
    Forward,
}

#[cfg(test)]
mod tests {
    use super::{Direction, Request};

    use std::convert::{TryFrom, TryInto};

    use js_int::UInt;
    use ruma_identifiers::RoomId;

    use crate::r0::filter::{LazyLoadOptions, RoomEventFilter};

    #[test]
    fn test_serialize_some_room_event_filter() {
        let room_id = RoomId::try_from("!roomid:example.org").unwrap();
        let filter = RoomEventFilter {
            lazy_load_options: LazyLoadOptions::Enabled {
                include_redundant_members: true,
            },
            rooms: Some(vec![room_id.clone()]),
            not_rooms: vec!["room".into(), "room2".into(), "room3".into()],
            not_types: vec!["type".into()],
            ..Default::default()
        };
        let req = Request {
            room_id,
            from: "token".into(),
            to: Some("token2".into()),
            dir: Direction::Backward,
            limit: Some(UInt::from(0u32)),
            filter: Some(filter),
        };

        let request: http::Request<Vec<u8>> = req.try_into().unwrap();
        assert_eq!(
            "from=token&to=token2&dir=b&limit=0&filter=%7B%22not_types%22%3A%5B%22type%22%5D%2C%22not_rooms%22%3A%5B%22room%22%2C%22room2%22%2C%22room3%22%5D%2C%22rooms%22%3A%5B%22%21roomid%3Aexample.org%22%5D%2C%22lazy_load_members%22%3Atrue%2C%22include_redundant_members%22%3Atrue%7D",
            request.uri().query().unwrap()
        );
    }

    #[test]
    fn test_serialize_none_room_event_filter() {
        let room_id = RoomId::try_from("!roomid:example.org").unwrap();
        let req = Request {
            room_id,
            from: "token".into(),
            to: Some("token2".into()),
            dir: Direction::Backward,
            limit: Some(UInt::from(0u32)),
            filter: None,
        };

        let request: http::Request<Vec<u8>> = req.try_into().unwrap();
        assert_eq!(
            "from=token&to=token2&dir=b&limit=0",
            request.uri().query().unwrap(),
        );
    }

    #[test]
    fn test_serialize_default_room_event_filter() {
        let room_id = RoomId::try_from("!roomid:example.org").unwrap();
        let req = Request {
            room_id,
            from: "token".into(),
            to: Some("token2".into()),
            dir: Direction::Backward,
            limit: Some(UInt::from(0u32)),
            filter: Some(RoomEventFilter::default()),
        };

        let request: http::Request<Vec<u8>> = req.try_into().unwrap();
        assert_eq!(
            "from=token&to=token2&dir=b&limit=0&filter=%7B%7D",
            request.uri().query().unwrap(),
        );
    }
}