From 7b39f4e9d9aa7fe9417a98b6f9227349b3cf113a Mon Sep 17 00:00:00 2001 From: Zomatree Date: Sat, 1 Apr 2023 22:49:34 +0100 Subject: [PATCH] change how webhook information is in the webhook --- crates/delta/src/routes/channels/message_delete.rs | 2 +- crates/delta/src/routes/channels/message_edit.rs | 2 +- crates/delta/src/routes/channels/webhook_create.rs | 2 +- crates/delta/src/routes/safety/report_content.rs | 2 +- .../src/routes/webhooks/webhook_delete_token.rs | 2 +- .../src/routes/webhooks/webhook_edit_token.rs | 2 +- .../delta/src/routes/webhooks/webhook_execute.rs | 2 +- .../src/routes/webhooks/webhook_execute_github.rs | 5 +++-- .../src/routes/webhooks/webhook_fetch_token.rs | 2 +- crates/quark/src/impl/dummy/channels/message.rs | 2 +- crates/quark/src/impl/dummy/channels/webhook.rs | 2 +- crates/quark/src/impl/generic/channels/channel.rs | 14 ++++++++------ crates/quark/src/impl/generic/channels/message.rs | 14 +++++--------- crates/quark/src/models/channels/message.rs | 9 ++++----- crates/quark/src/models/channels/webhook.rs | 2 +- 15 files changed, 31 insertions(+), 33 deletions(-) diff --git a/crates/delta/src/routes/channels/message_delete.rs b/crates/delta/src/routes/channels/message_delete.rs index 917d1367..6a427316 100644 --- a/crates/delta/src/routes/channels/message_delete.rs +++ b/crates/delta/src/routes/channels/message_delete.rs @@ -11,7 +11,7 @@ pub async fn req(db: &Db, user: User, target: Ref, msg: Ref) -> Result name: data.name, avatar, channel: channel.id().to_string(), - token: nanoid::nanoid!(64) + token: Some(nanoid::nanoid!(64)) }; webhook.create(db).await?; diff --git a/crates/delta/src/routes/safety/report_content.rs b/crates/delta/src/routes/safety/report_content.rs index 4321374e..295b4e2b 100644 --- a/crates/delta/src/routes/safety/report_content.rs +++ b/crates/delta/src/routes/safety/report_content.rs @@ -42,7 +42,7 @@ pub async fn report_content(db: &Db, user: User, data: Json) let message = db.fetch_message(id).await?; // Users cannot report themselves - if message.author.as_ref() == Some(&user.id) { + if message.author == user.id { return Err(Error::CannotReportYourself); } diff --git a/crates/delta/src/routes/webhooks/webhook_delete_token.rs b/crates/delta/src/routes/webhooks/webhook_delete_token.rs index 7f9bca11..2161ac24 100644 --- a/crates/delta/src/routes/webhooks/webhook_delete_token.rs +++ b/crates/delta/src/routes/webhooks/webhook_delete_token.rs @@ -8,7 +8,7 @@ use revolt_quark::{Db, Ref, Result, EmptyResponse, Error}; pub async fn req(db: &Db, target: Ref, token: String) -> Result { let webhook = target.as_webhook(db).await?; - (webhook.token == token) + (webhook.token.as_deref() == Some(&token)) .then_some(()) .ok_or(Error::InvalidCredentials)?; diff --git a/crates/delta/src/routes/webhooks/webhook_edit_token.rs b/crates/delta/src/routes/webhooks/webhook_edit_token.rs index efb18e4a..f99ff3c9 100644 --- a/crates/delta/src/routes/webhooks/webhook_edit_token.rs +++ b/crates/delta/src/routes/webhooks/webhook_edit_token.rs @@ -27,7 +27,7 @@ pub async fn req(db: &Db, target: Ref, token: String, data: Json Result { pub async fn req(db: &Db, target: Ref, token: String, event: EventHeader<'_>, data: String) -> Result<()> { let webhook = target.as_webhook(db).await?; - (webhook.token == token) + (webhook.token.as_deref() == Some(&token)) .then_some(()) .ok_or(Error::InvalidCredentials)?; @@ -978,9 +978,10 @@ pub async fn req(db: &Db, target: Ref, token: String, event: EventHeader<'_>, da let mut message = Message { id: message_id, + author: webhook.id.clone(), channel: webhook.channel.clone(), embeds: Some(vec![embed]), - webhook: Some(webhook.id.clone()), + webhook: Some(webhook.clone()), ..Default::default() }; diff --git a/crates/delta/src/routes/webhooks/webhook_fetch_token.rs b/crates/delta/src/routes/webhooks/webhook_fetch_token.rs index 6472ba0e..4c346ffc 100644 --- a/crates/delta/src/routes/webhooks/webhook_fetch_token.rs +++ b/crates/delta/src/routes/webhooks/webhook_fetch_token.rs @@ -9,7 +9,7 @@ use rocket::serde::json::Json; pub async fn req(db: &Db, target: Ref, token: String) -> Result> { let webhook = target.as_webhook(db).await?; - (webhook.token == token) + (webhook.token.as_deref() == Some(&token)) .then_some(()) .ok_or(Error::InvalidCredentials)?; diff --git a/crates/quark/src/impl/dummy/channels/message.rs b/crates/quark/src/impl/dummy/channels/message.rs index 4547c4ad..a01a32a1 100644 --- a/crates/quark/src/impl/dummy/channels/message.rs +++ b/crates/quark/src/impl/dummy/channels/message.rs @@ -9,7 +9,7 @@ impl AbstractMessage for DummyDb { Ok(Message { id: id.into(), channel: "channel".into(), - author: Some("author".into()), + author: "author".into(), content: Some("message content".into()), ..Default::default() diff --git a/crates/quark/src/impl/dummy/channels/webhook.rs b/crates/quark/src/impl/dummy/channels/webhook.rs index 13bcfb2c..c22b579b 100644 --- a/crates/quark/src/impl/dummy/channels/webhook.rs +++ b/crates/quark/src/impl/dummy/channels/webhook.rs @@ -22,7 +22,7 @@ impl AbstractWebhook for DummyDb { name: "".to_string(), avatar: None, channel: "0".to_string(), - token: "".to_owned(), + token: Some("".to_owned()), }) } diff --git a/crates/quark/src/impl/generic/channels/channel.rs b/crates/quark/src/impl/generic/channels/channel.rs index e89d81e9..00e3ae2c 100644 --- a/crates/quark/src/impl/generic/channels/channel.rs +++ b/crates/quark/src/impl/generic/channels/channel.rs @@ -428,6 +428,11 @@ impl Channel { } } + let (author_id, webhook) = match &author { + MessageAuthor::User(user) => (user.id.clone(), None), + MessageAuthor::Webhook(webhook) => (webhook.id.clone(), Some((*webhook).clone())) + }; + // Start constructing the message let message_id = Ulid::new().to_string(); let mut message = Message { @@ -435,14 +440,11 @@ impl Channel { channel: self.id().to_string(), masquerade: data.masquerade, interactions: data.interactions.unwrap_or_default(), + author: author_id, + webhook, ..Default::default() }; - match &author { - MessageAuthor::User(user) => message.author = Some(user.id.clone()), - MessageAuthor::Webhook(webhook) => message.webhook = Some(webhook.id.clone()), - } - // Parse mentions in message. let mut mentions = HashSet::new(); if let Some(content) = &data.content { @@ -464,7 +466,7 @@ impl Channel { let message = Ref::from_unchecked(id).as_message(db).await?; if mention { - mentions.insert(message.author_id().to_owned()); + mentions.insert(message.author.to_owned()); } replies.insert(message.id); diff --git a/crates/quark/src/impl/generic/channels/message.rs b/crates/quark/src/impl/generic/channels/message.rs index 6b1e5e20..2e66ea45 100644 --- a/crates/quark/src/impl/generic/channels/message.rs +++ b/crates/quark/src/impl/generic/channels/message.rs @@ -272,12 +272,8 @@ impl Message { db.clear_reaction(&self.id, emoji).await } - /// Gets either the user or the webhook id which sent this message - pub fn author_id(&self) -> &str { - match &self.author { - Some(id) => id, - None => self.webhook.as_deref().unwrap() - } + pub fn is_webhook(&self) -> bool { + self.webhook.is_some() } } @@ -289,8 +285,8 @@ impl IntoUsers for Message { fn get_user_ids(&self) -> Vec { let mut ids = Vec::new(); - if let Some(author) = &self.author { - ids.push(author.clone()); + if !self.is_webhook() { + ids.push(self.author.clone()); }; if let Some(msg) = &self.system { @@ -331,7 +327,7 @@ impl SystemMessage { Message { id: Ulid::new().to_string(), channel, - author: Some("00000000000000000000000000".to_string()), + author: "00000000000000000000000000".to_string(), system: Some(self), ..Default::default() diff --git a/crates/quark/src/models/channels/message.rs b/crates/quark/src/models/channels/message.rs index 167732d5..12c20deb 100644 --- a/crates/quark/src/models/channels/message.rs +++ b/crates/quark/src/models/channels/message.rs @@ -1,4 +1,4 @@ -use crate::util::regex::RE_COLOUR; +use crate::{util::regex::RE_COLOUR, models::Webhook}; use indexmap::{IndexMap, IndexSet}; use iso8601_timestamp::Timestamp; @@ -120,12 +120,11 @@ pub struct Message { pub nonce: Option, /// Id of the channel this message was sent in pub channel: String, - /// Id of the user that sent this message, will be none when a webhook sent the message - #[serde(skip_serializing_if = "Option::is_none")] - pub author: Option, + /// Id of the user or webhook that sent this message + pub author: String, /// Id of the webhook that sent this message, mutually exclusive with `author` #[serde(skip_serializing_if = "Option::is_none")] - pub webhook: Option, + pub webhook: Option, /// Message content #[serde(skip_serializing_if = "Option::is_none")] pub content: Option, diff --git a/crates/quark/src/models/channels/webhook.rs b/crates/quark/src/models/channels/webhook.rs index b97ea52e..5f3153e0 100644 --- a/crates/quark/src/models/channels/webhook.rs +++ b/crates/quark/src/models/channels/webhook.rs @@ -24,7 +24,7 @@ pub struct Webhook { pub channel: String, /// The private token for the webhook - pub token: String + pub token: Option } #[derive(Serialize, Deserialize, JsonSchema, Debug, Clone)]