revolt_database/models/channel_webhooks/ops/
reference.rs1use revolt_result::Result;
2
3use crate::ReferenceDb;
4use crate::{FieldsWebhook, PartialWebhook, Webhook};
5
6use super::AbstractWebhooks;
7
8#[async_trait]
9impl AbstractWebhooks for ReferenceDb {
10 async fn insert_webhook(&self, webhook: &Webhook) -> Result<()> {
12 let mut webhooks = self.channel_webhooks.lock().await;
13 if webhooks.contains_key(&webhook.id) {
14 Err(create_database_error!("insert", "webhook"))
15 } else {
16 webhooks.insert(webhook.id.to_string(), webhook.clone());
17 Ok(())
18 }
19 }
20
21 async fn fetch_webhook(&self, webhook_id: &str) -> Result<Webhook> {
23 let webhooks = self.channel_webhooks.lock().await;
24 webhooks
25 .get(webhook_id)
26 .cloned()
27 .ok_or_else(|| create_error!(NotFound))
28 }
29
30 async fn fetch_webhooks_for_channel(&self, channel_id: &str) -> Result<Vec<Webhook>> {
32 let webhooks = self.channel_webhooks.lock().await;
33 Ok(webhooks
34 .values()
35 .filter(|webhook| webhook.channel_id == channel_id)
36 .cloned()
37 .collect())
38 }
39
40 async fn update_webhook(
42 &self,
43 webhook_id: &str,
44 partial: &PartialWebhook,
45 remove: &[FieldsWebhook],
46 ) -> Result<()> {
47 let mut webhooks = self.channel_webhooks.lock().await;
48 if let Some(webhook) = webhooks.get_mut(webhook_id) {
49 for field in remove {
50 #[allow(clippy::disallowed_methods)]
51 webhook.remove_field(field);
52 }
53
54 webhook.apply_options(partial.clone());
55 Ok(())
56 } else {
57 Err(create_error!(NotFound))
58 }
59 }
60
61 async fn delete_webhook(&self, webhook_id: &str) -> Result<()> {
63 let mut webhooks = self.channel_webhooks.lock().await;
64 if webhooks.remove(webhook_id).is_some() {
65 Ok(())
66 } else {
67 Err(create_error!(NotFound))
68 }
69 }
70}