Skip to main content

zulip_api/message/edit_message/
response.rs

1use serde::{Deserialize, Serialize};
2
3use crate::message::Attachment;
4use crate::response::BaseResponse;
5use crate::{field_access, impl_default, impl_display, impl_response};
6
7/// Represents the response to a `Edit Message` API request.
8#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
9pub struct Response {
10    #[serde(flatten)]
11    base: BaseResponse,
12    #[serde(skip_serializing_if = "Option::is_none")]
13    detached_uploads: Option<Vec<Attachment>>,
14}
15
16impl_response!(Response, base);
17
18impl Response {
19    /// Creates a new [Response].
20    pub const fn new() -> Self {
21        Self {
22            base: BaseResponse::new(),
23            detached_uploads: None,
24        }
25    }
26}
27
28field_access! {
29    Response {
30        /// Details on all files uploaded by the acting user whose only references
31        /// were removed when editing this message. Clients should ask the acting user
32        /// if they wish to delete the uploaded files returned in this response,
33        /// which might otherwise remain visible only in message edit history.
34        ///
35        /// Note that [access to message edit history](https://zulip.com/help/restrict-message-edit-history-access)
36        /// is configurable; this detail may be important in presenting the
37        /// question clearly to users.
38        ///
39        /// New in Zulip 10.0 (feature level 285).
40        detached_uploads: option_cow_deref { [Attachment], Vec<Attachment> },
41    }
42}
43
44impl_default!(Response);
45impl_display!(Response);
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50    use crate::response::{ApiResult, ErrorCode, Response as _};
51
52    #[test]
53    fn test_success_response() {
54        let msg = "test response";
55        let msg_json = serde_json::to_string(&msg).unwrap();
56
57        let result = ApiResult::Success;
58        let result_json = serde_json::to_string(&result).unwrap();
59
60        let id = 1;
61        let name = "name";
62        let path_id = "path_id";
63        let size = 2;
64        let create_time = 3;
65
66        let detached = Attachment::new()
67            .with_id(id)
68            .with_name(name)
69            .with_path_id(path_id)
70            .with_size(size)
71            .with_create_time(create_time);
72
73        let detached_uploads = vec![detached];
74        let detached_uploads_json = serde_json::to_string(&detached_uploads).unwrap();
75
76        let response = Response::new()
77            .with_msg(msg)
78            .with_result(result)
79            .with_detached_uploads(detached_uploads.clone());
80
81        let response_json = format!(
82            r#"{{"msg":{msg_json},"result":{result_json},"detached_uploads":{detached_uploads_json}}}"#
83        );
84
85        assert_eq!(serde_json::to_string(&response).unwrap(), response_json);
86        assert_eq!(
87            serde_json::from_str::<Response>(&response_json).unwrap(),
88            response
89        );
90
91        assert_eq!(response.msg(), msg);
92        assert_eq!(response.result(), result);
93        assert_eq!(response.code(), None);
94        assert_eq!(
95            response.detached_uploads(),
96            Some(detached_uploads.as_slice())
97        );
98    }
99
100    #[test]
101    fn test_error_response() {
102        let msg = "test response";
103        let msg_json = serde_json::to_string(&msg).unwrap();
104
105        let result = ApiResult::Error;
106        let result_json = serde_json::to_string(&result).unwrap();
107
108        let code = ErrorCode::BadRequest;
109        let code_json = serde_json::to_string(&code).unwrap();
110
111        let response = Response::new()
112            .with_msg(msg)
113            .with_result(result)
114            .with_code(code);
115
116        let response_json =
117            format!(r#"{{"msg":{msg_json},"result":{result_json},"code":{code_json}}}"#);
118
119        assert_eq!(serde_json::to_string(&response).unwrap(), response_json);
120        assert_eq!(
121            serde_json::from_str::<Response>(&response_json).unwrap(),
122            response
123        );
124
125        assert_eq!(response.msg(), msg);
126        assert_eq!(response.result(), result);
127        assert_eq!(response.code(), Some(code));
128    }
129}