matrix_sdk_base/
recent_emojis.rs

1//! Data types used for handling the recently used emojis.
2//!
3//! There is no formal spec for this, only the implementation in Element Web:
4//! <https://github.com/element-hq/element-web/commit/a7f92f35f5a27a53a5a030ea7c471be97751a67a>
5
6use ruma::{UInt, events::macros::EventContent};
7use serde::{Deserialize, Serialize};
8
9/// An event type containing a list of recently used emojis for reactions.
10#[cfg(feature = "experimental-element-recent-emojis")]
11#[derive(Clone, Debug, Default, Deserialize, Serialize, EventContent)]
12#[ruma_event(type = "io.element.recent_emoji", kind = GlobalAccountData)]
13pub struct RecentEmojisContent {
14    /// The list of recently used emojis, ordered by recency. The tuple of
15    /// `String`, `UInt` values represent the actual emoji and the number of
16    /// times it's been used in total, for those clients that might be
17    /// interested.
18    pub recent_emoji: Vec<(String, UInt)>,
19}
20
21#[cfg(feature = "experimental-element-recent-emojis")]
22impl RecentEmojisContent {
23    /// Creates a new recent emojis event content given the provided recent
24    /// emojis.
25    pub fn new(recent_emoji: Vec<(String, UInt)>) -> Self {
26        Self { recent_emoji }
27    }
28}
29
30#[cfg(feature = "experimental-element-recent-emojis")]
31#[cfg(test)]
32mod tests {
33    use ruma::uint;
34    use serde_json::{from_value, json, to_value};
35
36    use crate::recent_emojis::RecentEmojisContent;
37
38    #[test]
39    fn serialization() {
40        let content = RecentEmojisContent::new(vec![
41            ("😁".to_owned(), uint!(2)),
42            ("🎉".to_owned(), uint!(10)),
43        ]);
44        let json = to_value(&content).expect("recent emoji serialization failed");
45        let expected = json!({
46            "recent_emoji": [
47                ["😁", 2],
48                ["🎉", 10],
49            ]
50        });
51
52        assert_eq!(json, expected);
53    }
54
55    #[test]
56    fn deserialization() {
57        let json = json!({
58            "recent_emoji": [
59                ["😁", 2],
60                ["🎉", 10],
61            ]
62        });
63        let content =
64            from_value::<RecentEmojisContent>(json).expect("recent emoji deserialization failed");
65        let expected = RecentEmojisContent::new(vec![
66            ("😁".to_owned(), uint!(2)),
67            ("🎉".to_owned(), uint!(10)),
68        ]);
69
70        assert_eq!(content.recent_emoji, expected.recent_emoji);
71    }
72}