Skip to main content

zulip_api/message/update_message_flags_narrow/
request.rs

1use serde::{Deserialize, Serialize};
2
3use crate::filter::{Anchor, Narrow};
4use crate::{field_access, impl_default, impl_display, Flag, Operation};
5
6/// Represents the request for the `Update Message Flags for Narrow` Zulip API endpoint.
7///
8/// See [update-message-flags-for-narrow](https://zulip.com/api/update-message-flags-for-narrow) for details.
9#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
10pub struct Request {
11    anchor: Anchor,
12    include_anchor: Option<bool>,
13    num_before: u64,
14    num_after: u64,
15    narrow: Vec<Narrow>,
16    op: Operation,
17    flag: Flag,
18}
19
20impl Request {
21    /// Creates a new [Request].
22    pub const fn new() -> Self {
23        Self {
24            anchor: Anchor::new(),
25            include_anchor: None,
26            num_before: 0,
27            num_after: 0,
28            narrow: Vec::new(),
29            op: Operation::new(),
30            flag: Flag::new(),
31        }
32    }
33}
34
35field_access! {
36    Request {
37        /// Represents an anchor updating of flags.
38        ///
39        /// Either an integer message ID, or a special string values for when
40        /// the client wants the server to compute the anchor to use.
41        anchor: Anchor,
42        /// Limit the number of messages preceding the anchor in the update range.
43        ///
44        /// The server may decrease this to bound transaction sizes.
45        num_before: u64,
46        /// Limit the number of messages following the anchor in the update range.
47        ///
48        /// The server may decrease this to bound transaction sizes.
49        num_after: u64,
50        /// Whether to `add` the flag or `remove` it.
51        op: Operation,
52        /// The flag that should be added/removed.
53        ///
54        /// See [available flags](https://zulip.com/api/update-message-flags#available-flags).
55        flag: Flag,
56    }
57}
58
59field_access! {
60    Request {
61        /// Whether a message with the specified ID matching the narrow
62        /// should be included in the update range.
63        include_anchor: option { bool },
64    }
65}
66
67field_access! {
68    Request {
69        /// The narrow you want update flags within. See how to
70        /// [construct a narrow](https://zulip.com/api/construct-narrow).
71        ///
72        /// Note that, when adding the `read` flag to messages, clients should
73        /// consider including a narrow with the `is:unread` filter as an
74        /// optimization. Including that filter takes advantage of the fact that
75        /// the server has a database index for unread messages.
76        ///
77        /// **Changes**: See [changes section](https://zulip.com/api/construct-narrow#changes)
78        /// of search/narrow filter documentation.
79        narrow: cow { [Narrow] },
80    }
81}
82
83impl_default!(Request);
84impl_display!(Request);
85
86#[cfg(test)]
87mod tests {
88    use super::*;
89
90    #[test]
91    fn test_request() {
92        let anchor = Anchor::new();
93        let include_anchor = true;
94        let num_before = 1;
95        let num_after = 2;
96        let narrow = [Narrow::new()];
97        let op = Operation::new();
98        let flag = Flag::new();
99
100        let request = Request::new()
101            .with_anchor(anchor)
102            .with_include_anchor(include_anchor)
103            .with_num_before(num_before)
104            .with_num_after(num_after)
105            .with_narrow(narrow.as_ref())
106            .with_op(op)
107            .with_flag(flag);
108        let request_json = request.to_string();
109
110        assert_eq!(serde_json::to_string(&request).unwrap(), request_json);
111        assert_eq!(
112            serde_json::from_str::<Request>(&request_json).unwrap(),
113            request
114        );
115
116        assert_eq!(request.anchor(), anchor);
117        assert_eq!(request.include_anchor(), Some(include_anchor));
118        assert_eq!(request.num_before(), num_before);
119        assert_eq!(request.num_after(), num_after);
120        assert_eq!(request.narrow(), narrow.as_ref());
121        assert_eq!(request.op(), op);
122        assert_eq!(request.flag(), flag);
123    }
124}