Skip to content

Commit

Permalink
Pass old Message to message_update (#459)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mishio595 authored and arqunis committed Apr 6, 2019
1 parent 3f0ea69 commit 40bf272
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 30 deletions.
48 changes: 48 additions & 0 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,54 @@ impl Cache {
})
}

/// Retrieves a [`Channel`]'s message from the cache based on the channel's and
/// message's given Ids.
///
/// **Note**: This will clone the entire message.
///
/// # Examples
///
/// Retrieving the message object from a channel, in a
/// [`EventHandler::message`] context:
///
/// ```rust,no_run
/// # extern crate parking_lot;
/// # extern crate serenity;
/// #
/// # use serenity::{cache::Cache, model::id::{ChannelId, MessageId}};
/// # use parking_lot::RwLock;
/// # use std::sync::Arc;
/// #
/// # let message = ChannelId(0).message(MessageId(1)).unwrap();
/// # let cache = Arc::new(RwLock::new(Cache::default()));
/// #
/// let cache = cache.read();
/// let fetched_message = cache.message(message.channel_id, message.id);
///
/// match fetched_message {
/// Some(m) => {
/// assert_eq!(message.content, m.content);
/// },
/// None => {
/// println!("No message found in cache.");
/// },
/// }
/// ```
///
/// [`EventHandler::message`]: ../client/trait.EventHandler.html#method.message
/// [`Channel`]: ../model/channel/struct.Channel.html
#[inline]
pub fn message<C, M>(&self, channel_id: C, message_id: M) -> Option<Message>
where C: Into<ChannelId>, M: Into<MessageId> {
self._message(channel_id.into(), message_id.into())
}

fn _message(&self, channel_id: ChannelId, message_id: MessageId) -> Option<Message> {
self.messages.get(&channel_id).and_then(|messages| {
messages.get(&message_id).cloned()
})
}

/// Retrieves a [`PrivateChannel`] from the cache's [`private_channels`]
/// map, if it exists.
///
Expand Down
9 changes: 7 additions & 2 deletions src/client/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,11 +521,16 @@ fn handle_event<H: EventHandler + Send + Sync + 'static>(
});
},
DispatchEvent::Model(Event::MessageUpdate(mut event)) => {
update!(cache_and_http, event);
let _before = update!(cache_and_http, event);
let event_handler = Arc::clone(event_handler);

threadpool.execute(move || {
event_handler.message_update(context, event);
feature_cache! {{
let _after = cache_and_http.cache.read().message(event.channel_id, event.id);
event_handler.message_update(context, _before, _after, event);
} else {
event_handler.message_update(context, event);
}}
});
},
DispatchEvent::Model(Event::PresencesReplace(mut event)) => {
Expand Down
21 changes: 16 additions & 5 deletions src/client/event_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,22 @@ pub trait EventHandler {
/// Provides the channel's id and the deleted messages' ids.
fn message_delete_bulk(&self, _ctx: Context, _channel_id: ChannelId, _multiple_deleted_messages_ids: Vec<MessageId>) {}

/// Dispatched when a message is updated.
///
/// Provides the old message if available,
/// the new message as an option in case of cache inconsistencies,
/// and the raw [`MessageUpdateEvent`] as a fallback.
///
/// [`MessageUpdateEvent`]: ../model/event/struct.MessageUpdateEvent.html
#[cfg(feature = "cache")]
fn message_update(&self, _ctx: Context, _old_if_available: Option<Message>, _new: Option<Message>, _event: MessageUpdateEvent) {}

/// Dispatched when a message is updated.
///
/// Provides the new data of the message.
#[cfg(not(feature = "cache"))]
fn message_update(&self, _ctx: Context, _new_data: MessageUpdateEvent) {}

/// Dispatched when a new reaction is attached to a message.
///
/// Provides the reaction's data.
Expand All @@ -227,11 +243,6 @@ pub trait EventHandler {
/// Provides the channel's id and the message's id.
fn reaction_remove_all(&self, _ctx: Context, _channel_id: ChannelId, _removed_from_message_id: MessageId) {}

/// Dispatched when a message is updated.
///
/// Provides the new data of the message.
fn message_update(&self, _ctx: Context, _new_data: MessageUpdateEvent) {}

fn presence_replace(&self, _ctx: Context, _: Vec<Presence>) {}

/// Dispatched when a user's presence is updated (e.g off -> on).
Expand Down
52 changes: 29 additions & 23 deletions src/model/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -847,38 +847,44 @@ pub struct MessageUpdateEvent {

#[cfg(feature = "cache")]
impl CacheUpdate for MessageUpdateEvent {
type Output = ();
type Output = Message;

fn update(&mut self, cache: &mut Cache) -> Option<Self::Output> {
let messages = cache.messages.get_mut(&self.channel_id)?;
let message = messages.get_mut(&self.id)?;
if let Some(messages) = cache.messages.get_mut(&self.channel_id) {

if let Some(attachments) = self.attachments.clone() {
message.attachments = attachments;
}
if let Some(message) = messages.get_mut(&self.id) {
let item = message.clone();

if let Some(content) = self.content.clone() {
message.content = content;
}
if let Some(attachments) = self.attachments.clone() {
message.attachments = attachments;
}

if let Some(edited_timestamp) = self.edited_timestamp {
message.edited_timestamp = Some(edited_timestamp);
}
if let Some(content) = self.content.clone() {
message.content = content;
}

if let Some(mentions) = self.mentions.clone() {
message.mentions = mentions;
}
if let Some(edited_timestamp) = self.edited_timestamp {
message.edited_timestamp = Some(edited_timestamp);
}

if let Some(mention_everyone) = self.mention_everyone {
message.mention_everyone = mention_everyone;
}
if let Some(mentions) = self.mentions.clone() {
message.mentions = mentions;
}

if let Some(mention_roles) = self.mention_roles.clone() {
message.mention_roles = mention_roles;
}
if let Some(mention_everyone) = self.mention_everyone {
message.mention_everyone = mention_everyone;
}

if let Some(pinned) = self.pinned {
message.pinned = pinned;
if let Some(mention_roles) = self.mention_roles.clone() {
message.mention_roles = mention_roles;
}

if let Some(pinned) = self.pinned {
message.pinned = pinned;
}

return Some(item);
}
}

None
Expand Down

0 comments on commit 40bf272

Please sign in to comment.