ruma_client_api/push/
get_notifications.rs

1//! `GET /_matrix/client/*/notifications`
2//!
3//! Paginate through the list of events that the user has been, or would have been notified about.
4
5pub mod v3 {
6    //! `/v3/` ([spec])
7    //!
8    //! [spec]: https://spec.matrix.org/latest/client-server-api/#get_matrixclientv3notifications
9
10    use js_int::UInt;
11    use ruma_common::{
12        api::{request, response, Metadata},
13        metadata,
14        push::Action,
15        serde::Raw,
16        MilliSecondsSinceUnixEpoch, OwnedRoomId,
17    };
18    use ruma_events::AnySyncTimelineEvent;
19    use serde::{Deserialize, Serialize};
20
21    const METADATA: Metadata = metadata! {
22        method: GET,
23        rate_limited: false,
24        authentication: AccessToken,
25        history: {
26            1.0 => "/_matrix/client/r0/notifications",
27            1.1 => "/_matrix/client/v3/notifications",
28        }
29    };
30
31    /// Request type for the `get_notifications` endpoint.
32    #[request(error = crate::Error)]
33    #[derive(Default)]
34    pub struct Request {
35        /// Pagination token given to retrieve the next set of events.
36        #[ruma_api(query)]
37        #[serde(skip_serializing_if = "Option::is_none")]
38        pub from: Option<String>,
39
40        /// Limit on the number of events to return in this request.
41        #[ruma_api(query)]
42        #[serde(skip_serializing_if = "Option::is_none")]
43        pub limit: Option<UInt>,
44
45        /// Allows basic filtering of events returned.
46        ///
47        /// Supply "highlight" to return only events where the notification had the 'highlight'
48        /// tweak set.
49        #[ruma_api(query)]
50        #[serde(skip_serializing_if = "Option::is_none")]
51        pub only: Option<String>,
52    }
53
54    /// Response type for the `get_notifications` endpoint.
55    #[response(error = crate::Error)]
56    pub struct Response {
57        /// The token to supply in the from param of the next /notifications request in order to
58        /// request more events.
59        ///
60        /// If this is absent, there are no more results.
61        #[serde(skip_serializing_if = "Option::is_none")]
62        pub next_token: Option<String>,
63
64        /// The list of events that triggered notifications.
65        pub notifications: Vec<Notification>,
66    }
67
68    impl Request {
69        /// Creates an empty `Request`.
70        pub fn new() -> Self {
71            Default::default()
72        }
73    }
74
75    impl Response {
76        /// Creates a new `Response` with the given notifications.
77        pub fn new(notifications: Vec<Notification>) -> Self {
78            Self { next_token: None, notifications }
79        }
80    }
81
82    /// Represents a notification.
83    #[derive(Clone, Debug, Deserialize, Serialize)]
84    #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
85    pub struct Notification {
86        /// The actions to perform when the conditions for this rule are met.
87        pub actions: Vec<Action>,
88
89        /// The event that triggered the notification.
90        pub event: Raw<AnySyncTimelineEvent>,
91
92        /// The profile tag of the rule that matched this event.
93        #[serde(skip_serializing_if = "Option::is_none")]
94        pub profile_tag: Option<String>,
95
96        /// Indicates whether the user has sent a read receipt indicating that they have read this
97        /// message.
98        pub read: bool,
99
100        /// The ID of the room in which the event was posted.
101        pub room_id: OwnedRoomId,
102
103        /// The time at which the event notification was sent.
104        pub ts: MilliSecondsSinceUnixEpoch,
105    }
106
107    impl Notification {
108        /// Creates a new `Notification` with the given actions, event, read flag, room ID and
109        /// timestamp.
110        pub fn new(
111            actions: Vec<Action>,
112            event: Raw<AnySyncTimelineEvent>,
113            read: bool,
114            room_id: OwnedRoomId,
115            ts: MilliSecondsSinceUnixEpoch,
116        ) -> Self {
117            Self { actions, event, profile_tag: None, read, room_id, ts }
118        }
119    }
120}