BlueGit

ict rusty-teams

Files

rusty-teams / crates / core / src / channel_mentions.rs

channel_mentions.rs

channel_mentions.rs 4.5 KiB Download Raw
1use std::collections::{HashMap, HashSet};
2
3use chatsvc::{ConversationState, MentionActivity, MentionKind};
4use store::ChannelMentions;
5
6/// Teams counts a mention only while its channel is unread, it is newer than the read horizon and not read.
7pub fn channel_mentions(
8 activities: &[MentionActivity],
9 states: &[ConversationState],
10 my_user_id: &str,
11 app_horizon_ms: i64,
12) -> Vec<(String, ChannelMentions)> {
13 let states: HashMap<&str, &ConversationState> = states
14 .iter()
15 .map(|state| (state.id.as_str(), state))
16 .collect();
17 let mut counted_activity_ids = HashSet::new();
18 let mut mentions: HashMap<String, ChannelMentions> = HashMap::new();
19 for activity in activities {
20 if activity.is_read || !counted_activity_ids.insert(activity.activity_id.as_str()) {
21 continue;
22 }
23 let Some(state) = states.get(activity.source_thread_id.as_str()) else {
24 continue;
25 };
26 let horizon_ms = state.read.consumed_until_ms.max(app_horizon_ms);
27 if activity.timestamp_ms <= horizon_ms
28 || !state.read.is_unread_after(my_user_id, app_horizon_ms)
29 {
30 continue;
31 }
32 let counts = mentions
33 .entry(activity.source_thread_id.clone())
34 .or_default();
35 match activity.kind {
36 MentionKind::Personal => counts.personal += 1,
37 MentionKind::Channel => counts.channel += 1,
38 MentionKind::Team => counts.team += 1,
39 }
40 }
41 let mut mentions: Vec<(String, ChannelMentions)> = mentions.into_iter().collect();
42 mentions.sort_by(|left, right| left.0.cmp(&right.0));
43 mentions
44}
45
46#[cfg(test)]
47mod tests {
48 use chatsvc::ReadHorizon;
49
50 use super::*;
51
52 const ME: &str = "8:orgid:me";
53
54 fn state(id: &str, consumed_until_ms: i64, last_message_ms: i64) -> ConversationState {
55 ConversationState {
56 id: id.to_owned(),
57 muted: false,
58 hidden: None,
59 read: ReadHorizon {
60 consumed_until_ms,
61 last_message_ms,
62 last_message_from: Some("8:orgid:someone".to_owned()),
63 ..ReadHorizon::default()
64 },
65 notification_settings: None,
66 history_cleared_ms: None,
67 }
68 }
69
70 fn activity(id: &str, thread: &str, kind: MentionKind, timestamp_ms: i64) -> MentionActivity {
71 MentionActivity {
72 activity_id: id.to_owned(),
73 kind,
74 source_thread_id: thread.to_owned(),
75 source_message_id: None,
76 timestamp_ms,
77 is_read: false,
78 }
79 }
80
81 fn counts(personal: u32, channel: u32, team: u32) -> ChannelMentions {
82 ChannelMentions {
83 personal,
84 channel,
85 team,
86 }
87 }
88
89 #[test]
90 fn counts_unread_mentions_after_the_horizon_per_kind() {
91 let states = [state("19:a", 100, 500), state("19:b", 100, 500)];
92 let activities = [
93 activity("1", "19:a", MentionKind::Personal, 200),
94 activity("2", "19:a", MentionKind::Personal, 300),
95 activity("2", "19:a", MentionKind::Personal, 300),
96 activity("3", "19:a", MentionKind::Channel, 300),
97 activity("4", "19:b", MentionKind::Team, 400),
98 activity("5", "19:a", MentionKind::Personal, 50),
99 MentionActivity {
100 is_read: true,
101 ..activity("6", "19:b", MentionKind::Personal, 400)
102 },
103 activity("7", "19:unknown", MentionKind::Personal, 400),
104 ];
105 assert_eq!(
106 channel_mentions(&activities, &states, ME, 0),
107 [
108 ("19:a".to_owned(), counts(2, 1, 0)),
109 ("19:b".to_owned(), counts(0, 0, 1)),
110 ]
111 );
112 }
113
114 #[test]
115 fn a_read_channel_has_no_mentions() {
116 let states = [state("19:a", 600, 500)];
117 let activities = [activity("1", "19:a", MentionKind::Personal, 550)];
118 assert!(channel_mentions(&activities, &states, ME, 0).is_empty());
119 }
120
121 #[test]
122 fn mark_all_as_read_hides_older_mentions() {
123 let states = [state("19:a", 100, 900)];
124 let activities = [
125 activity("1", "19:a", MentionKind::Personal, 300),
126 activity("2", "19:a", MentionKind::Personal, 800),
127 ];
128 assert_eq!(
129 channel_mentions(&activities, &states, ME, 500),
130 [("19:a".to_owned(), counts(1, 0, 0))]
131 );
132 assert!(channel_mentions(&activities, &states, ME, 950).is_empty());
133 }
134}