1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-22 21:39:21 +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/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

View File

@ -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.")
}

View File

@ -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())
}
}

View File

@ -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<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)
}
/// 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
pub fn has_user_similar_shortcut(user_id: &UserID, shortcut: &str) -> ResultBoxError<bool> {
database::QueryInfo::new(EMOJIS_TABLE)
@ -34,6 +41,19 @@ pub fn insert(emoji: &NewCustomEmoji) -> ResultBoxError<u64> {
.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<CustomEmoji> {
Ok(CustomEmoji {