| 1 | use std::collections::{BTreeSet, HashMap}; |
| 2 | |
| 3 | use chrono::{DateTime, Duration, FixedOffset, NaiveDate, Utc}; |
| 4 | use serde::{Deserialize, Serialize}; |
| 5 | use store::ActivityRecord; |
| 6 | |
| 7 | use crate::notify::Incoming; |
| 8 | use crate::reaction_model::{UNKNOWN_REACTOR, tooltip_text}; |
| 9 | |
| 10 | pub const RETENTION_DAYS: i64 = 14; |
| 11 | |
| 12 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 13 | pub enum Kind { |
| 14 | Messages, |
| 15 | Mention, |
| 16 | Reaction, |
| 17 | MissedCall, |
| 18 | } |
| 19 | |
| 20 | impl Kind { |
| 21 | fn key(self) -> &'static str { |
| 22 | match self { |
| 23 | Kind::Messages => "messages", |
| 24 | Kind::Mention => "mention", |
| 25 | Kind::Reaction => "reaction", |
| 26 | Kind::MissedCall => "missed_call", |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | fn from_key(key: &str) -> Option<Kind> { |
| 31 | [ |
| 32 | Kind::Messages, |
| 33 | Kind::Mention, |
| 34 | Kind::Reaction, |
| 35 | Kind::MissedCall, |
| 36 | ] |
| 37 | .into_iter() |
| 38 | .find(|kind| kind.key() == key) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 43 | pub enum Filter { |
| 44 | #[default] |
| 45 | All, |
| 46 | Mentions, |
| 47 | Unread, |
| 48 | } |
| 49 | |
| 50 | impl Filter { |
| 51 | pub const ALL: [Filter; 3] = [Filter::All, Filter::Mentions, Filter::Unread]; |
| 52 | |
| 53 | pub fn key(self) -> &'static str { |
| 54 | match self { |
| 55 | Filter::All => "all", |
| 56 | Filter::Mentions => "mentions", |
| 57 | Filter::Unread => "unread", |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | pub fn from_key(key: &str) -> Option<Filter> { |
| 62 | Filter::ALL.into_iter().find(|filter| filter.key() == key) |
| 63 | } |
| 64 | |
| 65 | pub fn label(self) -> &'static str { |
| 66 | match self { |
| 67 | Filter::All => "All", |
| 68 | Filter::Mentions => "@Mentions", |
| 69 | Filter::Unread => "Unread", |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | pub fn empty_label(self) -> &'static str { |
| 74 | match self { |
| 75 | Filter::All => "No activity yet", |
| 76 | Filter::Mentions => "No mentions", |
| 77 | Filter::Unread => "Nothing unread", |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | fn admits(self, entry: &Entry) -> bool { |
| 82 | match self { |
| 83 | Filter::All => true, |
| 84 | Filter::Mentions => entry.kind == Kind::Mention, |
| 85 | Filter::Unread => !entry.read, |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 91 | pub enum Bucket { |
| 92 | Today, |
| 93 | Yesterday, |
| 94 | Earlier, |
| 95 | } |
| 96 | |
| 97 | impl Bucket { |
| 98 | pub fn of(time: DateTime<Utc>, today: NaiveDate, offset: FixedOffset) -> Bucket { |
| 99 | let days_ago = (today - time.with_timezone(&offset).date_naive()).num_days(); |
| 100 | match days_ago { |
| 101 | ..=0 => Bucket::Today, |
| 102 | 1 => Bucket::Yesterday, |
| 103 | _ => Bucket::Earlier, |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | pub fn label(self) -> &'static str { |
| 108 | match self { |
| 109 | Bucket::Today => "Today", |
| 110 | Bucket::Yesterday => "Yesterday", |
| 111 | Bucket::Earlier => "Earlier", |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 117 | pub struct Actor { |
| 118 | pub user_id: Option<String>, |
| 119 | pub name: String, |
| 120 | } |
| 121 | |
| 122 | impl Actor { |
| 123 | pub fn is_unresolved(&self) -> bool { |
| 124 | self.user_id.is_some() && (self.name.is_empty() || self.name == UNKNOWN_REACTOR) |
| 125 | } |
| 126 | |
| 127 | pub fn display_name<'a>(&'a self, resolved: &'a HashMap<String, String>) -> &'a str { |
| 128 | if self.is_unresolved() |
| 129 | && let Some(name) = self |
| 130 | .user_id |
| 131 | .as_ref() |
| 132 | .and_then(|user_id| resolved.get(user_id)) |
| 133 | { |
| 134 | return name; |
| 135 | } |
| 136 | &self.name |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | #[derive(Debug, Clone, PartialEq)] |
| 141 | pub struct Entry { |
| 142 | pub id: i64, |
| 143 | pub conversation_id: String, |
| 144 | pub kind: Kind, |
| 145 | pub message_id: String, |
| 146 | pub actors: Vec<Actor>, |
| 147 | pub preview: String, |
| 148 | pub glyphs: Vec<String>, |
| 149 | pub count: u32, |
| 150 | pub updated_at: DateTime<Utc>, |
| 151 | pub read: bool, |
| 152 | } |
| 153 | |
| 154 | impl Entry { |
| 155 | pub fn names(&self, resolved: &HashMap<String, String>) -> String { |
| 156 | let names: Vec<&str> = self |
| 157 | .actors |
| 158 | .iter() |
| 159 | .map(|actor| actor.display_name(resolved)) |
| 160 | .collect(); |
| 161 | tooltip_text(&names) |
| 162 | } |
| 163 | |
| 164 | pub fn unresolved_user_ids(&self) -> impl Iterator<Item = &String> { |
| 165 | self.actors |
| 166 | .iter() |
| 167 | .filter(|actor| actor.is_unresolved()) |
| 168 | .filter_map(|actor| actor.user_id.as_ref()) |
| 169 | } |
| 170 | |
| 171 | pub fn latest_actor(&self) -> Option<&Actor> { |
| 172 | self.actors.last() |
| 173 | } |
| 174 | |
| 175 | pub fn to_record(&self) -> ActivityRecord { |
| 176 | ActivityRecord { |
| 177 | id: self.id, |
| 178 | conversation_id: self.conversation_id.clone(), |
| 179 | kind: self.kind.key().to_owned(), |
| 180 | message_id: self.message_id.clone(), |
| 181 | actors_json: serde_json::to_string(&self.actors).unwrap_or_else(|_| "[]".to_owned()), |
| 182 | preview: self.preview.clone(), |
| 183 | glyphs: serde_json::to_string(&self.glyphs).unwrap_or_else(|_| "[]".to_owned()), |
| 184 | count: self.count, |
| 185 | updated_at: self.updated_at, |
| 186 | read: self.read, |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | pub fn from_record(record: ActivityRecord) -> Option<Entry> { |
| 191 | Some(Entry { |
| 192 | id: record.id, |
| 193 | kind: Kind::from_key(&record.kind)?, |
| 194 | conversation_id: record.conversation_id, |
| 195 | message_id: record.message_id, |
| 196 | actors: serde_json::from_str(&record.actors_json).unwrap_or_default(), |
| 197 | preview: record.preview, |
| 198 | glyphs: serde_json::from_str(&record.glyphs).unwrap_or_default(), |
| 199 | count: record.count, |
| 200 | updated_at: record.updated_at, |
| 201 | read: record.read, |
| 202 | }) |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 207 | pub struct Sighting { |
| 208 | pub user_id: Option<String>, |
| 209 | pub name: String, |
| 210 | pub glyph: String, |
| 211 | pub created_at: Option<DateTime<Utc>>, |
| 212 | } |
| 213 | |
| 214 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 215 | pub struct ReactedMessage { |
| 216 | pub message_id: String, |
| 217 | pub preview: String, |
| 218 | pub sightings: Vec<Sighting>, |
| 219 | } |
| 220 | |
| 221 | pub struct Section<'a> { |
| 222 | pub bucket: Bucket, |
| 223 | pub entries: Vec<&'a Entry>, |
| 224 | } |
| 225 | |
| 226 | #[derive(Default)] |
| 227 | pub struct Feed { |
| 228 | entries: Vec<Entry>, |
| 229 | next_id: i64, |
| 230 | dirty: BTreeSet<i64>, |
| 231 | } |
| 232 | |
| 233 | pub fn start_time(saved: Option<DateTime<Utc>>, now: DateTime<Utc>) -> DateTime<Utc> { |
| 234 | saved |
| 235 | .unwrap_or(now) |
| 236 | .clamp(now - Duration::days(RETENTION_DAYS), now) |
| 237 | } |
| 238 | |
| 239 | impl Feed { |
| 240 | pub fn load(records: Vec<ActivityRecord>) -> Feed { |
| 241 | let entries: Vec<Entry> = records.into_iter().filter_map(Entry::from_record).collect(); |
| 242 | let next_id = entries.iter().map(|entry| entry.id).max().unwrap_or(0) + 1; |
| 243 | Feed { |
| 244 | entries, |
| 245 | next_id, |
| 246 | dirty: BTreeSet::new(), |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | pub fn add(&mut self, mut entry: Entry) -> i64 { |
| 251 | entry.id = self.next_id; |
| 252 | self.next_id += 1; |
| 253 | self.dirty.insert(entry.id); |
| 254 | let id = entry.id; |
| 255 | self.entries.push(entry); |
| 256 | id |
| 257 | } |
| 258 | |
| 259 | pub fn get(&self, id: i64) -> Option<&Entry> { |
| 260 | self.entries.iter().find(|entry| entry.id == id) |
| 261 | } |
| 262 | |
| 263 | pub fn unread_count(&self) -> usize { |
| 264 | self.entries.iter().filter(|entry| !entry.read).count() |
| 265 | } |
| 266 | |
| 267 | pub fn take_dirty(&mut self) -> Vec<Entry> { |
| 268 | let dirty = std::mem::take(&mut self.dirty); |
| 269 | self.entries |
| 270 | .iter() |
| 271 | .filter(|entry| dirty.contains(&entry.id)) |
| 272 | .cloned() |
| 273 | .collect() |
| 274 | } |
| 275 | |
| 276 | pub fn sections( |
| 277 | &self, |
| 278 | filter: Filter, |
| 279 | today: NaiveDate, |
| 280 | offset: FixedOffset, |
| 281 | ) -> Vec<Section<'_>> { |
| 282 | let mut admitted: Vec<&Entry> = self |
| 283 | .entries |
| 284 | .iter() |
| 285 | .filter(|entry| filter.admits(entry)) |
| 286 | .collect(); |
| 287 | admitted.sort_by_key(|entry| std::cmp::Reverse((entry.updated_at, entry.id))); |
| 288 | let mut sections: Vec<Section<'_>> = Vec::new(); |
| 289 | for entry in admitted { |
| 290 | let bucket = Bucket::of(entry.updated_at, today, offset); |
| 291 | match sections.last_mut() { |
| 292 | Some(section) if section.bucket == bucket => section.entries.push(entry), |
| 293 | _ => sections.push(Section { |
| 294 | bucket, |
| 295 | entries: vec![entry], |
| 296 | }), |
| 297 | } |
| 298 | } |
| 299 | sections |
| 300 | } |
| 301 | |
| 302 | pub fn record_message(&mut self, incoming: &Incoming, preview: String) { |
| 303 | let actor = Actor { |
| 304 | user_id: incoming.sender_id.clone(), |
| 305 | name: incoming.sender_name.clone(), |
| 306 | }; |
| 307 | if incoming.mentions_me { |
| 308 | self.add(Entry { |
| 309 | id: 0, |
| 310 | conversation_id: incoming.conversation_id.clone(), |
| 311 | kind: Kind::Mention, |
| 312 | message_id: incoming.message_id.clone(), |
| 313 | actors: vec![actor], |
| 314 | preview, |
| 315 | glyphs: Vec::new(), |
| 316 | count: 1, |
| 317 | updated_at: incoming.created_at, |
| 318 | read: false, |
| 319 | }); |
| 320 | return; |
| 321 | } |
| 322 | let open_row = self.entries.iter().position(|entry| { |
| 323 | entry.kind == Kind::Messages |
| 324 | && !entry.read |
| 325 | && entry.conversation_id == incoming.conversation_id |
| 326 | }); |
| 327 | let Some(index) = open_row else { |
| 328 | self.add(Entry { |
| 329 | id: 0, |
| 330 | conversation_id: incoming.conversation_id.clone(), |
| 331 | kind: Kind::Messages, |
| 332 | message_id: incoming.message_id.clone(), |
| 333 | actors: vec![actor], |
| 334 | preview, |
| 335 | glyphs: Vec::new(), |
| 336 | count: 1, |
| 337 | updated_at: incoming.created_at, |
| 338 | read: false, |
| 339 | }); |
| 340 | return; |
| 341 | }; |
| 342 | let entry = &mut self.entries[index]; |
| 343 | entry.count += 1; |
| 344 | entry.preview = preview; |
| 345 | entry.updated_at = incoming.created_at; |
| 346 | move_to_end(&mut entry.actors, actor); |
| 347 | self.dirty.insert(entry.id); |
| 348 | } |
| 349 | |
| 350 | pub fn record_missed_call(&mut self, conversation_id: &str, actor: Actor, at: DateTime<Utc>) { |
| 351 | let open_row = self.entries.iter().position(|entry| { |
| 352 | entry.kind == Kind::MissedCall |
| 353 | && !entry.read |
| 354 | && entry |
| 355 | .actors |
| 356 | .first() |
| 357 | .is_some_and(|known| known.user_id == actor.user_id && known.name == actor.name) |
| 358 | }); |
| 359 | match open_row { |
| 360 | Some(index) => { |
| 361 | let entry = &mut self.entries[index]; |
| 362 | entry.count += 1; |
| 363 | entry.updated_at = at; |
| 364 | self.dirty.insert(entry.id); |
| 365 | } |
| 366 | None => { |
| 367 | self.add(Entry { |
| 368 | id: 0, |
| 369 | conversation_id: conversation_id.to_owned(), |
| 370 | kind: Kind::MissedCall, |
| 371 | message_id: String::new(), |
| 372 | actors: vec![actor], |
| 373 | preview: String::new(), |
| 374 | glyphs: Vec::new(), |
| 375 | count: 1, |
| 376 | updated_at: at, |
| 377 | read: false, |
| 378 | }); |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | pub fn record_chat_preview( |
| 384 | &mut self, |
| 385 | conversation_id: &str, |
| 386 | actor: Actor, |
| 387 | preview: String, |
| 388 | at: DateTime<Utc>, |
| 389 | ) { |
| 390 | let known = self.entries.iter().any(|entry| { |
| 391 | entry.conversation_id == conversation_id |
| 392 | && entry.kind != Kind::Reaction |
| 393 | && entry.updated_at >= at |
| 394 | }); |
| 395 | if known { |
| 396 | return; |
| 397 | } |
| 398 | let open_row = self.entries.iter().position(|entry| { |
| 399 | entry.kind == Kind::Messages && !entry.read && entry.conversation_id == conversation_id |
| 400 | }); |
| 401 | match open_row { |
| 402 | Some(index) => { |
| 403 | let entry = &mut self.entries[index]; |
| 404 | entry.count += 1; |
| 405 | entry.preview = preview; |
| 406 | entry.updated_at = at; |
| 407 | move_to_end(&mut entry.actors, actor); |
| 408 | self.dirty.insert(entry.id); |
| 409 | } |
| 410 | None => { |
| 411 | self.add(Entry { |
| 412 | id: 0, |
| 413 | conversation_id: conversation_id.to_owned(), |
| 414 | kind: Kind::Messages, |
| 415 | message_id: String::new(), |
| 416 | actors: vec![actor], |
| 417 | preview, |
| 418 | glyphs: Vec::new(), |
| 419 | count: 1, |
| 420 | updated_at: at, |
| 421 | read: false, |
| 422 | }); |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | pub fn record_reactions( |
| 428 | &mut self, |
| 429 | conversation_id: &str, |
| 430 | message: &ReactedMessage, |
| 431 | my_user_id: &str, |
| 432 | cutoff: DateTime<Utc>, |
| 433 | now: DateTime<Utc>, |
| 434 | ) { |
| 435 | for sighting in &message.sightings { |
| 436 | let Some(user_id) = sighting.user_id.as_deref().filter(|id| *id != my_user_id) else { |
| 437 | continue; |
| 438 | }; |
| 439 | let existing = self.entries.iter().position(|entry| { |
| 440 | entry.kind == Kind::Reaction |
| 441 | && entry.conversation_id == conversation_id |
| 442 | && entry.message_id == message.message_id |
| 443 | }); |
| 444 | let known = existing.is_some_and(|index| { |
| 445 | self.entries[index] |
| 446 | .actors |
| 447 | .iter() |
| 448 | .any(|actor| actor.user_id.as_deref() == Some(user_id)) |
| 449 | }); |
| 450 | if known { |
| 451 | if let Some(index) = existing { |
| 452 | self.add_glyph(index, &sighting.glyph); |
| 453 | } |
| 454 | continue; |
| 455 | } |
| 456 | let fresh = match sighting.created_at { |
| 457 | Some(created_at) => created_at > cutoff, |
| 458 | None => existing.is_some(), |
| 459 | }; |
| 460 | if !fresh { |
| 461 | continue; |
| 462 | } |
| 463 | let actor = Actor { |
| 464 | user_id: Some(user_id.to_owned()), |
| 465 | name: sighting.name.clone(), |
| 466 | }; |
| 467 | let at = sighting.created_at.unwrap_or(now); |
| 468 | match existing { |
| 469 | Some(index) => { |
| 470 | let entry = &mut self.entries[index]; |
| 471 | entry.actors.push(actor); |
| 472 | entry.count = entry.actors.len() as u32; |
| 473 | entry.read = false; |
| 474 | entry.updated_at = entry.updated_at.max(at); |
| 475 | self.dirty.insert(entry.id); |
| 476 | self.add_glyph(index, &sighting.glyph); |
| 477 | } |
| 478 | None => { |
| 479 | self.add(Entry { |
| 480 | id: 0, |
| 481 | conversation_id: conversation_id.to_owned(), |
| 482 | kind: Kind::Reaction, |
| 483 | message_id: message.message_id.clone(), |
| 484 | actors: vec![actor], |
| 485 | preview: message.preview.clone(), |
| 486 | glyphs: vec![sighting.glyph.clone()], |
| 487 | count: 1, |
| 488 | updated_at: at, |
| 489 | read: false, |
| 490 | }); |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | fn add_glyph(&mut self, index: usize, glyph: &str) { |
| 497 | let entry = &mut self.entries[index]; |
| 498 | if !glyph.is_empty() && !entry.glyphs.iter().any(|known| known == glyph) { |
| 499 | entry.glyphs.push(glyph.to_owned()); |
| 500 | self.dirty.insert(entry.id); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | pub fn mark_read(&mut self, id: i64) -> bool { |
| 505 | self.mark_where(|entry| entry.id == id) |
| 506 | } |
| 507 | |
| 508 | pub fn mark_all_read(&mut self) -> bool { |
| 509 | self.mark_where(|_| true) |
| 510 | } |
| 511 | |
| 512 | pub fn mark_conversation_read(&mut self, conversation_id: &str) -> bool { |
| 513 | self.mark_where(|entry| entry.conversation_id == conversation_id) |
| 514 | } |
| 515 | |
| 516 | pub fn sync_read(&mut self, read_at: impl Fn(&str) -> Option<DateTime<Utc>>) -> bool { |
| 517 | self.mark_where(|entry| { |
| 518 | read_at(&entry.conversation_id).is_some_and(|read_at| entry.updated_at <= read_at) |
| 519 | }) |
| 520 | } |
| 521 | |
| 522 | fn mark_where(&mut self, matches: impl Fn(&Entry) -> bool) -> bool { |
| 523 | let mut changed = false; |
| 524 | for entry in self.entries.iter_mut().filter(|entry| !entry.read) { |
| 525 | if matches(entry) { |
| 526 | entry.read = true; |
| 527 | self.dirty.insert(entry.id); |
| 528 | changed = true; |
| 529 | } |
| 530 | } |
| 531 | changed |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | fn move_to_end(actors: &mut Vec<Actor>, actor: Actor) { |
| 536 | actors.retain(|known| match (&known.user_id, &actor.user_id) { |
| 537 | (Some(known_id), Some(id)) => known_id != id, |
| 538 | _ => known.name != actor.name, |
| 539 | }); |
| 540 | actors.push(actor); |
| 541 | } |
| 542 | |
| 543 | #[cfg(test)] |
| 544 | mod tests { |
| 545 | use chrono::TimeZone; |
| 546 | |
| 547 | use super::*; |
| 548 | use crate::notify::{ChatKind, Preview}; |
| 549 | |
| 550 | #[test] |
| 551 | fn filters_round_trip_through_their_key() { |
| 552 | for filter in Filter::ALL { |
| 553 | assert_eq!(Filter::from_key(filter.key()), Some(filter)); |
| 554 | } |
| 555 | assert_eq!(Filter::from_key("other"), None); |
| 556 | } |
| 557 | |
| 558 | fn at(hour: u32, minute: u32) -> DateTime<Utc> { |
| 559 | Utc.with_ymd_and_hms(2026, 10, 8, hour, minute, 0).unwrap() |
| 560 | } |
| 561 | |
| 562 | fn incoming(conversation_id: &str, message_id: &str, sender: &str, minute: u32) -> Incoming { |
| 563 | Incoming { |
| 564 | conversation_id: conversation_id.into(), |
| 565 | message_id: message_id.into(), |
| 566 | kind: ChatKind::Direct, |
| 567 | chat_title: "Chat".into(), |
| 568 | sender_id: Some(format!("id-{sender}")), |
| 569 | sender_name: sender.into(), |
| 570 | preview: Preview::Text(format!("text {message_id}")), |
| 571 | mentions_me: false, |
| 572 | muted: false, |
| 573 | signals: Default::default(), |
| 574 | created_at: at(9, minute), |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | fn record(feed: &mut Feed, incoming: &Incoming) { |
| 579 | feed.record_message(incoming, format!("text {}", incoming.message_id)); |
| 580 | } |
| 581 | |
| 582 | fn sighting(user: &str, glyph: &str, created_at: Option<DateTime<Utc>>) -> Sighting { |
| 583 | Sighting { |
| 584 | user_id: Some(format!("id-{user}")), |
| 585 | name: user.into(), |
| 586 | glyph: glyph.into(), |
| 587 | created_at, |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | fn react(feed: &mut Feed, sightings: &[Sighting]) { |
| 592 | let message = ReactedMessage { |
| 593 | message_id: "mine".into(), |
| 594 | preview: "my text".into(), |
| 595 | sightings: sightings.to_vec(), |
| 596 | }; |
| 597 | feed.record_reactions("chat", &message, "id-me", at(8, 0), at(10, 0)); |
| 598 | } |
| 599 | |
| 600 | fn all(feed: &Feed) -> Vec<&Entry> { |
| 601 | feed.sections(Filter::All, at(9, 0).date_naive(), offset()) |
| 602 | .into_iter() |
| 603 | .flat_map(|section| section.entries) |
| 604 | .collect() |
| 605 | } |
| 606 | |
| 607 | fn offset() -> FixedOffset { |
| 608 | FixedOffset::east_opt(0).unwrap() |
| 609 | } |
| 610 | |
| 611 | #[test] |
| 612 | fn three_messages_share_one_row_and_keep_the_first_message_id() { |
| 613 | let mut feed = Feed::default(); |
| 614 | for (index, message_id) in ["m1", "m2", "m3"].into_iter().enumerate() { |
| 615 | record( |
| 616 | &mut feed, |
| 617 | &incoming("chat", message_id, "Anna", index as u32), |
| 618 | ); |
| 619 | } |
| 620 | let entries = all(&feed); |
| 621 | assert_eq!(entries.len(), 1); |
| 622 | assert_eq!(entries[0].count, 3); |
| 623 | assert_eq!(entries[0].message_id, "m1"); |
| 624 | assert_eq!(entries[0].preview, "text m3"); |
| 625 | assert_eq!(entries[0].updated_at, at(9, 2)); |
| 626 | assert_eq!(feed.unread_count(), 1); |
| 627 | } |
| 628 | |
| 629 | #[test] |
| 630 | fn latest_sender_moves_to_the_end() { |
| 631 | let mut feed = Feed::default(); |
| 632 | record(&mut feed, &incoming("chat", "m1", "Anna", 1)); |
| 633 | record(&mut feed, &incoming("chat", "m2", "Ben", 2)); |
| 634 | record(&mut feed, &incoming("chat", "m3", "Anna", 3)); |
| 635 | let entries = all(&feed); |
| 636 | let entry = entries[0]; |
| 637 | assert_eq!(entry.names(&HashMap::new()), "Ben and Anna"); |
| 638 | assert_eq!(entry.latest_actor().unwrap().name, "Anna"); |
| 639 | } |
| 640 | |
| 641 | #[test] |
| 642 | fn chat_preview_adds_a_row_only_when_newer_than_the_feed() { |
| 643 | let mut feed = Feed::default(); |
| 644 | record(&mut feed, &incoming("chat", "m1", "Anna", 1)); |
| 645 | let anna = Actor { |
| 646 | user_id: Some("id-Anna".into()), |
| 647 | name: "Anna".into(), |
| 648 | }; |
| 649 | feed.record_chat_preview("chat", anna.clone(), "text m1".into(), at(9, 1)); |
| 650 | assert_eq!(all(&feed)[0].count, 1); |
| 651 | feed.record_chat_preview("chat", anna.clone(), "later".into(), at(9, 5)); |
| 652 | assert_eq!(all(&feed)[0].count, 2); |
| 653 | assert_eq!(all(&feed)[0].preview, "later"); |
| 654 | feed.record_chat_preview("other", anna, "offline".into(), at(9, 6)); |
| 655 | let entries = all(&feed); |
| 656 | assert_eq!(entries.len(), 2); |
| 657 | assert_eq!(entries[0].message_id, ""); |
| 658 | } |
| 659 | |
| 660 | #[test] |
| 661 | fn message_after_read_opens_a_new_row() { |
| 662 | let mut feed = Feed::default(); |
| 663 | record(&mut feed, &incoming("chat", "m1", "Anna", 1)); |
| 664 | feed.mark_conversation_read("chat"); |
| 665 | record(&mut feed, &incoming("chat", "m2", "Anna", 2)); |
| 666 | let entries = all(&feed); |
| 667 | assert_eq!(entries.len(), 2); |
| 668 | assert_eq!(entries[0].message_id, "m2"); |
| 669 | assert!(!entries[0].read); |
| 670 | assert!(entries[1].read); |
| 671 | assert_eq!(entries[1].count, 1); |
| 672 | } |
| 673 | |
| 674 | #[test] |
| 675 | fn mention_gets_its_own_row_and_is_not_counted_in_messages() { |
| 676 | let mut feed = Feed::default(); |
| 677 | record(&mut feed, &incoming("chat", "m1", "Anna", 1)); |
| 678 | let mut mention = incoming("chat", "m2", "Ben", 2); |
| 679 | mention.mentions_me = true; |
| 680 | record(&mut feed, &mention); |
| 681 | record(&mut feed, &incoming("chat", "m3", "Anna", 3)); |
| 682 | let entries = all(&feed); |
| 683 | assert_eq!(entries.len(), 2); |
| 684 | let mention_row = entries |
| 685 | .iter() |
| 686 | .find(|entry| entry.kind == Kind::Mention) |
| 687 | .unwrap(); |
| 688 | assert_eq!(mention_row.message_id, "m2"); |
| 689 | let messages_row = entries |
| 690 | .iter() |
| 691 | .find(|entry| entry.kind == Kind::Messages) |
| 692 | .unwrap(); |
| 693 | assert_eq!(messages_row.count, 2); |
| 694 | assert_eq!(messages_row.message_id, "m1"); |
| 695 | } |
| 696 | |
| 697 | #[test] |
| 698 | fn reactions_bundle_per_message_and_become_unread_again() { |
| 699 | let mut feed = Feed::default(); |
| 700 | react(&mut feed, &[sighting("Anna", "A", Some(at(9, 1)))]); |
| 701 | feed.mark_all_read(); |
| 702 | assert_eq!(feed.unread_count(), 0); |
| 703 | react( |
| 704 | &mut feed, |
| 705 | &[ |
| 706 | sighting("Anna", "A", Some(at(9, 1))), |
| 707 | sighting("Ben", "B", Some(at(9, 5))), |
| 708 | ], |
| 709 | ); |
| 710 | let entries = all(&feed); |
| 711 | assert_eq!(entries.len(), 1); |
| 712 | assert_eq!(entries[0].names(&HashMap::new()), "Anna and Ben"); |
| 713 | assert_eq!(entries[0].glyphs, vec!["A", "B"]); |
| 714 | assert_eq!(entries[0].count, 2); |
| 715 | assert_eq!(entries[0].updated_at, at(9, 5)); |
| 716 | assert!(!entries[0].read); |
| 717 | assert_eq!(entries[0].preview, "my text"); |
| 718 | } |
| 719 | |
| 720 | #[test] |
| 721 | fn known_reactor_adding_a_glyph_does_not_reopen_the_row() { |
| 722 | let mut feed = Feed::default(); |
| 723 | react(&mut feed, &[sighting("Anna", "A", Some(at(9, 1)))]); |
| 724 | feed.mark_all_read(); |
| 725 | react(&mut feed, &[sighting("Anna", "C", Some(at(9, 2)))]); |
| 726 | let entries = all(&feed); |
| 727 | assert_eq!(entries[0].glyphs, vec!["A", "C"]); |
| 728 | assert!(entries[0].read); |
| 729 | } |
| 730 | |
| 731 | #[test] |
| 732 | fn own_and_anonymous_reactions_are_ignored() { |
| 733 | let mut feed = Feed::default(); |
| 734 | let anonymous = Sighting { |
| 735 | user_id: None, |
| 736 | ..sighting("Anna", "A", Some(at(9, 1))) |
| 737 | }; |
| 738 | react(&mut feed, &[sighting("me", "A", Some(at(9, 1))), anonymous]); |
| 739 | assert!(all(&feed).is_empty()); |
| 740 | } |
| 741 | |
| 742 | #[test] |
| 743 | fn old_reactions_on_first_sight_are_ignored() { |
| 744 | let mut feed = Feed::default(); |
| 745 | react( |
| 746 | &mut feed, |
| 747 | &[ |
| 748 | sighting("Anna", "A", Some(at(7, 0))), |
| 749 | sighting("Ben", "B", None), |
| 750 | ], |
| 751 | ); |
| 752 | assert!(all(&feed).is_empty()); |
| 753 | react(&mut feed, &[sighting("Cleo", "C", Some(at(9, 0)))]); |
| 754 | react(&mut feed, &[sighting("Dan", "D", None)]); |
| 755 | assert_eq!(all(&feed)[0].names(&HashMap::new()), "Cleo and Dan"); |
| 756 | } |
| 757 | |
| 758 | #[test] |
| 759 | fn reading_a_conversation_leaves_others_unread() { |
| 760 | let mut feed = Feed::default(); |
| 761 | record(&mut feed, &incoming("a", "m1", "Anna", 1)); |
| 762 | record(&mut feed, &incoming("b", "m2", "Ben", 2)); |
| 763 | assert!(feed.mark_conversation_read("a")); |
| 764 | assert!(!feed.mark_conversation_read("a")); |
| 765 | assert_eq!(feed.unread_count(), 1); |
| 766 | let unread = all(&feed).into_iter().find(|entry| !entry.read).unwrap(); |
| 767 | assert_eq!(unread.conversation_id, "b"); |
| 768 | } |
| 769 | |
| 770 | #[test] |
| 771 | fn read_sync_only_covers_rows_up_to_the_read_time() { |
| 772 | let mut feed = Feed::default(); |
| 773 | record(&mut feed, &incoming("a", "m1", "Anna", 1)); |
| 774 | feed.mark_conversation_read("a"); |
| 775 | record(&mut feed, &incoming("a", "m2", "Anna", 30)); |
| 776 | record(&mut feed, &incoming("b", "m3", "Ben", 1)); |
| 777 | let changed = feed.sync_read(|conversation_id| (conversation_id == "a").then(|| at(9, 10))); |
| 778 | assert!(!changed); |
| 779 | let changed = feed.sync_read(|conversation_id| (conversation_id == "a").then(|| at(9, 45))); |
| 780 | assert!(changed); |
| 781 | assert_eq!(feed.unread_count(), 1); |
| 782 | } |
| 783 | |
| 784 | #[test] |
| 785 | fn mark_all_read_clears_the_count_and_reports_change_once() { |
| 786 | let mut feed = Feed::default(); |
| 787 | record(&mut feed, &incoming("a", "m1", "Anna", 1)); |
| 788 | record(&mut feed, &incoming("b", "m2", "Ben", 2)); |
| 789 | assert!(feed.mark_all_read()); |
| 790 | assert_eq!(feed.unread_count(), 0); |
| 791 | assert!(!feed.mark_all_read()); |
| 792 | } |
| 793 | |
| 794 | #[test] |
| 795 | fn filters_pick_mentions_and_unread() { |
| 796 | let mut feed = Feed::default(); |
| 797 | record(&mut feed, &incoming("a", "m1", "Anna", 1)); |
| 798 | let mut mention = incoming("b", "m2", "Ben", 2); |
| 799 | mention.mentions_me = true; |
| 800 | record(&mut feed, &mention); |
| 801 | feed.mark_conversation_read("a"); |
| 802 | let today = at(9, 0).date_naive(); |
| 803 | let count = |filter| { |
| 804 | feed.sections(filter, today, offset()) |
| 805 | .iter() |
| 806 | .map(|section| section.entries.len()) |
| 807 | .sum::<usize>() |
| 808 | }; |
| 809 | assert_eq!(count(Filter::All), 2); |
| 810 | assert_eq!(count(Filter::Mentions), 1); |
| 811 | assert_eq!(count(Filter::Unread), 1); |
| 812 | } |
| 813 | |
| 814 | #[test] |
| 815 | fn buckets_follow_the_local_day() { |
| 816 | let today = at(12, 0).date_naive(); |
| 817 | let utc = offset(); |
| 818 | assert_eq!(Bucket::of(at(0, 5), today, utc), Bucket::Today); |
| 819 | assert_eq!( |
| 820 | Bucket::of(at(0, 5) - Duration::minutes(10), today, utc), |
| 821 | Bucket::Yesterday |
| 822 | ); |
| 823 | assert_eq!( |
| 824 | Bucket::of(at(0, 5) - Duration::days(3), today, utc), |
| 825 | Bucket::Earlier |
| 826 | ); |
| 827 | let east = FixedOffset::east_opt(2 * 3600).unwrap(); |
| 828 | assert_eq!( |
| 829 | Bucket::of(at(23, 30) - Duration::days(1), today, east), |
| 830 | Bucket::Today |
| 831 | ); |
| 832 | } |
| 833 | |
| 834 | #[test] |
| 835 | fn sections_are_newest_first_and_grouped() { |
| 836 | let mut feed = Feed::default(); |
| 837 | let mut old = incoming("a", "m1", "Anna", 1); |
| 838 | old.created_at = at(9, 0) - Duration::days(1); |
| 839 | record(&mut feed, &old); |
| 840 | record(&mut feed, &incoming("b", "m2", "Ben", 2)); |
| 841 | record(&mut feed, &incoming("c", "m3", "Cleo", 3)); |
| 842 | let today = at(9, 0).date_naive(); |
| 843 | let sections = feed.sections(Filter::All, today, offset()); |
| 844 | let labels: Vec<&str> = sections |
| 845 | .iter() |
| 846 | .map(|section| section.bucket.label()) |
| 847 | .collect(); |
| 848 | assert_eq!(labels, ["Today", "Yesterday"]); |
| 849 | assert_eq!(sections[0].entries[0].conversation_id, "c"); |
| 850 | } |
| 851 | |
| 852 | #[test] |
| 853 | fn dirty_rows_are_reported_once_and_survive_a_round_trip() { |
| 854 | let mut feed = Feed::default(); |
| 855 | record(&mut feed, &incoming("a", "m1", "Anna", 1)); |
| 856 | react(&mut feed, &[sighting("Ben", "B", Some(at(9, 1)))]); |
| 857 | let dirty = feed.take_dirty(); |
| 858 | assert_eq!(dirty.len(), 2); |
| 859 | assert!(feed.take_dirty().is_empty()); |
| 860 | let reloaded = Feed::load(dirty.iter().map(Entry::to_record).collect()); |
| 861 | assert_eq!(all(&reloaded).len(), 2); |
| 862 | let mut reloaded = reloaded; |
| 863 | record(&mut reloaded, &incoming("c", "m9", "Cleo", 9)); |
| 864 | let ids: BTreeSet<i64> = reloaded.take_dirty().iter().map(|entry| entry.id).collect(); |
| 865 | assert_eq!(ids.len(), 1); |
| 866 | assert!(!dirty.iter().any(|entry| ids.contains(&entry.id))); |
| 867 | } |
| 868 | |
| 869 | #[test] |
| 870 | fn start_time_defaults_to_now_and_is_clamped() { |
| 871 | let now = at(12, 0); |
| 872 | assert_eq!(start_time(None, now), now); |
| 873 | assert_eq!(start_time(Some(now + Duration::hours(1)), now), now); |
| 874 | assert_eq!( |
| 875 | start_time(Some(now - Duration::days(30)), now), |
| 876 | now - Duration::days(RETENTION_DAYS) |
| 877 | ); |
| 878 | assert_eq!(start_time(Some(at(8, 0)), now), at(8, 0)); |
| 879 | } |
| 880 | #[test] |
| 881 | fn a_missed_call_is_one_row_per_caller_until_it_is_read() { |
| 882 | let mut feed = Feed::load(Vec::new()); |
| 883 | let caller = Actor { |
| 884 | user_id: Some("u1".into()), |
| 885 | name: "Cara".into(), |
| 886 | }; |
| 887 | feed.record_missed_call("19:dm", caller.clone(), at(9, 0)); |
| 888 | feed.record_missed_call("19:dm", caller.clone(), at(9, 5)); |
| 889 | assert_eq!(feed.unread_count(), 1); |
| 890 | let entry = feed.get(1).unwrap(); |
| 891 | assert_eq!(entry.kind, Kind::MissedCall); |
| 892 | assert_eq!(entry.count, 2); |
| 893 | assert_eq!(entry.updated_at, at(9, 5)); |
| 894 | feed.mark_all_read(); |
| 895 | feed.record_missed_call("19:dm", caller, at(10, 0)); |
| 896 | assert_eq!(feed.unread_count(), 1); |
| 897 | feed.record_missed_call( |
| 898 | "19:other", |
| 899 | Actor { |
| 900 | user_id: Some("u2".into()), |
| 901 | name: "Dan".into(), |
| 902 | }, |
| 903 | at(10, 1), |
| 904 | ); |
| 905 | assert_eq!(feed.unread_count(), 2); |
| 906 | } |
| 907 | |
| 908 | #[test] |
| 909 | fn missed_calls_survive_the_store_round_trip() { |
| 910 | let mut feed = Feed::load(Vec::new()); |
| 911 | feed.record_missed_call( |
| 912 | "19:dm", |
| 913 | Actor { |
| 914 | user_id: Some("u1".into()), |
| 915 | name: "Cara".into(), |
| 916 | }, |
| 917 | at(9, 0), |
| 918 | ); |
| 919 | let record = feed.get(1).unwrap().to_record(); |
| 920 | assert_eq!(record.kind, "missed_call"); |
| 921 | let restored = Entry::from_record(record).unwrap(); |
| 922 | assert_eq!(restored.kind, Kind::MissedCall); |
| 923 | } |
| 924 | } |