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

Fix embed suppression on non-self users when cache is enabled #1

Draft
wants to merge 5 commits into
base: current
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion src/builder/create_allowed_mentions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ enum ParseValue {
/// ```
///
/// [Discord docs](https://discord.com/developers/docs/resources/channel#allowed-mentions-object).
#[derive(Clone, Debug, Default, Serialize)]
#[derive(Clone, Debug, Default, Serialize, PartialEq)]
#[must_use]
pub struct CreateAllowedMentions {
parse: HashSet<ParseValue>,
Expand Down
8 changes: 4 additions & 4 deletions src/builder/create_attachment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::model::id::AttachmentId;
/// [Discord docs](https://discord.com/developers/docs/resources/channel#attachment-object-attachment-structure).
///
/// [`send_files`]: crate::model::id::ChannelId::send_files
#[derive(Clone, Debug, Serialize)]
#[derive(Clone, Debug, Serialize, PartialEq)]
#[non_exhaustive]
#[must_use]
pub struct CreateAttachment {
Expand Down Expand Up @@ -114,12 +114,12 @@ impl CreateAttachment {
}
}

#[derive(Debug, Clone, serde::Serialize)]
#[derive(Debug, Clone, serde::Serialize, PartialEq)]
struct ExistingAttachment {
id: AttachmentId,
}

#[derive(Debug, Clone, serde::Serialize)]
#[derive(Debug, Clone, serde::Serialize, PartialEq)]
#[serde(untagged)]
enum NewOrExisting {
New(CreateAttachment),
Expand Down Expand Up @@ -181,7 +181,7 @@ enum NewOrExisting {
///
/// Internally, this type is used not just for message editing endpoints, but also for message
/// creation endpoints.
#[derive(Default, Debug, Clone, serde::Serialize)]
#[derive(Default, Debug, Clone, serde::Serialize, PartialEq)]
#[serde(transparent)]
#[must_use]
pub struct EditAttachments {
Expand Down
12 changes: 6 additions & 6 deletions src/builder/create_components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::model::prelude::*;
/// A builder for creating a components action row in a message.
///
/// [Discord docs](https://discord.com/developers/docs/interactions/message-components#component-object).
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
#[must_use]
pub enum CreateActionRow {
Buttons(Vec<CreateButton>),
Expand All @@ -32,7 +32,7 @@ impl serde::Serialize for CreateActionRow {
}

/// A builder for creating a button component in a message
#[derive(Clone, Debug, Serialize)]
#[derive(Clone, Debug, Serialize, PartialEq)]
#[must_use]
pub struct CreateButton(Button);

Expand Down Expand Up @@ -128,7 +128,7 @@ impl Serialize for CreateSelectMenuDefault {
}

/// [Discord docs](https://discord.com/developers/docs/interactions/message-components#select-menu-object-select-menu-structure).
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
pub enum CreateSelectMenuKind {
String { options: Vec<CreateSelectMenuOption> },
User { default_users: Option<Vec<UserId>> },
Expand Down Expand Up @@ -198,7 +198,7 @@ impl Serialize for CreateSelectMenuKind {
/// A builder for creating a select menu component in a message
///
/// [Discord docs](https://discord.com/developers/docs/interactions/message-components#select-menu-object-select-menu-structure).
#[derive(Clone, Debug, Serialize)]
#[derive(Clone, Debug, Serialize, PartialEq)]
#[must_use]
pub struct CreateSelectMenu {
custom_id: String,
Expand Down Expand Up @@ -264,7 +264,7 @@ impl CreateSelectMenu {
/// A builder for creating an option of a select menu component in a message
///
/// [Discord docs](https://discord.com/developers/docs/interactions/message-components#select-menu-object-select-option-structure)
#[derive(Clone, Debug, Serialize)]
#[derive(Clone, Debug, Serialize, PartialEq)]
#[must_use]
pub struct CreateSelectMenuOption {
label: String,
Expand Down Expand Up @@ -324,7 +324,7 @@ impl CreateSelectMenuOption {
/// A builder for creating an input text component in a modal
///
/// [Discord docs](https://discord.com/developers/docs/interactions/message-components#text-inputs-text-input-structure).
#[derive(Clone, Debug, Serialize)]
#[derive(Clone, Debug, Serialize, PartialEq)]
#[must_use]
pub struct CreateInputText(InputText);

Expand Down
2 changes: 1 addition & 1 deletion src/builder/create_embed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::model::prelude::*;
/// A builder to create an embed in a message
///
/// [Discord docs](https://discord.com/developers/docs/resources/channel#embed-object)
#[derive(Clone, Debug, Serialize)]
#[derive(Clone, Debug, Serialize, PartialEq)]
#[must_use]
pub struct CreateEmbed(Embed);

Expand Down
15 changes: 13 additions & 2 deletions src/builder/edit_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use crate::model::prelude::*;
/// ```
///
/// [Discord docs](https://discord.com/developers/docs/resources/channel#edit-message)
#[derive(Clone, Debug, Default, Serialize)]
#[derive(Clone, Debug, Default, Serialize, PartialEq)]
#[must_use]
pub struct EditMessage {
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -221,7 +221,7 @@ impl EditMessage {
#[cfg(feature = "http")]
#[async_trait::async_trait]
impl Builder for EditMessage {
type Context<'ctx> = (ChannelId, MessageId);
type Context<'ctx> = (ChannelId, MessageId, Option<UserId>);
type Built = Message;

/// Edits a message in the channel.
Expand Down Expand Up @@ -254,6 +254,17 @@ impl Builder for EditMessage {
) -> Result<Self::Built> {
self.check_length()?;

#[cfg(feature = "cache")]
if let Some(user_id) = ctx.2 {
if let Some(cache) = cache_http.cache() {
let reference_builder = EditMessage::new().suppress_embeds(true);

if user_id != cache.current_user().id && self != reference_builder {
return Err(Error::Model(ModelError::InvalidUser));
}
}
}

let files = self.attachments.as_mut().map_or(Vec::new(), |a| a.take_files());

cache_http.http().edit_message(ctx.0, ctx.1, &self, files).await
Expand Down
2 changes: 1 addition & 1 deletion src/model/channel/channel_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ impl ChannelId {
message_id: impl Into<MessageId>,
builder: EditMessage,
) -> Result<Message> {
builder.execute(cache_http, (self, message_id.into())).await
builder.execute(cache_http, (self, message_id.into(), None)).await
}

/// Follows the News Channel
Expand Down
16 changes: 8 additions & 8 deletions src/model/channel/embed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::model::{Colour, Timestamp};
///
/// [slack's attachments]: https://api.slack.com/docs/message-attachments
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Default, Debug, Deserialize, Serialize)]
#[derive(Clone, Default, Debug, Deserialize, Serialize, PartialEq)]
#[non_exhaustive]
pub struct Embed {
/// The title of the embed.
Expand Down Expand Up @@ -73,7 +73,7 @@ pub struct Embed {
///
/// [Discord docs](https://discord.com/developers/docs/resources/channel#embed-object-embed-author-structure).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[non_exhaustive]
pub struct EmbedAuthor {
/// The name of the author.
Expand All @@ -95,7 +95,7 @@ pub struct EmbedAuthor {
///
/// [Discord docs](https://discord.com/developers/docs/resources/channel#embed-object-embed-field-structure).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[non_exhaustive]
pub struct EmbedField {
/// The name of the field.
Expand Down Expand Up @@ -137,7 +137,7 @@ impl EmbedField {
///
/// [Discord docs](https://discord.com/developers/docs/resources/channel#embed-object-embed-footer-structure).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[non_exhaustive]
pub struct EmbedFooter {
/// The associated text with the footer.
Expand All @@ -156,7 +156,7 @@ pub struct EmbedFooter {
///
/// [Discord docs](https://discord.com/developers/docs/resources/channel#embed-object-embed-image-structure).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[non_exhaustive]
pub struct EmbedImage {
/// Source URL of the image.
Expand All @@ -175,7 +175,7 @@ pub struct EmbedImage {
///
/// [Discord docs](https://discord.com/developers/docs/resources/channel#embed-object-embed-provider-structure).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[non_exhaustive]
pub struct EmbedProvider {
/// The name of the provider.
Expand All @@ -188,7 +188,7 @@ pub struct EmbedProvider {
///
/// [Discord docs](https://discord.com/developers/docs/resources/channel#embed-object-embed-thumbnail-structure).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[non_exhaustive]
pub struct EmbedThumbnail {
/// The source URL of the thumbnail.
Expand All @@ -207,7 +207,7 @@ pub struct EmbedThumbnail {
///
/// [Discord docs](https://discord.com/developers/docs/resources/channel#embed-object-embed-video-structure).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[non_exhaustive]
pub struct EmbedVideo {
/// The source URL of the video.
Expand Down
11 changes: 2 additions & 9 deletions src/model/channel/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,21 +346,14 @@ impl Message {
///
/// [Manage Messages]: Permissions::MANAGE_MESSAGES
pub async fn edit(&mut self, cache_http: impl CacheHttp, builder: EditMessage) -> Result<()> {
#[cfg(feature = "cache")]
{
if let Some(cache) = cache_http.cache() {
if self.author.id != cache.current_user().id {
return Err(Error::Model(ModelError::InvalidUser));
}
}
}
if let Some(flags) = self.flags {
if flags.contains(MessageFlags::IS_VOICE_MESSAGE) {
return Err(Error::Model(ModelError::CannotEditVoiceMessage));
}
}

*self = builder.execute(cache_http, (self.channel_id, self.id)).await?;
*self =
builder.execute(cache_http, (self.channel_id, self.id, Some(self.author.id))).await?;
Ok(())
}

Expand Down