BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-state / src / notify / taskbar.rs

taskbar.rs

fc95684c History
taskbar.rs 4.2 KiB Download Raw
1use std::collections::HashSet;
2
3use store::Sidebar;
4
5use crate::activity::feed::Feed;
6use crate::sidebar_model::unread_chat_count;
7
8pub 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)]
30mod 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 rights: Default::default(),
59 team: TeamRecord {
60 id: "team".into(),
61 name: "Team".into(),
62 },
63 channels: vec![channel("channel-a"), channel("channel-b")],
64 hidden: false,
65 hidden_channel_ids: vec!["channel-b".into()],
66 notifications: Default::default(),
67 mentions: Default::default(),
68 }],
69 }
70 }
71
72 fn feed(entries: &[(&str, Kind, bool)]) -> Feed {
73 Feed::new(
74 entries
75 .iter()
76 .enumerate()
77 .map(|(index, (conversation_id, kind, read))| Entry {
78 id: index.to_string(),
79 conversation_id: (*conversation_id).into(),
80 kind: *kind,
81 subtype: "person".into(),
82 read: *read,
83 counts_for_bell: !*read,
84 ..Entry::default()
85 })
86 .collect(),
87 )
88 }
89
90 #[test]
91 fn unread_channel_posts_without_activity_do_not_count() {
92 assert_eq!(taskbar_badge_count(&sidebar(), &feed(&[]), true), 1);
93 }
94
95 #[test]
96 fn muted_chats_never_count_as_chats() {
97 let mentioned = feed(&[("chat-muted", Kind::Mention, false)]);
98 assert_eq!(taskbar_badge_count(&sidebar(), &mentioned, false), 2);
99 }
100
101 #[test]
102 fn the_activity_term_is_the_bell() {
103 let mut rows = feed(&[
104 ("chat-unread", Kind::Reply, false),
105 ("channel-a", Kind::Reaction, true),
106 ("channel-a", Kind::Posted, false),
107 ]);
108 assert_eq!(taskbar_badge_count(&sidebar(), &rows, false), 3);
109 rows = Feed::new(
110 rows.entries()
111 .iter()
112 .cloned()
113 .map(|entry| Entry {
114 counts_for_bell: false,
115 ..entry
116 })
117 .collect(),
118 );
119 assert_eq!(taskbar_badge_count(&sidebar(), &rows, false), 1);
120 }
121
122 #[test]
123 fn the_adaptive_rail_adds_one_per_channel_with_an_unread_mention() {
124 let mentions = feed(&[
125 ("channel-a", Kind::Mention, false),
126 ("channel-a", Kind::Mention, false),
127 ("channel-b", Kind::Mention, false),
128 ("chat-unread", Kind::Mention, false),
129 ]);
130 assert_eq!(taskbar_badge_count(&sidebar(), &mentions, false), 5);
131 assert_eq!(taskbar_badge_count(&sidebar(), &mentions, true), 7);
132 }
133}