2022-12-30 14:16:33 +00:00
|
|
|
// Copyright (c) 2022 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2024-07-09 15:01:42 +00:00
|
|
|
use xso::{AsXml, FromXml};
|
2024-06-26 16:05:13 +00:00
|
|
|
|
2022-12-30 14:16:33 +00:00
|
|
|
use crate::message::MessagePayload;
|
2024-06-26 16:05:13 +00:00
|
|
|
use crate::ns;
|
2022-12-30 14:16:33 +00:00
|
|
|
|
|
|
|
generate_element!(
|
|
|
|
/// Container for a set of reactions.
|
|
|
|
Reactions, "reactions", REACTIONS,
|
|
|
|
attributes: [
|
|
|
|
/// The id of the message these reactions apply to.
|
|
|
|
id: Required<String> = "id",
|
|
|
|
],
|
|
|
|
children: [
|
|
|
|
/// The list of reactions.
|
|
|
|
reactions: Vec<Reaction> = ("reaction", REACTIONS) => Reaction,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
impl MessagePayload for Reactions {}
|
|
|
|
|
2024-06-26 16:05:13 +00:00
|
|
|
/// One emoji reaction.
|
2024-07-09 15:01:42 +00:00
|
|
|
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
|
2024-06-26 16:05:13 +00:00
|
|
|
#[xml(namespace = ns::REACTIONS, name = "reaction")]
|
|
|
|
pub struct Reaction {
|
|
|
|
/// The text of this reaction.
|
|
|
|
#[xml(text)]
|
|
|
|
pub emoji: String,
|
|
|
|
}
|
2022-12-30 14:16:33 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use crate::Element;
|
|
|
|
|
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
#[test]
|
|
|
|
fn test_size() {
|
|
|
|
assert_size!(Reactions, 24);
|
|
|
|
assert_size!(Reaction, 12);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_pointer_width = "64")]
|
|
|
|
#[test]
|
|
|
|
fn test_size() {
|
|
|
|
assert_size!(Reactions, 48);
|
|
|
|
assert_size!(Reaction, 24);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_empty() {
|
|
|
|
let elem: Element = "<reactions xmlns='urn:xmpp:reactions:0' id='foo'/>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
|
|
|
let elem2 = elem.clone();
|
|
|
|
let reactions = Reactions::try_from(elem2).unwrap();
|
|
|
|
assert_eq!(reactions.id, "foo");
|
|
|
|
assert_eq!(reactions.reactions.len(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_multi() {
|
|
|
|
let elem: Element =
|
|
|
|
"<reactions xmlns='urn:xmpp:reactions:0' id='foo'><reaction>👋</reaction><reaction>🐢</reaction></reactions>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
|
|
|
let elem2 = elem.clone();
|
|
|
|
let reactions = Reactions::try_from(elem2).unwrap();
|
|
|
|
assert_eq!(reactions.id, "foo");
|
|
|
|
assert_eq!(reactions.reactions.len(), 2);
|
|
|
|
let [hand, turtle]: [Reaction; 2] = reactions.reactions.try_into().unwrap();
|
|
|
|
assert_eq!(hand.emoji, "👋");
|
|
|
|
assert_eq!(turtle.emoji, "🐢");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_zwj_emoji() {
|
|
|
|
let elem: Element =
|
|
|
|
"<reactions xmlns='urn:xmpp:reactions:0' id='foo'><reaction>👩🏾❤️👩🏼</reaction></reactions>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
|
|
|
let elem2 = elem.clone();
|
|
|
|
let mut reactions = Reactions::try_from(elem2).unwrap();
|
|
|
|
assert_eq!(reactions.id, "foo");
|
|
|
|
assert_eq!(reactions.reactions.len(), 1);
|
|
|
|
let reaction = reactions.reactions.pop().unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
reaction.emoji,
|
|
|
|
"\u{1F469}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F469}\u{1F3FC}"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|