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