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

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