use ruma::{UInt, events::macros::EventContent};
use serde::{Deserialize, Serialize};
#[cfg(feature = "experimental-element-recent-emojis")]
#[derive(Clone, Debug, Default, Deserialize, Serialize, EventContent)]
#[ruma_event(type = "io.element.recent_emoji", kind = GlobalAccountData)]
pub struct RecentEmojisContent {
pub recent_emoji: Vec<(String, UInt)>,
}
#[cfg(feature = "experimental-element-recent-emojis")]
impl RecentEmojisContent {
pub fn new(recent_emoji: Vec<(String, UInt)>) -> Self {
Self { recent_emoji }
}
}
#[cfg(feature = "experimental-element-recent-emojis")]
#[cfg(test)]
mod tests {
use ruma::uint;
use serde_json::{from_value, json, to_value};
use crate::recent_emojis::RecentEmojisContent;
#[test]
fn serialization() {
let content = RecentEmojisContent::new(vec![
("😁".to_owned(), uint!(2)),
("🎉".to_owned(), uint!(10)),
]);
let json = to_value(&content).expect("recent emoji serialization failed");
let expected = json!({
"recent_emoji": [
["😁", 2],
["🎉", 10],
]
});
assert_eq!(json, expected);
}
#[test]
fn deserialization() {
let json = json!({
"recent_emoji": [
["😁", 2],
["🎉", 10],
]
});
let content =
from_value::<RecentEmojisContent>(json).expect("recent emoji deserialization failed");
let expected = RecentEmojisContent::new(vec![
("😁".to_owned(), uint!(2)),
("🎉".to_owned(), uint!(10)),
]);
assert_eq!(content.recent_emoji, expected.recent_emoji);
}
}