revolt_database/models/channel_webhooks/ops/
mongodb.rs1use futures::StreamExt;
2use revolt_result::Result;
3
4use crate::{FieldsWebhook, PartialWebhook, Webhook};
5use crate::{IntoDocumentPath, MongoDb};
6
7use super::AbstractWebhooks;
8
9static COL: &str = "channel_webhooks";
10
11#[async_trait]
12impl AbstractWebhooks for MongoDb {
13 async fn insert_webhook(&self, webhook: &Webhook) -> Result<()> {
15 query!(self, insert_one, COL, &webhook).map(|_| ())
16 }
17
18 async fn fetch_webhook(&self, webhook_id: &str) -> Result<Webhook> {
20 query!(self, find_one_by_id, COL, webhook_id)?.ok_or_else(|| create_error!(NotFound))
21 }
22
23 async fn fetch_webhooks_for_channel(&self, channel_id: &str) -> Result<Vec<Webhook>> {
25 Ok(self
26 .col::<Webhook>(COL)
27 .find(doc! {
28 "channel_id": channel_id,
29 })
30 .await
31 .map_err(|_| create_database_error!("find", COL))?
32 .filter_map(|s| async {
33 if cfg!(debug_assertions) {
34 Some(s.unwrap())
35 } else {
36 s.ok()
37 }
38 })
39 .collect()
40 .await)
41 }
42
43 async fn update_webhook(
45 &self,
46 webhook_id: &str,
47 partial: &PartialWebhook,
48 remove: &[FieldsWebhook],
49 ) -> Result<()> {
50 query!(
51 self,
52 update_one_by_id,
53 COL,
54 webhook_id,
55 partial,
56 remove.iter().map(|x| x as &dyn IntoDocumentPath).collect(),
57 None
58 )
59 .map(|_| ())
60 }
61
62 async fn delete_webhook(&self, webhook_id: &str) -> Result<()> {
64 query!(self, delete_one_by_id, COL, webhook_id).map(|_| ())
65 }
66}
67
68impl IntoDocumentPath for FieldsWebhook {
69 fn as_path(&self) -> Option<&'static str> {
70 Some(match self {
71 FieldsWebhook::Avatar => "avatar",
72 })
73 }
74}