Skip to main content

zulip_api/message/send_message/
response.rs

1use serde::{Deserialize, Serialize};
2
3use crate::response::BaseResponse;
4use crate::{field_access, impl_default, impl_display, impl_response, VisibilityPolicy};
5
6/// Represents the response to a `Send Message` API request.
7#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
8pub struct Response {
9    #[serde(flatten)]
10    base: BaseResponse,
11    #[serde(skip_serializing_if = "Option::is_none")]
12    id: Option<u64>,
13    #[serde(skip_serializing_if = "Option::is_none")]
14    automatic_new_visibility_policy: Option<VisibilityPolicy>,
15}
16
17impl_response!(Response, base);
18
19field_access! {
20    Response {
21        /// Unique ID assigned to the sent message.
22        id: option { u64 },
23        /// If the message's sender had configured their [visibility policy settings](https://zulip.com/help/mute-a-topic)
24        /// to potentially automatically follow or unmute topics when sending messages,
25        /// and one of these policies did in fact change the user's visibility policy
26        /// for the topic where this message was sent, the new value for that user's
27        /// visibility policy for the recipient topic.
28        ///
29        /// Only present if the sender's visibility was in fact changed.
30        ///
31        /// The value can be either [unmuted or followed](https://zulip.com/api/update-user-topic#parameter-visibility_policy).
32        ///
33        /// Clients will also be notified about the change in policy via a
34        /// `user_topic` event as usual. This field is intended to be used by clients
35        /// to explicitly inform the user when a topic's visibility policy was changed
36        /// automatically due to sending a message.
37        ///
38        /// For example, the Zulip web application uses this field to decide whether
39        /// to display a warning or notice suggesting to unmute the topic after
40        /// sending a message to a muted channel. Such a notice would be confusing in
41        /// the event that the act of sending the message had already resulted in the
42        /// user automatically unmuting or following the topic in question.
43        ///
44        /// **Changes**: New in Zulip 8.0 (feature level 218).
45        automatic_new_visibility_policy: option { VisibilityPolicy },
46    }
47}
48
49impl Response {
50    /// Creates a new [Response].
51    pub const fn new() -> Self {
52        Self {
53            base: BaseResponse::new(),
54            id: None,
55            automatic_new_visibility_policy: None,
56        }
57    }
58}
59
60impl_default!(Response);
61impl_display!(Response);
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66    use crate::response::{ApiResult, ErrorCode, Response as _};
67
68    #[test]
69    fn test_success_message_response() {
70        let msg = "test response";
71        let msg_json = serde_json::to_string(&msg).unwrap();
72
73        let result = ApiResult::Success;
74        let result_json = serde_json::to_string(&result).unwrap();
75
76        let id = 42;
77        let id_json = serde_json::to_string(&id).unwrap();
78
79        let response = Response::new()
80            .with_msg(msg)
81            .with_result(result)
82            .with_id(id);
83
84        let response_json =
85            format!(r#"{{"msg":{msg_json},"result":{result_json},"id":{id_json}}}"#);
86
87        assert_eq!(serde_json::to_string(&response).unwrap(), response_json);
88        assert_eq!(
89            serde_json::from_str::<Response>(&response_json).unwrap(),
90            response
91        );
92
93        assert_eq!(response.msg(), msg);
94        assert_eq!(response.result(), result);
95        assert_eq!(response.code(), None);
96        assert_eq!(response.id(), Some(id));
97    }
98
99    #[test]
100    fn test_error_message_response() {
101        let msg = "test response";
102        let msg_json = serde_json::to_string(&msg).unwrap();
103
104        let result = ApiResult::Error;
105        let result_json = serde_json::to_string(&result).unwrap();
106
107        let code = ErrorCode::BadRequest;
108        let code_json = serde_json::to_string(&code).unwrap();
109
110        let id = 42;
111        let id_json = serde_json::to_string(&id).unwrap();
112
113        let response = Response::new()
114            .with_msg(msg)
115            .with_result(result)
116            .with_code(code)
117            .with_id(id);
118
119        let response_json = format!(
120            r#"{{"msg":{msg_json},"result":{result_json},"code":{code_json},"id":{id_json}}}"#
121        );
122
123        assert_eq!(serde_json::to_string(&response).unwrap(), response_json);
124        assert_eq!(
125            serde_json::from_str::<Response>(&response_json).unwrap(),
126            response
127        );
128
129        assert_eq!(response.msg(), msg);
130        assert_eq!(response.result(), result);
131        assert_eq!(response.code(), Some(code));
132        assert_eq!(response.id(), Some(id));
133    }
134}