Skip to content

Commit

Permalink
add reaction_exists db check
Browse files Browse the repository at this point in the history
  • Loading branch information
jay3332 committed Jul 1, 2024
1 parent 6637598 commit 9a8ad72
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/db/emoji.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,28 @@ pub trait EmojiDbExt<'t>: DbExt<'t> {
Ok(())
}

/// Returns whether the given emoji is already an existing reaction on the given message.
async fn reaction_exists(&self, message_id: u64, emoji: PartialEmoji) -> crate::Result<bool> {
let exists = sqlx::query!(
"SELECT EXISTS(
SELECT 1 FROM reactions
WHERE
message_id = $1
AND emoji_id IS NOT DISTINCT FROM $2
AND emoji_name = $3
) AS exists",
message_id as i64,
emoji.id.map(|id| id as i64),
emoji.name,
)
.fetch_one(self.executor())
.await?
.exists
.unwrap_or(false);

Ok(exists)
}

/// Fetches all reactions from the message with the given ID.
async fn fetch_reactions(&self, message_id: u64) -> crate::Result<Vec<Reaction>> {
let reactions = sqlx::query!(
Expand Down
9 changes: 9 additions & 0 deletions src/models/emoji.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ pub struct PartialEmoji {
pub name: String,
}

impl From<CustomEmoji> for PartialEmoji {
fn from(emoji: CustomEmoji) -> Self {
Self {
id: Some(emoji.id),
name: emoji.name,
}
}
}

/// Represents a reaction on a message.
#[derive(Clone, Debug, Serialize, PartialEq, Eq)]
#[cfg_attr(feature = "client", derive(Deserialize))]
Expand Down

0 comments on commit 9a8ad72

Please sign in to comment.