1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-26 23:39:22 +00:00

Can delete a custom emoji

This commit is contained in:
Pierre HUBERT 2021-01-20 18:46:21 +01:00
parent 99c7bb2899
commit 8253779725
5 changed files with 58 additions and 1 deletions

View File

@ -107,6 +107,7 @@ pub fn get_routes() -> Vec<Route> {
Route::post("/settings/delete_account_image", Box::new(settings_controller::delete_account_image)), Route::post("/settings/delete_account_image", Box::new(settings_controller::delete_account_image)),
Route::post("/settings/set_account_image_visibility", Box::new(settings_controller::set_account_image_visibility)), Route::post("/settings/set_account_image_visibility", Box::new(settings_controller::set_account_image_visibility)),
Route::post("/settings/upload_custom_emoji", Box::new(settings_controller::upload_custom_emoji)), Route::post("/settings/upload_custom_emoji", Box::new(settings_controller::upload_custom_emoji)),
Route::post("/settings/delete_custom_emoji", Box::new(settings_controller::delete_custom_emoji)),
// Friends controller // Friends controller

View File

@ -193,3 +193,12 @@ pub fn upload_custom_emoji(r: &mut HttpRequestHandler) -> RequestResult {
r.set_response(ResCreateCustomEmoji::new(emoji_id)) r.set_response(ResCreateCustomEmoji::new(emoji_id))
} }
/// Delete custom emoji
pub fn delete_custom_emoji(r: &mut HttpRequestHandler) -> RequestResult {
let emoji = r.post_emoji_id("emojiID")?;
custom_emojies_helper::delete(&emoji)?;
r.success("Emoji deleted.")
}

View File

@ -2,7 +2,10 @@
//! //!
//! @author Pierre Hubert //! @author Pierre Hubert
use std::path::PathBuf;
use crate::data::user::UserID; use crate::data::user::UserID;
use crate::utils::user_data_utils::user_data_path;
#[derive(Debug)] #[derive(Debug)]
pub struct CustomEmoji { pub struct CustomEmoji {
@ -11,3 +14,10 @@ pub struct CustomEmoji {
pub shortcut: String, pub shortcut: String,
pub path: String, pub path: String,
} }
impl CustomEmoji {
/// Get the system path pointing on this custom emoji
pub fn sys_path(&self) -> PathBuf {
user_data_path(self.path.as_ref())
}
}

View File

@ -15,12 +15,13 @@ use crate::controllers::routes::RequestResult;
use crate::data::api_client::APIClient; use crate::data::api_client::APIClient;
use crate::data::comment::Comment; use crate::data::comment::Comment;
use crate::data::config::conf; use crate::data::config::conf;
use crate::data::custom_emoji::CustomEmoji;
use crate::data::error::{ExecError, ResultBoxError}; use crate::data::error::{ExecError, ResultBoxError};
use crate::data::group::GroupAccessLevel; use crate::data::group::GroupAccessLevel;
use crate::data::group_id::GroupID; use crate::data::group_id::GroupID;
use crate::data::post::{Post, PostAccessLevel}; use crate::data::post::{Post, PostAccessLevel};
use crate::data::user::UserID; use crate::data::user::UserID;
use crate::helpers::{account_helper, api_helper, comments_helper, conversations_helper, friends_helper, groups_helper, movies_helper, posts_helper, user_helper, virtual_directory_helper}; use crate::helpers::{account_helper, api_helper, comments_helper, conversations_helper, custom_emojies_helper, friends_helper, groups_helper, movies_helper, posts_helper, user_helper, virtual_directory_helper};
use crate::helpers::virtual_directory_helper::VirtualDirType; use crate::helpers::virtual_directory_helper::VirtualDirType;
use crate::utils::pdf_utils::is_valid_pdf; use crate::utils::pdf_utils::is_valid_pdf;
use crate::utils::string_utils::{check_emoji_code, check_string_before_insert, check_url, remove_html_nodes}; use crate::utils::string_utils::{check_emoji_code, check_string_before_insert, check_url, remove_html_nodes};
@ -682,4 +683,20 @@ impl HttpRequestHandler {
Ok(emoji_shortcut) Ok(emoji_shortcut)
} }
/// Get information about an emoji included in a POST request
pub fn post_emoji_id(&mut self, field: &str) -> ResultBoxError<CustomEmoji> {
let emoji_id = self.post_u64(field)?;
let info = self.ok_or_not_found(
custom_emojies_helper::get_single(emoji_id),
"Requested emoji not found!"
)?;
if &info.user_id != self.user_id_ref()? {
self.forbidden("You do not own this emoji!".to_string())?;
}
Ok(info)
}
} }

View File

@ -16,6 +16,13 @@ pub fn get_list_user(user_id: &UserID) -> ResultBoxError<Vec<CustomEmoji>> {
.exec(db_to_custom_emoji) .exec(db_to_custom_emoji)
} }
/// Get information about a single emoji
pub fn get_single(emoji_id: u64) -> ResultBoxError<CustomEmoji> {
database::QueryInfo::new(EMOJIS_TABLE)
.cond_u64("id", emoji_id)
.query_row(db_to_custom_emoji)
}
/// Check if a given user already as an emoji shortcut or not /// Check if a given user already as an emoji shortcut or not
pub fn has_user_similar_shortcut(user_id: &UserID, shortcut: &str) -> ResultBoxError<bool> { pub fn has_user_similar_shortcut(user_id: &UserID, shortcut: &str) -> ResultBoxError<bool> {
database::QueryInfo::new(EMOJIS_TABLE) database::QueryInfo::new(EMOJIS_TABLE)
@ -34,6 +41,19 @@ pub fn insert(emoji: &NewCustomEmoji) -> ResultBoxError<u64> {
.insert_expect_result() .insert_expect_result()
} }
/// Delete a custom emoji
pub fn delete(c: &CustomEmoji) -> ResultBoxError {
let path = c.sys_path();
if path.exists() {
std::fs::remove_file(path)?;
}
database::DeleteQuery::new(EMOJIS_TABLE)
.cond_u64("id", c.id)
.exec()
}
/// Turn a database entry into a [CustomEmoji] /// Turn a database entry into a [CustomEmoji]
fn db_to_custom_emoji(row: &database::RowResult) -> ResultBoxError<CustomEmoji> { fn db_to_custom_emoji(row: &database::RowResult) -> ResultBoxError<CustomEmoji> {
Ok(CustomEmoji { Ok(CustomEmoji {