BlueGit

ict rusty-teams

Files

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

models.rs

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