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