BlueGit

ict rusty-teams

Files

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

taskbar.rs

0a54bb25 History
taskbar.rs 4.1 KiB Download Raw
1use std::collections::HashSet;
2
3use store::Sidebar;
4
5use crate::activity::Kind;
6use crate::activity::feed::Feed;
7use crate::sidebar_model::unread_chat_count;
8
9pub 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)]
34mod 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 }],
72 }
73 }
74
75 fn feed(entries: &[(&str, Kind, bool)]) -> Feed {
76 let mut feed = Feed::load(Vec::new());
77 for (conversation_id, kind, read) in entries {
78 feed.add(Entry {
79 id: 0,
80 conversation_id: (*conversation_id).into(),
81 kind: *kind,
82 message_id: String::new(),
83 actors: Vec::new(),
84 preview: String::new(),
85 glyphs: Vec::new(),
86 count: 1,
87 updated_at: Utc::now(),
88 read: *read,
89 });
90 }
91 feed
92 }
93
94 #[test]
95 fn unread_channel_posts_without_activity_do_not_count() {
96 assert_eq!(taskbar_badge_count(&sidebar(), &feed(&[]), true), 1);
97 }
98
99 #[test]
100 fn muted_chats_never_count_as_chats() {
101 let mentioned = feed(&[("chat-muted", Kind::Mention, false)]);
102 assert_eq!(taskbar_badge_count(&sidebar(), &mentioned, false), 2);
103 }
104
105 #[test]
106 fn chat_message_rows_are_counted_once_by_the_chat() {
107 let rows = feed(&[
108 ("chat-unread", Kind::Messages, false),
109 ("chat-unread", Kind::Reaction, false),
110 ("channel-a", Kind::Messages, false),
111 ("channel-a", Kind::Reaction, true),
112 ]);
113 assert_eq!(taskbar_badge_count(&sidebar(), &rows, false), 3);
114 }
115
116 #[test]
117 fn the_adaptive_rail_adds_one_per_channel_with_an_unread_mention() {
118 let mentions = feed(&[
119 ("channel-a", Kind::Mention, false),
120 ("channel-a", Kind::Mention, false),
121 ("channel-b", Kind::Mention, false),
122 ("chat-unread", Kind::Mention, false),
123 ]);
124 assert_eq!(taskbar_badge_count(&sidebar(), &mentions, false), 5);
125 assert_eq!(taskbar_badge_count(&sidebar(), &mentions, true), 7);
126 }
127}