BlueGit

ict rusty-teams

Files

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

models.rs

models.rs 9.4 KiB Download Raw
1use std::collections::HashMap;
2
3use chrono::{DateTime, Utc};
4
5pub const BOT_MRI_PREFIX: &str = "28:";
6
7pub fn is_bot_id(user_id: &str) -> bool {
8 user_id.starts_with(BOT_MRI_PREFIX)
9}
10
11#[derive(Debug, Clone, PartialEq, Default)]
12pub struct MemberRecord {
13 pub user_id: Option<String>,
14 pub display_name: String,
15}
16
17impl MemberRecord {
18 pub fn is_bot(&self) -> bool {
19 self.user_id.as_deref().is_some_and(is_bot_id)
20 }
21}
22
23#[derive(Debug, Clone, PartialEq, Default)]
24pub struct ChatRecord {
25 pub id: String,
26 pub kind: String,
27 pub title: String,
28 pub member_summary: String,
29 pub last_message_at: Option<DateTime<Utc>>,
30 pub last_read_at: Option<DateTime<Utc>>,
31 pub unread: bool,
32 pub muted: bool,
33 pub members: Vec<MemberRecord>,
34 pub last_message_preview: Option<String>,
35 pub last_message_sender_id: Option<String>,
36 pub last_message_sender_name: Option<String>,
37 pub last_message_deleted: bool,
38 pub last_event_system: bool,
39 pub last_event_call: bool,
40}
41
42impl ChatRecord {
43 pub fn display_title(&self) -> String {
44 if !self.title.trim().is_empty() {
45 self.title.clone()
46 } else if !self.member_summary.trim().is_empty() {
47 self.member_summary.clone()
48 } else {
49 "Chat".to_owned()
50 }
51 }
52}
53
54#[derive(Debug, Clone, PartialEq)]
55pub struct AvatarRecord {
56 pub user_id: String,
57 pub bytes: Option<Vec<u8>>,
58 pub content_type: String,
59 pub fetched_at: DateTime<Utc>,
60}
61
62#[derive(Debug, Clone, PartialEq)]
63pub struct FolderRecord {
64 pub id: String,
65 pub name: String,
66 pub kind: String,
67 pub expanded: bool,
68 pub conversation_ids: Vec<String>,
69}
70
71#[derive(Debug, Clone, PartialEq)]
72pub struct TeamRecord {
73 pub id: String,
74 pub name: String,
75}
76
77#[derive(Debug, Clone, PartialEq)]
78pub struct ChannelRecord {
79 pub id: String,
80 pub team_id: String,
81 pub name: String,
82 pub membership_type: Option<String>,
83 pub last_message_at: Option<DateTime<Utc>>,
84 pub unread: bool,
85}
86
87#[derive(Debug, Clone, PartialEq, Default)]
88pub struct MessageRecord {
89 pub conversation_id: String,
90 pub message_id: String,
91 pub reply_to_id: Option<String>,
92 pub sender_id: Option<String>,
93 pub sender_name: Option<String>,
94 pub created_at: DateTime<Utc>,
95 pub edited_at: Option<DateTime<Utc>>,
96 pub deleted: bool,
97 pub body_html: String,
98 pub attachments_json: String,
99 pub reactions_json: String,
100 pub mentions_json: String,
101 pub sender_application_id: Option<String>,
102 pub links_json: String,
103 pub subject: Option<String>,
104 pub call_event_json: Option<String>,
105}
106
107#[derive(Debug, Clone, PartialEq)]
108pub struct RecentScope {
109 pub conversation_id: String,
110 pub after: DateTime<Utc>,
111}
112
113/// The last read position of one conversation member, as chatsvc reports it.
114#[derive(Debug, Clone, PartialEq, Eq)]
115pub struct ReadHorizonRecord {
116 pub member_id: String,
117 pub read_until: DateTime<Utc>,
118 pub read_at: Option<DateTime<Utc>>,
119}
120
121#[derive(Debug, Clone, PartialEq)]
122pub struct SyncState {
123 pub newest_seen: Option<DateTime<Utc>>,
124 pub oldest_loaded: Option<DateTime<Utc>>,
125 pub has_more: bool,
126 pub older_cursor: Option<String>,
127 pub delta_link: Option<String>,
128}
129
130impl Default for SyncState {
131 fn default() -> Self {
132 SyncState {
133 newest_seen: None,
134 oldest_loaded: None,
135 has_more: true,
136 older_cursor: None,
137 delta_link: None,
138 }
139 }
140}
141
142#[derive(Debug, Clone, PartialEq)]
143pub struct SidebarTeam {
144 pub team: TeamRecord,
145 pub channels: Vec<ChannelRecord>,
146 pub hidden: bool,
147 pub hidden_channel_ids: Vec<String>,
148 pub notifications: HashMap<String, ChannelNotifications>,
149}
150
151impl SidebarTeam {
152 pub fn channel_notifications(&self, channel_id: &str) -> ChannelNotifications {
153 self.notifications
154 .get(channel_id)
155 .copied()
156 .unwrap_or_default()
157 }
158}
159
160#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
161pub enum ChannelNotificationLevel {
162 BannerAndFeed,
163 #[default]
164 Feed,
165 Off,
166}
167
168impl ChannelNotificationLevel {
169 pub fn key(self) -> &'static str {
170 match self {
171 ChannelNotificationLevel::BannerAndFeed => "banner",
172 ChannelNotificationLevel::Feed => "feed",
173 ChannelNotificationLevel::Off => "off",
174 }
175 }
176
177 pub fn from_key(key: &str) -> Self {
178 match key {
179 "banner" => ChannelNotificationLevel::BannerAndFeed,
180 "off" => ChannelNotificationLevel::Off,
181 _ => ChannelNotificationLevel::Feed,
182 }
183 }
184}
185
186#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
187pub struct ChannelNotifications {
188 pub level: ChannelNotificationLevel,
189 pub include_replies: bool,
190}
191
192#[derive(Debug, Clone, PartialEq, Eq)]
193pub struct ChannelTabRecord {
194 pub tab_id: String,
195 pub name: String,
196 pub definition_id: String,
197 pub open_url: Option<String>,
198}
199
200#[derive(Debug, Clone, PartialEq, Eq)]
201pub struct ChannelLayoutRecord {
202 pub channel_id: String,
203 pub general: bool,
204 pub hidden: bool,
205 pub tabs: Vec<ChannelTabRecord>,
206 pub notifications: ChannelNotifications,
207}
208
209#[derive(Debug, Clone, PartialEq, Eq)]
210pub struct TeamLayoutRecord {
211 pub team_id: String,
212 pub hidden: bool,
213 pub channels: Vec<ChannelLayoutRecord>,
214}
215
216/// 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.
217#[derive(Debug, Clone, PartialEq, Default)]
218pub struct Sidebar {
219 pub chats: Vec<ChatRecord>,
220 pub teams: Vec<SidebarTeam>,
221}
222
223#[derive(Debug, Clone, Copy, PartialEq)]
224pub struct ChatPreview<'a> {
225 pub text: Option<&'a str>,
226 pub sender_id: Option<&'a str>,
227 pub sender_name: Option<&'a str>,
228 pub deleted: bool,
229}
230
231#[derive(Debug, Clone, PartialEq)]
232pub struct ImageRecord {
233 pub key: String,
234 pub bytes: Vec<u8>,
235 pub content_type: String,
236 pub fetched_at: DateTime<Utc>,
237}
238
239#[derive(Debug, Clone, PartialEq)]
240pub struct SearchHit {
241 pub conversation_id: String,
242 pub message_id: String,
243 pub sender_name: Option<String>,
244 pub created_at: DateTime<Utc>,
245 pub snippet: String,
246}
247
248#[derive(Debug, Clone, PartialEq, Eq)]
249pub struct ConversationHit {
250 pub conversation_id: String,
251 pub title: String,
252}
253
254#[derive(Debug, Clone, PartialEq)]
255pub struct ActivityRecord {
256 pub id: i64,
257 pub conversation_id: String,
258 pub kind: String,
259 pub message_id: String,
260 pub actors_json: String,
261 pub preview: String,
262 pub glyphs: String,
263 pub count: u32,
264 pub updated_at: DateTime<Utc>,
265 pub read: bool,
266}
267
268#[derive(Debug, Clone, PartialEq, Eq)]
269pub struct AttachmentImage {
270 pub name: String,
271 pub format: String,
272 pub bytes: Vec<u8>,
273 pub width: Option<u32>,
274 pub height: Option<u32>,
275}
276
277#[derive(Debug, Clone, Copy, PartialEq, Eq)]
278pub enum OutboxTarget {
279 Flat,
280 Post,
281 Thread,
282}
283
284impl OutboxTarget {
285 pub(crate) fn as_str(self) -> &'static str {
286 match self {
287 OutboxTarget::Flat => "flat",
288 OutboxTarget::Post => "post",
289 OutboxTarget::Thread => "thread",
290 }
291 }
292
293 pub(crate) fn parse(value: &str) -> Option<Self> {
294 match value {
295 "flat" => Some(OutboxTarget::Flat),
296 "post" => Some(OutboxTarget::Post),
297 "thread" => Some(OutboxTarget::Thread),
298 _ => None,
299 }
300 }
301}
302
303#[derive(Debug, Clone, Copy, PartialEq, Eq)]
304pub enum OutboxState {
305 Sending,
306 Failed,
307}
308
309impl OutboxState {
310 pub(crate) fn as_str(self) -> &'static str {
311 match self {
312 OutboxState::Sending => "sending",
313 OutboxState::Failed => "failed",
314 }
315 }
316
317 pub(crate) fn parse(value: &str) -> Option<Self> {
318 match value {
319 "sending" => Some(OutboxState::Sending),
320 "failed" => Some(OutboxState::Failed),
321 _ => None,
322 }
323 }
324}
325
326#[derive(Debug, Clone, PartialEq, Eq)]
327pub struct OutboxRecord {
328 pub id: String,
329 pub conversation_id: String,
330 pub target: OutboxTarget,
331 pub thread_root_id: Option<String>,
332 pub payload: String,
333 pub images: Vec<AttachmentImage>,
334 pub state: OutboxState,
335 pub last_error: Option<String>,
336 pub created_at: DateTime<Utc>,
337}
338
339#[derive(Debug, Clone, PartialEq, Eq)]
340pub struct DraftRecord {
341 pub conversation_id: String,
342 pub payload: String,
343 pub preview: String,
344 pub images: Vec<AttachmentImage>,
345 pub updated_at: DateTime<Utc>,
346}
347
348#[derive(Debug, Clone, PartialEq, Eq)]
349pub struct AgentProposalMention {
350 pub user_id: String,
351 pub name: String,
352}
353
354#[derive(Debug, Clone, PartialEq, Eq)]
355pub struct AgentProposalRecord {
356 pub id: String,
357 pub conversation_id: String,
358 pub reply_to_message_id: Option<String>,
359 pub text: String,
360 pub mentions: Vec<AgentProposalMention>,
361 pub images: Vec<AttachmentImage>,
362 pub agent: Option<String>,
363 pub session_id: Option<String>,
364 pub created_at: DateTime<Utc>,
365 pub state: String,
366 pub resolved_at: Option<DateTime<Utc>>,
367}
368
369#[derive(Debug, Clone, PartialEq, Eq)]
370pub struct AgentReactionRecord {
371 pub id: String,
372 pub conversation_id: String,
373 pub message_id: String,
374 pub emoji: String,
375 pub agent: Option<String>,
376 pub session_id: Option<String>,
377 pub created_at: DateTime<Utc>,
378 pub state: String,
379 pub resolved_at: Option<DateTime<Utc>>,
380}