BlueGit

ict rusty-teams

Files

rusty-teams / crates / store / src / models.rs

models.rs

models.rs 12.1 KiB Download Raw
1use std::collections::HashMap;
2
3use chrono::{DateTime, Utc};
4
5pub const BOT_MRI_PREFIX: &str = "28:";
6pub const UNKNOWN_USER_NAME: &str = "Unknown User";
7
8pub fn is_bot_id(user_id: &str) -> bool {
9 user_id.starts_with(BOT_MRI_PREFIX)
10}
11
12#[derive(Debug, Clone, PartialEq, Default)]
13pub struct MemberRecord {
14 pub user_id: Option<String>,
15 pub display_name: String,
16}
17
18impl MemberRecord {
19 pub fn is_bot(&self) -> bool {
20 self.user_id.as_deref().is_some_and(is_bot_id)
21 }
22}
23
24#[derive(Debug, Clone, PartialEq, Default)]
25pub struct ChatRecord {
26 pub id: String,
27 pub kind: String,
28 pub title: String,
29 pub member_summary: String,
30 pub last_message_at: Option<DateTime<Utc>>,
31 pub preview_at: Option<DateTime<Utc>>,
32 pub last_read_at: Option<DateTime<Utc>>,
33 pub unread: bool,
34 pub muted: bool,
35 pub members: Vec<MemberRecord>,
36 pub last_message_preview: Option<String>,
37 pub last_message_sender_id: Option<String>,
38 pub last_message_sender_name: Option<String>,
39 pub last_message_deleted: bool,
40 pub last_event_system: bool,
41 pub keeps_stored_preview: bool,
42 pub without_last_message: bool,
43}
44
45impl ChatRecord {
46 pub fn display_title(&self) -> String {
47 if !self.title.trim().is_empty() {
48 self.title.clone()
49 } else if !self.member_summary.trim().is_empty() {
50 self.member_summary.clone()
51 } else {
52 "Chat".to_owned()
53 }
54 }
55}
56
57#[derive(Debug, Clone, PartialEq)]
58pub struct AvatarRecord {
59 pub user_id: String,
60 pub bytes: Option<Vec<u8>>,
61 pub content_type: String,
62 pub fetched_at: DateTime<Utc>,
63}
64
65#[derive(Debug, Clone, PartialEq)]
66pub struct FolderRecord {
67 pub id: String,
68 pub name: String,
69 pub kind: String,
70 pub expanded: bool,
71 pub conversation_ids: Vec<String>,
72}
73
74#[derive(Debug, Clone, PartialEq)]
75pub struct TeamRecord {
76 pub id: String,
77 pub name: String,
78}
79
80#[derive(Debug, Clone, PartialEq)]
81pub struct ChannelReadState {
82 pub channel_id: String,
83 pub unread: bool,
84 pub last_message_at: Option<DateTime<Utc>>,
85 pub read_until: Option<DateTime<Utc>>,
86}
87
88#[derive(Debug, Clone, PartialEq)]
89pub struct ChannelRecord {
90 pub id: String,
91 pub team_id: String,
92 pub name: String,
93 pub membership_type: Option<String>,
94 pub last_message_at: Option<DateTime<Utc>>,
95 pub unread: bool,
96}
97
98#[derive(Debug, Clone, PartialEq, Default)]
99pub struct MessageRecord {
100 pub conversation_id: String,
101 pub message_id: String,
102 pub reply_to_id: Option<String>,
103 pub sender_id: Option<String>,
104 pub sender_name: Option<String>,
105 pub created_at: DateTime<Utc>,
106 pub edited_at: Option<DateTime<Utc>>,
107 pub deleted: bool,
108 pub body_html: String,
109 pub attachments_json: String,
110 pub reactions_json: String,
111 pub mentions_json: String,
112 pub sender_application_id: Option<String>,
113 pub links_json: String,
114 pub subject: Option<String>,
115 pub call_event_json: Option<String>,
116 pub importance: MessageImportance,
117}
118
119/// Graph `chatMessage.importance`; chatsvc keeps the same value in the message property `importance`.
120#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
121pub enum MessageImportance {
122 #[default]
123 Normal,
124 High,
125 Urgent,
126}
127
128impl MessageImportance {
129 pub fn from_graph(value: &str) -> MessageImportance {
130 match value.trim().to_ascii_lowercase().as_str() {
131 "high" => MessageImportance::High,
132 "urgent" => MessageImportance::Urgent,
133 _ => MessageImportance::Normal,
134 }
135 }
136
137 pub fn graph_value(self) -> &'static str {
138 match self {
139 MessageImportance::Normal => "normal",
140 MessageImportance::High => "high",
141 MessageImportance::Urgent => "urgent",
142 }
143 }
144
145 pub fn stored(self) -> Option<&'static str> {
146 (self != MessageImportance::Normal).then(|| self.graph_value())
147 }
148
149 pub fn from_stored(value: Option<&str>) -> MessageImportance {
150 value.map_or(MessageImportance::Normal, MessageImportance::from_graph)
151 }
152
153 pub fn is_marked(self) -> bool {
154 self != MessageImportance::Normal
155 }
156}
157
158#[derive(Debug, Clone, PartialEq)]
159pub struct RecentScope {
160 pub conversation_id: String,
161 pub after: DateTime<Utc>,
162}
163
164/// The last read position of one conversation member, as chatsvc reports it.
165#[derive(Debug, Clone, PartialEq, Eq)]
166pub struct ReadHorizonRecord {
167 pub member_id: String,
168 pub read_until: DateTime<Utc>,
169 pub read_at: Option<DateTime<Utc>>,
170}
171
172#[derive(Debug, Clone, PartialEq)]
173pub struct SyncState {
174 pub newest_seen: Option<DateTime<Utc>>,
175 pub oldest_loaded: Option<DateTime<Utc>>,
176 pub has_more: bool,
177 pub older_cursor: Option<String>,
178 pub delta_link: Option<String>,
179}
180
181impl Default for SyncState {
182 fn default() -> Self {
183 SyncState {
184 newest_seen: None,
185 oldest_loaded: None,
186 has_more: true,
187 older_cursor: None,
188 delta_link: None,
189 }
190 }
191}
192
193#[derive(Debug, Clone, PartialEq)]
194pub struct SidebarTeam {
195 pub team: TeamRecord,
196 pub channels: Vec<ChannelRecord>,
197 pub hidden: bool,
198 pub rights: TeamRights,
199 pub hidden_channel_ids: Vec<String>,
200 pub notifications: HashMap<String, ChannelNotifications>,
201 pub mentions: HashMap<String, ChannelMentions>,
202}
203
204impl SidebarTeam {
205 pub fn channel_notifications(&self, channel_id: &str) -> ChannelNotifications {
206 self.notifications
207 .get(channel_id)
208 .copied()
209 .unwrap_or_default()
210 }
211
212 pub fn channel_mentions(&self, channel_id: &str) -> ChannelMentions {
213 self.mentions.get(channel_id).copied().unwrap_or_default()
214 }
215}
216
217/// Unread @mentions of one channel: personal (person or tag), @channel, @team.
218#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
219pub struct ChannelMentions {
220 pub personal: u32,
221 pub channel: u32,
222 pub team: u32,
223}
224
225impl ChannelMentions {
226 pub fn is_empty(&self) -> bool {
227 *self == ChannelMentions::default()
228 }
229}
230
231#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
232pub enum ChannelNotificationLevel {
233 BannerAndFeed,
234 #[default]
235 Feed,
236 Off,
237}
238
239impl ChannelNotificationLevel {
240 pub fn key(self) -> &'static str {
241 match self {
242 ChannelNotificationLevel::BannerAndFeed => "banner",
243 ChannelNotificationLevel::Feed => "feed",
244 ChannelNotificationLevel::Off => "off",
245 }
246 }
247
248 pub fn from_key(key: &str) -> Self {
249 match key {
250 "banner" => ChannelNotificationLevel::BannerAndFeed,
251 "off" => ChannelNotificationLevel::Off,
252 _ => ChannelNotificationLevel::Feed,
253 }
254 }
255}
256
257#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
258pub struct ChannelNotifications {
259 pub level: ChannelNotificationLevel,
260 pub include_replies: bool,
261}
262
263#[derive(Debug, Clone, PartialEq, Eq)]
264pub struct ChannelTabRecord {
265 pub tab_id: String,
266 pub name: String,
267 pub definition_id: String,
268 pub open_url: Option<String>,
269}
270
271#[derive(Debug, Clone, PartialEq, Eq)]
272pub struct ChannelLayoutRecord {
273 pub channel_id: String,
274 pub general: bool,
275 pub hidden: bool,
276 pub tabs: Vec<ChannelTabRecord>,
277 pub notifications: ChannelNotifications,
278 pub substrate_group_id: Option<String>,
279}
280
281#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
282pub struct TeamRights {
283 pub owner: bool,
284 pub add_channel: bool,
285 pub add_private_channel: bool,
286 pub edit_channel: bool,
287 pub delete_channel: bool,
288}
289
290#[derive(Debug, Clone, PartialEq, Eq)]
291pub struct TeamLayoutRecord {
292 pub team_id: String,
293 pub hidden: bool,
294 pub rights: TeamRights,
295 pub channels: Vec<ChannelLayoutRecord>,
296}
297
298/// Everything the sidebar renders: chats by last message (newest first), teams in the user's Teams order (alphabetical without one), General first, then channels by name.
299#[derive(Debug, Clone, PartialEq, Default)]
300pub struct Sidebar {
301 pub chats: Vec<ChatRecord>,
302 pub teams: Vec<SidebarTeam>,
303}
304
305#[derive(Debug, Clone, Copy, PartialEq)]
306pub struct ChatPreview<'a> {
307 pub text: Option<&'a str>,
308 pub sender_id: Option<&'a str>,
309 pub sender_name: Option<&'a str>,
310 pub deleted: bool,
311}
312
313#[derive(Debug, Clone, PartialEq)]
314pub struct ImageRecord {
315 pub key: String,
316 pub bytes: Vec<u8>,
317 pub content_type: String,
318 pub fetched_at: DateTime<Utc>,
319}
320
321#[derive(Debug, Clone, PartialEq)]
322pub struct SearchHit {
323 pub conversation_id: String,
324 pub message_id: String,
325 pub sender_name: Option<String>,
326 pub created_at: DateTime<Utc>,
327 pub snippet: String,
328}
329
330#[derive(Debug, Clone, PartialEq, Eq)]
331pub struct ConversationHit {
332 pub conversation_id: String,
333 pub title: String,
334}
335
336#[derive(Debug, Clone, PartialEq)]
337pub struct ActivityRecord {
338 pub id: i64,
339 pub conversation_id: String,
340 pub kind: String,
341 pub message_id: String,
342 pub actors_json: String,
343 pub preview: String,
344 pub glyphs: String,
345 pub count: u32,
346 pub updated_at: DateTime<Utc>,
347 pub read: bool,
348}
349
350#[derive(Debug, Clone, PartialEq, Eq)]
351pub struct AttachmentImage {
352 pub name: String,
353 pub format: String,
354 pub bytes: Vec<u8>,
355 pub width: Option<u32>,
356 pub height: Option<u32>,
357}
358
359#[derive(Debug, Clone, Copy, PartialEq, Eq)]
360pub enum OutboxTarget {
361 Flat,
362 Post,
363 Thread,
364}
365
366impl OutboxTarget {
367 pub(crate) fn as_str(self) -> &'static str {
368 match self {
369 OutboxTarget::Flat => "flat",
370 OutboxTarget::Post => "post",
371 OutboxTarget::Thread => "thread",
372 }
373 }
374
375 pub(crate) fn parse(value: &str) -> Option<Self> {
376 match value {
377 "flat" => Some(OutboxTarget::Flat),
378 "post" => Some(OutboxTarget::Post),
379 "thread" => Some(OutboxTarget::Thread),
380 _ => None,
381 }
382 }
383}
384
385#[derive(Debug, Clone, Copy, PartialEq, Eq)]
386pub enum OutboxState {
387 Sending,
388 Failed,
389}
390
391impl OutboxState {
392 pub(crate) fn as_str(self) -> &'static str {
393 match self {
394 OutboxState::Sending => "sending",
395 OutboxState::Failed => "failed",
396 }
397 }
398
399 pub(crate) fn parse(value: &str) -> Option<Self> {
400 match value {
401 "sending" => Some(OutboxState::Sending),
402 "failed" => Some(OutboxState::Failed),
403 _ => None,
404 }
405 }
406}
407
408#[derive(Debug, Clone, PartialEq, Eq)]
409pub struct OutboxRecord {
410 pub id: String,
411 pub conversation_id: String,
412 pub target: OutboxTarget,
413 pub thread_root_id: Option<String>,
414 pub payload: String,
415 pub images: Vec<AttachmentImage>,
416 pub state: OutboxState,
417 pub last_error: Option<String>,
418 pub created_at: DateTime<Utc>,
419}
420
421#[derive(Debug, Clone, PartialEq, Eq)]
422pub struct DraftRecord {
423 pub conversation_id: String,
424 pub payload: String,
425 pub preview: String,
426 pub images: Vec<AttachmentImage>,
427 pub updated_at: DateTime<Utc>,
428}
429
430#[derive(Debug, Clone, PartialEq, Eq)]
431pub struct AgentProposalMention {
432 pub user_id: String,
433 pub name: String,
434}
435
436#[derive(Debug, Clone, PartialEq, Eq)]
437pub struct AgentProposalRecord {
438 pub id: String,
439 pub conversation_id: String,
440 pub reply_to_message_id: Option<String>,
441 pub text: String,
442 pub mentions: Vec<AgentProposalMention>,
443 pub images: Vec<AttachmentImage>,
444 pub agent: Option<String>,
445 pub session_id: Option<String>,
446 pub created_at: DateTime<Utc>,
447 pub state: String,
448 pub resolved_at: Option<DateTime<Utc>>,
449}
450
451#[derive(Debug, Clone, PartialEq, Eq)]
452pub struct AgentMeetingRecord {
453 pub id: String,
454 pub subject: String,
455 pub start: DateTime<Utc>,
456 pub end: DateTime<Utc>,
457 pub attendees: Vec<String>,
458 pub description: String,
459 pub agent: Option<String>,
460 pub session_id: Option<String>,
461 pub created_at: DateTime<Utc>,
462}
463
464#[derive(Debug, Clone, PartialEq, Eq)]
465pub struct AgentReactionRecord {
466 pub id: String,
467 pub conversation_id: String,
468 pub message_id: String,
469 pub emoji: String,
470 pub agent: Option<String>,
471 pub session_id: Option<String>,
472 pub created_at: DateTime<Utc>,
473 pub state: String,
474 pub resolved_at: Option<DateTime<Utc>>,
475}