BlueGit

ict rusty-teams

Files

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

channel_mentions.rs

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