refactor(quark): strip webhook code

This commit is contained in:
Paul Makles
2023-06-03 13:01:28 +01:00
parent e393e17b59
commit f9f5a30e2c
16 changed files with 43 additions and 259 deletions

View File

@@ -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" }

View File

@@ -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<FieldsWebhook>
remove: Vec<FieldsWebhook>,
},
/// Delete webhook
WebhookDelete {
id: String
},
WebhookDelete { id: String },
/// New report
ReportCreate(Report),

View File

@@ -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<Webhook> {
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<Vec<Webhook>> {
info!("fetching webhooks for channel {channel}");
Ok(vec![self.fetch_webhook("0").await?])
}
}

View File

@@ -14,7 +14,6 @@ pub mod channels {
pub mod channel_invite;
pub mod channel_unread;
pub mod message;
pub mod webhook;
}
pub mod servers {

View File

@@ -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<Message> {
pub async fn send_message(
&self,
db: &Database,
data: DataMessageSend,
author: MessageAuthor<'_>,
mut idempotency: IdempotencyKey,
) -> Result<Message> {
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?,
);
}
}

View File

@@ -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<FieldsWebhook>
) -> 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)
}
}
}

View File

@@ -10,7 +10,6 @@ pub mod channels {
pub mod channel_invite;
pub mod channel_unread;
pub mod message;
pub mod webhook;
}
pub mod servers {

View File

@@ -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<Webhook> {
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<Vec<Webhook>> {
self.find(
COL,
doc! {
"channel": channel
})
.await
}
}
impl IntoDocumentPath for FieldsWebhook {
fn as_path(&self) -> Option<&'static str> {
match self {
FieldsWebhook::Avatar => Some("avatar"),
}
}
}

View File

@@ -25,7 +25,6 @@ pub mod channels {
pub mod channel_invite;
pub mod channel_unread;
pub mod message;
pub mod webhook;
}
pub mod servers {

View File

@@ -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<String>
}
/// 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<MessageWebhook>,
/// Message content

View File

@@ -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<File>,
/// The channel this webhook belongs to
pub channel: String,
/// The private token for the webhook
pub token: Option<String>
}
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone)]
pub enum FieldsWebhook {
Avatar
}

View File

@@ -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;

View File

@@ -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<Webhook>;
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<Vec<Webhook>>;
}

View File

@@ -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
{

View File

@@ -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<MessageAuthor<'_>>, 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())
}

View File

@@ -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<Webhook> {
db.fetch_webhook(&self.id).await
}
/// Fetch report from Ref
pub async fn as_report(&self, db: &Database) -> Result<Report> {
db.fetch_report(&self.id).await