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