Skip to main content

zulip_api/message/update_message_flags/
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 response for the `Update Message Flags` Zulip API endpoint.
7///
8/// See [update-message-flags](https://zulip.com/api/update-message-flags) for details.
9#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
10pub struct Response {
11    #[serde(flatten)]
12    base: BaseResponse,
13    messages: Option<Vec<u64>>,
14    ignored_because_not_subscribed_channels: Option<Vec<u64>>,
15}
16
17impl Response {
18    /// Creates a new [Response].
19    pub const fn new() -> Self {
20        Self {
21            base: BaseResponse::new(),
22            messages: None,
23            ignored_because_not_subscribed_channels: None,
24        }
25    }
26}
27
28field_access! {
29    Response {
30        /// An array with the IDs of the modified messages.
31        messages: option_cow_deref { [u64], Vec<u64> },
32        /// Only present if the flag is `read` and the operation is `remove`.
33        ///
34        /// Zulip has an invariant that all unread messages must be in channels
35        /// the user is subscribed to. This field will contain a list of the
36        /// channels whose messages were skipped to mark as unread because the
37        /// user is not subscribed to them.
38        ///
39        /// **Changes**: New in Zulip 10.0 (feature level 355).
40        ignored_because_not_subscribed_channels: option_cow_deref { [u64], Vec<u64> },
41    }
42}
43
44impl_default!(Response);
45impl_display!(Response);
46impl_response!(Response, base);
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51
52    #[test]
53    fn test_response() {
54        let messages = [1u64];
55        let ignored_because_not_subscribed_channels = [2u64];
56
57        let response = Response::new()
58            .with_messages(messages.as_ref())
59            .with_ignored_because_not_subscribed_channels(
60                ignored_because_not_subscribed_channels.as_ref(),
61            );
62        let response_json = response.to_string();
63
64        assert_eq!(serde_json::to_string(&response).unwrap(), response_json);
65        assert_eq!(
66            serde_json::from_str::<Response>(&response_json).unwrap(),
67            response
68        );
69
70        assert_eq!(response.messages(), Some(messages.as_ref()));
71        assert_eq!(
72            response.ignored_because_not_subscribed_channels(),
73            Some(ignored_because_not_subscribed_channels.as_ref())
74        );
75    }
76}