Commit 0f17bbfa17
Unregistered key
Layout: unified · split
src/base.rs +79 −4
| @@ -3,7 +3,7 @@ | |||
| 3 | //! The types defined here get used throughout iamb. | 3 | //! The types defined here get used throughout iamb. |
| 4 | use std::borrow::Cow; | 4 | use std::borrow::Cow; |
| 5 | use std::collections::hash_map::IntoIter; | 5 | use std::collections::hash_map::IntoIter; |
| 6 | use std::collections::{HashMap, HashSet}; | 6 | use std::collections::{BTreeSet, HashMap, HashSet}; |
| 7 | use std::convert::TryFrom; | 7 | use std::convert::TryFrom; |
| 8 | use std::fmt::{self, Display}; | 8 | use std::fmt::{self, Display}; |
| 9 | use std::hash::Hash; | 9 | use std::hash::Hash; |
| @@ -838,9 +838,14 @@ impl RoomInfo { | |||
| 838 | if let Some(reacts) = self.reactions.get(event_id) { | 838 | if let Some(reacts) = self.reactions.get(event_id) { |
| 839 | let mut counts = HashMap::new(); | 839 | let mut counts = HashMap::new(); |
| 840 | 840 | ||
| 841 | for (key, _) in reacts.values() { | 841 | let mut seen_user_reactions = BTreeSet::new(); |
| 842 | let count = counts.entry(key.as_str()).or_default(); | 842 | |
| 843 | *count += 1; | 843 | for (key, user) in reacts.values() { |
| 844 | if !seen_user_reactions.contains(&(key, user)) { | ||
| 845 | seen_user_reactions.insert((key, user)); | ||
| 846 | let count = counts.entry(key.as_str()).or_default(); | ||
| 847 | *count += 1; | ||
| 848 | } | ||
| 844 | } | 849 | } |
| 845 | 850 | ||
| 846 | let mut reactions = counts.into_iter().collect::<Vec<_>>(); | 851 | let mut reactions = counts.into_iter().collect::<Vec<_>>(); |
| @@ -1855,8 +1860,78 @@ pub mod tests { | |||
| 1855 | use super::*; | 1860 | use super::*; |
| 1856 | use crate::config::user_style_from_color; | 1861 | use crate::config::user_style_from_color; |
| 1857 | use crate::tests::*; | 1862 | use crate::tests::*; |
| 1863 | use matrix_sdk::ruma::{ | ||
| 1864 | events::{reaction::ReactionEventContent, relation::Annotation, MessageLikeUnsigned}, | ||
| 1865 | owned_event_id, | ||
| 1866 | owned_room_id, | ||
| 1867 | owned_user_id, | ||
| 1868 | MilliSecondsSinceUnixEpoch, | ||
| 1869 | }; | ||
| 1870 | use pretty_assertions::assert_eq; | ||
| 1858 | use ratatui::style::Color; | 1871 | use ratatui::style::Color; |
| 1859 | 1872 | ||
| 1873 | #[test] | ||
| 1874 | fn multiple_identical_reactions() { | ||
| 1875 | let mut info = RoomInfo::default(); | ||
| 1876 | |||
| 1877 | let content = ReactionEventContent::new(Annotation::new( | ||
| 1878 | owned_event_id!("$my_reaction"), | ||
| 1879 | "🏠".to_owned(), | ||
| 1880 | )); | ||
| 1881 | |||
| 1882 | for i in 0..3 { | ||
| 1883 | let event_id = format!("$house_{}", i); | ||
| 1884 | info.insert_reaction(MessageLikeEvent::Original( | ||
| 1885 | matrix_sdk::ruma::events::OriginalMessageLikeEvent { | ||
| 1886 | content: content.clone(), | ||
| 1887 | event_id: OwnedEventId::from_str(&event_id).unwrap(), | ||
| 1888 | sender: owned_user_id!("@foo:example.org"), | ||
| 1889 | origin_server_ts: MilliSecondsSinceUnixEpoch::now(), | ||
| 1890 | room_id: owned_room_id!("!foo:example.org"), | ||
| 1891 | unsigned: MessageLikeUnsigned::new(), | ||
| 1892 | }, | ||
| 1893 | )); | ||
| 1894 | } | ||
| 1895 | |||
| 1896 | let content = ReactionEventContent::new(Annotation::new( | ||
| 1897 | owned_event_id!("$my_reaction"), | ||
| 1898 | "🙂".to_owned(), | ||
| 1899 | )); | ||
| 1900 | |||
| 1901 | for i in 0..2 { | ||
| 1902 | let event_id = format!("$smile_{}", i); | ||
| 1903 | info.insert_reaction(MessageLikeEvent::Original( | ||
| 1904 | matrix_sdk::ruma::events::OriginalMessageLikeEvent { | ||
| 1905 | content: content.clone(), | ||
| 1906 | event_id: OwnedEventId::from_str(&event_id).unwrap(), | ||
| 1907 | sender: owned_user_id!("@foo:example.org"), | ||
| 1908 | origin_server_ts: MilliSecondsSinceUnixEpoch::now(), | ||
| 1909 | room_id: owned_room_id!("!foo:example.org"), | ||
| 1910 | unsigned: MessageLikeUnsigned::new(), | ||
| 1911 | }, | ||
| 1912 | )); | ||
| 1913 | } | ||
| 1914 | |||
| 1915 | for i in 2..4 { | ||
| 1916 | let event_id = format!("$smile_{}", i); | ||
| 1917 | info.insert_reaction(MessageLikeEvent::Original( | ||
| 1918 | matrix_sdk::ruma::events::OriginalMessageLikeEvent { | ||
| 1919 | content: content.clone(), | ||
| 1920 | event_id: OwnedEventId::from_str(&event_id).unwrap(), | ||
| 1921 | sender: owned_user_id!("@bar:example.org"), | ||
| 1922 | origin_server_ts: MilliSecondsSinceUnixEpoch::now(), | ||
| 1923 | room_id: owned_room_id!("!foo:example.org"), | ||
| 1924 | unsigned: MessageLikeUnsigned::new(), | ||
| 1925 | }, | ||
| 1926 | )); | ||
| 1927 | } | ||
| 1928 | |||
| 1929 | assert_eq!(info.get_reactions(&owned_event_id!("$my_reaction")), vec![ | ||
| 1930 | ("🏠", 1), | ||
| 1931 | ("🙂", 2) | ||
| 1932 | ]); | ||
| 1933 | } | ||
| 1934 | |||
| 1860 | #[test] | 1935 | #[test] |
| 1861 | fn test_typing_spans() { | 1936 | fn test_typing_spans() { |
| 1862 | let mut info = RoomInfo::default(); | 1937 | let mut info = RoomInfo::default(); |