summary refs log tree commit diff
path: root/src/mattermost/model.rs
blob: 9d4d78e55588a0c06e92d5fd4804914a2b46a35a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
use serde::Deserialize;
type PostId = String;
type UserId = String;

#[derive(Deserialize, Debug, PartialEq, Eq)]
#[serde(untagged)]
pub enum WebsocketMessage {
    Update(WebsocketUpdate),
    Reply(WebsocketReply),
}
#[derive(Deserialize, Debug, PartialEq, Eq)]
pub struct WebsocketReply {
    pub status: String,
    pub seq_reply: u32,
}
#[derive(Deserialize, Debug, PartialEq, Eq)]
pub struct Broadcast {
    pub channel_id: String,
    pub connection_id: String,
    pub omit_connection_id: String,
    // omit_users:
    pub team_id: String,
    pub user_id: String,
}
#[derive(Deserialize, Debug, PartialEq, Eq)]
pub struct WebsocketUpdateStuff {
    pub broadcast: Broadcast,
    pub seq: u32,
}
#[derive(Deserialize, Debug, PartialEq, Eq)]
pub struct WebsocketUpdate {
    #[serde(flatten)]
    pub stuff: WebsocketUpdateStuff,
    #[serde(flatten)]
    pub update: Update,
}
#[derive(Deserialize, Debug, PartialEq, Eq)]
#[serde(tag = "event", content = "data")]
pub enum Update {
    #[serde(rename = "posted")]
    Posted(Posted),
    #[serde(rename = "hello")]
    Hello(Hello),
    #[serde(rename = "status_change")]
    StatusChange {},
    #[serde(rename = "typing")]
    Typing {},
    #[serde(rename = "channel_viewed")]
    ChannelViewed {},
    #[serde(other)]
    Other,
}

#[derive(Deserialize, Debug, PartialEq, Eq)]
pub struct Posted {
    pub channel_display_name: String,
    pub channel_name: String,
    pub channel_type: String,
    #[serde(deserialize_with = "deserialize_json_field")]
    pub mentions: Vec<String>,
    #[serde(deserialize_with = "deserialize_json_field")]
    pub post: Post,
}
fn deserialize_json_field<'de, D, T>(deserializer: D) -> Result<T, D::Error>
where
    D: serde::Deserializer<'de>,
    T: serde::de::DeserializeOwned,
{
    let buf: String = String::deserialize(deserializer)?;
    serde_json::from_str(&buf).map_err(|x| {
        serde::de::Error::custom(format!("Error while deserializing json encoded field {x}"))
    })
}

#[derive(Deserialize, Debug, PartialEq, Eq)]
pub struct Post {
    pub id: String,
    pub create_at: u64,
    pub update_at: u64,
    pub edit_at: u64,
    pub delete_at: u64,
    pub is_pinned: bool,
    pub user_id: String,
    pub channel_id: String,
    // pub hashtags: String,
    // pub last_reply_at: u64,
    pub message: String,
    // pub metadata:,
    // pub original_id: String,
    // pub participants:,
    // pub pending_post_id: String,
    // pub preops
    // pub reply_count: u64,
    pub root_id: String,
    #[serde(rename = "type")]
    pub ttype: String,
}

#[derive(Deserialize, Debug, PartialEq, Eq)]
pub struct Hello {
    pub connection_id: String,
    pub server_version: String,
}
#[cfg(test)]
mod tests {
    use serde_json::Value;

    use crate::mattermost::model::{
        Broadcast, Post, Update, WebsocketUpdate, WebsocketUpdateStuff,
    };
    #[test]
    fn parse_post() {
        let post = Post {
            id: "a".to_owned(),
            create_at: 1,
            update_at: 2,
            edit_at: 3,
            delete_at: 4,
            is_pinned: true,
            user_id: "b".to_owned(),
            channel_id: "c".to_owned(),
            message: "d".to_owned(),
            root_id: "e".to_owned(),
            ttype: "f".to_owned(),
        };
        let post_str = r#"{
                    "id":"a",
                    "create_at":1,
                    "update_at":2,
                    "edit_at":3,
                    "delete_at":4,
                    "is_pinned":true,
                    "user_id":"b",
                    "channel_id":"c",
                    "message":"d",
                    "root_id":"e",
                    "type":"f"
                }"#;
        let d: Result<Post, _> = serde_json::from_str(post_str);
        assert_eq!(d.unwrap(), post);
        let data = format!(
            "\"{}\"",
            post_str.replace('\n', "\\n").replace('\"', "\\\"")
        );
        let x: Value = serde_json::from_str(&data).unwrap();
        let s = x.as_str().unwrap();
        let d: Result<Post, _> = serde_json::from_str(s);
        assert_eq!(d.unwrap(), post);
    }

    #[test]
    fn parse_update() {
        let post = Post {
            id: "a".to_owned(),
            create_at: 1,
            update_at: 2,
            edit_at: 3,
            delete_at: 4,
            is_pinned: true,
            user_id: "b".to_owned(),
            channel_id: "c".to_owned(),
            message: "d".to_owned(),
            root_id: "e".to_owned(),
            ttype: "f".to_owned(),
        };
        let data = r#"
        {
            "data":{
                "connection_id":  "aaaa",
                "server_version": "2137"
            },
            "event":"hello"
        }"#;
        let update: Result<Update, _> = serde_json::from_str(data);
        assert_eq!(
            update.unwrap(),
            Update::Hello(super::Hello {
                connection_id: "aaaa".to_owned(),
                server_version: "2137".to_owned()
            })
        );
        let data = r#"
        {
            "data":{
                "channel_display_name":"a",
                "channel_name":"b",
                "channel_type":"c",
                "mentions":"[\"d\"]",
                "post": "{\"id\":\"a\",\"create_at\":1,\"update_at\":2,\"edit_at\":3,\"delete_at\":4,\"is_pinned\":true,\"user_id\":\"b\",\"channel_id\":\"c\",\"message\":\"d\",\"root_id\":\"e\",\"type\":\"f\"}"
            },
            "event":"posted"
        }"#;
        let update: Result<Update, _> = serde_json::from_str(&data);
        assert_eq!(
            update.unwrap(),
            Update::Posted(super::Posted {
                channel_display_name: "a".to_owned(),
                channel_name: "b".to_owned(),
                channel_type: "c".to_owned(),
                mentions: vec!["d".to_owned()],
                post: post
            })
        );
    }
    #[test]
    fn parse_websocketupdate() {
        let post = Post {
            id: "a".to_owned(),
            create_at: 1,
            update_at: 2,
            edit_at: 3,
            delete_at: 4,
            is_pinned: true,
            user_id: "b".to_owned(),
            channel_id: "c".to_owned(),
            message: "d".to_owned(),
            root_id: "e".to_owned(),
            ttype: "f".to_owned(),
        };
        let data = r#"
        {
            "broadcast":{
                "channel_id":"a",
                "connection_id":"b",
                "omit_connection_id":"c",
                "team_id":"d",
                "user_id":"e"
            },
            "data":{
                "connection_id":  "aaaa",
                "server_version": "2137"
            },
            "event":"hello",
            "seq":2137
        }"#;
        let update: Result<WebsocketUpdate, _> = serde_json::from_str(data);
        assert_eq!(
            update.unwrap(),
            WebsocketUpdate {
                stuff: WebsocketUpdateStuff {
                    broadcast: Broadcast {
                        channel_id: "a".to_owned(),
                        connection_id: "b".to_owned(),
                        omit_connection_id: "c".to_owned(),
                        team_id: "d".to_owned(),
                        user_id: "e".to_owned()
                    },
                    seq: 2137,
                },
                update: Update::Hello(super::Hello {
                    connection_id: "aaaa".to_owned(),
                    server_version: "2137".to_owned()
                })
            }
        );
        let data = r#"
        {
            "broadcast":{
                "channel_id":"a",
                "connection_id":"b",
                "omit_connection_id":"c",
                "team_id":"d",
                "user_id":"e"
            },
            "data":{
                "channel_display_name":"a",
                "channel_name":"b",
                "channel_type":"c",
                "mentions":"[\"d\"]",
                "post": "{\"id\":\"a\",\"create_at\":1,\"update_at\":2,\"edit_at\":3,\"delete_at\":4,\"is_pinned\":true,\"user_id\":\"b\",\"channel_id\":\"c\",\"message\":\"d\",\"root_id\":\"e\",\"type\":\"f\"}"
            },
            "event":"posted",
            "seq": 2137
        }"#;
        let update: Result<WebsocketUpdate, _> = serde_json::from_str(&data);
        assert_eq!(
            update.unwrap(),
            WebsocketUpdate {
                stuff: WebsocketUpdateStuff {
                    broadcast: Broadcast {
                        channel_id: "a".to_owned(),
                        connection_id: "b".to_owned(),
                        omit_connection_id: "c".to_owned(),
                        team_id: "d".to_owned(),
                        user_id: "e".to_owned()
                    },
                    seq: 2137,
                },
                update: Update::Posted(super::Posted {
                    channel_display_name: "a".to_owned(),
                    channel_name: "b".to_owned(),
                    channel_type: "c".to_owned(),
                    mentions: vec!["d".to_owned()],
                    post: post
                })
            }
        );
    }
}