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 hidden_at_ms: None,
68 }
69 }
70
71 fn activity(id: &str, thread: &str, kind: MentionKind, timestamp_ms: i64) -> MentionActivity {
72 MentionActivity {
73 activity_id: id.to_owned(),
74 kind,
75 source_thread_id: thread.to_owned(),
76 source_message_id: None,
77 timestamp_ms,
78 is_read: false,
79 }
80 }
81
82 fn counts(personal: u32, channel: u32, team: u32) -> ChannelMentions {
83 ChannelMentions {
84 personal,
85 channel,
86 team,
87 }
88 }
89
90 #[test]
91 fn counts_unread_mentions_after_the_horizon_per_kind() {
92 let states = [state("19:a", 100, 500), state("19:b", 100, 500)];
93 let activities = [
94 activity("1", "19:a", MentionKind::Personal, 200),
95 activity("2", "19:a", MentionKind::Personal, 300),
96 activity("2", "19:a", MentionKind::Personal, 300),
97 activity("3", "19:a", MentionKind::Channel, 300),
98 activity("4", "19:b", MentionKind::Team, 400),
99 activity("5", "19:a", MentionKind::Personal, 50),
100 MentionActivity {
101 is_read: true,
102 ..activity("6", "19:b", MentionKind::Personal, 400)
103 },
104 activity("7", "19:unknown", MentionKind::Personal, 400),
105 ];
106 assert_eq!(
107 channel_mentions(&activities, &states, ME, 0),
108 [
109 ("19:a".to_owned(), counts(2, 1, 0)),
110 ("19:b".to_owned(), counts(0, 0, 1)),
111 ]
112 );
113 }
114
115 #[test]
116 fn a_read_channel_has_no_mentions() {
117 let states = [state("19:a", 600, 500)];
118 let activities = [activity("1", "19:a", MentionKind::Personal, 550)];
119 assert!(channel_mentions(&activities, &states, ME, 0).is_empty());
120 }
121
122 #[test]
123 fn mark_all_as_read_hides_older_mentions() {
124 let states = [state("19:a", 100, 900)];
125 let activities = [
126 activity("1", "19:a", MentionKind::Personal, 300),
127 activity("2", "19:a", MentionKind::Personal, 800),
128 ];
129 assert_eq!(
130 channel_mentions(&activities, &states, ME, 500),
131 [("19:a".to_owned(), counts(1, 0, 0))]
132 );
133 assert!(channel_mentions(&activities, &states, ME, 950).is_empty());
134 }
135}