Skip to content

Commit

Permalink
Make Message::react return its Reaction (#885)
Browse files Browse the repository at this point in the history
  • Loading branch information
meithecatte committed Jun 14, 2020
1 parent cdf42b6 commit e7eb52d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
4 changes: 3 additions & 1 deletion examples/e12_timing_and_events/src/main.rs
Expand Up @@ -85,8 +85,10 @@ impl EventHandler for Handler {
context.get_mut::<DispatcherKey>().expect("Expected Dispatcher.").clone()
};

// We may safely unwrap the user_id as the Reaction comes from an event and not
// Message::react.
dispatcher.write().dispatch_event(
&DispatchEvent::ReactEvent(reaction.message_id, reaction.user_id));
&DispatchEvent::ReactEvent(reaction.message_id, reaction.user_id.unwrap()));
}
}

Expand Down
20 changes: 17 additions & 3 deletions src/model/channel/message.rs
Expand Up @@ -471,11 +471,14 @@ impl Message {
/// ../permissions/struct.Permissions.html#associatedconstant.ADD_REACTIONS
/// [permissions]: ../permissions/index.html
#[inline]
pub fn react<R: Into<ReactionType>>(&self, cache_http: impl CacheHttp, reaction_type: R) -> Result<()> {
pub fn react<R: Into<ReactionType>>(&self, cache_http: impl CacheHttp, reaction_type: R) -> Result<Reaction> {
self._react(cache_http, &reaction_type.into())
}

fn _react(&self, cache_http: impl CacheHttp, reaction_type: &ReactionType) -> Result<()> {
fn _react(&self, cache_http: impl CacheHttp, reaction_type: &ReactionType) -> Result<Reaction> {
#[allow(unused_mut)]
let mut user_id = None;

#[cfg(feature = "cache")]
{
if let Some(cache) = cache_http.cache() {
Expand All @@ -487,10 +490,21 @@ impl Message {
return Err(Error::Model(ModelError::InvalidPermissions(req)));
}
}

user_id = Some(cache.read().user.id);
}
}

cache_http.http().create_reaction(self.channel_id.0, self.id.0, reaction_type)
cache_http.http().create_reaction(self.channel_id.0, self.id.0, reaction_type)?;

Ok(Reaction {
channel_id: self.channel_id,
emoji: reaction_type.clone(),
message_id: self.id,
user_id,
guild_id: self.guild_id,
_nonexhaustive: (),
})
}

/// Replies to the user, mentioning them prior to the content in the form
Expand Down
26 changes: 21 additions & 5 deletions src/model/channel/reaction.rs
Expand Up @@ -37,8 +37,11 @@ pub struct Reaction {
pub message_id: MessageId,
/// The Id of the [`User`] that sent the reaction.
///
/// Set to `None` by [`Message::react`] when cache is not available.
///
/// [`User`]: ../user/struct.User.html
pub user_id: UserId,
/// [`Message::react`]: struct.Message.html#method.react
pub user_id: Option<UserId>,
/// The optional Id of the [`Guild`] where the reaction was sent.
///
/// [`Guild`]: ../guild/struct.Guild.html
Expand Down Expand Up @@ -81,13 +84,13 @@ impl Reaction {
pub fn delete(&self, cache_http: impl CacheHttp) -> Result<()> {
// Silences a warning when compiling without the `cache` feature.
#[allow(unused_mut)]
let mut user_id = Some(self.user_id.0);
let mut user_id = self.user_id.map(|id| id.0);

#[cfg(feature = "cache")]
{
if let Some(cache) = cache_http.cache() {

if self.user_id == cache.read().user.id {
if self.user_id.is_some() && self.user_id == Some(cache.read().user.id) {
user_id = None;
}

Expand Down Expand Up @@ -151,9 +154,22 @@ impl Reaction {
/// If the cache is enabled, this will search for the already-cached user.
/// If not - or the user was not found - this will perform a request over
/// the REST API for the user.
#[inline]
pub fn user(&self, cache_http: impl CacheHttp) -> Result<User> {
self.user_id.to_user(cache_http)
match self.user_id {
Some(id) => id.to_user(cache_http),
None => {
// This can happen if only Http was passed to Message::react, even though
// "cache" was enabled.
#[cfg(feature = "cache")]
{
if let Some(cache) = cache_http.cache() {
return Ok(User::from(&cache.read().user));
}
}

Ok(cache_http.http().get_current_user()?.into())
}
}
}

/// Retrieves the list of [`User`]s who have reacted to a [`Message`] with a
Expand Down

0 comments on commit e7eb52d

Please sign in to comment.