Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(model, http)!: Add support for super reaction types #2347

Merged
merged 4 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 26 additions & 20 deletions twilight-cache-inmemory/src/event/reaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
CacheableModels, InMemoryCache, UpdateCache,
};
use twilight_model::{
channel::message::{Reaction, ReactionCountDetails, ReactionType},
channel::message::{EmojiReactionType, Reaction, ReactionCountDetails},
gateway::payload::incoming::{
ReactionAdd, ReactionRemove, ReactionRemoveAll, ReactionRemoveEmoji,
},
Expand Down Expand Up @@ -124,14 +124,16 @@ impl<CacheModels: CacheableModels> UpdateCache<CacheModels> for ReactionRemoveEm
}
}

fn reactions_eq(a: &ReactionType, b: &ReactionType) -> bool {
fn reactions_eq(a: &EmojiReactionType, b: &EmojiReactionType) -> bool {
match (a, b) {
(ReactionType::Custom { id: id_a, .. }, ReactionType::Custom { id: id_b, .. }) => {
id_a == id_b
}
(ReactionType::Unicode { name: name_a }, ReactionType::Unicode { name: name_b }) => {
name_a == name_b
}
(
EmojiReactionType::Custom { id: id_a, .. },
EmojiReactionType::Custom { id: id_b, .. },
) => id_a == id_b,
(
EmojiReactionType::Unicode { name: name_a },
EmojiReactionType::Unicode { name: name_b },
) => name_a == name_b,
_ => false,
}
}
Expand All @@ -141,7 +143,7 @@ mod tests {
use super::reactions_eq;
use crate::{model::CachedMessage, test};
use twilight_model::{
channel::message::{Reaction, ReactionType},
channel::message::{EmojiReactionType, Reaction},
gateway::{
payload::incoming::{ReactionRemove, ReactionRemoveAll, ReactionRemoveEmoji},
GatewayReaction,
Expand All @@ -153,7 +155,7 @@ mod tests {
msg.reactions.iter().find(|&r| {
reactions_eq(
&r.emoji,
&ReactionType::Custom {
&EmojiReactionType::Custom {
animated: false,
id: Id::new(6),
name: None,
Expand All @@ -172,11 +174,11 @@ mod tests {
let world_react = msg
.reactions
.iter()
.find(|&r| matches!(&r.emoji, ReactionType::Unicode {name} if name == "🗺️"));
.find(|&r| matches!(&r.emoji, EmojiReactionType::Unicode {name} if name == "🗺️"));
let smiley_react = msg
.reactions
.iter()
.find(|&r| matches!(&r.emoji, ReactionType::Unicode {name} if name == "😀"));
.find(|&r| matches!(&r.emoji, EmojiReactionType::Unicode {name} if name == "😀"));
let custom_react = find_custom_react(&msg);

assert!(world_react.is_some());
Expand All @@ -191,8 +193,10 @@ mod tests {
fn reaction_remove() {
let cache = test::cache_with_message_and_reactions();
cache.update(&ReactionRemove(GatewayReaction {
burst: false,
burst_colors: Vec::new(),
channel_id: Id::new(2),
emoji: ReactionType::Unicode {
emoji: EmojiReactionType::Unicode {
name: "😀".to_owned(),
},
guild_id: Some(Id::new(1)),
Expand All @@ -202,8 +206,10 @@ mod tests {
user_id: Id::new(5),
}));
cache.update(&ReactionRemove(GatewayReaction {
burst: false,
burst_colors: Vec::new(),
channel_id: Id::new(2),
emoji: ReactionType::Custom {
emoji: EmojiReactionType::Custom {
animated: false,
id: Id::new(6),
name: None,
Expand All @@ -222,11 +228,11 @@ mod tests {
let world_react = msg
.reactions
.iter()
.find(|&r| matches!(&r.emoji, ReactionType::Unicode {name} if name == "🗺️"));
.find(|&r| matches!(&r.emoji, EmojiReactionType::Unicode {name} if name == "🗺️"));
let smiley_react = msg
.reactions
.iter()
.find(|&r| matches!(&r.emoji, ReactionType::Unicode {name} if name == "😀"));
.find(|&r| matches!(&r.emoji, EmojiReactionType::Unicode {name} if name == "😀"));
let custom_react = find_custom_react(&msg);

assert!(world_react.is_some());
Expand Down Expand Up @@ -255,15 +261,15 @@ mod tests {
let cache = test::cache_with_message_and_reactions();
cache.update(&ReactionRemoveEmoji {
channel_id: Id::new(2),
emoji: ReactionType::Unicode {
emoji: EmojiReactionType::Unicode {
name: "😀".to_owned(),
},
guild_id: Id::new(1),
message_id: Id::new(4),
});
cache.update(&ReactionRemoveEmoji {
channel_id: Id::new(2),
emoji: ReactionType::Custom {
emoji: EmojiReactionType::Custom {
animated: false,
id: Id::new(6),
name: None,
Expand All @@ -279,11 +285,11 @@ mod tests {
let world_react = msg
.reactions
.iter()
.find(|&r| matches!(&r.emoji, ReactionType::Unicode {name} if name == "🗺️"));
.find(|&r| matches!(&r.emoji, EmojiReactionType::Unicode {name} if name == "🗺️"));
let smiley_react = msg
.reactions
.iter()
.find(|&r| matches!(&r.emoji, ReactionType::Unicode {name} if name == "😀"));
.find(|&r| matches!(&r.emoji, EmojiReactionType::Unicode {name} if name == "😀"));
let custom_react = find_custom_react(&msg);

assert!(world_react.is_some());
Expand Down
10 changes: 6 additions & 4 deletions twilight-cache-inmemory/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use twilight_model::{
channel::{
message::{
sticker::{Sticker, StickerFormatType, StickerType},
Message, MessageFlags, MessageType, ReactionType,
EmojiReactionType, Message, MessageFlags, MessageType,
},
Channel, ChannelType,
},
Expand Down Expand Up @@ -102,8 +102,10 @@ pub fn cache_with_message_and_reactions() -> DefaultInMemoryCache {
cache.update(&MessageCreate(msg));

let mut reaction = ReactionAdd(GatewayReaction {
burst: false,
burst_colors: Vec::new(),
channel_id: Id::new(2),
emoji: ReactionType::Unicode {
emoji: EmojiReactionType::Unicode {
name: "😀".to_owned(),
},
guild_id: Some(Id::new(1)),
Expand Down Expand Up @@ -183,13 +185,13 @@ pub fn cache_with_message_and_reactions() -> DefaultInMemoryCache {

cache.update(&reaction);

reaction.emoji = ReactionType::Unicode {
reaction.emoji = EmojiReactionType::Unicode {
name: "🗺️".to_owned(),
};

cache.update(&reaction);

reaction.emoji = ReactionType::Custom {
reaction.emoji = EmojiReactionType::Custom {
animated: true,
id: Id::new(6),
name: Some("custom".to_owned()),
Expand Down
15 changes: 15 additions & 0 deletions twilight-http/src/request/channel/reaction/get_reactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
};
use std::future::IntoFuture;
use twilight_model::{
channel::message::ReactionType,
id::{
marker::{ChannelMarker, MessageMarker, UserMarker},
Id,
Expand All @@ -21,6 +22,7 @@ use twilight_validate::request::{
struct GetReactionsFields {
after: Option<Id<UserMarker>>,
limit: Option<u16>,
kind: Option<ReactionType>,
}

/// Get a list of users that reacted to a message with an `emoji`.
Expand Down Expand Up @@ -49,6 +51,7 @@ impl<'a> GetReactions<'a> {
fields: Ok(GetReactionsFields {
after: None,
limit: None,
kind: None,
}),
http,
message_id,
Expand Down Expand Up @@ -85,6 +88,17 @@ impl<'a> GetReactions<'a> {

self
}

/// Set the kind of reaction to retrieve.
///
/// This can be either a super reaction or a normal reaction.
pub fn kind(mut self, kind: ReactionType) -> Self {
if let Ok(fields) = self.fields.as_mut() {
fields.kind = Some(kind);
}

self
}
}

impl IntoFuture for GetReactions<'_> {
Expand Down Expand Up @@ -112,6 +126,7 @@ impl TryIntoRequest for GetReactions<'_> {
emoji: self.emoji,
limit: fields.limit,
message_id: self.message_id.get(),
kind: fields.kind.map(Into::into),
}))
}
}
6 changes: 5 additions & 1 deletion twilight-http/src/routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,8 @@ pub enum Route<'a> {
limit: Option<u16>,
/// The ID of the message.
message_id: u64,
/// The type of reactions to fetch.
kind: Option<u8>,
},
GetSKUs {
/// The ID of the application.
Expand Down Expand Up @@ -2735,6 +2737,7 @@ impl Display for Route<'_> {
emoji,
limit,
message_id,
kind,
} => {
f.write_str("channels/")?;
Display::fmt(channel_id, f)?;
Expand All @@ -2746,7 +2749,8 @@ impl Display for Route<'_> {
let mut query_formatter = QueryStringFormatter::new(f);

query_formatter.write_opt_param("after", after.as_ref())?;
query_formatter.write_opt_param("limit", limit.as_ref())
query_formatter.write_opt_param("limit", limit.as_ref())?;
query_formatter.write_opt_param("type", kind.as_ref())
}
Route::GetSticker { sticker_id } => {
f.write_str("stickers/")?;
Expand Down
4 changes: 2 additions & 2 deletions twilight-model/src/channel/message/component/button.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::channel::message::ReactionType;
use crate::channel::message::EmojiReactionType;
use serde::{Deserialize, Serialize};

/// Clickable [`Component`] below messages.
Expand All @@ -20,7 +20,7 @@ pub struct Button {
/// Defaults to `false`.
pub disabled: bool,
/// Visual emoji for clients to display with the button.
pub emoji: Option<ReactionType>,
pub emoji: Option<EmojiReactionType>,
/// Text appearing on the button.
pub label: Option<String>,
/// Style variant of the button.
Expand Down
16 changes: 8 additions & 8 deletions twilight-model/src/channel/message/component/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub use self::{
text_input::{TextInput, TextInputStyle},
};

use super::ReactionType;
use super::EmojiReactionType;
use crate::channel::ChannelType;
use serde::{
de::{Deserializer, Error as DeError, IgnoredAny, MapAccess, Visitor},
Expand Down Expand Up @@ -58,7 +58,7 @@ use std::fmt::{Formatter, Result as FmtResult};
/// use twilight_model::{
/// channel::message::{
/// component::{ActionRow, Component, SelectMenu, SelectMenuOption, SelectMenuType},
/// ReactionType,
/// EmojiReactionType,
/// },
/// id::Id,
/// };
Expand All @@ -75,7 +75,7 @@ use std::fmt::{Formatter, Result as FmtResult};
/// options: Some(Vec::from([
/// SelectMenuOption {
/// default: false,
/// emoji: Some(ReactionType::Custom {
/// emoji: Some(EmojiReactionType::Custom {
/// animated: false,
/// id: Id::new(625891304148303894),
/// name: Some("rogue".to_owned()),
Expand All @@ -86,7 +86,7 @@ use std::fmt::{Formatter, Result as FmtResult};
/// },
/// SelectMenuOption {
/// default: false,
/// emoji: Some(ReactionType::Custom {
/// emoji: Some(EmojiReactionType::Custom {
/// animated: false,
/// id: Id::new(625891304081063986),
/// name: Some("mage".to_owned()),
Expand All @@ -97,7 +97,7 @@ use std::fmt::{Formatter, Result as FmtResult};
/// },
/// SelectMenuOption {
/// default: false,
/// emoji: Some(ReactionType::Custom {
/// emoji: Some(EmojiReactionType::Custom {
/// animated: false,
/// id: Id::new(625891303795982337),
/// name: Some("priest".to_owned()),
Expand Down Expand Up @@ -239,7 +239,7 @@ impl<'de> Visitor<'de> for ComponentVisitor {
let mut channel_types: Option<Vec<ChannelType>> = None;
let mut default_values: Option<Vec<SelectDefaultValue>> = None;
let mut disabled: Option<bool> = None;
let mut emoji: Option<Option<ReactionType>> = None;
let mut emoji: Option<Option<EmojiReactionType>> = None;
let mut max_length: Option<Option<u16>> = None;
let mut max_values: Option<Option<u8>> = None;
let mut min_length: Option<Option<u16>> = None;
Expand Down Expand Up @@ -899,7 +899,7 @@ mod tests {
let value = Component::Button(Button {
custom_id: Some("test".to_owned()),
disabled: false,
emoji: Some(ReactionType::Unicode {
emoji: Some(EmojiReactionType::Unicode {
name: FLAG.to_owned(),
}),
label: Some("Test".to_owned()),
Expand All @@ -922,7 +922,7 @@ mod tests {
Token::String("emoji"),
Token::Some,
Token::Struct {
name: "ReactionType",
name: "EmojiReactionType",
len: 1,
},
Token::String("name"),
Expand Down
4 changes: 2 additions & 2 deletions twilight-model/src/channel/message/component/select_menu.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::channel::{message::ReactionType, ChannelType};
use crate::channel::{message::EmojiReactionType, ChannelType};
use crate::id::marker::{ChannelMarker, RoleMarker, UserMarker};
use crate::id::Id;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -65,7 +65,7 @@ pub struct SelectMenuOption {
/// Emoji associated with the option. Appears left of the label and
/// description.
#[serde(skip_serializing_if = "Option::is_none")]
pub emoji: Option<ReactionType>,
pub emoji: Option<EmojiReactionType>,
/// User-facing name.
pub label: String,
/// Developer defined value.
Expand Down
12 changes: 7 additions & 5 deletions twilight-model/src/channel/message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod interaction;
mod kind;
mod mention;
mod reaction;
mod reaction_type;
mod reference;
mod role_subscription_data;

Expand All @@ -26,7 +27,8 @@ pub use self::{
interaction::MessageInteraction,
kind::MessageType,
mention::Mention,
reaction::{Reaction, ReactionCountDetails, ReactionType},
reaction::{EmojiReactionType, Reaction, ReactionCountDetails},
reaction_type::ReactionType,
reference::MessageReference,
role_subscription_data::RoleSubscriptionData,
sticker::Sticker,
Expand Down Expand Up @@ -198,8 +200,8 @@ mod tests {
use super::{
reaction::ReactionCountDetails,
sticker::{MessageSticker, StickerFormatType},
Message, MessageActivity, MessageActivityType, MessageApplication, MessageFlags,
MessageReference, MessageType, Reaction, ReactionType,
EmojiReactionType, Message, MessageActivity, MessageActivityType, MessageApplication,
MessageFlags, MessageReference, MessageType, Reaction,
};
use crate::{
channel::{ChannelMention, ChannelType},
Expand Down Expand Up @@ -486,7 +488,7 @@ mod tests {
burst: 0,
normal: 7,
},
emoji: ReactionType::Unicode {
emoji: EmojiReactionType::Unicode {
name: "a".to_owned(),
},
me: true,
Expand Down Expand Up @@ -681,7 +683,7 @@ mod tests {
Token::StructEnd,
Token::Str("emoji"),
Token::Struct {
name: "ReactionType",
name: "EmojiReactionType",
len: 1,
},
Token::Str("name"),
Expand Down
Loading