From f9f5a30e2c7720fd5606932090f749dfb77337a0 Mon Sep 17 00:00:00 2001 From: Paul Makles Date: Sat, 3 Jun 2023 13:01:28 +0100 Subject: [PATCH] refactor(quark): strip webhook code --- crates/quark/Cargo.toml | 3 +- crates/quark/src/events/client.rs | 10 ++- .../quark/src/impl/dummy/channels/webhook.rs | 40 ----------- crates/quark/src/impl/dummy/mod.rs | 1 - .../src/impl/generic/channels/channel.rs | 36 +++++++--- .../src/impl/generic/channels/webhook.rs | 72 ------------------- crates/quark/src/impl/generic/mod.rs | 1 - .../quark/src/impl/mongo/channels/webhook.rs | 57 --------------- crates/quark/src/impl/mongo/mod.rs | 1 - crates/quark/src/models/channels/message.rs | 12 +--- crates/quark/src/models/channels/webhook.rs | 33 --------- crates/quark/src/models/mod.rs | 2 - crates/quark/src/traits/channels/webhook.rs | 11 --- crates/quark/src/traits/mod.rs | 3 - crates/quark/src/types/push.rs | 13 ++-- crates/quark/src/util/ref.rs | 7 +- 16 files changed, 43 insertions(+), 259 deletions(-) delete mode 100644 crates/quark/src/impl/dummy/channels/webhook.rs delete mode 100644 crates/quark/src/impl/generic/channels/webhook.rs delete mode 100644 crates/quark/src/impl/mongo/channels/webhook.rs delete mode 100644 crates/quark/src/models/channels/webhook.rs delete mode 100644 crates/quark/src/traits/channels/webhook.rs diff --git a/crates/quark/Cargo.toml b/crates/quark/Cargo.toml index 36204037..da6a6e85 100644 --- a/crates/quark/Cargo.toml +++ b/crates/quark/Cargo.toml @@ -28,7 +28,7 @@ default = [ "test" ] # Serialisation revolt_optional_struct = "0.2.0" serde = { version = "1", features = ["derive"] } -validator = { version = "0.14", features = ["derive"] } +validator = { version = "0.16", features = ["derive"] } iso8601-timestamp = { version = "0.1.8", features = ["schema", "bson"] } # Formats @@ -93,3 +93,4 @@ sentry = "0.25.0" # Core revolt-result = { path = "../core/result", features = [ "serde", "schemas" ] } revolt-presence = { path = "../core/presence", features = [ "redis-is-patched" ] } +revolt-models = { path = "../core/models" } diff --git a/crates/quark/src/events/client.rs b/crates/quark/src/events/client.rs index 25c4d585..219a4641 100644 --- a/crates/quark/src/events/client.rs +++ b/crates/quark/src/events/client.rs @@ -1,13 +1,13 @@ use authifier::AuthifierEvent; +use revolt_models::v0::{FieldsWebhook, PartialWebhook, Webhook}; use serde::{Deserialize, Serialize}; use crate::models::channel::{FieldsChannel, PartialChannel}; -use crate::models::webhook::{PartialWebhook, FieldsWebhook}; use crate::models::message::{AppendMessage, PartialMessage}; use crate::models::server::{FieldsRole, FieldsServer, PartialRole, PartialServer}; use crate::models::server_member::{FieldsMember, MemberCompositeKey, PartialMember}; use crate::models::user::{FieldsUser, PartialUser, RelationshipStatus}; -use crate::models::{Channel, Emoji, Member, Message, Server, User, UserSettings, Webhook, Report}; +use crate::models::{Channel, Emoji, Member, Message, Report, Server, User, UserSettings}; use crate::Error; /// WebSocket Client Errors @@ -222,13 +222,11 @@ pub enum EventV1 { WebhookUpdate { id: String, data: PartialWebhook, - remove: Vec + remove: Vec, }, /// Delete webhook - WebhookDelete { - id: String - }, + WebhookDelete { id: String }, /// New report ReportCreate(Report), diff --git a/crates/quark/src/impl/dummy/channels/webhook.rs b/crates/quark/src/impl/dummy/channels/webhook.rs deleted file mode 100644 index 63917732..00000000 --- a/crates/quark/src/impl/dummy/channels/webhook.rs +++ /dev/null @@ -1,40 +0,0 @@ -use crate::models::webhook::{Webhook, FieldsWebhook, PartialWebhook}; -use crate::{AbstractWebhook, Result}; - -use super::super::DummyDb; - -#[async_trait] -impl AbstractWebhook for DummyDb { - async fn insert_webhook(&self, webhook: &Webhook) -> Result<()> { - info!("Created webhook {} in {}", webhook.name, webhook.channel); - Ok(()) - } - - async fn delete_webhook(&self, webhook_id: &str) -> Result<()> { - info!("deleting webhook {webhook_id}"); - - Ok(()) - } - - async fn fetch_webhook(&self, webhook_id: &str) -> Result { - Ok(Webhook { - id: webhook_id.to_string(), - name: "".to_string(), - avatar: None, - channel: "0".to_string(), - token: Some("".to_owned()), - }) - } - - async fn update_webhook(&self, webhook_id: &str, partial_webhook: &PartialWebhook, remove: &[FieldsWebhook]) -> Result<()> { - info!("updating webhook {webhook_id}, {partial_webhook:?}, removing {remove:?}"); - - Ok(()) - } - - async fn fetch_webhooks_for_channel(&self, channel: &str) -> Result> { - info!("fetching webhooks for channel {channel}"); - - Ok(vec![self.fetch_webhook("0").await?]) - } -} diff --git a/crates/quark/src/impl/dummy/mod.rs b/crates/quark/src/impl/dummy/mod.rs index 400b55bf..194f2edb 100644 --- a/crates/quark/src/impl/dummy/mod.rs +++ b/crates/quark/src/impl/dummy/mod.rs @@ -14,7 +14,6 @@ pub mod channels { pub mod channel_invite; pub mod channel_unread; pub mod message; - pub mod webhook; } pub mod servers { diff --git a/crates/quark/src/impl/generic/channels/channel.rs b/crates/quark/src/impl/generic/channels/channel.rs index 0f27c02a..7c650f5a 100644 --- a/crates/quark/src/impl/generic/channels/channel.rs +++ b/crates/quark/src/impl/generic/channels/channel.rs @@ -6,11 +6,14 @@ use crate::{ events::client::EventV1, models::{ channel::{FieldsChannel, PartialChannel}, - message::{SystemMessage, Message, DataMessageSend, Reply, RE_MENTION}, + message::{DataMessageSend, Message, Reply, SystemMessage, RE_MENTION}, Channel, }, tasks::{ack::AckEvent, process_embeds}, - Database, Error, OverrideField, Result, types::push::MessageAuthor, web::idempotency::IdempotencyKey, Ref, variables::delta::{MAX_ATTACHMENT_COUNT, MAX_REPLY_COUNT}, + types::push::MessageAuthor, + variables::delta::{MAX_ATTACHMENT_COUNT, MAX_REPLY_COUNT}, + web::idempotency::IdempotencyKey, + Database, Error, OverrideField, Ref, Result, }; impl Channel { @@ -400,7 +403,13 @@ impl Channel { } /// Creates a message in a channel - pub async fn send_message(&self, db: &Database, data: DataMessageSend, author: MessageAuthor<'_>, mut idempotency: IdempotencyKey) -> Result { + pub async fn send_message( + &self, + db: &Database, + data: DataMessageSend, + author: MessageAuthor<'_>, + mut idempotency: IdempotencyKey, + ) -> Result { Message::validate_sum(&data.content, &data.embeds)?; idempotency.consume_nonce(data.nonce).await?; @@ -430,7 +439,7 @@ impl Channel { let (author_id, webhook) = match &author { MessageAuthor::User(user) => (user.id.clone(), None), - MessageAuthor::Webhook(webhook) => (webhook.id.clone(), Some((*webhook).clone())) + MessageAuthor::Webhook(webhook) => (webhook.id.clone(), Some((*webhook).clone())), }; // Start constructing the message @@ -441,7 +450,7 @@ impl Channel { masquerade: data.masquerade, interactions: data.interactions.unwrap_or_default(), author: author_id, - webhook: webhook.map(|w| w.into_message_webhook()), + webhook: webhook.map(|w| w.into()), ..Default::default() }; @@ -459,7 +468,9 @@ impl Channel { let mut replies = HashSet::new(); if let Some(entries) = data.replies { if entries.len() > *MAX_REPLY_COUNT { - return Err(Error::TooManyReplies { max: *MAX_REPLY_COUNT }); + return Err(Error::TooManyReplies { + max: *MAX_REPLY_COUNT, + }); } for Reply { id, mention } in entries { @@ -499,13 +510,20 @@ impl Channel { let mut attachments = vec![]; if let Some(ids) = &data.attachments { if ids.len() > *MAX_ATTACHMENT_COUNT { - return Err(Error::TooManyAttachments { max: *MAX_ATTACHMENT_COUNT} ); + return Err(Error::TooManyAttachments { + max: *MAX_ATTACHMENT_COUNT, + }); } for attachment_id in ids { attachments.push( - db.find_and_use_attachment(attachment_id, "attachments", "message", &message_id) - .await?, + db.find_and_use_attachment( + attachment_id, + "attachments", + "message", + &message_id, + ) + .await?, ); } } diff --git a/crates/quark/src/impl/generic/channels/webhook.rs b/crates/quark/src/impl/generic/channels/webhook.rs deleted file mode 100644 index c830c0ad..00000000 --- a/crates/quark/src/impl/generic/channels/webhook.rs +++ /dev/null @@ -1,72 +0,0 @@ -use crate::{ - events::client::EventV1, - models::{ - webhook::{Webhook, PartialWebhook, FieldsWebhook}, - message::MessageWebhook - }, - Database, Result -}; - -impl Webhook { - pub async fn create(&self, db: &Database) -> Result<()> { - db.insert_webhook(self).await?; - - // Avoid leaking the token to people who receive the event - let mut webhook = self.clone(); - webhook.token = None; - - EventV1::WebhookCreate(webhook) - .p(self.channel.clone()).await; - - Ok(()) - } - - pub async fn delete(&self, db: &Database) -> Result<()> { - db.delete_webhook(&self.id).await?; - - EventV1::WebhookDelete { id: self.id.clone() } - .p(self.channel.clone()).await; - - Ok(()) - } - - pub async fn update( - &mut self, - db: &Database, - mut partial: PartialWebhook, - remove: Vec - ) -> Result<()> { - for field in &remove { - self.remove(field) - }; - - self.apply_options(partial.clone()); - - db.update_webhook(&self.id, &partial, &remove).await?; - - partial.token = None; // Avoid leaking the token to people who receive the event - - EventV1::WebhookUpdate { - id: self.id.clone(), - data: partial, - remove - } - .p(self.channel.clone()) - .await; - - Ok(()) - } - - pub fn remove(&mut self, field: &FieldsWebhook) { - match field { - FieldsWebhook::Avatar => self.avatar = None - } - } - - pub fn into_message_webhook(self) -> MessageWebhook { - MessageWebhook { - name: self.name, - avatar: self.avatar.map(|f| f.id) - } - } -} diff --git a/crates/quark/src/impl/generic/mod.rs b/crates/quark/src/impl/generic/mod.rs index 17bde1bc..b050cae5 100644 --- a/crates/quark/src/impl/generic/mod.rs +++ b/crates/quark/src/impl/generic/mod.rs @@ -10,7 +10,6 @@ pub mod channels { pub mod channel_invite; pub mod channel_unread; pub mod message; - pub mod webhook; } pub mod servers { diff --git a/crates/quark/src/impl/mongo/channels/webhook.rs b/crates/quark/src/impl/mongo/channels/webhook.rs deleted file mode 100644 index b7657b3d..00000000 --- a/crates/quark/src/impl/mongo/channels/webhook.rs +++ /dev/null @@ -1,57 +0,0 @@ -use crate::models::{webhook::{Webhook, PartialWebhook, FieldsWebhook}}; -use crate::r#impl::mongo::IntoDocumentPath; -use crate::{Result, AbstractWebhook}; - -use super::super::MongoDb; - -static COL: &str = "webhooks"; - -#[async_trait] -impl AbstractWebhook for MongoDb { - async fn insert_webhook(&self, webhook: &Webhook) -> Result<()> { - self.insert_one(COL, webhook).await?; - - Ok(()) - } - - async fn fetch_webhook(&self, webhook_id: &str) -> Result { - info!("{COL} {webhook_id}"); - - self.find_one_by_id(COL, webhook_id).await - } - - async fn delete_webhook(&self, webhook_id: &str) -> Result<()> { - self.delete_one_by_id(COL, webhook_id).await?; - - Ok(()) - } - - async fn update_webhook(&self, webhook_id: &str, partial_webhook: &PartialWebhook, remove: &[FieldsWebhook]) -> Result<()> { - self.update_one_by_id( - COL, - webhook_id, - partial_webhook, - remove.iter().map(|x| x as &dyn IntoDocumentPath).collect(), - None, - ) - .await - .map(|_| ()) - } - - async fn fetch_webhooks_for_channel(&self, channel: &str) -> Result> { - self.find( - COL, - doc! { - "channel": channel - }) - .await - } -} - -impl IntoDocumentPath for FieldsWebhook { - fn as_path(&self) -> Option<&'static str> { - match self { - FieldsWebhook::Avatar => Some("avatar"), - } - } -} diff --git a/crates/quark/src/impl/mongo/mod.rs b/crates/quark/src/impl/mongo/mod.rs index 02c265a9..7711ab0f 100644 --- a/crates/quark/src/impl/mongo/mod.rs +++ b/crates/quark/src/impl/mongo/mod.rs @@ -25,7 +25,6 @@ pub mod channels { pub mod channel_invite; pub mod channel_unread; pub mod message; - pub mod webhook; } pub mod servers { diff --git a/crates/quark/src/models/channels/message.rs b/crates/quark/src/models/channels/message.rs index c9c74f59..917e35d1 100644 --- a/crates/quark/src/models/channels/message.rs +++ b/crates/quark/src/models/channels/message.rs @@ -4,6 +4,7 @@ use indexmap::{IndexMap, IndexSet}; use iso8601_timestamp::Timestamp; use once_cell::sync::Lazy; use regex::Regex; +use revolt_models::v0::MessageWebhook; use serde::{Deserialize, Serialize}; use validator::Validate; @@ -109,15 +110,6 @@ pub struct Interactions { pub restrict_reactions: bool, } -/// Information about the webhook which -#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, Validate, Default)] -pub struct MessageWebhook { - // The name of the webhook - 1 to 32 chars - pub name: String, - // The id of the avatar of the webhook, if it has one - pub avatar: Option -} - /// Representation of a Message on Revolt #[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, OptionalStruct, Default)] #[optional_derive(Serialize, Deserialize, JsonSchema, Debug, Default, Clone)] @@ -135,7 +127,7 @@ pub struct Message { pub channel: String, /// Id of the user or webhook that sent this message pub author: String, - /// The webhook that sent this message, mutually exclusive with `author` + /// The webhook that sent this message #[serde(skip_serializing_if = "Option::is_none")] pub webhook: Option, /// Message content diff --git a/crates/quark/src/models/channels/webhook.rs b/crates/quark/src/models/channels/webhook.rs deleted file mode 100644 index 5f3153e0..00000000 --- a/crates/quark/src/models/channels/webhook.rs +++ /dev/null @@ -1,33 +0,0 @@ -use crate::models::File; -use serde::{Deserialize, Serialize}; - -/// Respresents a webhook -#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, OptionalStruct)] -#[optional_derive(Serialize, Deserialize, JsonSchema, Debug, Default, Clone)] -#[optional_name = "PartialWebhook"] -#[opt_skip_serializing_none] -#[opt_some_priority] - -pub struct Webhook { - /// Unique Id - #[serde(rename = "_id")] - pub id: String, - - /// The name of the webhook - pub name: String, - - /// The avatar of the webhook - #[serde(skip_serializing_if = "Option::is_none")] - pub avatar: Option, - - /// The channel this webhook belongs to - pub channel: String, - - /// The private token for the webhook - pub token: Option -} - -#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone)] -pub enum FieldsWebhook { - Avatar -} diff --git a/crates/quark/src/models/mod.rs b/crates/quark/src/models/mod.rs index 85dffc29..370c72a1 100644 --- a/crates/quark/src/models/mod.rs +++ b/crates/quark/src/models/mod.rs @@ -13,7 +13,6 @@ mod channels { pub mod channel_invite; pub mod channel_unread; pub mod message; - pub mod webhook; } mod servers { @@ -55,4 +54,3 @@ pub use simple::SimpleModel; pub use snapshot::Snapshot; pub use user::User; pub use user_settings::UserSettings; -pub use webhook::Webhook; diff --git a/crates/quark/src/traits/channels/webhook.rs b/crates/quark/src/traits/channels/webhook.rs deleted file mode 100644 index bee30c5b..00000000 --- a/crates/quark/src/traits/channels/webhook.rs +++ /dev/null @@ -1,11 +0,0 @@ -use crate::models::webhook::{Webhook, PartialWebhook, FieldsWebhook}; -use crate::Result; - -#[async_trait] -pub trait AbstractWebhook: Sync + Send { - async fn insert_webhook(&self, webhook: &Webhook) -> Result<()>; - async fn fetch_webhook(&self, webhook_id: &str) -> Result; - async fn delete_webhook(&self, webhook_id: &str) -> Result<()>; - async fn update_webhook(&self, webhook_id: &str, partial_webhook: &PartialWebhook, remove: &[FieldsWebhook]) -> Result<()>; - async fn fetch_webhooks_for_channel(&self, channel: &str) -> Result>; -} diff --git a/crates/quark/src/traits/mod.rs b/crates/quark/src/traits/mod.rs index 4b252e08..341565a1 100644 --- a/crates/quark/src/traits/mod.rs +++ b/crates/quark/src/traits/mod.rs @@ -12,7 +12,6 @@ mod channels { pub mod channel_invite; pub mod channel_unread; pub mod message; - pub mod webhook; } mod servers { @@ -41,7 +40,6 @@ pub use channels::channel::AbstractChannel; pub use channels::channel_invite::AbstractChannelInvite; pub use channels::channel_unread::AbstractChannelUnread; pub use channels::message::AbstractMessage; -pub use channels::webhook::AbstractWebhook; pub use servers::server::AbstractServer; pub use servers::server_ban::AbstractServerBan; @@ -70,7 +68,6 @@ pub trait AbstractDatabase: + AbstractBot + AbstractUser + AbstractUserSettings - + AbstractWebhook + AbstractReport + AbstractSnapshot { diff --git a/crates/quark/src/types/push.rs b/crates/quark/src/types/push.rs index 5a2a299e..6384b6d1 100644 --- a/crates/quark/src/types/push.rs +++ b/crates/quark/src/types/push.rs @@ -1,8 +1,9 @@ use std::time::SystemTime; +use revolt_models::v0::Webhook; use serde::{Deserialize, Serialize}; -use crate::models::{Message, User, Webhook, File}; +use crate::models::{Message, User}; use crate::variables::delta::{APP_URL, AUTUMN_URL, PUBLIC_URL}; /// Push Notification @@ -27,7 +28,7 @@ pub struct PushNotification { pub enum MessageAuthor<'a> { User(&'a User), - Webhook(&'a Webhook) + Webhook(&'a Webhook), } impl<'a> MessageAuthor<'a> { @@ -38,10 +39,10 @@ impl<'a> MessageAuthor<'a> { } } - pub fn avatar(&self) -> Option<&File> { + pub fn avatar(&self) -> Option<&str> { match self { - MessageAuthor::User(user) => user.avatar.as_ref(), - MessageAuthor::Webhook(webhook) => webhook.avatar.as_ref(), + MessageAuthor::User(user) => user.avatar.as_ref().map(|file| file.id.as_str()), + MessageAuthor::Webhook(webhook) => webhook.avatar.as_ref().map(|file| file.id.as_str()), } } @@ -58,7 +59,7 @@ impl PushNotification { pub fn new(msg: Message, author: Option>, channel_id: &str) -> Self { let icon = if let Some(author) = &author { if let Some(avatar) = author.avatar() { - format!("{}/avatars/{}", &*AUTUMN_URL, avatar.id) + format!("{}/avatars/{}", &*AUTUMN_URL, avatar) } else { format!("{}/users/{}/default_avatar", &*PUBLIC_URL, author.id()) } diff --git a/crates/quark/src/util/ref.rs b/crates/quark/src/util/ref.rs index df78f52b..1b3ff148 100644 --- a/crates/quark/src/util/ref.rs +++ b/crates/quark/src/util/ref.rs @@ -6,7 +6,7 @@ use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use crate::models::{ - Bot, Channel, Emoji, Invite, Member, Message, Report, Server, ServerBan, User, Webhook + Bot, Channel, Emoji, Invite, Member, Message, Report, Server, ServerBan, User, }; use crate::{Database, Error, Result}; @@ -81,11 +81,6 @@ impl Ref { db.fetch_emoji(&self.id).await } - /// Fetches webhook from Ref - pub async fn as_webhook(&self, db: &Database) -> Result { - db.fetch_webhook(&self.id).await - } - /// Fetch report from Ref pub async fn as_report(&self, db: &Database) -> Result { db.fetch_report(&self.id).await