Skip to content

Commit

Permalink
fix: only display a single reaction per user/key pair
Browse files Browse the repository at this point in the history
  • Loading branch information
Kladki committed Apr 2, 2024
1 parent 82645c8 commit 2506da4
Showing 1 changed file with 79 additions and 4 deletions.
83 changes: 79 additions & 4 deletions src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! The types defined here get used throughout iamb.
use std::borrow::Cow;
use std::collections::hash_map::IntoIter;
use std::collections::{HashMap, HashSet};
use std::collections::{BTreeSet, HashMap, HashSet};
use std::convert::TryFrom;
use std::fmt::{self, Display};
use std::hash::Hash;
Expand Down Expand Up @@ -835,9 +835,14 @@ impl RoomInfo {
if let Some(reacts) = self.reactions.get(event_id) {
let mut counts = HashMap::new();

for (key, _) in reacts.values() {
let count = counts.entry(key.as_str()).or_default();
*count += 1;
let mut seen_user_reactions = BTreeSet::new();

for (key, user) in reacts.values() {
if !seen_user_reactions.contains(&(key, user)) {
seen_user_reactions.insert((key, user));
let count = counts.entry(key.as_str()).or_default();
*count += 1;
}
}

let mut reactions = counts.into_iter().collect::<Vec<_>>();
Expand Down Expand Up @@ -1836,8 +1841,78 @@ pub mod tests {
use super::*;
use crate::config::user_style_from_color;
use crate::tests::*;
use matrix_sdk::ruma::{
events::{reaction::ReactionEventContent, relation::Annotation, MessageLikeUnsigned},
owned_event_id,
owned_room_id,
owned_user_id,
MilliSecondsSinceUnixEpoch,
};
use pretty_assertions::assert_eq;
use ratatui::style::Color;

#[test]
fn multiple_identical_reactions() {
let mut info = RoomInfo::default();

let content = ReactionEventContent::new(Annotation::new(
owned_event_id!("$my_reaction"),
"🏠".to_owned(),
));

for i in 0..3 {
let event_id = format!("$house_{}", i);
info.insert_reaction(MessageLikeEvent::Original(
matrix_sdk::ruma::events::OriginalMessageLikeEvent {
content: content.clone(),
event_id: OwnedEventId::from_str(&event_id).unwrap(),
sender: owned_user_id!("@foo:example.org"),
origin_server_ts: MilliSecondsSinceUnixEpoch::now(),
room_id: owned_room_id!("!foo:example.org"),
unsigned: MessageLikeUnsigned::new(),
},
));
}

let content = ReactionEventContent::new(Annotation::new(
owned_event_id!("$my_reaction"),
"🙂".to_owned(),
));

for i in 0..2 {
let event_id = format!("$smile_{}", i);
info.insert_reaction(MessageLikeEvent::Original(
matrix_sdk::ruma::events::OriginalMessageLikeEvent {
content: content.clone(),
event_id: OwnedEventId::from_str(&event_id).unwrap(),
sender: owned_user_id!("@foo:example.org"),
origin_server_ts: MilliSecondsSinceUnixEpoch::now(),
room_id: owned_room_id!("!foo:example.org"),
unsigned: MessageLikeUnsigned::new(),
},
));
}

for i in 2..4 {
let event_id = format!("$smile_{}", i);
info.insert_reaction(MessageLikeEvent::Original(
matrix_sdk::ruma::events::OriginalMessageLikeEvent {
content: content.clone(),
event_id: OwnedEventId::from_str(&event_id).unwrap(),
sender: owned_user_id!("@bar:example.org"),
origin_server_ts: MilliSecondsSinceUnixEpoch::now(),
room_id: owned_room_id!("!foo:example.org"),
unsigned: MessageLikeUnsigned::new(),
},
));
}

assert_eq!(info.get_reactions(&owned_event_id!("$my_reaction")), vec![
("🏠", 1),
("🙂", 2)
]);
}

#[test]
fn test_typing_spans() {
let mut info = RoomInfo::default();
Expand Down

0 comments on commit 2506da4

Please sign in to comment.