BlueGit

ict rusty-teams

Files

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

models.rs

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