twilight_model/channel/message/reaction.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
use crate::{
id::{marker::EmojiMarker, Id},
util::HexColor,
};
use serde::{Deserialize, Serialize};
/// Reaction below a message.
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub struct Reaction {
/// HEX colors used for super reaction.
pub burst_colors: Vec<HexColor>,
/// Amount of reactions this emoji has.
pub count: u64,
/// Reaction count details for each type of reaction.
pub count_details: ReactionCountDetails,
/// Emoji of this reaction.
pub emoji: EmojiReactionType,
/// Whether the current user has reacted with this emoji.
pub me: bool,
/// Whether the current user super-reacted using this emoji
pub me_burst: bool,
}
/// Type of emoji in a [`Reaction`].
#[derive(Clone, Debug, Eq, Hash, PartialEq, Deserialize, Serialize)]
#[serde(untagged)]
pub enum EmojiReactionType {
/// Custom [`Emoji`].
///
/// [`Emoji`]: crate::guild::Emoji
Custom {
/// Whether the emoji is animated.
#[serde(default)]
animated: bool,
/// Emoji identifier.
// Even though it says that the id can be nil in the docs,
// it is a bit misleading as that should only happen when
// the reaction is a unicode emoji and then it is caught by
// the other variant.
id: Id<EmojiMarker>,
/// Emoji name.
// Name is nil if the emoji data is no longer available, for
// example if the emoji have been deleted off the guild.
name: Option<String>,
},
/// Standard [Unicode] emoji value.
///
/// Unicode reactions must be specified by their unicode value, and *not*
/// their Discord display name. Instead of using `:arrow_right:`, use "➡️".
///
/// [Unicode]: https://unicode.org/emoji/
Unicode {
/// Unicode name identifier.
name: String,
},
}
/// Breakdown of normal and super reaction counts for the associated emoji.
#[derive(Clone, Debug, Eq, Hash, PartialEq, Deserialize, Serialize)]
pub struct ReactionCountDetails {
/// Count of super reactions.
pub burst: u64,
/// Count of normal reactions.
pub normal: u64,
}
#[cfg(test)]
mod tests {
use super::{EmojiReactionType, Reaction, ReactionCountDetails};
use crate::{id::Id, util::HexColor};
use serde_test::Token;
#[test]
fn message_reaction_unicode() {
let value = Reaction {
burst_colors: Vec::from([HexColor(255, 255, 255)]),
count: 7,
count_details: ReactionCountDetails {
burst: 0,
normal: 7,
},
emoji: EmojiReactionType::Unicode {
name: "a".to_owned(),
},
me: true,
me_burst: false,
};
serde_test::assert_tokens(
&value,
&[
Token::Struct {
name: "Reaction",
len: 6,
},
Token::Str("burst_colors"),
Token::Seq { len: Some(1) },
Token::Str("#FFFFFF"),
Token::SeqEnd,
Token::Str("count"),
Token::U64(7),
Token::Str("count_details"),
Token::Struct {
name: "ReactionCountDetails",
len: 2,
},
Token::Str("burst"),
Token::U64(0),
Token::Str("normal"),
Token::U64(7),
Token::StructEnd,
Token::Str("emoji"),
Token::Struct {
name: "EmojiReactionType",
len: 1,
},
Token::Str("name"),
Token::Str("a"),
Token::StructEnd,
Token::Str("me"),
Token::Bool(true),
Token::Str("me_burst"),
Token::Bool(false),
Token::StructEnd,
],
);
}
#[test]
fn custom() {
let value = EmojiReactionType::Custom {
animated: false,
id: Id::new(1337),
name: Some("foo".to_owned()),
};
serde_test::assert_tokens(
&value,
&[
Token::Struct {
name: "EmojiReactionType",
len: 3,
},
Token::Str("animated"),
Token::Bool(false),
Token::Str("id"),
Token::NewtypeStruct { name: "Id" },
Token::Str("1337"),
Token::Str("name"),
Token::Some,
Token::Str("foo"),
Token::StructEnd,
],
);
// When `animated` isn't present in the payload it should default to
// `false`.
serde_test::assert_de_tokens(
&value,
&[
Token::Struct {
name: "ReactionType",
len: 2,
},
Token::Str("id"),
Token::NewtypeStruct { name: "Id" },
Token::Str("1337"),
Token::Str("name"),
Token::Some,
Token::Str("foo"),
Token::StructEnd,
],
);
}
#[test]
fn unicode() {
let value = EmojiReactionType::Unicode {
name: "\u{1f643}".to_owned(),
};
serde_test::assert_tokens(
&value,
&[
Token::Struct {
name: "EmojiReactionType",
len: 1,
},
Token::Str("name"),
Token::Str("\u{1f643}"),
Token::StructEnd,
],
);
}
}