BlueGit

ict rusty-teams

Files

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

models.rs

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