| 1 | use std::collections::{HashMap, HashSet}; |
| 2 | |
| 3 | use chrono::{DateTime, FixedOffset, Utc}; |
| 4 | use store::{ChannelRecord, ChatRecord, Sidebar, SidebarTeam}; |
| 5 | use teams_core::ChatSection; |
| 6 | use app_ui_kit::widgets::Unread; |
| 7 | |
| 8 | use crate::app_state::chat_title; |
| 9 | use crate::data::{ |
| 10 | Directory, FolderKind, Person, face_members, is_one_on_one, others, unread_count, |
| 11 | }; |
| 12 | use crate::format; |
| 13 | use crate::typing::{TypingState, preview_label}; |
| 14 | |
| 15 | pub const FAVORITES_FALLBACK_NAME: &str = "Favorites"; |
| 16 | pub const OTHERS_NAME: &str = "Other chats"; |
| 17 | pub const OTHERS_ID: &str = "others"; |
| 18 | pub const MUTED_NAME: &str = "Muted"; |
| 19 | pub const MUTED_ID: &str = "muted_chats"; |
| 20 | pub const SECTION_PAGE_SIZE: usize = 5; |
| 21 | pub const EMPTY_FOLDER_HINT: &str = "Empty. Drag chats here."; |
| 22 | pub const DELETED_PREVIEW: &str = "Message deleted"; |
| 23 | const OWN_PREFIX: &str = "You"; |
| 24 | const GROUP_KIND: &str = "group"; |
| 25 | |
| 26 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 27 | pub enum Preview { |
| 28 | Empty, |
| 29 | Deleted, |
| 30 | Typing(String), |
| 31 | Text { |
| 32 | prefix: Option<String>, |
| 33 | text: String, |
| 34 | }, |
| 35 | } |
| 36 | |
| 37 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 38 | pub struct Face { |
| 39 | pub user_id: Option<String>, |
| 40 | pub name: String, |
| 41 | } |
| 42 | |
| 43 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 44 | pub enum AvatarSpec { |
| 45 | Single(Face), |
| 46 | Pair(Face, Face), |
| 47 | Meeting, |
| 48 | Muted, |
| 49 | } |
| 50 | |
| 51 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 52 | pub struct ChatItem { |
| 53 | pub id: String, |
| 54 | pub title: String, |
| 55 | pub time_label: String, |
| 56 | pub preview: Preview, |
| 57 | pub unread: Unread, |
| 58 | pub muted: bool, |
| 59 | pub is_group: bool, |
| 60 | pub member_count: usize, |
| 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 | Muted, |
| 72 | } |
| 73 | |
| 74 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 75 | pub struct Section { |
| 76 | pub id: String, |
| 77 | pub name: String, |
| 78 | pub kind: SectionKind, |
| 79 | pub collapsed: bool, |
| 80 | pub count: usize, |
| 81 | pub unread_chats: u32, |
| 82 | pub items: Vec<ChatItem>, |
| 83 | pub hidden_count: usize, |
| 84 | pub list_expanded: bool, |
| 85 | pub show_empty_hint: bool, |
| 86 | } |
| 87 | |
| 88 | pub struct SectionInput<'a> { |
| 89 | pub chats: &'a [ChatRecord], |
| 90 | pub directory: &'a Directory, |
| 91 | pub collapsed: &'a HashSet<String>, |
| 92 | pub expanded_lists: &'a HashSet<String>, |
| 93 | pub typing: &'a TypingState, |
| 94 | pub now: DateTime<Utc>, |
| 95 | pub offset: FixedOffset, |
| 96 | } |
| 97 | |
| 98 | pub fn preview_for(chat: &ChatRecord, me: Option<&Person>) -> Preview { |
| 99 | if chat.last_message_deleted { |
| 100 | return Preview::Deleted; |
| 101 | } |
| 102 | let Some(text) = chat |
| 103 | .last_message_preview |
| 104 | .as_deref() |
| 105 | .map(str::trim) |
| 106 | .filter(|text| !text.is_empty()) |
| 107 | else { |
| 108 | return Preview::Empty; |
| 109 | }; |
| 110 | let from_me = match (chat.last_message_sender_id.as_deref(), me) { |
| 111 | (Some(sender), Some(me)) => sender == me.user_id, |
| 112 | _ => false, |
| 113 | }; |
| 114 | let prefix = if from_me { |
| 115 | Some(OWN_PREFIX.to_owned()) |
| 116 | } else if is_one_on_one(chat) { |
| 117 | None |
| 118 | } else { |
| 119 | chat.last_message_sender_name |
| 120 | .as_deref() |
| 121 | .map(|name| format::first_name(name).to_owned()) |
| 122 | .filter(|name| !name.is_empty()) |
| 123 | }; |
| 124 | Preview::Text { |
| 125 | prefix, |
| 126 | text: text.to_owned(), |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | pub fn avatar_for(chat: &ChatRecord, me: Option<&Person>) -> AvatarSpec { |
| 131 | if chat.kind.eq_ignore_ascii_case("meeting") { |
| 132 | return AvatarSpec::Meeting; |
| 133 | } |
| 134 | let faces = face_members(chat, me); |
| 135 | let face = |(user_id, name): (Option<String>, String)| Face { user_id, name }; |
| 136 | if is_one_on_one(chat) || faces.len() < 2 { |
| 137 | return AvatarSpec::Single(match faces.into_iter().next() { |
| 138 | Some(first) => face(first), |
| 139 | None => Face { |
| 140 | user_id: None, |
| 141 | name: chat_title(chat), |
| 142 | }, |
| 143 | }); |
| 144 | } |
| 145 | let mut iterator = faces.into_iter(); |
| 146 | let (first, second) = (iterator.next(), iterator.next()); |
| 147 | match (first, second) { |
| 148 | (Some(first), Some(second)) => AvatarSpec::Pair(face(first), face(second)), |
| 149 | _ => AvatarSpec::Single(Face { |
| 150 | user_id: None, |
| 151 | name: chat_title(chat), |
| 152 | }), |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | pub fn chat_item(chat: &ChatRecord, input: &SectionInput<'_>, folder_id: Option<&str>) -> ChatItem { |
| 157 | let me = input.directory.me.as_ref(); |
| 158 | let unread = if chat.unread { |
| 159 | unread_count(input.directory, chat) |
| 160 | .filter(|count| *count > 0) |
| 161 | .map_or(Unread::Dot, Unread::Count) |
| 162 | } else { |
| 163 | Unread::None |
| 164 | }; |
| 165 | let presence_user = (is_one_on_one(chat) && !chat.muted) |
| 166 | .then(|| { |
| 167 | others(chat, me) |
| 168 | .into_iter() |
| 169 | .find_map(|(user_id, _)| user_id) |
| 170 | }) |
| 171 | .flatten(); |
| 172 | let typing_names = input.typing.names(&chat.id); |
| 173 | let preview = if typing_names.is_empty() { |
| 174 | preview_for(chat, me) |
| 175 | } else { |
| 176 | Preview::Typing(preview_label(&typing_names, is_one_on_one(chat))) |
| 177 | }; |
| 178 | ChatItem { |
| 179 | id: chat.id.clone(), |
| 180 | title: chat_title(chat), |
| 181 | time_label: chat |
| 182 | .last_message_at |
| 183 | .map(|time| { |
| 184 | format::list_time_label( |
| 185 | time, |
| 186 | input.now.with_timezone(&input.offset).date_naive(), |
| 187 | input.offset, |
| 188 | ) |
| 189 | }) |
| 190 | .unwrap_or_default(), |
| 191 | preview, |
| 192 | unread, |
| 193 | muted: chat.muted, |
| 194 | is_group: chat.kind == GROUP_KIND, |
| 195 | member_count: chat.members.len(), |
| 196 | avatar: if chat.muted { |
| 197 | AvatarSpec::Muted |
| 198 | } else { |
| 199 | avatar_for(chat, me) |
| 200 | }, |
| 201 | presence_user, |
| 202 | folder_id: folder_id.map(str::to_owned), |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | pub fn unread_chat_count(chats: &[ChatRecord]) -> u32 { |
| 207 | chats |
| 208 | .iter() |
| 209 | .filter(|chat| chat.unread && !chat.muted) |
| 210 | .count() as u32 |
| 211 | } |
| 212 | |
| 213 | pub fn any_unread_channel(sidebar: &Sidebar) -> bool { |
| 214 | sidebar |
| 215 | .teams |
| 216 | .iter() |
| 217 | .any(|team| team.channels.iter().any(|channel| channel.unread)) |
| 218 | } |
| 219 | |
| 220 | pub fn build_sections(input: &SectionInput<'_>) -> Vec<Section> { |
| 221 | let by_id: HashMap<&str, &ChatRecord> = input |
| 222 | .chats |
| 223 | .iter() |
| 224 | .map(|chat| (chat.id.as_str(), chat)) |
| 225 | .collect(); |
| 226 | let mut placed: HashSet<&str> = HashSet::new(); |
| 227 | let mut built: Vec<Option<Section>> = input |
| 228 | .directory |
| 229 | .folders |
| 230 | .iter() |
| 231 | .map(|folder| { |
| 232 | let (kind, name) = match folder.kind { |
| 233 | FolderKind::Favorites => ( |
| 234 | SectionKind::Favorites, |
| 235 | if folder.name.trim().is_empty() { |
| 236 | FAVORITES_FALLBACK_NAME.to_owned() |
| 237 | } else { |
| 238 | folder.name.clone() |
| 239 | }, |
| 240 | ), |
| 241 | FolderKind::UserCreated => (SectionKind::Folder, folder.name.clone()), |
| 242 | FolderKind::Recent | FolderKind::Meeting | FolderKind::Muted => return None, |
| 243 | }; |
| 244 | let items: Vec<ChatItem> = folder |
| 245 | .conversation_ids |
| 246 | .iter() |
| 247 | .filter_map(|id| by_id.get(id.as_str()).copied()) |
| 248 | .filter(|chat| placed.insert(chat.id.as_str())) |
| 249 | .map(|chat| chat_item(chat, input, Some(&folder.id))) |
| 250 | .collect(); |
| 251 | Some(section(folder.id.clone(), name, kind, items, input)) |
| 252 | }) |
| 253 | .collect(); |
| 254 | let settings = &input.directory.section_settings; |
| 255 | let mut muted = Vec::new(); |
| 256 | let mut rest = Vec::new(); |
| 257 | for chat in input |
| 258 | .chats |
| 259 | .iter() |
| 260 | .filter(|chat| !placed.contains(chat.id.as_str())) |
| 261 | { |
| 262 | let item = chat_item(chat, input, None); |
| 263 | if chat.muted && settings.enabled(ChatSection::Muted) { |
| 264 | muted.push(item); |
| 265 | } else { |
| 266 | rest.push(item); |
| 267 | } |
| 268 | } |
| 269 | let folder_id = |kind: FolderKind, fallback: &str| { |
| 270 | input |
| 271 | .directory |
| 272 | .folders |
| 273 | .iter() |
| 274 | .find(|folder| folder.kind == kind) |
| 275 | .map_or_else(|| fallback.to_owned(), |folder| folder.id.clone()) |
| 276 | }; |
| 277 | let mut others = Some(section( |
| 278 | folder_id(FolderKind::Recent, OTHERS_ID), |
| 279 | OTHERS_NAME.to_owned(), |
| 280 | SectionKind::Others, |
| 281 | rest, |
| 282 | input, |
| 283 | )); |
| 284 | let mut muted = (!muted.is_empty()).then(|| { |
| 285 | section( |
| 286 | folder_id(FolderKind::Muted, MUTED_ID), |
| 287 | MUTED_NAME.to_owned(), |
| 288 | SectionKind::Muted, |
| 289 | muted, |
| 290 | input, |
| 291 | ) |
| 292 | }); |
| 293 | let mut sections = Vec::new(); |
| 294 | for (folder, section) in input.directory.folders.iter().zip(&mut built) { |
| 295 | match folder.kind { |
| 296 | FolderKind::Favorites | FolderKind::UserCreated => sections.extend(section.take()), |
| 297 | FolderKind::Recent => sections.extend(others.take()), |
| 298 | FolderKind::Meeting => {} |
| 299 | FolderKind::Muted => sections.extend(muted.take()), |
| 300 | } |
| 301 | } |
| 302 | sections.extend(others); |
| 303 | sections.extend(muted); |
| 304 | sections |
| 305 | } |
| 306 | |
| 307 | pub fn wants_older_chats( |
| 308 | others_end: Option<f32>, |
| 309 | viewport_bottom: f32, |
| 310 | scroll_top: f32, |
| 311 | last_trigger_top: Option<f32>, |
| 312 | loading: bool, |
| 313 | ) -> bool { |
| 314 | !loading |
| 315 | && others_end.is_some_and(|end| end <= viewport_bottom) |
| 316 | && last_trigger_top != Some(scroll_top) |
| 317 | } |
| 318 | |
| 319 | pub fn older_chats_trigger_resets(previous: (bool, bool), current: (bool, bool)) -> bool { |
| 320 | let ((had_more, was_loading), (has_more, is_loading)) = (previous, current); |
| 321 | (was_loading && !is_loading) || (!had_more && has_more) |
| 322 | } |
| 323 | |
| 324 | pub fn visible_chat_ids(sections: &[Section]) -> Vec<&str> { |
| 325 | sections |
| 326 | .iter() |
| 327 | .filter(|section| !section.collapsed) |
| 328 | .flat_map(|section| section.items.iter().map(|item| item.id.as_str())) |
| 329 | .collect() |
| 330 | } |
| 331 | |
| 332 | pub fn next_chat_id(sections: &[Section], removed_id: &str) -> Option<String> { |
| 333 | let visible = visible_chat_ids(sections); |
| 334 | let position = visible.iter().position(|id| *id == removed_id)?; |
| 335 | visible |
| 336 | .get(position + 1) |
| 337 | .or_else(|| { |
| 338 | position |
| 339 | .checked_sub(1) |
| 340 | .and_then(|previous| visible.get(previous)) |
| 341 | }) |
| 342 | .map(|id| (*id).to_owned()) |
| 343 | } |
| 344 | |
| 345 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 346 | pub enum Step { |
| 347 | Previous, |
| 348 | Next, |
| 349 | } |
| 350 | |
| 351 | pub fn step_id(ids: &[&str], current: Option<&str>, step: Step) -> Option<String> { |
| 352 | let position = current.and_then(|current| ids.iter().position(|id| *id == current)); |
| 353 | let target = match (position, step) { |
| 354 | (Some(position), Step::Next) => position + 1, |
| 355 | (Some(position), Step::Previous) => position.checked_sub(1)?, |
| 356 | (None, Step::Next) => 0, |
| 357 | (None, Step::Previous) => ids.len().checked_sub(1)?, |
| 358 | }; |
| 359 | ids.get(target).map(|id| (*id).to_owned()) |
| 360 | } |
| 361 | |
| 362 | pub struct ChannelOrderInput<'a> { |
| 363 | pub teams: &'a [SidebarTeam], |
| 364 | pub pinned_ids: &'a [String], |
| 365 | pub collapsed_teams: &'a HashSet<String>, |
| 366 | pub revealed_channel_teams: &'a HashSet<String>, |
| 367 | pub hidden_teams_open: bool, |
| 368 | pub selected_channel: Option<&'a str>, |
| 369 | } |
| 370 | |
| 371 | pub fn visible_channel_ids<'a>(input: &ChannelOrderInput<'a>) -> Vec<&'a str> { |
| 372 | let mut ids: Vec<&str> = Vec::new(); |
| 373 | let mut push = |id: &'a str| { |
| 374 | if !ids.contains(&id) { |
| 375 | ids.push(id); |
| 376 | } |
| 377 | }; |
| 378 | for pinned_id in input.pinned_ids { |
| 379 | let known = input |
| 380 | .teams |
| 381 | .iter() |
| 382 | .flat_map(|entry| entry.channels.iter()) |
| 383 | .find(|channel| &channel.id == pinned_id); |
| 384 | if let Some(channel) = known { |
| 385 | push(&channel.id); |
| 386 | } |
| 387 | } |
| 388 | let holds_selection = |entry: &SidebarTeam| { |
| 389 | entry |
| 390 | .channels |
| 391 | .iter() |
| 392 | .any(|channel| Some(channel.id.as_str()) == input.selected_channel) |
| 393 | }; |
| 394 | let hidden_teams_shown = input.hidden_teams_open |
| 395 | || input |
| 396 | .teams |
| 397 | .iter() |
| 398 | .any(|entry| entry.hidden && holds_selection(entry)); |
| 399 | let teams = input.teams.iter().filter(|entry| !entry.hidden).chain( |
| 400 | input |
| 401 | .teams |
| 402 | .iter() |
| 403 | .filter(|entry| entry.hidden && hidden_teams_shown), |
| 404 | ); |
| 405 | for entry in teams { |
| 406 | if input.collapsed_teams.contains(&entry.team.id) { |
| 407 | continue; |
| 408 | } |
| 409 | let is_hidden = |channel: &ChannelRecord| entry.hidden_channel_ids.contains(&channel.id); |
| 410 | let is_selected = |
| 411 | |channel: &ChannelRecord| Some(channel.id.as_str()) == input.selected_channel; |
| 412 | let revealed = input.revealed_channel_teams.contains(&entry.team.id); |
| 413 | for channel in entry |
| 414 | .channels |
| 415 | .iter() |
| 416 | .filter(|channel| !is_hidden(channel) || is_selected(channel)) |
| 417 | { |
| 418 | push(&channel.id); |
| 419 | } |
| 420 | if revealed { |
| 421 | for channel in entry |
| 422 | .channels |
| 423 | .iter() |
| 424 | .filter(|channel| is_hidden(channel) && !is_selected(channel)) |
| 425 | { |
| 426 | push(&channel.id); |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | ids |
| 431 | } |
| 432 | |
| 433 | fn section( |
| 434 | id: String, |
| 435 | name: String, |
| 436 | kind: SectionKind, |
| 437 | mut items: Vec<ChatItem>, |
| 438 | input: &SectionInput<'_>, |
| 439 | ) -> Section { |
| 440 | let collapsed = input.collapsed.contains(&id); |
| 441 | let count = items.len(); |
| 442 | let unread_chats = items |
| 443 | .iter() |
| 444 | .filter(|item| item.unread.is_unread() && !item.muted) |
| 445 | .count() as u32; |
| 446 | let paged = kind == SectionKind::Muted; |
| 447 | let list_expanded = paged && count > SECTION_PAGE_SIZE && input.expanded_lists.contains(&id); |
| 448 | let hidden_count = if paged && !list_expanded { |
| 449 | count.saturating_sub(SECTION_PAGE_SIZE) |
| 450 | } else { |
| 451 | 0 |
| 452 | }; |
| 453 | items.truncate(count - hidden_count); |
| 454 | for item in items.iter_mut().filter(|item| item.muted) { |
| 455 | if item.unread.is_unread() { |
| 456 | item.unread = Unread::Silent; |
| 457 | } |
| 458 | } |
| 459 | Section { |
| 460 | collapsed, |
| 461 | count, |
| 462 | unread_chats, |
| 463 | hidden_count, |
| 464 | list_expanded, |
| 465 | show_empty_hint: items.is_empty() |
| 466 | && !collapsed |
| 467 | && matches!(kind, SectionKind::Favorites | SectionKind::Folder), |
| 468 | id, |
| 469 | name, |
| 470 | kind, |
| 471 | items, |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | #[cfg(test)] |
| 476 | mod tests { |
| 477 | use chrono::TimeZone; |
| 478 | use store::MemberRecord; |
| 479 | |
| 480 | use super::*; |
| 481 | use crate::data::FolderInfo; |
| 482 | |
| 483 | fn me() -> Person { |
| 484 | Person { |
| 485 | user_id: "me".into(), |
| 486 | display_name: "Me Myself".into(), |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | fn chat(id: &str, kind: &str, unread: bool) -> ChatRecord { |
| 491 | ChatRecord { |
| 492 | id: id.into(), |
| 493 | kind: kind.into(), |
| 494 | title: id.to_uppercase(), |
| 495 | unread, |
| 496 | last_message_at: Some(Utc.with_ymd_and_hms(2026, 10, 7, 9, 30, 0).unwrap()), |
| 497 | members: vec![ |
| 498 | MemberRecord { |
| 499 | user_id: Some("me".into()), |
| 500 | display_name: "Me Myself".into(), |
| 501 | }, |
| 502 | MemberRecord { |
| 503 | user_id: Some("ada".into()), |
| 504 | display_name: "Ada Example".into(), |
| 505 | }, |
| 506 | MemberRecord { |
| 507 | user_id: Some("bob".into()), |
| 508 | display_name: "Bob Sample".into(), |
| 509 | }, |
| 510 | ], |
| 511 | ..Default::default() |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | fn folder(id: &str, kind: FolderKind, ids: &[&str]) -> FolderInfo { |
| 516 | FolderInfo { |
| 517 | id: id.into(), |
| 518 | name: format!("name-{id}"), |
| 519 | kind, |
| 520 | expanded: None, |
| 521 | conversation_ids: ids.iter().map(|id| (*id).to_owned()).collect(), |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | fn muted_chat(id: &str) -> ChatRecord { |
| 526 | ChatRecord { |
| 527 | muted: true, |
| 528 | ..chat(id, "group", false) |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | fn meeting_chat(id: &str) -> ChatRecord { |
| 533 | chat(id, "meeting", false) |
| 534 | } |
| 535 | |
| 536 | fn sectioned_directory(muted: bool, folders: Vec<FolderInfo>) -> Directory { |
| 537 | let mut directory = Directory { |
| 538 | folders, |
| 539 | ..Default::default() |
| 540 | }; |
| 541 | directory.section_settings.set(ChatSection::Muted, muted); |
| 542 | directory |
| 543 | } |
| 544 | |
| 545 | fn section_ids(sections: &[Section]) -> Vec<&str> { |
| 546 | sections.iter().map(|section| section.id.as_str()).collect() |
| 547 | } |
| 548 | |
| 549 | fn item_ids(section: &Section) -> Vec<&str> { |
| 550 | section.items.iter().map(|item| item.id.as_str()).collect() |
| 551 | } |
| 552 | |
| 553 | fn input<'a>( |
| 554 | chats: &'a [ChatRecord], |
| 555 | directory: &'a Directory, |
| 556 | collapsed: &'a HashSet<String>, |
| 557 | ) -> SectionInput<'a> { |
| 558 | typing_input(chats, directory, collapsed, &NO_TYPING) |
| 559 | } |
| 560 | |
| 561 | static NO_EXPANDED: std::sync::LazyLock<HashSet<String>> = |
| 562 | std::sync::LazyLock::new(HashSet::new); |
| 563 | |
| 564 | static NO_TYPING: std::sync::LazyLock<TypingState> = |
| 565 | std::sync::LazyLock::new(TypingState::default); |
| 566 | |
| 567 | fn typing_input<'a>( |
| 568 | chats: &'a [ChatRecord], |
| 569 | directory: &'a Directory, |
| 570 | collapsed: &'a HashSet<String>, |
| 571 | typing: &'a TypingState, |
| 572 | ) -> SectionInput<'a> { |
| 573 | SectionInput { |
| 574 | chats, |
| 575 | directory, |
| 576 | collapsed, |
| 577 | expanded_lists: &NO_EXPANDED, |
| 578 | typing, |
| 579 | now: Utc.with_ymd_and_hms(2026, 10, 7, 12, 0, 0).unwrap(), |
| 580 | offset: FixedOffset::east_opt(0).unwrap(), |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | #[test] |
| 585 | fn next_chat_is_the_following_row_then_the_previous_one() { |
| 586 | let chats = [ |
| 587 | chat("a", "group", false), |
| 588 | chat("b", "group", false), |
| 589 | chat("c", "group", false), |
| 590 | ]; |
| 591 | let directory = Directory::default(); |
| 592 | let collapsed = HashSet::new(); |
| 593 | let sections = build_sections(&input(&chats, &directory, &collapsed)); |
| 594 | assert_eq!(next_chat_id(§ions, "a"), Some("b".to_owned())); |
| 595 | assert_eq!(next_chat_id(§ions, "c"), Some("b".to_owned())); |
| 596 | assert_eq!(next_chat_id(§ions, "zzz"), None); |
| 597 | let single = [chat("a", "group", false)]; |
| 598 | let sections = build_sections(&input(&single, &directory, &collapsed)); |
| 599 | assert_eq!(next_chat_id(§ions, "a"), None); |
| 600 | } |
| 601 | |
| 602 | #[test] |
| 603 | fn stepping_follows_the_visible_order_without_wrapping() { |
| 604 | let ids = ["a", "b", "c"]; |
| 605 | assert_eq!(step_id(&ids, Some("a"), Step::Next), Some("b".to_owned())); |
| 606 | assert_eq!( |
| 607 | step_id(&ids, Some("b"), Step::Previous), |
| 608 | Some("a".to_owned()) |
| 609 | ); |
| 610 | assert_eq!(step_id(&ids, Some("c"), Step::Next), None); |
| 611 | assert_eq!(step_id(&ids, Some("a"), Step::Previous), None); |
| 612 | assert_eq!(step_id(&ids, None, Step::Next), Some("a".to_owned())); |
| 613 | assert_eq!( |
| 614 | step_id(&ids, Some("gone"), Step::Previous), |
| 615 | Some("c".to_owned()) |
| 616 | ); |
| 617 | assert_eq!(step_id(&[], None, Step::Next), None); |
| 618 | } |
| 619 | |
| 620 | #[test] |
| 621 | fn visible_chats_skip_collapsed_sections_and_see_more_rows() { |
| 622 | let mut chats = vec![chat("fav", "group", false), chat("hid", "group", false)]; |
| 623 | chats.extend((0..7).map(|index| muted_chat(&format!("m{index}")))); |
| 624 | chats.push(chat("other", "group", false)); |
| 625 | let directory = sectioned_directory( |
| 626 | true, |
| 627 | vec![ |
| 628 | folder("fav-folder", FolderKind::Favorites, &["fav"]), |
| 629 | folder("work", FolderKind::UserCreated, &["hid"]), |
| 630 | folder("recent", FolderKind::Recent, &[]), |
| 631 | folder("muted-folder", FolderKind::Muted, &[]), |
| 632 | ], |
| 633 | ); |
| 634 | let mut collapsed = HashSet::new(); |
| 635 | let sections = build_sections(&input(&chats, &directory, &collapsed)); |
| 636 | assert_eq!( |
| 637 | visible_chat_ids(§ions), |
| 638 | vec!["fav", "hid", "other", "m0", "m1", "m2", "m3", "m4"] |
| 639 | ); |
| 640 | collapsed.insert("work".to_owned()); |
| 641 | collapsed.insert("muted-folder".to_owned()); |
| 642 | let sections = build_sections(&input(&chats, &directory, &collapsed)); |
| 643 | assert_eq!(visible_chat_ids(§ions), vec!["fav", "other"]); |
| 644 | } |
| 645 | |
| 646 | fn team( |
| 647 | id: &str, |
| 648 | hidden: bool, |
| 649 | channel_ids: &[&str], |
| 650 | hidden_channel_ids: &[&str], |
| 651 | ) -> SidebarTeam { |
| 652 | SidebarTeam { |
| 653 | team: store::TeamRecord { |
| 654 | id: id.into(), |
| 655 | name: id.into(), |
| 656 | }, |
| 657 | channels: channel_ids |
| 658 | .iter() |
| 659 | .map(|channel_id| ChannelRecord { |
| 660 | id: (*channel_id).into(), |
| 661 | team_id: id.into(), |
| 662 | name: (*channel_id).into(), |
| 663 | membership_type: None, |
| 664 | last_message_at: None, |
| 665 | unread: false, |
| 666 | }) |
| 667 | .collect(), |
| 668 | hidden, |
| 669 | hidden_channel_ids: hidden_channel_ids |
| 670 | .iter() |
| 671 | .map(|id| (*id).to_owned()) |
| 672 | .collect(), |
| 673 | notifications: HashMap::new(), |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | #[test] |
| 678 | fn visible_channels_list_pinned_first_then_open_teams() { |
| 679 | let teams = [ |
| 680 | team("t1", false, &["a", "b", "c"], &["b"]), |
| 681 | team("t2", false, &["d", "e"], &[]), |
| 682 | team("t3", true, &["f"], &[]), |
| 683 | ]; |
| 684 | let pinned = ["e".to_owned(), "missing".to_owned()]; |
| 685 | let order = |selected: Option<&str>, |
| 686 | hidden_teams_open: bool, |
| 687 | collapsed: &[&str], |
| 688 | revealed: &[&str]| { |
| 689 | let to_set = |ids: &[&str]| { |
| 690 | ids.iter() |
| 691 | .map(|id| (*id).to_owned()) |
| 692 | .collect::<HashSet<_>>() |
| 693 | }; |
| 694 | let (collapsed, revealed) = (to_set(collapsed), to_set(revealed)); |
| 695 | let input = ChannelOrderInput { |
| 696 | teams: &teams, |
| 697 | pinned_ids: &pinned, |
| 698 | collapsed_teams: &collapsed, |
| 699 | revealed_channel_teams: &revealed, |
| 700 | hidden_teams_open, |
| 701 | selected_channel: selected, |
| 702 | }; |
| 703 | visible_channel_ids(&input) |
| 704 | .into_iter() |
| 705 | .map(str::to_owned) |
| 706 | .collect::<Vec<_>>() |
| 707 | }; |
| 708 | assert_eq!(order(None, false, &[], &[]), vec!["e", "a", "c", "d"]); |
| 709 | assert_eq!( |
| 710 | order(Some("b"), false, &[], &[]), |
| 711 | vec!["e", "a", "b", "c", "d"] |
| 712 | ); |
| 713 | assert_eq!( |
| 714 | order(Some("f"), false, &[], &[]), |
| 715 | vec!["e", "a", "c", "d", "f"] |
| 716 | ); |
| 717 | assert_eq!(order(None, true, &[], &[]), vec!["e", "a", "c", "d", "f"]); |
| 718 | assert_eq!( |
| 719 | order(None, true, &["t2"], &["t1"]), |
| 720 | vec!["e", "a", "c", "b", "f"] |
| 721 | ); |
| 722 | } |
| 723 | |
| 724 | #[test] |
| 725 | fn muted_chats_stay_out_of_unread_counts() { |
| 726 | let mut quiet = chat("a", "group", true); |
| 727 | quiet.muted = true; |
| 728 | let chats = [quiet, chat("b", "group", true)]; |
| 729 | assert_eq!(unread_chat_count(&chats), 1); |
| 730 | let directory = sectioned_directory(true, Vec::new()); |
| 731 | let collapsed = HashSet::new(); |
| 732 | let sections = build_sections(&input(&chats, &directory, &collapsed)); |
| 733 | assert_eq!(sections[0].unread_chats, 1); |
| 734 | assert_eq!(sections[1].kind, SectionKind::Muted); |
| 735 | assert!(sections[1].items[0].muted); |
| 736 | assert!(sections[1].items[0].unread.is_unread()); |
| 737 | } |
| 738 | |
| 739 | #[test] |
| 740 | fn preview_prefix_is_you_for_own_and_first_name_for_groups() { |
| 741 | let mut group = chat("g", "group", false); |
| 742 | group.last_message_preview = Some("Hello".into()); |
| 743 | group.last_message_sender_id = Some("ada".into()); |
| 744 | group.last_message_sender_name = Some("Ada Example".into()); |
| 745 | assert_eq!( |
| 746 | preview_for(&group, Some(&me())), |
| 747 | Preview::Text { |
| 748 | prefix: Some("Ada".into()), |
| 749 | text: "Hello".into() |
| 750 | } |
| 751 | ); |
| 752 | group.last_message_sender_id = Some("me".into()); |
| 753 | assert_eq!( |
| 754 | preview_for(&group, Some(&me())), |
| 755 | Preview::Text { |
| 756 | prefix: Some("You".into()), |
| 757 | text: "Hello".into() |
| 758 | } |
| 759 | ); |
| 760 | let mut direct = chat("d", "oneOnOne", false); |
| 761 | direct.last_message_preview = Some("Hi".into()); |
| 762 | direct.last_message_sender_id = Some("ada".into()); |
| 763 | direct.last_message_sender_name = Some("Ada Example".into()); |
| 764 | assert_eq!( |
| 765 | preview_for(&direct, Some(&me())), |
| 766 | Preview::Text { |
| 767 | prefix: None, |
| 768 | text: "Hi".into() |
| 769 | } |
| 770 | ); |
| 771 | } |
| 772 | |
| 773 | #[test] |
| 774 | fn deleted_and_blank_previews() { |
| 775 | let mut deleted = chat("d", "group", false); |
| 776 | deleted.last_message_deleted = true; |
| 777 | deleted.last_message_preview = Some("secret".into()); |
| 778 | assert_eq!(preview_for(&deleted, None), Preview::Deleted); |
| 779 | assert_eq!( |
| 780 | preview_for(&chat("e", "group", false), None), |
| 781 | Preview::Empty |
| 782 | ); |
| 783 | } |
| 784 | |
| 785 | #[test] |
| 786 | fn sections_follow_folder_order_and_leave_the_rest_for_others() { |
| 787 | let chats = vec![ |
| 788 | chat("a", "group", false), |
| 789 | chat("b", "oneOnOne", true), |
| 790 | chat("c", "group", false), |
| 791 | chat("d", "group", false), |
| 792 | ]; |
| 793 | let directory = Directory { |
| 794 | me: Some(me()), |
| 795 | folders: vec![ |
| 796 | folder("fav", FolderKind::Favorites, &["c"]), |
| 797 | folder("work", FolderKind::UserCreated, &["b", "gone", "a"]), |
| 798 | folder("todo", FolderKind::UserCreated, &[]), |
| 799 | ], |
| 800 | ..Default::default() |
| 801 | }; |
| 802 | let collapsed = HashSet::new(); |
| 803 | let sections = build_sections(&input(&chats, &directory, &collapsed)); |
| 804 | let ids: Vec<&str> = sections.iter().map(|section| section.id.as_str()).collect(); |
| 805 | assert_eq!(ids, vec!["fav", "work", "todo", OTHERS_ID]); |
| 806 | let work: Vec<&str> = sections[1] |
| 807 | .items |
| 808 | .iter() |
| 809 | .map(|item| item.id.as_str()) |
| 810 | .collect(); |
| 811 | assert_eq!(work, vec!["b", "a"]); |
| 812 | assert_eq!(sections[1].count, 2); |
| 813 | assert_eq!(sections[2].count, 0); |
| 814 | assert!(sections[2].show_empty_hint); |
| 815 | let others: Vec<&str> = sections[3] |
| 816 | .items |
| 817 | .iter() |
| 818 | .map(|item| item.id.as_str()) |
| 819 | .collect(); |
| 820 | assert_eq!(others, vec!["d"]); |
| 821 | assert!(!sections[3].show_empty_hint); |
| 822 | } |
| 823 | |
| 824 | #[test] |
| 825 | fn collapsed_sections_keep_the_aggregated_unread() { |
| 826 | let chats = vec![ |
| 827 | chat("a", "group", true), |
| 828 | chat("b", "group", true), |
| 829 | chat("c", "group", false), |
| 830 | ]; |
| 831 | let directory = Directory { |
| 832 | folders: vec![folder("work", FolderKind::UserCreated, &["a", "b", "c"])], |
| 833 | ..Default::default() |
| 834 | }; |
| 835 | let collapsed: HashSet<String> = ["work".to_owned()].into(); |
| 836 | let sections = build_sections(&input(&chats, &directory, &collapsed)); |
| 837 | assert!(sections[0].collapsed); |
| 838 | assert_eq!(sections[0].unread_chats, 2); |
| 839 | assert_eq!(sections[0].count, 3); |
| 840 | assert!(!sections[0].show_empty_hint); |
| 841 | } |
| 842 | |
| 843 | #[test] |
| 844 | fn collapsed_empty_folder_hides_the_hint() { |
| 845 | let directory = Directory { |
| 846 | folders: vec![folder("todo", FolderKind::UserCreated, &[])], |
| 847 | ..Default::default() |
| 848 | }; |
| 849 | let collapsed: HashSet<String> = ["todo".to_owned()].into(); |
| 850 | let sections = build_sections(&input(&[], &directory, &collapsed)); |
| 851 | assert!(!sections[0].show_empty_hint); |
| 852 | } |
| 853 | |
| 854 | #[test] |
| 855 | fn favorites_without_a_server_name_say_angeheftet() { |
| 856 | let mut favorites = folder("fav", FolderKind::Favorites, &[]); |
| 857 | favorites.name = String::new(); |
| 858 | let directory = Directory { |
| 859 | folders: vec![favorites], |
| 860 | ..Default::default() |
| 861 | }; |
| 862 | let sections = build_sections(&input(&[], &directory, &HashSet::new())); |
| 863 | assert_eq!(sections[0].name, FAVORITES_FALLBACK_NAME); |
| 864 | } |
| 865 | |
| 866 | #[test] |
| 867 | fn unread_is_a_count_when_known_else_a_dot() { |
| 868 | let chats = vec![ |
| 869 | chat("a", "group", true), |
| 870 | chat("b", "group", true), |
| 871 | chat("c", "group", false), |
| 872 | ]; |
| 873 | let mut directory = Directory::default(); |
| 874 | directory.unread_counts.insert("a".into(), 5); |
| 875 | let collapsed = HashSet::new(); |
| 876 | let context = input(&chats, &directory, &collapsed); |
| 877 | assert_eq!( |
| 878 | chat_item(&chats[0], &context, None).unread, |
| 879 | Unread::Count(5) |
| 880 | ); |
| 881 | assert_eq!(chat_item(&chats[1], &context, None).unread, Unread::Dot); |
| 882 | assert_eq!(chat_item(&chats[2], &context, None).unread, Unread::None); |
| 883 | assert_eq!(unread_chat_count(&chats), 2); |
| 884 | } |
| 885 | |
| 886 | #[test] |
| 887 | fn one_on_one_has_presence_and_a_single_face_groups_have_a_pair() { |
| 888 | let directory = Directory { |
| 889 | me: Some(me()), |
| 890 | ..Default::default() |
| 891 | }; |
| 892 | let collapsed = HashSet::new(); |
| 893 | let chats = vec![chat("d", "oneOnOne", false), chat("g", "group", false)]; |
| 894 | let context = input(&chats, &directory, &collapsed); |
| 895 | let direct = chat_item(&chats[0], &context, None); |
| 896 | assert_eq!(direct.presence_user.as_deref(), Some("ada")); |
| 897 | assert!( |
| 898 | matches!(direct.avatar, AvatarSpec::Single(ref face) if face.name == "Ada Example") |
| 899 | ); |
| 900 | let group = chat_item(&chats[1], &context, None); |
| 901 | assert_eq!(group.presence_user, None); |
| 902 | assert!(matches!(group.avatar, AvatarSpec::Pair(..))); |
| 903 | assert_eq!(avatar_for(&meeting_chat("meeting-1"), None), AvatarSpec::Meeting); |
| 904 | } |
| 905 | |
| 906 | #[test] |
| 907 | fn typing_replaces_the_preview_even_in_muted_chats() { |
| 908 | let mut direct = chat("d", "oneOnOne", false); |
| 909 | direct.last_message_preview = Some("Earlier".into()); |
| 910 | direct.muted = true; |
| 911 | let group = chat("g", "group", false); |
| 912 | let chats = vec![direct, group]; |
| 913 | let directory = Directory::default(); |
| 914 | let collapsed = HashSet::new(); |
| 915 | let now = std::time::Instant::now(); |
| 916 | let mut typing = TypingState::default(); |
| 917 | typing.start("d", "ada", "Ada Example", now, Utc::now()); |
| 918 | typing.start("g", "ada", "Ada Example", now, Utc::now()); |
| 919 | let context = typing_input(&chats, &directory, &collapsed, &typing); |
| 920 | assert_eq!( |
| 921 | chat_item(&chats[0], &context, None).preview, |
| 922 | Preview::Typing("typing...".into()) |
| 923 | ); |
| 924 | assert_eq!( |
| 925 | chat_item(&chats[1], &context, None).preview, |
| 926 | Preview::Typing("Ada is typing...".into()) |
| 927 | ); |
| 928 | typing.clear("d", "ada"); |
| 929 | let context = typing_input(&chats, &directory, &collapsed, &typing); |
| 930 | assert_eq!( |
| 931 | chat_item(&chats[0], &context, None).preview, |
| 932 | Preview::Text { |
| 933 | prefix: None, |
| 934 | text: "Earlier".into() |
| 935 | } |
| 936 | ); |
| 937 | } |
| 938 | |
| 939 | #[test] |
| 940 | fn time_label_uses_the_list_rules() { |
| 941 | let chats = vec![chat("a", "group", false)]; |
| 942 | let directory = Directory::default(); |
| 943 | let collapsed = HashSet::new(); |
| 944 | let item = chat_item(&chats[0], &input(&chats, &directory, &collapsed), None); |
| 945 | assert_eq!(item.time_label, "09:30"); |
| 946 | } |
| 947 | |
| 948 | #[test] |
| 949 | fn muted_chats_leave_others_unless_pinned_or_in_a_custom_section_and_meeting_chats_stay() { |
| 950 | let chats = vec![ |
| 951 | chat("plain", "group", false), |
| 952 | muted_chat("quiet"), |
| 953 | muted_chat("pinned-quiet"), |
| 954 | meeting_chat("sync"), |
| 955 | meeting_chat("custom-sync"), |
| 956 | ChatRecord { |
| 957 | muted: true, |
| 958 | ..meeting_chat("quiet-sync") |
| 959 | }, |
| 960 | ]; |
| 961 | let directory = sectioned_directory( |
| 962 | true, |
| 963 | vec![ |
| 964 | folder("fav", FolderKind::Favorites, &["pinned-quiet"]), |
| 965 | folder("work", FolderKind::UserCreated, &["custom-sync"]), |
| 966 | ], |
| 967 | ); |
| 968 | let sections = build_sections(&input(&chats, &directory, &HashSet::new())); |
| 969 | assert_eq!( |
| 970 | section_ids(§ions), |
| 971 | vec!["fav", "work", OTHERS_ID, MUTED_ID] |
| 972 | ); |
| 973 | assert_eq!(item_ids(§ions[0]), vec!["pinned-quiet"]); |
| 974 | assert_eq!(item_ids(§ions[1]), vec!["custom-sync"]); |
| 975 | assert_eq!(item_ids(§ions[2]), vec!["plain", "sync"]); |
| 976 | assert_eq!(item_ids(§ions[3]), vec!["quiet", "quiet-sync"]); |
| 977 | assert_eq!(sections[3].kind, SectionKind::Muted); |
| 978 | assert_eq!(sections[3].count, 2); |
| 979 | } |
| 980 | |
| 981 | #[test] |
| 982 | fn turning_the_muted_section_off_puts_its_chats_back_in_others() { |
| 983 | let chats = vec![muted_chat("quiet"), meeting_chat("sync")]; |
| 984 | let directory = sectioned_directory(false, Vec::new()); |
| 985 | let sections = build_sections(&input(&chats, &directory, &HashSet::new())); |
| 986 | assert_eq!(section_ids(§ions), vec![OTHERS_ID]); |
| 987 | assert_eq!(item_ids(§ions[0]), vec!["quiet", "sync"]); |
| 988 | } |
| 989 | |
| 990 | #[test] |
| 991 | fn muted_chats_stay_in_others_until_the_user_turns_the_section_on() { |
| 992 | let chats = vec![muted_chat("quiet"), meeting_chat("sync")]; |
| 993 | let directory = Directory::default(); |
| 994 | let sections = build_sections(&input(&chats, &directory, &HashSet::new())); |
| 995 | assert_eq!(section_ids(§ions), vec![OTHERS_ID]); |
| 996 | assert_eq!(item_ids(§ions[0]), vec!["quiet", "sync"]); |
| 997 | assert!(sections[0].items[0].muted); |
| 998 | assert_eq!(sections[0].items[0].avatar, AvatarSpec::Muted); |
| 999 | assert_eq!(sections[0].items[1].avatar, AvatarSpec::Meeting); |
| 1000 | } |
| 1001 | |
| 1002 | #[test] |
| 1003 | fn a_muted_chat_in_others_stays_bold_without_a_count() { |
| 1004 | let mut quiet = muted_chat("quiet"); |
| 1005 | quiet.unread = true; |
| 1006 | let chats = vec![quiet]; |
| 1007 | let mut directory = Directory::default(); |
| 1008 | directory.unread_counts.insert("quiet".into(), 4); |
| 1009 | let sections = build_sections(&input(&chats, &directory, &HashSet::new())); |
| 1010 | assert_eq!(sections[0].kind, SectionKind::Others); |
| 1011 | assert_eq!(sections[0].items[0].unread, Unread::Silent); |
| 1012 | assert_eq!(sections[0].unread_chats, 0); |
| 1013 | } |
| 1014 | |
| 1015 | #[test] |
| 1016 | fn older_chats_load_only_when_the_end_of_other_chats_is_on_screen_once_per_scroll_position() { |
| 1017 | assert!(wants_older_chats(Some(900.), 1000., 0., None, false)); |
| 1018 | assert!(!wants_older_chats(Some(1500.), 1000., 0., None, false)); |
| 1019 | assert!(!wants_older_chats(None, 1000., 0., None, false)); |
| 1020 | assert!(!wants_older_chats(Some(900.), 1000., 0., Some(0.), false)); |
| 1021 | assert!(wants_older_chats(Some(900.), 1000., 120., Some(0.), false)); |
| 1022 | } |
| 1023 | |
| 1024 | #[test] |
| 1025 | fn a_running_load_never_consumes_the_trigger() { |
| 1026 | assert!(!wants_older_chats(Some(900.), 1000., 0., None, true)); |
| 1027 | } |
| 1028 | |
| 1029 | #[test] |
| 1030 | fn the_trigger_resets_when_a_load_ends_or_more_chats_appear_but_not_while_idle() { |
| 1031 | assert!(older_chats_trigger_resets((true, true), (true, false))); |
| 1032 | assert!(older_chats_trigger_resets((true, true), (false, false))); |
| 1033 | assert!(older_chats_trigger_resets((false, false), (true, false))); |
| 1034 | assert!(!older_chats_trigger_resets((true, false), (true, false))); |
| 1035 | assert!(!older_chats_trigger_resets((true, false), (true, true))); |
| 1036 | assert!(!older_chats_trigger_resets((false, false), (false, false))); |
| 1037 | } |
| 1038 | |
| 1039 | #[test] |
| 1040 | fn empty_system_sections_are_hidden() { |
| 1041 | let chats = vec![chat("a", "group", false)]; |
| 1042 | let directory = sectioned_directory(true, Vec::new()); |
| 1043 | let sections = build_sections(&input(&chats, &directory, &HashSet::new())); |
| 1044 | assert_eq!(section_ids(§ions), vec![OTHERS_ID]); |
| 1045 | } |
| 1046 | |
| 1047 | #[test] |
| 1048 | fn sections_follow_the_server_folder_order_with_system_folders() { |
| 1049 | let chats = vec![ |
| 1050 | chat("a", "group", false), |
| 1051 | muted_chat("quiet"), |
| 1052 | meeting_chat("sync"), |
| 1053 | ]; |
| 1054 | let directory = sectioned_directory( |
| 1055 | true, |
| 1056 | vec![ |
| 1057 | folder("muted-id", FolderKind::Muted, &[]), |
| 1058 | folder("fav", FolderKind::Favorites, &[]), |
| 1059 | folder("recent-id", FolderKind::Recent, &[]), |
| 1060 | folder("meeting-id", FolderKind::Meeting, &[]), |
| 1061 | ], |
| 1062 | ); |
| 1063 | let collapsed: HashSet<String> = ["muted-id".to_owned()].into(); |
| 1064 | let sections = build_sections(&input(&chats, &directory, &collapsed)); |
| 1065 | assert_eq!(section_ids(§ions), vec!["muted-id", "fav", "recent-id"]); |
| 1066 | assert!(sections[0].collapsed); |
| 1067 | assert_eq!(sections[0].name, MUTED_NAME); |
| 1068 | assert_eq!(item_ids(§ions[2]), vec!["a", "sync"]); |
| 1069 | } |
| 1070 | |
| 1071 | #[test] |
| 1072 | fn newest_five_show_then_see_more_expands_and_see_less_collapses() { |
| 1073 | let chats: Vec<ChatRecord> = (0..8) |
| 1074 | .map(|index| muted_chat(&format!("m{index}"))) |
| 1075 | .collect(); |
| 1076 | let directory = sectioned_directory(true, Vec::new()); |
| 1077 | let collapsed = HashSet::new(); |
| 1078 | let sections = build_sections(&input(&chats, &directory, &collapsed)); |
| 1079 | let muted = §ions[1]; |
| 1080 | assert_eq!(item_ids(muted), vec!["m0", "m1", "m2", "m3", "m4"]); |
| 1081 | assert_eq!(muted.count, 8); |
| 1082 | assert_eq!(muted.hidden_count, 3); |
| 1083 | assert!(!muted.list_expanded); |
| 1084 | |
| 1085 | let expanded: HashSet<String> = [MUTED_ID.to_owned()].into(); |
| 1086 | let mut context = input(&chats, &directory, &collapsed); |
| 1087 | context.expanded_lists = &expanded; |
| 1088 | let sections = build_sections(&context); |
| 1089 | let muted = §ions[1]; |
| 1090 | assert_eq!(muted.items.len(), 8); |
| 1091 | assert_eq!(muted.hidden_count, 0); |
| 1092 | assert!(muted.list_expanded); |
| 1093 | } |
| 1094 | |
| 1095 | #[test] |
| 1096 | fn five_chats_need_no_see_more_even_when_marked_expanded() { |
| 1097 | let chats: Vec<ChatRecord> = (0..5) |
| 1098 | .map(|index| muted_chat(&format!("m{index}"))) |
| 1099 | .collect(); |
| 1100 | let directory = sectioned_directory(true, Vec::new()); |
| 1101 | let collapsed = HashSet::new(); |
| 1102 | let expanded: HashSet<String> = [MUTED_ID.to_owned()].into(); |
| 1103 | let mut context = input(&chats, &directory, &collapsed); |
| 1104 | context.expanded_lists = &expanded; |
| 1105 | let muted = &build_sections(&context)[1]; |
| 1106 | assert_eq!(muted.hidden_count, 0); |
| 1107 | assert!(!muted.list_expanded); |
| 1108 | } |
| 1109 | |
| 1110 | #[test] |
| 1111 | fn muted_section_keeps_unread_bold_without_a_badge_or_a_count() { |
| 1112 | let mut quiet = muted_chat("quiet"); |
| 1113 | quiet.unread = true; |
| 1114 | let chats = vec![ |
| 1115 | quiet, |
| 1116 | ChatRecord { |
| 1117 | muted: true, |
| 1118 | ..meeting_chat("x") |
| 1119 | }, |
| 1120 | ]; |
| 1121 | let mut directory = sectioned_directory(true, Vec::new()); |
| 1122 | directory.unread_counts.insert("quiet".into(), 4); |
| 1123 | let sections = build_sections(&input(&chats, &directory, &HashSet::new())); |
| 1124 | let muted = §ions[1]; |
| 1125 | assert_eq!(muted.items[0].unread, Unread::Silent); |
| 1126 | assert!(muted.items[0].unread.is_unread()); |
| 1127 | assert_eq!(muted.unread_chats, 0); |
| 1128 | assert_eq!(unread_chat_count(&chats), 0); |
| 1129 | } |
| 1130 | |
| 1131 | #[test] |
| 1132 | fn meeting_chats_keep_their_unread_count_in_others() { |
| 1133 | let mut sync = meeting_chat("sync"); |
| 1134 | sync.unread = true; |
| 1135 | let chats = vec![sync]; |
| 1136 | let mut directory = sectioned_directory(true, Vec::new()); |
| 1137 | directory.unread_counts.insert("sync".into(), 3); |
| 1138 | let sections = build_sections(&input(&chats, &directory, &HashSet::new())); |
| 1139 | assert_eq!(sections[0].id, OTHERS_ID); |
| 1140 | assert_eq!(sections[0].items[0].unread, Unread::Count(3)); |
| 1141 | assert_eq!(sections[0].unread_chats, 1); |
| 1142 | } |
| 1143 | } |