use super::*; use crate::cache::Cache; use crate::model::{ Error, Result, auth::User, permissions::FinePermission, communities_permissions::CommunityPermission, channels::Channel, }; use crate::{auto_method, execute, get, query_row, query_rows, params}; #[cfg(feature = "sqlite")] use rusqlite::Row; #[cfg(feature = "postgres")] use tokio_postgres::Row; impl DataManager { /// Get a [`Channel`] from an SQL row. pub(crate) fn get_channel_from_row( #[cfg(feature = "sqlite")] x: &Row<'_>, #[cfg(feature = "postgres")] x: &Row, ) -> Channel { Channel { id: get!(x->0(i64)) as usize, community: get!(x->1(i64)) as usize, owner: get!(x->2(i64)) as usize, created: get!(x->3(i64)) as usize, minimum_role_read: get!(x->4(i32)) as u32, minimum_role_write: get!(x->5(i32)) as u32, position: get!(x->6(i32)) as usize, } } auto_method!(get_channel_by_id(usize)@get_channel_from_row -> "SELECT * FROM channels WHERE id = $1" --name="channel" --returns=Channel --cache-key-tmpl="atto.channel:{}"); /// Get all channels by user. /// /// # Arguments /// * `community` - the ID of the community to fetch channels for pub async fn get_channels_by_community(&self, community: usize) -> Result> { let conn = match self.connect().await { Ok(c) => c, Err(e) => return Err(Error::DatabaseConnection(e.to_string())), }; let res = query_rows!( &conn, "SELECT * FROM channels WHERE community = $1 ORDER BY position DESC", &[&(community as i64)], |x| { Self::get_channel_from_row(x) } ); if res.is_err() { return Err(Error::GeneralNotFound("channel".to_string())); } Ok(res.unwrap()) } /// Create a new channel in the database. /// /// # Arguments /// * `data` - a mock [`Channel`] object to insert pub async fn create_channel(&self, data: Channel) -> Result<()> { let user = self.get_user_by_id(data.owner).await?; // check user permission in community let membership = self .get_membership_by_owner_community(user.id, data.community) .await?; if !membership.role.check(CommunityPermission::MANAGE_CHANNELS) && !user.permissions.check(FinePermission::MANAGE_CHANNELS) { return Err(Error::NotAllowed); } // ... let conn = match self.connect().await { Ok(c) => c, Err(e) => return Err(Error::DatabaseConnection(e.to_string())), }; let res = execute!( &conn, "INSERT INTO channels VALUES ($1, $2, $3, $4, $5, $6, $7)", params![ &(data.id as i64), &(data.community as i64), &(data.owner as i64), &(data.created as i64), &(data.minimum_role_read as i32), &(data.minimum_role_write as i32), &(data.position as i32) ] ); if let Err(e) = res { return Err(Error::DatabaseError(e.to_string())); } Ok(()) } pub async fn delete_channel(&self, id: usize, user: User) -> Result<()> { let channel = self.get_channel_by_id(id).await?; // check user permission in community let membership = self .get_membership_by_owner_community(user.id, channel.community) .await?; if !membership.role.check(CommunityPermission::MANAGE_CHANNELS) { return Err(Error::NotAllowed); } // ... let conn = match self.connect().await { Ok(c) => c, Err(e) => return Err(Error::DatabaseConnection(e.to_string())), }; let res = execute!(&conn, "DELETE FROM channels WHERE id = $1", &[&(id as i64)]); if let Err(e) = res { return Err(Error::DatabaseError(e.to_string())); } self.2.remove(format!("atto.channel:{}", id)).await; Ok(()) } auto_method!(update_channel_position(i32)@get_channel_by_id:MANAGE_COMMUNITIES -> "UPDATE channels SET position = $1 WHERE id = $2" --cache-key-tmpl="atto.channel:{}"); auto_method!(update_channel_minimum_role_read(i32)@get_channel_by_id:MANAGE_COMMUNITIES -> "UPDATE channels SET minimum_role_read = $1 WHERE id = $2" --cache-key-tmpl="atto.channel:{}"); auto_method!(update_channel_minimum_role_write(i32)@get_channel_by_id:MANAGE_COMMUNITIES -> "UPDATE channels SET minimum_role_write = $1 WHERE id = $2" --cache-key-tmpl="atto.channel:{}"); }