BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / activity / reactions.rs

reactions.rs

reactions.rs 3.0 KiB Download Raw
1use store::MessageRecord;
2
3use super::feed::{ReactedMessage, Sighting};
4use super::preview_label;
5use crate::notify::preview_of;
6use crate::reaction_model::UNKNOWN_REACTOR;
7use crate::rows::reaction_glyph;
8
9pub fn reacted_messages(
10 records: &[MessageRecord],
11 my_user_id: &str,
12 name_of: impl Fn(&str) -> Option<String>,
13) -> Vec<ReactedMessage> {
14 records
15 .iter()
16 .filter(|record| !record.deleted && record.sender_id.as_deref() == Some(my_user_id))
17 .filter_map(|record| {
18 let sightings: Vec<Sighting> = teams_core::reactions(record)
19 .into_iter()
20 .map(|reaction| Sighting {
21 name: reaction
22 .user_name
23 .or_else(|| reaction.user_id.as_deref().and_then(&name_of))
24 .unwrap_or_else(|| UNKNOWN_REACTOR.to_owned()),
25 glyph: reaction_glyph(&reaction.reaction_type),
26 user_id: reaction.user_id,
27 created_at: reaction.created_at,
28 })
29 .collect();
30 (!sightings.is_empty()).then(|| ReactedMessage {
31 message_id: record.message_id.clone(),
32 preview: preview_label(&preview_of(record)),
33 sightings,
34 })
35 })
36 .collect()
37}
38
39#[cfg(test)]
40mod tests {
41 use chrono::{TimeZone, Utc};
42
43 use super::*;
44
45 fn record(message_id: &str, sender: &str, reactions_json: &str) -> MessageRecord {
46 MessageRecord {
47 conversation_id: "chat".into(),
48 message_id: message_id.into(),
49 sender_id: Some(sender.into()),
50 created_at: Utc.with_ymd_and_hms(2026, 10, 8, 9, 0, 0).unwrap(),
51 body_html: "<p>hello there</p>".into(),
52 attachments_json: "[]".into(),
53 reactions_json: reactions_json.into(),
54 mentions_json: "[]".into(),
55 ..Default::default()
56 }
57 }
58
59 #[test]
60 fn only_own_reacted_messages_are_returned() {
61 let reaction =
62 r#"[{"reaction_type":"like","user_id":"u1","user_name":null,"created_at":null}]"#;
63 let records = vec![
64 record("mine", "me", reaction),
65 record("theirs", "u2", reaction),
66 record("quiet", "me", "[]"),
67 ];
68 let found = reacted_messages(&records, "me", |user_id| {
69 (user_id == "u1").then(|| "Anna".to_owned())
70 });
71 assert_eq!(found.len(), 1);
72 assert_eq!(found[0].message_id, "mine");
73 assert_eq!(found[0].preview, "hello there");
74 assert_eq!(found[0].sightings[0].name, "Anna");
75 assert_eq!(found[0].sightings[0].glyph, "\u{1F44D}");
76 }
77
78 #[test]
79 fn unknown_reactor_gets_the_placeholder_name() {
80 let reaction = r#"[{"reaction_type":"heart","user_id":"u9"}]"#;
81 let found = reacted_messages(&[record("mine", "me", reaction)], "me", |_| None);
82 assert_eq!(found[0].sightings[0].name, UNKNOWN_REACTOR);
83 }
84}