| 1 | use std::collections::{HashMap, HashSet}; |
| 2 | |
| 3 | use chrono::{DateTime, FixedOffset, Utc}; |
| 4 | use store::{ChatRecord, Sidebar}; |
| 5 | |
| 6 | use crate::app_state::chat_title; |
| 7 | use crate::data::{ |
| 8 | Directory, FolderKind, Person, face_members, is_one_on_one, others, unread_count, |
| 9 | }; |
| 10 | use crate::format; |
| 11 | |
| 12 | pub const FAVORITES_FALLBACK_NAME: &str = "Angeheftet"; |
| 13 | pub const OTHERS_NAME: &str = "Weitere Chats"; |
| 14 | pub const OTHERS_ID: &str = "others"; |
| 15 | pub const EMPTY_FOLDER_HINT: &str = "Leer. Chats hierher ziehen."; |
| 16 | pub const DELETED_PREVIEW: &str = "Nachricht gelöscht"; |
| 17 | const OWN_PREFIX: &str = "Du"; |
| 18 | |
| 19 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 20 | pub enum Preview { |
| 21 | Empty, |
| 22 | Deleted, |
| 23 | Text { |
| 24 | prefix: Option<String>, |
| 25 | text: String, |
| 26 | }, |
| 27 | } |
| 28 | |
| 29 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 30 | pub enum Unread { |
| 31 | None, |
| 32 | Dot, |
| 33 | Count(u32), |
| 34 | } |
| 35 | |
| 36 | impl Unread { |
| 37 | pub fn is_unread(self) -> bool { |
| 38 | self != Unread::None |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 43 | pub struct Face { |
| 44 | pub user_id: Option<String>, |
| 45 | pub name: String, |
| 46 | } |
| 47 | |
| 48 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 49 | pub enum AvatarSpec { |
| 50 | Single(Face), |
| 51 | Pair(Face, Face), |
| 52 | } |
| 53 | |
| 54 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 55 | pub struct ChatItem { |
| 56 | pub id: String, |
| 57 | pub title: String, |
| 58 | pub time_label: String, |
| 59 | pub preview: Preview, |
| 60 | pub unread: Unread, |
| 61 | pub avatar: AvatarSpec, |
| 62 | pub presence_user: Option<String>, |
| 63 | pub folder_id: Option<String>, |
| 64 | } |
| 65 | |
| 66 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 67 | pub enum SectionKind { |
| 68 | Favorites, |
| 69 | Folder, |
| 70 | Others, |
| 71 | } |
| 72 | |
| 73 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 74 | pub struct Section { |
| 75 | pub id: String, |
| 76 | pub name: String, |
| 77 | pub kind: SectionKind, |
| 78 | pub collapsed: bool, |
| 79 | pub count: usize, |
| 80 | pub unread_chats: u32, |
| 81 | pub items: Vec<ChatItem>, |
| 82 | pub show_empty_hint: bool, |
| 83 | } |
| 84 | |
| 85 | pub struct SectionInput<'a> { |
| 86 | pub chats: &'a [ChatRecord], |
| 87 | pub directory: &'a Directory, |
| 88 | pub collapsed: &'a HashSet<String>, |
| 89 | pub now: DateTime<Utc>, |
| 90 | pub offset: FixedOffset, |
| 91 | } |
| 92 | |
| 93 | pub fn preview_for(chat: &ChatRecord, me: Option<&Person>) -> Preview { |
| 94 | if chat.last_message_deleted { |
| 95 | return Preview::Deleted; |
| 96 | } |
| 97 | let Some(text) = chat |
| 98 | .last_message_preview |
| 99 | .as_deref() |
| 100 | .map(str::trim) |
| 101 | .filter(|text| !text.is_empty()) |
| 102 | else { |
| 103 | return Preview::Empty; |
| 104 | }; |
| 105 | let from_me = match (chat.last_message_sender_id.as_deref(), me) { |
| 106 | (Some(sender), Some(me)) => sender == me.user_id, |
| 107 | _ => false, |
| 108 | }; |
| 109 | let prefix = if from_me { |
| 110 | Some(OWN_PREFIX.to_owned()) |
| 111 | } else if is_one_on_one(chat) { |
| 112 | None |
| 113 | } else { |
| 114 | chat.last_message_sender_name |
| 115 | .as_deref() |
| 116 | .map(|name| format::first_name(name).to_owned()) |
| 117 | .filter(|name| !name.is_empty()) |
| 118 | }; |
| 119 | Preview::Text { |
| 120 | prefix, |
| 121 | text: text.to_owned(), |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | pub fn avatar_for(chat: &ChatRecord, me: Option<&Person>) -> AvatarSpec { |
| 126 | let faces = face_members(chat, me); |
| 127 | let face = |(user_id, name): (Option<String>, String)| Face { user_id, name }; |
| 128 | if is_one_on_one(chat) || faces.len() < 2 { |
| 129 | return AvatarSpec::Single(match faces.into_iter().next() { |
| 130 | Some(first) => face(first), |
| 131 | None => Face { |
| 132 | user_id: None, |
| 133 | name: chat_title(chat), |
| 134 | }, |
| 135 | }); |
| 136 | } |
| 137 | let mut iterator = faces.into_iter(); |
| 138 | let (first, second) = (iterator.next(), iterator.next()); |
| 139 | match (first, second) { |
| 140 | (Some(first), Some(second)) => AvatarSpec::Pair(face(first), face(second)), |
| 141 | _ => AvatarSpec::Single(Face { |
| 142 | user_id: None, |
| 143 | name: chat_title(chat), |
| 144 | }), |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | pub fn chat_item(chat: &ChatRecord, input: &SectionInput<'_>, folder_id: Option<&str>) -> ChatItem { |
| 149 | let me = input.directory.me.as_ref(); |
| 150 | let unread = if chat.unread { |
| 151 | unread_count(input.directory, chat) |
| 152 | .filter(|count| *count > 0) |
| 153 | .map_or(Unread::Dot, Unread::Count) |
| 154 | } else { |
| 155 | Unread::None |
| 156 | }; |
| 157 | let presence_user = is_one_on_one(chat) |
| 158 | .then(|| { |
| 159 | others(chat, me) |
| 160 | .into_iter() |
| 161 | .find_map(|(user_id, _)| user_id) |
| 162 | }) |
| 163 | .flatten(); |
| 164 | ChatItem { |
| 165 | id: chat.id.clone(), |
| 166 | title: chat_title(chat), |
| 167 | time_label: chat |
| 168 | .last_message_at |
| 169 | .map(|time| { |
| 170 | format::list_time_label( |
| 171 | time, |
| 172 | input.now.with_timezone(&input.offset).date_naive(), |
| 173 | input.offset, |
| 174 | ) |
| 175 | }) |
| 176 | .unwrap_or_default(), |
| 177 | preview: preview_for(chat, me), |
| 178 | unread, |
| 179 | avatar: avatar_for(chat, me), |
| 180 | presence_user, |
| 181 | folder_id: folder_id.map(str::to_owned), |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | pub fn unread_chat_count(chats: &[ChatRecord]) -> u32 { |
| 186 | chats.iter().filter(|chat| chat.unread).count() as u32 |
| 187 | } |
| 188 | |
| 189 | pub fn any_unread_channel(sidebar: &Sidebar) -> bool { |
| 190 | sidebar |
| 191 | .teams |
| 192 | .iter() |
| 193 | .any(|team| team.channels.iter().any(|channel| channel.unread)) |
| 194 | } |
| 195 | |
| 196 | pub fn build_sections(input: &SectionInput<'_>) -> Vec<Section> { |
| 197 | let by_id: HashMap<&str, &ChatRecord> = input |
| 198 | .chats |
| 199 | .iter() |
| 200 | .map(|chat| (chat.id.as_str(), chat)) |
| 201 | .collect(); |
| 202 | let mut placed: HashSet<&str> = HashSet::new(); |
| 203 | let mut sections = Vec::new(); |
| 204 | for folder in &input.directory.folders { |
| 205 | let items: Vec<ChatItem> = folder |
| 206 | .conversation_ids |
| 207 | .iter() |
| 208 | .filter_map(|id| by_id.get(id.as_str()).copied()) |
| 209 | .filter(|chat| placed.insert(chat.id.as_str())) |
| 210 | .map(|chat| chat_item(chat, input, Some(&folder.id))) |
| 211 | .collect(); |
| 212 | let (kind, name) = match folder.kind { |
| 213 | FolderKind::Favorites => ( |
| 214 | SectionKind::Favorites, |
| 215 | if folder.name.trim().is_empty() { |
| 216 | FAVORITES_FALLBACK_NAME.to_owned() |
| 217 | } else { |
| 218 | folder.name.clone() |
| 219 | }, |
| 220 | ), |
| 221 | FolderKind::UserCreated => (SectionKind::Folder, folder.name.clone()), |
| 222 | }; |
| 223 | sections.push(section(folder.id.clone(), name, kind, items, input)); |
| 224 | } |
| 225 | let rest: Vec<ChatItem> = input |
| 226 | .chats |
| 227 | .iter() |
| 228 | .filter(|chat| !placed.contains(chat.id.as_str())) |
| 229 | .map(|chat| chat_item(chat, input, None)) |
| 230 | .collect(); |
| 231 | sections.push(section( |
| 232 | OTHERS_ID.to_owned(), |
| 233 | OTHERS_NAME.to_owned(), |
| 234 | SectionKind::Others, |
| 235 | rest, |
| 236 | input, |
| 237 | )); |
| 238 | sections |
| 239 | } |
| 240 | |
| 241 | fn section( |
| 242 | id: String, |
| 243 | name: String, |
| 244 | kind: SectionKind, |
| 245 | items: Vec<ChatItem>, |
| 246 | input: &SectionInput<'_>, |
| 247 | ) -> Section { |
| 248 | let collapsed = input.collapsed.contains(&id); |
| 249 | let unread_chats = items.iter().filter(|item| item.unread.is_unread()).count() as u32; |
| 250 | Section { |
| 251 | collapsed, |
| 252 | count: items.len(), |
| 253 | unread_chats, |
| 254 | show_empty_hint: items.is_empty() && !collapsed && kind != SectionKind::Others, |
| 255 | id, |
| 256 | name, |
| 257 | kind, |
| 258 | items, |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | #[cfg(test)] |
| 263 | mod tests { |
| 264 | use chrono::TimeZone; |
| 265 | use store::MemberRecord; |
| 266 | |
| 267 | use super::*; |
| 268 | use crate::data::FolderInfo; |
| 269 | |
| 270 | fn me() -> Person { |
| 271 | Person { |
| 272 | user_id: "me".into(), |
| 273 | display_name: "Me Myself".into(), |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | fn chat(id: &str, kind: &str, unread: bool) -> ChatRecord { |
| 278 | ChatRecord { |
| 279 | id: id.into(), |
| 280 | kind: kind.into(), |
| 281 | title: id.to_uppercase(), |
| 282 | unread, |
| 283 | last_message_at: Some(Utc.with_ymd_and_hms(2026, 10, 7, 9, 30, 0).unwrap()), |
| 284 | members: vec![ |
| 285 | MemberRecord { |
| 286 | user_id: Some("me".into()), |
| 287 | display_name: "Me Myself".into(), |
| 288 | }, |
| 289 | MemberRecord { |
| 290 | user_id: Some("ada".into()), |
| 291 | display_name: "Ada Example".into(), |
| 292 | }, |
| 293 | MemberRecord { |
| 294 | user_id: Some("bob".into()), |
| 295 | display_name: "Bob Sample".into(), |
| 296 | }, |
| 297 | ], |
| 298 | ..Default::default() |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | fn folder(id: &str, kind: FolderKind, ids: &[&str]) -> FolderInfo { |
| 303 | FolderInfo { |
| 304 | id: id.into(), |
| 305 | name: format!("name-{id}"), |
| 306 | kind, |
| 307 | conversation_ids: ids.iter().map(|id| (*id).to_owned()).collect(), |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | fn input<'a>( |
| 312 | chats: &'a [ChatRecord], |
| 313 | directory: &'a Directory, |
| 314 | collapsed: &'a HashSet<String>, |
| 315 | ) -> SectionInput<'a> { |
| 316 | SectionInput { |
| 317 | chats, |
| 318 | directory, |
| 319 | collapsed, |
| 320 | now: Utc.with_ymd_and_hms(2026, 10, 7, 12, 0, 0).unwrap(), |
| 321 | offset: FixedOffset::east_opt(0).unwrap(), |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | #[test] |
| 326 | fn preview_prefix_is_du_for_own_and_first_name_for_groups() { |
| 327 | let mut group = chat("g", "group", false); |
| 328 | group.last_message_preview = Some("Hallo".into()); |
| 329 | group.last_message_sender_id = Some("ada".into()); |
| 330 | group.last_message_sender_name = Some("Ada Example".into()); |
| 331 | assert_eq!( |
| 332 | preview_for(&group, Some(&me())), |
| 333 | Preview::Text { |
| 334 | prefix: Some("Ada".into()), |
| 335 | text: "Hallo".into() |
| 336 | } |
| 337 | ); |
| 338 | group.last_message_sender_id = Some("me".into()); |
| 339 | assert_eq!( |
| 340 | preview_for(&group, Some(&me())), |
| 341 | Preview::Text { |
| 342 | prefix: Some("Du".into()), |
| 343 | text: "Hallo".into() |
| 344 | } |
| 345 | ); |
| 346 | let mut direct = chat("d", "oneOnOne", false); |
| 347 | direct.last_message_preview = Some("Hi".into()); |
| 348 | direct.last_message_sender_id = Some("ada".into()); |
| 349 | direct.last_message_sender_name = Some("Ada Example".into()); |
| 350 | assert_eq!( |
| 351 | preview_for(&direct, Some(&me())), |
| 352 | Preview::Text { |
| 353 | prefix: None, |
| 354 | text: "Hi".into() |
| 355 | } |
| 356 | ); |
| 357 | } |
| 358 | |
| 359 | #[test] |
| 360 | fn deleted_and_blank_previews() { |
| 361 | let mut deleted = chat("d", "group", false); |
| 362 | deleted.last_message_deleted = true; |
| 363 | deleted.last_message_preview = Some("secret".into()); |
| 364 | assert_eq!(preview_for(&deleted, None), Preview::Deleted); |
| 365 | assert_eq!( |
| 366 | preview_for(&chat("e", "group", false), None), |
| 367 | Preview::Empty |
| 368 | ); |
| 369 | } |
| 370 | |
| 371 | #[test] |
| 372 | fn sections_follow_folder_order_and_leave_the_rest_for_others() { |
| 373 | let chats = vec![ |
| 374 | chat("a", "group", false), |
| 375 | chat("b", "oneOnOne", true), |
| 376 | chat("c", "group", false), |
| 377 | chat("d", "group", false), |
| 378 | ]; |
| 379 | let directory = Directory { |
| 380 | me: Some(me()), |
| 381 | folders: vec![ |
| 382 | folder("fav", FolderKind::Favorites, &["c"]), |
| 383 | folder("work", FolderKind::UserCreated, &["b", "gone", "a"]), |
| 384 | folder("todo", FolderKind::UserCreated, &[]), |
| 385 | ], |
| 386 | ..Default::default() |
| 387 | }; |
| 388 | let collapsed = HashSet::new(); |
| 389 | let sections = build_sections(&input(&chats, &directory, &collapsed)); |
| 390 | let ids: Vec<&str> = sections.iter().map(|section| section.id.as_str()).collect(); |
| 391 | assert_eq!(ids, vec!["fav", "work", "todo", OTHERS_ID]); |
| 392 | let work: Vec<&str> = sections[1] |
| 393 | .items |
| 394 | .iter() |
| 395 | .map(|item| item.id.as_str()) |
| 396 | .collect(); |
| 397 | assert_eq!(work, vec!["b", "a"]); |
| 398 | assert_eq!(sections[1].count, 2); |
| 399 | assert_eq!(sections[2].count, 0); |
| 400 | assert!(sections[2].show_empty_hint); |
| 401 | let others: Vec<&str> = sections[3] |
| 402 | .items |
| 403 | .iter() |
| 404 | .map(|item| item.id.as_str()) |
| 405 | .collect(); |
| 406 | assert_eq!(others, vec!["d"]); |
| 407 | assert!(!sections[3].show_empty_hint); |
| 408 | } |
| 409 | |
| 410 | #[test] |
| 411 | fn collapsed_sections_keep_the_aggregated_unread() { |
| 412 | let chats = vec![ |
| 413 | chat("a", "group", true), |
| 414 | chat("b", "group", true), |
| 415 | chat("c", "group", false), |
| 416 | ]; |
| 417 | let directory = Directory { |
| 418 | folders: vec![folder("work", FolderKind::UserCreated, &["a", "b", "c"])], |
| 419 | ..Default::default() |
| 420 | }; |
| 421 | let collapsed: HashSet<String> = ["work".to_owned()].into(); |
| 422 | let sections = build_sections(&input(&chats, &directory, &collapsed)); |
| 423 | assert!(sections[0].collapsed); |
| 424 | assert_eq!(sections[0].unread_chats, 2); |
| 425 | assert_eq!(sections[0].count, 3); |
| 426 | assert!(!sections[0].show_empty_hint); |
| 427 | } |
| 428 | |
| 429 | #[test] |
| 430 | fn collapsed_empty_folder_hides_the_hint() { |
| 431 | let directory = Directory { |
| 432 | folders: vec![folder("todo", FolderKind::UserCreated, &[])], |
| 433 | ..Default::default() |
| 434 | }; |
| 435 | let collapsed: HashSet<String> = ["todo".to_owned()].into(); |
| 436 | let sections = build_sections(&input(&[], &directory, &collapsed)); |
| 437 | assert!(!sections[0].show_empty_hint); |
| 438 | } |
| 439 | |
| 440 | #[test] |
| 441 | fn favorites_without_a_server_name_say_angeheftet() { |
| 442 | let mut favorites = folder("fav", FolderKind::Favorites, &[]); |
| 443 | favorites.name = String::new(); |
| 444 | let directory = Directory { |
| 445 | folders: vec![favorites], |
| 446 | ..Default::default() |
| 447 | }; |
| 448 | let sections = build_sections(&input(&[], &directory, &HashSet::new())); |
| 449 | assert_eq!(sections[0].name, FAVORITES_FALLBACK_NAME); |
| 450 | } |
| 451 | |
| 452 | #[test] |
| 453 | fn unread_is_a_count_when_known_else_a_dot() { |
| 454 | let chats = vec![ |
| 455 | chat("a", "group", true), |
| 456 | chat("b", "group", true), |
| 457 | chat("c", "group", false), |
| 458 | ]; |
| 459 | let mut directory = Directory::default(); |
| 460 | directory.unread_counts.insert("a".into(), 5); |
| 461 | let collapsed = HashSet::new(); |
| 462 | let context = input(&chats, &directory, &collapsed); |
| 463 | assert_eq!( |
| 464 | chat_item(&chats[0], &context, None).unread, |
| 465 | Unread::Count(5) |
| 466 | ); |
| 467 | assert_eq!(chat_item(&chats[1], &context, None).unread, Unread::Dot); |
| 468 | assert_eq!(chat_item(&chats[2], &context, None).unread, Unread::None); |
| 469 | assert_eq!(unread_chat_count(&chats), 2); |
| 470 | } |
| 471 | |
| 472 | #[test] |
| 473 | fn one_on_one_has_presence_and_a_single_face_groups_have_a_pair() { |
| 474 | let directory = Directory { |
| 475 | me: Some(me()), |
| 476 | ..Default::default() |
| 477 | }; |
| 478 | let collapsed = HashSet::new(); |
| 479 | let chats = vec![chat("d", "oneOnOne", false), chat("g", "group", false)]; |
| 480 | let context = input(&chats, &directory, &collapsed); |
| 481 | let direct = chat_item(&chats[0], &context, None); |
| 482 | assert_eq!(direct.presence_user.as_deref(), Some("ada")); |
| 483 | assert!( |
| 484 | matches!(direct.avatar, AvatarSpec::Single(ref face) if face.name == "Ada Example") |
| 485 | ); |
| 486 | let group = chat_item(&chats[1], &context, None); |
| 487 | assert_eq!(group.presence_user, None); |
| 488 | assert!(matches!(group.avatar, AvatarSpec::Pair(..))); |
| 489 | } |
| 490 | |
| 491 | #[test] |
| 492 | fn time_label_uses_the_list_rules() { |
| 493 | let chats = vec![chat("a", "group", false)]; |
| 494 | let directory = Directory::default(); |
| 495 | let collapsed = HashSet::new(); |
| 496 | let item = chat_item(&chats[0], &input(&chats, &directory, &collapsed), None); |
| 497 | assert_eq!(item.time_label, "09:30"); |
| 498 | } |
| 499 | } |