Skip to main content

zulip_api/message/
edit_history.rs

1use serde::{Deserialize, Serialize};
2
3use crate::{field_access, impl_cow, impl_default, impl_display};
4
5/// Represents a the edit history of a message returned by the Zulip API server.
6#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
7pub struct EditHistory {
8    #[serde(skip_serializing_if = "Option::is_none")]
9    prev_content: Option<String>,
10    #[serde(skip_serializing_if = "Option::is_none")]
11    prev_rendered_content: Option<String>,
12    #[serde(skip_serializing_if = "Option::is_none")]
13    prev_stream: Option<u64>,
14    #[serde(skip_serializing_if = "Option::is_none")]
15    prev_topic: Option<String>,
16    #[serde(skip_serializing_if = "Option::is_none")]
17    stream: Option<String>,
18    #[serde(skip_serializing_if = "Option::is_none")]
19    timestamp: Option<u64>,
20    #[serde(skip_serializing_if = "Option::is_none")]
21    topic: Option<String>,
22    #[serde(skip_serializing_if = "Option::is_none")]
23    user_id: Option<u64>,
24}
25
26impl EditHistory {
27    /// Creates a new [EditHistory].
28    pub const fn new() -> Self {
29        Self {
30            prev_content: None,
31            prev_rendered_content: None,
32            prev_stream: None,
33            prev_topic: None,
34            stream: None,
35            timestamp: None,
36            topic: None,
37            user_id: None,
38        }
39    }
40}
41
42field_access! {
43    EditHistory {
44        /// Only present if message's content was edited.
45        ///
46        /// The content of the message immediately prior to this edit event.
47        prev_content: option_cow_deref { str, String },
48        /// Only present if message's content was edited.
49        ///
50        /// The rendered HTML representation of `prev_content`.
51        ///
52        /// See [Markdown message formatting](https://zulip.com/api/message-formatting) for details on Zulip's HTML format.
53        prev_rendered_content: option_cow_deref { str, String },
54        /// Only present if message's topic was edited.
55        ///
56        /// The topic of the message immediately prior to this edit event.
57        ///
58        /// **Changes**: New in Zulip 5.0 (feature level 118).
59        /// Previously, this field was called `prev_subject`;
60        /// clients are recommended to rename `prev_subject` to
61        /// `prev_topic` if present for compatibility with older Zulip servers.
62        prev_topic: option_cow_deref { str, String },
63        /// Only present if message's channel was edited.
64        ///
65        /// The ID of the channel containing the message
66        /// immediately after this edit event.
67        ///
68        /// **Changes**: New in Zulip 5.0 (feature level 118).
69        stream: option_cow_deref { str, String },
70        /// Only present if message's topic was edited.
71        ///
72        /// The topic of the message immediately after this edit event.
73        ///
74        /// **Changes**: New in Zulip 5.0 (feature level 118).
75        topic: option_cow_deref { str, String },
76    }
77}
78
79field_access! {
80    EditHistory {
81        /// Only present if message's channel was edited.
82        ///
83        /// The channel ID of the message immediately prior to this edit event.
84        ///
85        /// **Changes**: New in Zulip 3.0 (feature level 1).
86        prev_stream: option { u64 },
87        /// The UNIX timestamp for the edit.
88        timestamp: option { u64 },
89        /// The ID of the user that made the edit.
90        ///
91        /// Will be `null` only for edit history events predating March 2017.
92        ///
93        /// Clients can display edit history events where this
94        /// is `null` as modified by either the sender (for content
95        /// edits) or an unknown user (for topic edits).
96        user_id: option { u64 },
97    }
98}
99
100impl_cow!(EditHistory);
101impl_default!(EditHistory);
102impl_display!(EditHistory);
103
104#[cfg(test)]
105mod tests {
106    use super::*;
107
108    #[test]
109    fn test_edit_history_minimal() {
110        let user_id = 42;
111        let timestamp = 468460800;
112
113        let edit_history = EditHistory::new()
114            .with_user_id(user_id)
115            .with_timestamp(timestamp);
116
117        let edit_history_json = edit_history.to_string();
118
119        assert_eq!(edit_history.timestamp(), Some(timestamp));
120        assert_eq!(edit_history.user_id(), Some(user_id));
121
122        assert_eq!(
123            serde_json::to_string(&edit_history).unwrap(),
124            edit_history_json
125        );
126        assert_eq!(
127            serde_json::from_str::<EditHistory>(&edit_history_json).unwrap(),
128            edit_history
129        );
130    }
131
132    #[test]
133    fn test_edit_history_full() {
134        let user_id = 42;
135        let timestamp = 468460800;
136        let prev_content = "prev content";
137        let prev_rendered_content = "prev rendered content";
138        let prev_stream = 1;
139        let prev_topic = "prev topic";
140        let stream = "stream";
141        let topic = "topic";
142
143        let edit_history = EditHistory::new()
144            .with_user_id(user_id)
145            .with_timestamp(timestamp)
146            .with_prev_content(prev_content)
147            .with_prev_rendered_content(prev_rendered_content)
148            .with_prev_stream(prev_stream)
149            .with_prev_topic(prev_topic)
150            .with_stream(stream)
151            .with_topic(topic);
152
153        let edit_history_json = edit_history.to_string();
154
155        assert_eq!(edit_history.timestamp(), Some(timestamp));
156        assert_eq!(edit_history.user_id(), Some(user_id));
157        assert_eq!(edit_history.prev_content(), Some(prev_content));
158        assert_eq!(
159            edit_history.prev_rendered_content(),
160            Some(prev_rendered_content)
161        );
162        assert_eq!(edit_history.prev_stream(), Some(prev_stream));
163        assert_eq!(edit_history.prev_topic(), Some(prev_topic));
164        assert_eq!(edit_history.stream(), Some(stream));
165        assert_eq!(edit_history.topic(), Some(topic));
166
167        assert_eq!(
168            serde_json::to_string(&edit_history).unwrap(),
169            edit_history_json
170        );
171        assert_eq!(
172            serde_json::from_str::<EditHistory>(&edit_history_json).unwrap(),
173            edit_history
174        );
175    }
176}