tetratto-core 12.0.1

The core behind Tetratto
Documentation
use std::collections::HashMap;

use oiseau::cache::Cache;
use crate::model::{
    Error, Result, auth::User, permissions::FinePermission,
    communities_permissions::CommunityPermission, uploads::CustomEmoji,
};
use crate::{auto_method, DataManager};

use oiseau::{PostgresRow, execute, get, query_row, query_rows, params};

impl DataManager {
    /// Get a [`CustomEmoji`] from an SQL row.
    pub(crate) fn get_emoji_from_row(x: &PostgresRow) -> CustomEmoji {
        CustomEmoji {
            id: get!(x->0(i64)) as usize,
            owner: get!(x->1(i64)) as usize,
            created: get!(x->2(i64)) as usize,
            community: get!(x->3(i64)) as usize,
            upload_id: get!(x->4(i64)) as usize,
            name: get!(x->5(String)),
            is_animated: get!(x->6(i32)) as i8 == 1,
        }
    }

    auto_method!(get_emoji_by_id(usize as i64)@get_emoji_from_row -> "SELECT * FROM emojis WHERE id = $1" --name="emoji" --returns=CustomEmoji --cache-key-tmpl="atto.emoji:{}");

    /// Get all emojis by community.
    ///
    /// # Arguments
    /// * `community` - the ID of the community to fetch emojis for
    pub async fn get_emojis_by_community(&self, community: usize) -> Result<Vec<CustomEmoji>> {
        let conn = match self.0.connect().await {
            Ok(c) => c,
            Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
        };

        let res = query_rows!(
            &conn,
            "SELECT * FROM emojis WHERE community = $1 ORDER BY name ASC",
            &[&(community as i64)],
            |x| { Self::get_emoji_from_row(x) }
        );

        if res.is_err() {
            return Err(Error::GeneralNotFound("emoji".to_string()));
        }

        Ok(res.unwrap())
    }

    /// Get all emojis by their community for the communities the given user is in.
    pub async fn get_user_emojis(
        &self,
        id: usize,
    ) -> Result<HashMap<usize, (String, Vec<CustomEmoji>)>> {
        let memberships = self.get_memberships_by_owner(id).await?;
        let mut out = HashMap::new();

        for membership in memberships {
            let community = self.get_community_by_id(membership.community).await?;

            out.insert(
                community.id,
                (
                    community.title.clone(),
                    self.get_emojis_by_community(community.id).await?,
                ),
            );
        }

        Ok(out)
    }

    /// Get an emoji by community and name.
    ///
    /// # Arguments
    /// * `community` - the ID of the community to fetch emoji from
    /// * `name` - the name of the emoji
    ///
    /// # Returns
    /// `(custom emoji, emoji string)`
    ///
    /// Custom emoji will be none if emoji string is some, and vice versa. Emoji string
    /// will only be some if the community is 0 (no community ID in parsed string, or `0.emoji_name`)
    ///
    /// Regular unicode emojis should have a community ID of 0, since they don't belong
    /// to any community and can be used by anyone.
    pub async fn get_emoji_by_community_name(
        &self,
        community: usize,
        name: &str,
    ) -> Result<(Option<CustomEmoji>, Option<String>)> {
        if community == 0 {
            return Ok((None, Some("🐇".to_string())));
        }

        // ...
        let conn = match self.0.connect().await {
            Ok(c) => c,
            Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
        };

        let res = query_row!(
            &conn,
            "SELECT * FROM emojis WHERE community = $1 AND name = $2 ORDER BY name ASC",
            params![&(community as i64), &name],
            |x| { Ok((Some(Self::get_emoji_from_row(x)), None)) }
        );

        if res.is_err() {
            return Err(Error::GeneralNotFound("emoji".to_string()));
        }

        Ok(res.unwrap())
    }

    /// Create a new emoji in the database.
    ///
    /// # Arguments
    /// * `data` - a mock [`CustomEmoji`] object to insert
    pub async fn create_emoji(&self, data: CustomEmoji) -> Result<()> {
        let user = self.get_user_by_id(data.owner).await?;

        // check if we can create animated emojis
        if !user.permissions.check(FinePermission::SUPPORTER) && data.is_animated {
            return Err(Error::RequiresSupporter);
        }

        // check user permission in community
        if data.community != 0 {
            let membership = self
                .get_membership_by_owner_community(user.id, data.community)
                .await?;

            if !membership.role.check(CommunityPermission::MANAGE_EMOJIS)
                && !user.permissions.check(FinePermission::MANAGE_EMOJIS)
            {
                return Err(Error::NotAllowed);
            }
        }

        // ...
        let conn = match self.0.connect().await {
            Ok(c) => c,
            Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
        };

        let res = execute!(
            &conn,
            "INSERT INTO emojis VALUES ($1, $2, $3, $4, $5, $6, $7)",
            params![
                &(data.id as i64),
                &(data.created as i64),
                &(data.owner as i64),
                &(data.community as i64),
                &(data.upload_id as i64),
                &data.name,
                &{ if data.is_animated { 1 } else { 0 } },
            ]
        );

        if let Err(e) = res {
            return Err(Error::DatabaseError(e.to_string()));
        }

        Ok(())
    }

    pub async fn delete_emoji(&self, id: usize, user: &User) -> Result<()> {
        let emoji = self.get_emoji_by_id(id).await?;

        // check user permission in community
        if user.id != emoji.owner {
            let membership = self
                .get_membership_by_owner_community(user.id, emoji.community)
                .await?;

            if !membership.role.check(CommunityPermission::MANAGE_EMOJIS) {
                return Err(Error::NotAllowed);
            }
        }

        // ...
        let conn = match self.0.connect().await {
            Ok(c) => c,
            Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
        };

        let res = execute!(&conn, "DELETE FROM emojis WHERE id = $1", &[&(id as i64)]);

        if let Err(e) = res {
            return Err(Error::DatabaseError(e.to_string()));
        }

        // delete upload
        self.delete_upload(emoji.upload_id).await?;

        // ...
        self.0.1.remove(format!("atto.emoji:{}", id)).await;
        Ok(())
    }

    auto_method!(update_emoji_name(&str)@get_emoji_by_id:FinePermission::MANAGE_EMOJIS; -> "UPDATE emojis SET name = $1 WHERE id = $2" --cache-key-tmpl="atto.emoji:{}");
}