zulip_api/message/edit_message/
request.rs1use serde::{Deserialize, Serialize};
2
3use crate::message::PropagateMode;
4use crate::{field_access, impl_default, impl_display};
5
6#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
8pub struct Request {
9 #[serde(skip)]
10 message_id: u64,
11 content: String,
12 #[serde(skip_serializing_if = "Option::is_none")]
13 topic: Option<String>,
14 #[serde(skip_serializing_if = "Option::is_none")]
15 propagate_mode: Option<PropagateMode>,
16 #[serde(skip_serializing_if = "Option::is_none")]
17 send_notification_to_old_thread: Option<bool>,
18 #[serde(skip_serializing_if = "Option::is_none")]
19 send_notification_to_new_thread: Option<bool>,
20 #[serde(skip_serializing_if = "Option::is_none")]
21 prev_content_sha256: Option<String>,
22 #[serde(skip_serializing_if = "Option::is_none")]
23 stream_id: Option<u64>,
24}
25
26impl Request {
27 pub const fn new() -> Self {
29 Self {
30 message_id: 0,
31 content: String::new(),
32 topic: None,
33 propagate_mode: None,
34 send_notification_to_old_thread: None,
35 send_notification_to_new_thread: None,
36 prev_content_sha256: None,
37 stream_id: None,
38 }
39 }
40}
41
42field_access! {
43 Request {
44 message_id: u64,
45 }
46}
47
48field_access! {
49 Request {
50 content: cow { str },
51 }
52}
53
54field_access! {
55 Request {
56 topic: option_cow_deref { str, String },
57 prev_content_sha256: option_cow_deref { str, String },
58 }
59}
60
61field_access! {
62 Request {
63 propagate_mode: option { PropagateMode },
64 send_notification_to_old_thread: option { bool },
65 send_notification_to_new_thread: option { bool },
66 stream_id: option { u64 },
67 }
68}
69
70impl_default!(Request);
71impl_display!(Request);
72
73#[cfg(test)]
74mod tests {
75 use super::*;
76
77 #[test]
78 fn test_edit_message_minimal() {
79 let message_id = 1;
80
81 let content = "content";
82 let content_json = serde_json::to_string(&content).unwrap();
83
84 let mut req = Request::new()
85 .with_message_id(message_id)
86 .with_content(content);
87
88 let req_json = format!(r#"{{"content":{content_json}}}"#);
89
90 assert_eq!(req.message_id(), message_id);
91 assert_eq!(req.content(), content);
92
93 assert_eq!(serde_json::to_string(&req).unwrap(), req_json);
94
95 req.set_message_id(0);
96 assert_eq!(serde_json::from_str::<Request>(&req_json).unwrap(), req);
97 }
98
99 #[test]
100 fn test_edit_message_full() {
101 let message_id = 1;
102
103 let content = "content";
104 let content_json = serde_json::to_string(&content).unwrap();
105
106 let topic = "topic";
107 let topic_json = serde_json::to_string(&topic).unwrap();
108
109 let propagate_mode = PropagateMode::new();
110 let propagate_mode_json = serde_json::to_string(&propagate_mode).unwrap();
111
112 let send_notification_to_old_thread = false;
113 let send_notification_to_old_thread_json =
114 serde_json::to_string(&send_notification_to_old_thread).unwrap();
115
116 let send_notification_to_new_thread = false;
117 let send_notification_to_new_thread_json =
118 serde_json::to_string(&send_notification_to_new_thread).unwrap();
119
120 let prev_content_sha256 =
121 "6ae8a75555209fd6c44157c0aed8016e763ff435a19cf186f76863140143ff72";
122 let prev_content_sha256_json = serde_json::to_string(&prev_content_sha256).unwrap();
123
124 let stream_id = 2;
125 let stream_id_json = serde_json::to_string(&stream_id).unwrap();
126
127 let mut req = Request::new()
128 .with_message_id(message_id)
129 .with_content(content)
130 .with_topic(topic)
131 .with_propagate_mode(propagate_mode)
132 .with_send_notification_to_old_thread(send_notification_to_old_thread)
133 .with_send_notification_to_new_thread(send_notification_to_new_thread)
134 .with_prev_content_sha256(prev_content_sha256)
135 .with_stream_id(stream_id);
136
137 let req_json = format!(
138 r#"{{"content":{content_json},"topic":{topic_json},"propagate_mode":{propagate_mode_json},"send_notification_to_old_thread":{send_notification_to_old_thread_json},"send_notification_to_new_thread":{send_notification_to_new_thread_json},"prev_content_sha256":{prev_content_sha256_json},"stream_id":{stream_id_json}}}"#
139 );
140
141 assert_eq!(req.message_id(), message_id);
142 assert_eq!(req.content(), content);
143
144 assert_eq!(req.propagate_mode(), Some(propagate_mode));
145 assert_eq!(
146 req.send_notification_to_old_thread(),
147 Some(send_notification_to_old_thread)
148 );
149 assert_eq!(
150 req.send_notification_to_new_thread(),
151 Some(send_notification_to_new_thread)
152 );
153 assert_eq!(req.prev_content_sha256(), Some(prev_content_sha256));
154 assert_eq!(req.stream_id(), Some(stream_id));
155
156 assert_eq!(serde_json::to_string(&req).unwrap(), req_json);
157
158 req.set_message_id(0);
159 assert_eq!(serde_json::from_str::<Request>(&req_json).unwrap(), req);
160 }
161}