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

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