| 1 | use std::collections::HashSet; |
| 2 | |
| 3 | use store::Sidebar; |
| 4 | |
| 5 | use crate::activity::feed::Feed; |
| 6 | use crate::sidebar_model::unread_chat_count; |
| 7 | |
| 8 | pub fn taskbar_badge_count(sidebar: &Sidebar, feed: &Feed, adaptive_rail: bool) -> usize { |
| 9 | let channel_ids: HashSet<&str> = sidebar |
| 10 | .teams |
| 11 | .iter() |
| 12 | .flat_map(|team| team.channels.iter()) |
| 13 | .map(|channel| channel.id.as_str()) |
| 14 | .collect(); |
| 15 | let channels_with_mentions = if adaptive_rail { |
| 16 | feed.unread_entries() |
| 17 | .filter(|entry| { |
| 18 | entry.is_personal_mention() && channel_ids.contains(entry.conversation_id.as_str()) |
| 19 | }) |
| 20 | .map(|entry| entry.conversation_id.as_str()) |
| 21 | .collect::<HashSet<_>>() |
| 22 | .len() |
| 23 | } else { |
| 24 | 0 |
| 25 | }; |
| 26 | feed.bell_count() + unread_chat_count(&sidebar.chats) as usize + channels_with_mentions |
| 27 | } |
| 28 | |
| 29 | #[cfg(test)] |
| 30 | mod tests { |
| 31 | use store::{ChannelRecord, ChatRecord, SidebarTeam, TeamRecord}; |
| 32 | |
| 33 | use super::*; |
| 34 | use crate::activity::{Entry, Kind}; |
| 35 | |
| 36 | fn sidebar() -> Sidebar { |
| 37 | let chat = |id: &str, unread: bool, muted: bool| ChatRecord { |
| 38 | id: id.into(), |
| 39 | unread, |
| 40 | muted, |
| 41 | ..Default::default() |
| 42 | }; |
| 43 | let channel = |id: &str| ChannelRecord { |
| 44 | id: id.into(), |
| 45 | team_id: "team".into(), |
| 46 | name: id.into(), |
| 47 | membership_type: None, |
| 48 | last_message_at: None, |
| 49 | unread: true, |
| 50 | }; |
| 51 | Sidebar { |
| 52 | chats: vec![ |
| 53 | chat("chat-unread", true, false), |
| 54 | chat("chat-muted", true, true), |
| 55 | chat("chat-read", false, false), |
| 56 | ], |
| 57 | teams: vec![SidebarTeam { |
| 58 | team: TeamRecord { |
| 59 | id: "team".into(), |
| 60 | name: "Team".into(), |
| 61 | }, |
| 62 | channels: vec![channel("channel-a"), channel("channel-b")], |
| 63 | hidden: false, |
| 64 | hidden_channel_ids: vec!["channel-b".into()], |
| 65 | notifications: Default::default(), |
| 66 | mentions: Default::default(), |
| 67 | }], |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | fn feed(entries: &[(&str, Kind, bool)]) -> Feed { |
| 72 | Feed::new( |
| 73 | entries |
| 74 | .iter() |
| 75 | .enumerate() |
| 76 | .map(|(index, (conversation_id, kind, read))| Entry { |
| 77 | id: index.to_string(), |
| 78 | conversation_id: (*conversation_id).into(), |
| 79 | kind: *kind, |
| 80 | subtype: "person".into(), |
| 81 | read: *read, |
| 82 | counts_for_bell: !*read, |
| 83 | ..Entry::default() |
| 84 | }) |
| 85 | .collect(), |
| 86 | ) |
| 87 | } |
| 88 | |
| 89 | #[test] |
| 90 | fn unread_channel_posts_without_activity_do_not_count() { |
| 91 | assert_eq!(taskbar_badge_count(&sidebar(), &feed(&[]), true), 1); |
| 92 | } |
| 93 | |
| 94 | #[test] |
| 95 | fn muted_chats_never_count_as_chats() { |
| 96 | let mentioned = feed(&[("chat-muted", Kind::Mention, false)]); |
| 97 | assert_eq!(taskbar_badge_count(&sidebar(), &mentioned, false), 2); |
| 98 | } |
| 99 | |
| 100 | #[test] |
| 101 | fn the_activity_term_is_the_bell() { |
| 102 | let mut rows = feed(&[ |
| 103 | ("chat-unread", Kind::Reply, false), |
| 104 | ("channel-a", Kind::Reaction, true), |
| 105 | ("channel-a", Kind::Posted, false), |
| 106 | ]); |
| 107 | assert_eq!(taskbar_badge_count(&sidebar(), &rows, false), 3); |
| 108 | rows = Feed::new( |
| 109 | rows.entries() |
| 110 | .iter() |
| 111 | .cloned() |
| 112 | .map(|entry| Entry { |
| 113 | counts_for_bell: false, |
| 114 | ..entry |
| 115 | }) |
| 116 | .collect(), |
| 117 | ); |
| 118 | assert_eq!(taskbar_badge_count(&sidebar(), &rows, false), 1); |
| 119 | } |
| 120 | |
| 121 | #[test] |
| 122 | fn the_adaptive_rail_adds_one_per_channel_with_an_unread_mention() { |
| 123 | let mentions = feed(&[ |
| 124 | ("channel-a", Kind::Mention, false), |
| 125 | ("channel-a", Kind::Mention, false), |
| 126 | ("channel-b", Kind::Mention, false), |
| 127 | ("chat-unread", Kind::Mention, false), |
| 128 | ]); |
| 129 | assert_eq!(taskbar_badge_count(&sidebar(), &mentions, false), 5); |
| 130 | assert_eq!(taskbar_badge_count(&sidebar(), &mentions, true), 7); |
| 131 | } |
| 132 | } |