From 96ab83af7bf86f948678195ae865fb176c087672 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 3 Apr 2020 18:55:50 +0200 Subject: [PATCH] Can delete an emoji --- src/controllers/Routes.ts | 2 ++ src/controllers/SettingsController.ts | 13 +++++++++++++ src/entities/CustomEmoji.ts | 4 ++++ src/helpers/CustomEmojisHelper.ts | 17 +++++++++++++++++ 4 files changed, 36 insertions(+) diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index 977b622..374486a 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -119,6 +119,8 @@ export const Routes : Route[] = [ {path: "/settings/upload_custom_emoji", cb: (h) => SettingsController.UploadCustomEmoji(h)}, + {path: "/settings/delete_custom_emoji", cb: (h) => SettingsController.DeleteCustomEmoji(h)}, + // Friends controller {path: "/friends/getList", cb: (h) => FriendsController.GetList(h)}, diff --git a/src/controllers/SettingsController.ts b/src/controllers/SettingsController.ts index 299f4ed..dd02d22 100644 --- a/src/controllers/SettingsController.ts +++ b/src/controllers/SettingsController.ts @@ -286,6 +286,19 @@ export class SettingsController { } + /** + * Delete a custom emoji of the user + * + * @param h Request handler + */ + public static async DeleteCustomEmoji(h: RequestHandler) { + const emoji = await h.postEmojiID("emojiID"); + + await CustomEmojisHelper.Delete(emoji); + + h.success() + } + /** * Convert an emoji into an API entry * diff --git a/src/entities/CustomEmoji.ts b/src/entities/CustomEmoji.ts index d5964a5..5bc883e 100644 --- a/src/entities/CustomEmoji.ts +++ b/src/entities/CustomEmoji.ts @@ -29,4 +29,8 @@ export class CustomEmoji implements CustomEmojiBuilder { get url() : string { return pathUserData(this.path, false); } + + get sysPath() : string { + return pathUserData(this.path, true); + } } \ No newline at end of file diff --git a/src/helpers/CustomEmojisHelper.ts b/src/helpers/CustomEmojisHelper.ts index 5b6d48f..cb7b3cc 100644 --- a/src/helpers/CustomEmojisHelper.ts +++ b/src/helpers/CustomEmojisHelper.ts @@ -6,6 +6,7 @@ import { CustomEmoji } from "../entities/CustomEmoji"; import { DatabaseHelper } from "./DatabaseHelper"; +import { existsSync, unlinkSync } from "fs"; const EMOJIS_TABLE = "comunic_custom_emojis"; @@ -62,6 +63,22 @@ export class CustomEmojisHelper { return this.DbToCustomEmoji(row); } + /** + * Delete a custom emoji + * + * @param e Information about the emoji to delete + */ + public static async Delete(e: CustomEmoji) { + + if(existsSync(e.sysPath)) + unlinkSync(e.sysPath) + + await DatabaseHelper.DeleteRows(EMOJIS_TABLE, { + id: e.id + }) + + } + /** * Turn a database entry into a custom emoji *