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
use chrono::prelude::*;
use chrono::DateTime;
use chrono::TimeZone;
use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue;
use std::cmp::Ordering;
use std::collections::HashMap;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Message {
pub sender: String,
pub mtype: String,
pub body: String,
pub date: DateTime<Local>,
pub room: String,
pub thumb: Option<String>,
pub url: Option<String>,
pub id: String,
pub formatted_body: Option<String>,
pub format: Option<String>,
pub source: Option<String>,
pub receipt: HashMap<String, i64>,
pub redacted: bool,
pub in_reply_to: Option<String>,
pub extra_content: Option<JsonValue>,
}
impl PartialEq for Message {
fn eq(&self, other: &Message) -> bool {
self.id == other.id
}
}
impl PartialOrd for Message {
fn partial_cmp(&self, other: &Message) -> Option<Ordering> {
if self == other {
Some(Ordering::Equal)
} else {
self.date.partial_cmp(&other.date)
}
}
}
impl Message {
pub fn new(room: String, sender: String, body: String, mtype: String) -> Self {
let date = Local::now();
Message {
id: get_txn_id(&room, &body, &date.to_string()),
sender,
mtype,
body,
date,
room,
thumb: None,
url: None,
formatted_body: None,
format: None,
source: None,
receipt: HashMap::new(),
redacted: false,
in_reply_to: None,
extra_content: None,
}
}
pub fn types() -> [&'static str; 2] {
["m.room.message", "m.sticker"]
}
pub fn supported_event(ev: &&JsonValue) -> bool {
let type_ = ev["type"].as_str().unwrap_or_default();
for t in Message::types().iter() {
if t == &type_ {
return true;
}
}
false
}
pub fn parse_room_message(roomid: &str, msg: &JsonValue) -> Message {
let sender = msg["sender"].as_str().unwrap_or_default();
let timestamp = msg["origin_server_ts"].as_i64().unwrap_or_default() / 1000;
let server_timestamp: DateTime<Local> = Local.timestamp(timestamp, 0);
let id = msg["event_id"].as_str().unwrap_or_default();
let type_ = msg["type"].as_str().unwrap_or_default();
let redacted = msg["unsigned"].get("redacted_because") != None;
let mut message = Message {
sender: sender.to_string(),
date: server_timestamp,
room: String::from(roomid),
id: id.to_string(),
mtype: type_.to_string(),
body: String::new(),
url: None,
thumb: None,
formatted_body: None,
format: None,
source: serde_json::to_string_pretty(&msg).ok(),
receipt: HashMap::new(),
redacted,
in_reply_to: None,
extra_content: None,
};
let c = &msg["content"];
match type_ {
"m.room.message" => Message::parse_m_room_message(&mut message, c),
"m.sticker" => Message::parse_m_sticker(&mut message, c),
_ => {}
};
message
}
fn parse_m_room_message(msg: &mut Message, c: &JsonValue) {
let mtype = c["msgtype"].as_str().unwrap_or_default();
let body = c["body"].as_str().unwrap_or_default();
let formatted_body = c["formatted_body"].as_str().map(String::from);
let format = c["format"].as_str().map(String::from);
match mtype {
"m.image" | "m.file" | "m.video" | "m.audio" => {
let url = String::from(c["url"].as_str().unwrap_or_default());
let mut t = String::from(c["info"]["thumbnail_url"].as_str().unwrap_or_default());
if t.is_empty() && !url.is_empty() {
t = url.clone();
}
msg.url = Some(url);
msg.thumb = Some(t);
}
"m.text" => {
msg.in_reply_to = c["m.relates_to"]["m.in_reply_to"]["event_id"]
.as_str()
.map(String::from)
}
_ => {}
};
msg.mtype = mtype.to_string();
msg.body = body.to_string();
msg.formatted_body = formatted_body;
msg.format = format;
}
fn parse_m_sticker(msg: &mut Message, c: &JsonValue) {
let body = c["body"].as_str().unwrap_or_default();
let url = String::from(c["url"].as_str().unwrap_or_default());
let mut t = String::from(c["info"]["thumbnail_url"].as_str().unwrap_or_default());
if t.is_empty() && !url.is_empty() {
t = url.clone();
}
msg.body = body.to_string();
msg.url = Some(url);
msg.thumb = Some(t);
}
pub fn from_json_events_iter<'a, I>(roomid: &str, events: I) -> Vec<Message>
where
I: Iterator<Item = &'a JsonValue>,
{
let mut ms = vec![];
let evs = events.filter(Message::supported_event);
for msg in evs {
let m = Message::parse_room_message(roomid.clone(), msg);
ms.push(m);
}
ms
}
pub fn set_receipt(&mut self, receipt: HashMap<String, i64>) {
self.receipt = receipt;
}
}
pub fn get_txn_id(room: &str, body: &str, date: &str) -> String {
let msg = format!("{}{}{}", room, body, date);
let digest = md5::compute(msg.as_bytes());
format!("{:x}", digest)
}