| 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 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 | |
| 43 | impl 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)] |
| 56 | pub 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)] |
| 64 | pub 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)] |
| 73 | pub struct TeamRecord { |
| 74 | pub id: String, |
| 75 | pub name: String, |
| 76 | } |
| 77 | |
| 78 | #[derive(Debug, Clone, PartialEq)] |
| 79 | pub 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)] |
| 89 | pub 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)] |
| 109 | pub 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)] |
| 116 | pub 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)] |
| 123 | pub 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 | |
| 131 | impl 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)] |
| 144 | pub 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 | } |
| 151 | |
| 152 | impl SidebarTeam { |
| 153 | pub fn channel_notifications(&self, channel_id: &str) -> ChannelNotifications { |
| 154 | self.notifications |
| 155 | .get(channel_id) |
| 156 | .copied() |
| 157 | .unwrap_or_default() |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 162 | pub enum ChannelNotificationLevel { |
| 163 | BannerAndFeed, |
| 164 | #[default] |
| 165 | Feed, |
| 166 | Off, |
| 167 | } |
| 168 | |
| 169 | impl ChannelNotificationLevel { |
| 170 | pub fn key(self) -> &'static str { |
| 171 | match self { |
| 172 | ChannelNotificationLevel::BannerAndFeed => "banner", |
| 173 | ChannelNotificationLevel::Feed => "feed", |
| 174 | ChannelNotificationLevel::Off => "off", |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | pub fn from_key(key: &str) -> Self { |
| 179 | match key { |
| 180 | "banner" => ChannelNotificationLevel::BannerAndFeed, |
| 181 | "off" => ChannelNotificationLevel::Off, |
| 182 | _ => ChannelNotificationLevel::Feed, |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 188 | pub struct ChannelNotifications { |
| 189 | pub level: ChannelNotificationLevel, |
| 190 | pub include_replies: bool, |
| 191 | } |
| 192 | |
| 193 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 194 | pub struct ChannelTabRecord { |
| 195 | pub tab_id: String, |
| 196 | pub name: String, |
| 197 | pub definition_id: String, |
| 198 | pub open_url: Option<String>, |
| 199 | } |
| 200 | |
| 201 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 202 | pub struct ChannelLayoutRecord { |
| 203 | pub channel_id: String, |
| 204 | pub general: bool, |
| 205 | pub hidden: bool, |
| 206 | pub tabs: Vec<ChannelTabRecord>, |
| 207 | pub notifications: ChannelNotifications, |
| 208 | } |
| 209 | |
| 210 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 211 | pub struct TeamLayoutRecord { |
| 212 | pub team_id: String, |
| 213 | pub hidden: bool, |
| 214 | pub channels: Vec<ChannelLayoutRecord>, |
| 215 | } |
| 216 | |
| 217 | /// 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. |
| 218 | #[derive(Debug, Clone, PartialEq, Default)] |
| 219 | pub struct Sidebar { |
| 220 | pub chats: Vec<ChatRecord>, |
| 221 | pub teams: Vec<SidebarTeam>, |
| 222 | } |
| 223 | |
| 224 | #[derive(Debug, Clone, Copy, PartialEq)] |
| 225 | pub struct ChatPreview<'a> { |
| 226 | pub text: Option<&'a str>, |
| 227 | pub sender_id: Option<&'a str>, |
| 228 | pub sender_name: Option<&'a str>, |
| 229 | pub deleted: bool, |
| 230 | } |
| 231 | |
| 232 | #[derive(Debug, Clone, PartialEq)] |
| 233 | pub struct ImageRecord { |
| 234 | pub key: String, |
| 235 | pub bytes: Vec<u8>, |
| 236 | pub content_type: String, |
| 237 | pub fetched_at: DateTime<Utc>, |
| 238 | } |
| 239 | |
| 240 | #[derive(Debug, Clone, PartialEq)] |
| 241 | pub struct SearchHit { |
| 242 | pub conversation_id: String, |
| 243 | pub message_id: String, |
| 244 | pub sender_name: Option<String>, |
| 245 | pub created_at: DateTime<Utc>, |
| 246 | pub snippet: String, |
| 247 | } |
| 248 | |
| 249 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 250 | pub struct ConversationHit { |
| 251 | pub conversation_id: String, |
| 252 | pub title: String, |
| 253 | } |
| 254 | |
| 255 | #[derive(Debug, Clone, PartialEq)] |
| 256 | pub struct ActivityRecord { |
| 257 | pub id: i64, |
| 258 | pub conversation_id: String, |
| 259 | pub kind: String, |
| 260 | pub message_id: String, |
| 261 | pub actors_json: String, |
| 262 | pub preview: String, |
| 263 | pub glyphs: String, |
| 264 | pub count: u32, |
| 265 | pub updated_at: DateTime<Utc>, |
| 266 | pub read: bool, |
| 267 | } |
| 268 | |
| 269 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 270 | pub struct AttachmentImage { |
| 271 | pub name: String, |
| 272 | pub format: String, |
| 273 | pub bytes: Vec<u8>, |
| 274 | pub width: Option<u32>, |
| 275 | pub height: Option<u32>, |
| 276 | } |
| 277 | |
| 278 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 279 | pub enum OutboxTarget { |
| 280 | Flat, |
| 281 | Post, |
| 282 | Thread, |
| 283 | } |
| 284 | |
| 285 | impl OutboxTarget { |
| 286 | pub(crate) fn as_str(self) -> &'static str { |
| 287 | match self { |
| 288 | OutboxTarget::Flat => "flat", |
| 289 | OutboxTarget::Post => "post", |
| 290 | OutboxTarget::Thread => "thread", |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | pub(crate) fn parse(value: &str) -> Option<Self> { |
| 295 | match value { |
| 296 | "flat" => Some(OutboxTarget::Flat), |
| 297 | "post" => Some(OutboxTarget::Post), |
| 298 | "thread" => Some(OutboxTarget::Thread), |
| 299 | _ => None, |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 305 | pub enum OutboxState { |
| 306 | Sending, |
| 307 | Failed, |
| 308 | } |
| 309 | |
| 310 | impl OutboxState { |
| 311 | pub(crate) fn as_str(self) -> &'static str { |
| 312 | match self { |
| 313 | OutboxState::Sending => "sending", |
| 314 | OutboxState::Failed => "failed", |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | pub(crate) fn parse(value: &str) -> Option<Self> { |
| 319 | match value { |
| 320 | "sending" => Some(OutboxState::Sending), |
| 321 | "failed" => Some(OutboxState::Failed), |
| 322 | _ => None, |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 328 | pub struct OutboxRecord { |
| 329 | pub id: String, |
| 330 | pub conversation_id: String, |
| 331 | pub target: OutboxTarget, |
| 332 | pub thread_root_id: Option<String>, |
| 333 | pub payload: String, |
| 334 | pub images: Vec<AttachmentImage>, |
| 335 | pub state: OutboxState, |
| 336 | pub last_error: Option<String>, |
| 337 | pub created_at: DateTime<Utc>, |
| 338 | } |
| 339 | |
| 340 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 341 | pub struct DraftRecord { |
| 342 | pub conversation_id: String, |
| 343 | pub payload: String, |
| 344 | pub preview: String, |
| 345 | pub images: Vec<AttachmentImage>, |
| 346 | pub updated_at: DateTime<Utc>, |
| 347 | } |
| 348 | |
| 349 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 350 | pub struct AgentProposalMention { |
| 351 | pub user_id: String, |
| 352 | pub name: String, |
| 353 | } |
| 354 | |
| 355 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 356 | pub struct AgentProposalRecord { |
| 357 | pub id: String, |
| 358 | pub conversation_id: String, |
| 359 | pub reply_to_message_id: Option<String>, |
| 360 | pub text: String, |
| 361 | pub mentions: Vec<AgentProposalMention>, |
| 362 | pub images: Vec<AttachmentImage>, |
| 363 | pub agent: Option<String>, |
| 364 | pub session_id: Option<String>, |
| 365 | pub created_at: DateTime<Utc>, |
| 366 | pub state: String, |
| 367 | pub resolved_at: Option<DateTime<Utc>>, |
| 368 | } |
| 369 | |
| 370 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 371 | pub struct AgentReactionRecord { |
| 372 | pub id: String, |
| 373 | pub conversation_id: String, |
| 374 | pub message_id: String, |
| 375 | pub emoji: String, |
| 376 | pub agent: Option<String>, |
| 377 | pub session_id: Option<String>, |
| 378 | pub created_at: DateTime<Utc>, |
| 379 | pub state: String, |
| 380 | pub resolved_at: Option<DateTime<Utc>>, |
| 381 | } |