diff --git a/src/controllers/routes.rs b/src/controllers/routes.rs index 943dc79..69ea1bf 100644 --- a/src/controllers/routes.rs +++ b/src/controllers/routes.rs @@ -107,6 +107,7 @@ pub fn get_routes() -> Vec { 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/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 diff --git a/src/controllers/settings_controller.rs b/src/controllers/settings_controller.rs index daae350..37ccecf 100644 --- a/src/controllers/settings_controller.rs +++ b/src/controllers/settings_controller.rs @@ -192,4 +192,13 @@ pub fn upload_custom_emoji(r: &mut HttpRequestHandler) -> RequestResult { })?; 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.") } \ No newline at end of file diff --git a/src/data/custom_emoji.rs b/src/data/custom_emoji.rs index 58ac825..fa8d4d8 100644 --- a/src/data/custom_emoji.rs +++ b/src/data/custom_emoji.rs @@ -2,7 +2,10 @@ //! //! @author Pierre Hubert +use std::path::PathBuf; + use crate::data::user::UserID; +use crate::utils::user_data_utils::user_data_path; #[derive(Debug)] pub struct CustomEmoji { @@ -10,4 +13,11 @@ pub struct CustomEmoji { pub user_id: UserID, pub shortcut: 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()) + } } \ No newline at end of file diff --git a/src/data/http_request_handler.rs b/src/data/http_request_handler.rs index 6fe8d11..c991929 100644 --- a/src/data/http_request_handler.rs +++ b/src/data/http_request_handler.rs @@ -15,12 +15,13 @@ use crate::controllers::routes::RequestResult; use crate::data::api_client::APIClient; use crate::data::comment::Comment; use crate::data::config::conf; +use crate::data::custom_emoji::CustomEmoji; use crate::data::error::{ExecError, ResultBoxError}; use crate::data::group::GroupAccessLevel; use crate::data::group_id::GroupID; use crate::data::post::{Post, PostAccessLevel}; 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::utils::pdf_utils::is_valid_pdf; 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) } + + /// Get information about an emoji included in a POST request + pub fn post_emoji_id(&mut self, field: &str) -> ResultBoxError { + 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) + } } \ No newline at end of file diff --git a/src/helpers/custom_emojies_helper.rs b/src/helpers/custom_emojies_helper.rs index a896512..3dc4dae 100644 --- a/src/helpers/custom_emojies_helper.rs +++ b/src/helpers/custom_emojies_helper.rs @@ -16,6 +16,13 @@ pub fn get_list_user(user_id: &UserID) -> ResultBoxError> { .exec(db_to_custom_emoji) } +/// Get information about a single emoji +pub fn get_single(emoji_id: u64) -> ResultBoxError { + 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 pub fn has_user_similar_shortcut(user_id: &UserID, shortcut: &str) -> ResultBoxError { database::QueryInfo::new(EMOJIS_TABLE) @@ -34,6 +41,19 @@ pub fn insert(emoji: &NewCustomEmoji) -> ResultBoxError { .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] fn db_to_custom_emoji(row: &database::RowResult) -> ResultBoxError { Ok(CustomEmoji {