revolt_database/models/channel_invites/ops/
mongodb.rs

1use futures::StreamExt;
2use revolt_result::Result;
3
4use crate::Invite;
5use crate::MongoDb;
6
7use super::AbstractChannelInvites;
8
9static COL: &str = "channel_invites";
10
11#[async_trait]
12impl AbstractChannelInvites for MongoDb {
13    /// Insert a new invite into the database
14    async fn insert_invite(&self, invite: &Invite) -> Result<()> {
15        query!(self, insert_one, COL, &invite).map(|_| ())
16    }
17
18    /// Fetch an invite by the code
19    async fn fetch_invite(&self, code: &str) -> Result<Invite> {
20        query!(self, find_one_by_id, COL, code)?.ok_or_else(|| create_error!(NotFound))
21    }
22
23    /// Fetch all invites for a server
24    async fn fetch_invites_for_server(&self, server_id: &str) -> Result<Vec<Invite>> {
25        Ok(self
26            .col::<Invite>(COL)
27            .find(doc! {
28                "server": server_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    /// Delete an invite by its code
44    async fn delete_invite(&self, code: &str) -> Result<()> {
45        query!(self, delete_one_by_id, COL, code).map(|_| ())
46    }
47}