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