BlueGit

ict rusty-teams

Files

rusty-teams / crates / core / src / mapping.rs

mapping.rs

eba4ad94 History
mapping.rs 8.2 KiB Download Raw
1use chrono::{DateTime, Utc};
2use graph::{Attachment, Chat, Mention, Message, Reaction};
3use store::{ChannelRecord, ChatRecord, MemberRecord, MessageRecord, TeamRecord};
4
5use crate::card::card_content_text;
6use crate::markdown::plain_text_to_html;
7use crate::preview::OwnedPreview;
8use crate::stored::{AttachmentInfo, MentionInfo, QuoteInfo, ReactionInfo};
9
10const NO_NAME: &str = "?";
11
12pub fn chat_record(chat: &Chat, my_user_id: &str) -> Option<ChatRecord> {
13 if chat
14 .viewpoint
15 .as_ref()
16 .and_then(|viewpoint| viewpoint.is_hidden)
17 == Some(true)
18 {
19 return None;
20 }
21 let others: Vec<&graph::Member> = chat
22 .members
23 .iter()
24 .filter(|member| member.user_id.as_deref() != Some(my_user_id))
25 .collect();
26 let last_message_at = chat.last_message_time().or(chat.last_updated_date_time);
27 let last_read_at = chat
28 .viewpoint
29 .as_ref()
30 .and_then(|viewpoint| viewpoint.last_message_read_date_time);
31 let last_from_me = chat
32 .last_message_preview
33 .as_ref()
34 .and_then(|preview| preview.from.as_ref())
35 .and_then(|sender| sender.user_id())
36 == Some(my_user_id);
37 let preview = chat
38 .last_message_preview
39 .as_ref()
40 .map(OwnedPreview::from_graph);
41 Some(ChatRecord {
42 id: chat.id.clone(),
43 kind: chat.chat_type.clone(),
44 title: chat.title(my_user_id),
45 member_summary: others
46 .iter()
47 .map(|member| member_name(member))
48 .collect::<Vec<_>>()
49 .join(", "),
50 last_message_at,
51 last_read_at,
52 unread: is_unread(chat.last_message_time(), last_read_at, last_from_me),
53 members: chat
54 .members
55 .iter()
56 .map(|member| MemberRecord {
57 user_id: member.user_id.clone(),
58 display_name: member_name(member).to_owned(),
59 })
60 .collect(),
61 last_message_deleted: preview.as_ref().is_some_and(|preview| preview.deleted),
62 last_message_preview: preview.as_ref().and_then(|preview| preview.text.clone()),
63 last_message_sender_id: preview
64 .as_ref()
65 .and_then(|preview| preview.sender_id.clone()),
66 last_message_sender_name: preview
67 .as_ref()
68 .and_then(|preview| preview.sender_name.clone()),
69 })
70}
71
72fn member_name(member: &graph::Member) -> &str {
73 member
74 .display_name
75 .as_deref()
76 .or(member.email.as_deref())
77 .unwrap_or(NO_NAME)
78}
79
80fn is_unread(
81 last_message: Option<DateTime<Utc>>,
82 last_read: Option<DateTime<Utc>>,
83 last_from_me: bool,
84) -> bool {
85 match (last_message, last_read) {
86 (Some(message), Some(read)) => message > read && !last_from_me,
87 (Some(_), None) => !last_from_me,
88 _ => false,
89 }
90}
91
92pub fn team_record(team: &graph::Team) -> TeamRecord {
93 TeamRecord {
94 id: team.id.clone(),
95 name: team.display_name.clone(),
96 }
97}
98
99pub fn channel_record(team_id: &str, channel: &graph::Channel) -> ChannelRecord {
100 ChannelRecord {
101 id: channel.id.clone(),
102 team_id: team_id.to_owned(),
103 name: channel.display_name.clone(),
104 membership_type: channel.membership_type.clone(),
105 last_message_at: None,
106 unread: false,
107 }
108}
109
110/// `None` for system events and messages without a creation time.
111pub fn message_record(conversation_id: &str, message: &Message) -> Option<MessageRecord> {
112 if message
113 .message_type
114 .as_deref()
115 .is_some_and(|kind| kind != "message")
116 {
117 return None;
118 }
119 let created_at = message.created_date_time?;
120 let deleted = message.is_deleted();
121 let sender = message.from.as_ref();
122 Some(MessageRecord {
123 conversation_id: conversation_id.to_owned(),
124 message_id: message.id.clone(),
125 reply_to_id: message.reply_to_id.clone(),
126 sender_id: sender
127 .and_then(|sender| sender.user_id())
128 .map(str::to_owned),
129 sender_name: sender
130 .and_then(|sender| sender.display_name())
131 .map(str::to_owned),
132 created_at,
133 edited_at: message.last_edited_date_time,
134 deleted,
135 body_html: if deleted {
136 String::new()
137 } else {
138 body_html(message)
139 },
140 attachments_json: to_json(
141 &message
142 .attachments
143 .iter()
144 .map(attachment_info)
145 .collect::<Vec<_>>(),
146 ),
147 reactions_json: to_json(
148 &message
149 .reactions
150 .iter()
151 .map(reaction_info)
152 .collect::<Vec<_>>(),
153 ),
154 mentions_json: to_json(
155 &message
156 .mentions
157 .iter()
158 .filter_map(mention_info)
159 .collect::<Vec<_>>(),
160 ),
161 })
162}
163
164pub fn flatten_thread(conversation_id: &str, thread: &Message) -> Vec<MessageRecord> {
165 std::iter::once(thread)
166 .chain(thread.replies.iter())
167 .filter_map(|message| message_record(conversation_id, message))
168 .collect()
169}
170
171fn body_html(message: &Message) -> String {
172 let Some(body) = message.body.as_ref() else {
173 return String::new();
174 };
175 let content = body.content.as_deref().unwrap_or_default();
176 if body.content_type.eq_ignore_ascii_case("html") {
177 content.to_owned()
178 } else {
179 plain_text_to_html(content.trim())
180 }
181}
182
183pub(crate) fn attachment_info(attachment: &Attachment) -> AttachmentInfo {
184 let is_card = attachment
185 .content_type
186 .as_deref()
187 .is_some_and(|kind| kind.contains("card"));
188 let text = match (&attachment.content, is_card) {
189 (Some(content), true) => card_content_text(content),
190 _ => attachment
191 .name
192 .as_ref()
193 .map(|name| format!("[attachment: {name}]")),
194 };
195 AttachmentInfo {
196 content_type: attachment.content_type.clone(),
197 name: attachment.name.clone(),
198 url: attachment.content_url.clone(),
199 text,
200 size: attachment.content.as_deref().and_then(content_size),
201 quote: attachment
202 .content
203 .as_deref()
204 .filter(|_| attachment.content_type.as_deref() == Some("messageReference"))
205 .and_then(quote_info),
206 id: attachment.id.clone(),
207 content: attachment
208 .content
209 .clone()
210 .filter(|_| is_card || attachment.content_type.as_deref() == Some("messageReference")),
211 }
212}
213
214fn content_size(content: &str) -> Option<u64> {
215 serde_json::from_str::<serde_json::Value>(content)
216 .ok()?
217 .get("size")?
218 .as_u64()
219}
220
221fn quote_info(content: &str) -> Option<QuoteInfo> {
222 let value: serde_json::Value = serde_json::from_str(content).ok()?;
223 Some(QuoteInfo {
224 message_id: value.get("messageId")?.as_str()?.to_owned(),
225 preview: value
226 .get("messagePreview")
227 .and_then(serde_json::Value::as_str)
228 .unwrap_or_default()
229 .to_owned(),
230 sender_name: value
231 .pointer("/messageSender/user/displayName")
232 .and_then(serde_json::Value::as_str)
233 .map(str::to_owned),
234 })
235}
236
237fn reaction_info(reaction: &Reaction) -> ReactionInfo {
238 ReactionInfo {
239 reaction_type: reaction.reaction_type.clone(),
240 user_id: reaction
241 .user
242 .as_ref()
243 .and_then(|sender| sender.user_id())
244 .map(str::to_owned),
245 user_name: reaction
246 .user
247 .as_ref()
248 .and_then(|sender| sender.display_name())
249 .map(str::to_owned),
250 }
251}
252
253fn mention_info(mention: &Mention) -> Option<MentionInfo> {
254 let mentioned = mention.mentioned.as_ref();
255 let name = mention.mention_text.clone().or_else(|| {
256 mentioned
257 .and_then(|sender| sender.display_name())
258 .map(str::to_owned)
259 })?;
260 Some(MentionInfo {
261 user_id: mentioned
262 .and_then(|sender| sender.user_id())
263 .map(str::to_owned),
264 name,
265 })
266}
267
268fn to_json<T: serde::Serialize>(values: &[T]) -> String {
269 serde_json::to_string(values).unwrap_or_else(|_| "[]".to_owned())
270}