Skip to main content

zulip_api/message/update_message_flags_narrow/
response.rs

1use serde::{Deserialize, Serialize};
2
3use crate::response::BaseResponse;
4use crate::{field_access, impl_default, impl_display, impl_response};
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 Response {
11    #[serde(flatten)]
12    base: BaseResponse,
13    processed_count: Option<u64>,
14    updated_count: Option<u64>,
15    first_processed_id: Option<u64>,
16    last_processed_id: Option<u64>,
17    found_oldest: Option<bool>,
18    found_newest: Option<bool>,
19    ignored_because_not_subscribed_channels: Option<Vec<u64>>,
20}
21
22impl Response {
23    /// Creates a new [Response].
24    pub const fn new() -> Self {
25        Self {
26            base: BaseResponse::new(),
27            processed_count: None,
28            updated_count: None,
29            first_processed_id: None,
30            last_processed_id: None,
31            found_oldest: None,
32            found_newest: None,
33            ignored_because_not_subscribed_channels: None,
34        }
35    }
36}
37
38field_access! {
39    Response {
40        /// The number of messages that were within the
41        /// update range (at most `num_before + 1 + num_after`).
42        processed_count: option { u64 },
43        /// The number of messages where the flag's
44        /// value was changed (at most `processed_count`).
45        updated_count: option { u64 },
46        /// The ID of the oldest message within the
47        /// update range, or `null` if the range was empty.
48        first_processed_id: option { u64 },
49        /// The ID of the newest message within the
50        /// update range, or `null` if the range was empty.
51        last_processed_id: option { u64 },
52        /// Whether the update range reached backward
53        /// far enough to include very oldest message
54        /// matching the narrow (used by clients doing a
55        /// bulk update to decide whether to issue
56        /// another request anchored at
57        /// `first_processed_id`).
58        found_oldest: option { bool },
59        /// Whether the update range reached forward far
60        /// enough to include very oldest message
61        /// matching the narrow (used by clients doing a
62        /// bulk update to decide whether to issue
63        /// another request anchored at
64        /// `last_processed_id`).
65        found_newest: option { bool },
66    }
67}
68
69field_access! {
70    Response {
71        /// Zulip has an invariant that all unread messages must be in channels
72        /// the user is subscribed to. This field will contain a list of the
73        /// channels whose messages were skipped to mark as unread because the
74        /// user is not subscribed to them.
75        ///
76        /// Only present if the flag is `read` and the operation is `remove`.
77        ///
78        /// **Changes**: New in Zulip 10.0 (feature level 355).
79        ignored_because_not_subscribed_channels: option_cow_deref { [u64], Vec<u64> },
80    }
81}
82
83impl_default!(Response);
84impl_display!(Response);
85impl_response!(Response, base);
86
87#[cfg(test)]
88mod tests {
89    use super::*;
90
91    #[test]
92    fn test_response() {
93        let processed_count = 1;
94        let updated_count = 2;
95        let first_processed_id = 3;
96        let last_processed_id = 4;
97        let found_oldest = true;
98        let found_newest = false;
99        let ignored_because_not_subscribed_channels = [5];
100
101        let response = Response::new()
102            .with_processed_count(processed_count)
103            .with_updated_count(updated_count)
104            .with_first_processed_id(first_processed_id)
105            .with_last_processed_id(last_processed_id)
106            .with_found_oldest(found_oldest)
107            .with_found_newest(found_newest)
108            .with_ignored_because_not_subscribed_channels(
109                ignored_because_not_subscribed_channels.as_ref(),
110            );
111        let response_json = response.to_string();
112
113        assert_eq!(serde_json::to_string(&response).unwrap(), response_json);
114        assert_eq!(
115            serde_json::from_str::<Response>(&response_json).unwrap(),
116            response
117        );
118
119        assert_eq!(response.processed_count(), Some(processed_count));
120        assert_eq!(response.updated_count(), Some(updated_count));
121        assert_eq!(response.first_processed_id(), Some(first_processed_id));
122        assert_eq!(response.last_processed_id(), Some(last_processed_id));
123        assert_eq!(response.found_oldest(), Some(found_oldest));
124        assert_eq!(response.found_newest(), Some(found_newest));
125        assert_eq!(
126            response.ignored_because_not_subscribed_channels(),
127            Some(ignored_because_not_subscribed_channels.as_ref())
128        );
129    }
130}