BlueGit

ict rusty-teams

Files

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

models.rs

models.rs 11.3 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 hidden_channel_ids: Vec<String>,
197 pub notifications: HashMap<String, ChannelNotifications>,
198 pub mentions: HashMap<String, ChannelMentions>,
199}
200
201impl SidebarTeam {
202 pub fn channel_notifications(&self, channel_id: &str) -> ChannelNotifications {
203 self.notifications
204 .get(channel_id)
205 .copied()
206 .unwrap_or_default()
207 }
208
209 pub fn channel_mentions(&self, channel_id: &str) -> ChannelMentions {
210 self.mentions.get(channel_id).copied().unwrap_or_default()
211 }
212}
213
214/// Unread @mentions of one channel: personal (person or tag), @channel, @team.
215#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
216pub struct ChannelMentions {
217 pub personal: u32,
218 pub channel: u32,
219 pub team: u32,
220}
221
222impl ChannelMentions {
223 pub fn is_empty(&self) -> bool {
224 *self == ChannelMentions::default()
225 }
226}
227
228#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
229pub enum ChannelNotificationLevel {
230 BannerAndFeed,
231 #[default]
232 Feed,
233 Off,
234}
235
236impl ChannelNotificationLevel {
237 pub fn key(self) -> &'static str {
238 match self {
239 ChannelNotificationLevel::BannerAndFeed => "banner",
240 ChannelNotificationLevel::Feed => "feed",
241 ChannelNotificationLevel::Off => "off",
242 }
243 }
244
245 pub fn from_key(key: &str) -> Self {
246 match key {
247 "banner" => ChannelNotificationLevel::BannerAndFeed,
248 "off" => ChannelNotificationLevel::Off,
249 _ => ChannelNotificationLevel::Feed,
250 }
251 }
252}
253
254#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
255pub struct ChannelNotifications {
256 pub level: ChannelNotificationLevel,
257 pub include_replies: bool,
258}
259
260#[derive(Debug, Clone, PartialEq, Eq)]
261pub struct ChannelTabRecord {
262 pub tab_id: String,
263 pub name: String,
264 pub definition_id: String,
265 pub open_url: Option<String>,
266}
267
268#[derive(Debug, Clone, PartialEq, Eq)]
269pub struct ChannelLayoutRecord {
270 pub channel_id: String,
271 pub general: bool,
272 pub hidden: bool,
273 pub tabs: Vec<ChannelTabRecord>,
274 pub notifications: ChannelNotifications,
275}
276
277#[derive(Debug, Clone, PartialEq, Eq)]
278pub struct TeamLayoutRecord {
279 pub team_id: String,
280 pub hidden: bool,
281 pub channels: Vec<ChannelLayoutRecord>,
282}
283
284/// 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.
285#[derive(Debug, Clone, PartialEq, Default)]
286pub struct Sidebar {
287 pub chats: Vec<ChatRecord>,
288 pub teams: Vec<SidebarTeam>,
289}
290
291#[derive(Debug, Clone, Copy, PartialEq)]
292pub struct ChatPreview<'a> {
293 pub text: Option<&'a str>,
294 pub sender_id: Option<&'a str>,
295 pub sender_name: Option<&'a str>,
296 pub deleted: bool,
297}
298
299#[derive(Debug, Clone, PartialEq)]
300pub struct ImageRecord {
301 pub key: String,
302 pub bytes: Vec<u8>,
303 pub content_type: String,
304 pub fetched_at: DateTime<Utc>,
305}
306
307#[derive(Debug, Clone, PartialEq)]
308pub struct SearchHit {
309 pub conversation_id: String,
310 pub message_id: String,
311 pub sender_name: Option<String>,
312 pub created_at: DateTime<Utc>,
313 pub snippet: String,
314}
315
316#[derive(Debug, Clone, PartialEq, Eq)]
317pub struct ConversationHit {
318 pub conversation_id: String,
319 pub title: String,
320}
321
322#[derive(Debug, Clone, PartialEq)]
323pub struct ActivityRecord {
324 pub id: i64,
325 pub conversation_id: String,
326 pub kind: String,
327 pub message_id: String,
328 pub actors_json: String,
329 pub preview: String,
330 pub glyphs: String,
331 pub count: u32,
332 pub updated_at: DateTime<Utc>,
333 pub read: bool,
334}
335
336#[derive(Debug, Clone, PartialEq, Eq)]
337pub struct AttachmentImage {
338 pub name: String,
339 pub format: String,
340 pub bytes: Vec<u8>,
341 pub width: Option<u32>,
342 pub height: Option<u32>,
343}
344
345#[derive(Debug, Clone, Copy, PartialEq, Eq)]
346pub enum OutboxTarget {
347 Flat,
348 Post,
349 Thread,
350}
351
352impl OutboxTarget {
353 pub(crate) fn as_str(self) -> &'static str {
354 match self {
355 OutboxTarget::Flat => "flat",
356 OutboxTarget::Post => "post",
357 OutboxTarget::Thread => "thread",
358 }
359 }
360
361 pub(crate) fn parse(value: &str) -> Option<Self> {
362 match value {
363 "flat" => Some(OutboxTarget::Flat),
364 "post" => Some(OutboxTarget::Post),
365 "thread" => Some(OutboxTarget::Thread),
366 _ => None,
367 }
368 }
369}
370
371#[derive(Debug, Clone, Copy, PartialEq, Eq)]
372pub enum OutboxState {
373 Sending,
374 Failed,
375}
376
377impl OutboxState {
378 pub(crate) fn as_str(self) -> &'static str {
379 match self {
380 OutboxState::Sending => "sending",
381 OutboxState::Failed => "failed",
382 }
383 }
384
385 pub(crate) fn parse(value: &str) -> Option<Self> {
386 match value {
387 "sending" => Some(OutboxState::Sending),
388 "failed" => Some(OutboxState::Failed),
389 _ => None,
390 }
391 }
392}
393
394#[derive(Debug, Clone, PartialEq, Eq)]
395pub struct OutboxRecord {
396 pub id: String,
397 pub conversation_id: String,
398 pub target: OutboxTarget,
399 pub thread_root_id: Option<String>,
400 pub payload: String,
401 pub images: Vec<AttachmentImage>,
402 pub state: OutboxState,
403 pub last_error: Option<String>,
404 pub created_at: DateTime<Utc>,
405}
406
407#[derive(Debug, Clone, PartialEq, Eq)]
408pub struct DraftRecord {
409 pub conversation_id: String,
410 pub payload: String,
411 pub preview: String,
412 pub images: Vec<AttachmentImage>,
413 pub updated_at: DateTime<Utc>,
414}
415
416#[derive(Debug, Clone, PartialEq, Eq)]
417pub struct AgentProposalMention {
418 pub user_id: String,
419 pub name: String,
420}
421
422#[derive(Debug, Clone, PartialEq, Eq)]
423pub struct AgentProposalRecord {
424 pub id: String,
425 pub conversation_id: String,
426 pub reply_to_message_id: Option<String>,
427 pub text: String,
428 pub mentions: Vec<AgentProposalMention>,
429 pub images: Vec<AttachmentImage>,
430 pub agent: Option<String>,
431 pub session_id: Option<String>,
432 pub created_at: DateTime<Utc>,
433 pub state: String,
434 pub resolved_at: Option<DateTime<Utc>>,
435}
436
437#[derive(Debug, Clone, PartialEq, Eq)]
438pub struct AgentReactionRecord {
439 pub id: String,
440 pub conversation_id: String,
441 pub message_id: String,
442 pub emoji: String,
443 pub agent: Option<String>,
444 pub session_id: Option<String>,
445 pub created_at: DateTime<Utc>,
446 pub state: String,
447 pub resolved_at: Option<DateTime<Utc>>,
448}