From 131735a5f0534581a17695e3c6c462137e03a2bd Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 3 Apr 2020 16:20:08 +0200 Subject: [PATCH] Create first custom emoji --- src/controllers/Routes.ts | 2 ++ src/controllers/SettingsController.ts | 25 +++++++++++++++++++++ src/entities/BaseRequestsHandler.ts | 16 +++++++++++++- src/entities/CustomEmoji.ts | 26 ++++++++++++++++++++++ src/helpers/CustomEmojisHelper.ts | 31 +++++++++++++++++++++++++++ src/utils/StringUtils.ts | 9 ++++++++ 6 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 src/entities/CustomEmoji.ts create mode 100644 src/helpers/CustomEmojisHelper.ts diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index e5da1b6..977b622 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -117,6 +117,8 @@ export const Routes : Route[] = [ {path: "/settings/set_account_image_visibility", cb: (h) => SettingsController.SetAccountImageVisibility(h)}, + {path: "/settings/upload_custom_emoji", cb: (h) => SettingsController.UploadCustomEmoji(h)}, + // Friends controller {path: "/friends/getList", cb: (h) => FriendsController.GetList(h)}, diff --git a/src/controllers/SettingsController.ts b/src/controllers/SettingsController.ts index 9d0c4f9..61dc803 100644 --- a/src/controllers/SettingsController.ts +++ b/src/controllers/SettingsController.ts @@ -13,6 +13,8 @@ import { AccountHelper } from "../helpers/AccountHelper"; import { AccountImageVisibilityLevel } from "../entities/AccountImage"; import { AccountImageHelper } from "../helpers/AccountImageHelper"; import { findKey } from "../utils/ArrayUtils"; +import { CustomEmojisHelper } from "../helpers/CustomEmojisHelper"; +import { CustomEmoji } from "../entities/CustomEmoji"; /** * API account image visibility levels @@ -260,4 +262,27 @@ export class SettingsController { h.success(); } + + /** + * Upload a custom emoji to the server + * + * @param h Request handler + */ + public static async UploadCustomEmoji(h: RequestHandler) { + const shorcut = h.postEmojiShorcut("shorcut"); + const path = await h.savePostImage("image", "custom_emojies", 72, 72); + + // Create the emoji + const emojiID = await CustomEmojisHelper.Insert(new CustomEmoji({ + id: -1, + userID: h.getUserId(), + shorcut: shorcut, + path: path + })) + + h.send({ + emojiID: emojiID + }) + + } } \ No newline at end of file diff --git a/src/entities/BaseRequestsHandler.ts b/src/entities/BaseRequestsHandler.ts index faba6bd..91c7b96 100644 --- a/src/entities/BaseRequestsHandler.ts +++ b/src/entities/BaseRequestsHandler.ts @@ -5,7 +5,7 @@ */ import { UserHelper } from "../helpers/UserHelper"; -import { removeHTMLNodes, checkMail, checkURL } from "../utils/StringUtils"; +import { removeHTMLNodes, checkMail, checkURL, checkEmojiCode } from "../utils/StringUtils"; import { FriendsHelper } from "../helpers/FriendsHelper"; import { AccountHelper } from "../helpers/AccountHelper"; import { GroupsHelper } from "../helpers/GroupsHelper"; @@ -405,4 +405,18 @@ export abstract class BaseRequestsHandler implements AbstractUserConnectionConta return convID; } + + /** + * Get the shorcut of an emoji included in a POST request + * + * @param name The name of the POST field containing the id of the emoji + */ + public postEmojiShorcut(name: string) : string { + const emojiID = this.postString(name); + + if(!checkEmojiCode(emojiID)) + this.error(401, "Invalid emoji specified at " + name + "!") + + return emojiID; + } } \ No newline at end of file diff --git a/src/entities/CustomEmoji.ts b/src/entities/CustomEmoji.ts new file mode 100644 index 0000000..76864b9 --- /dev/null +++ b/src/entities/CustomEmoji.ts @@ -0,0 +1,26 @@ +/** + * Custom smiley + * + * @author Pierre Hubert + */ + +export interface CustomEmojiBuilder { + id: number, + userID: number, + shorcut: string, + path: string, +} + +export class CustomEmoji implements CustomEmojiBuilder { + id: number; + userID: number; + shorcut: string; + path: string; + + public constructor(info: CustomEmojiBuilder) { + for (const key in info) { + if (info.hasOwnProperty(key)) + this[key] = info[key]; + } + } +} \ No newline at end of file diff --git a/src/helpers/CustomEmojisHelper.ts b/src/helpers/CustomEmojisHelper.ts new file mode 100644 index 0000000..2d10a35 --- /dev/null +++ b/src/helpers/CustomEmojisHelper.ts @@ -0,0 +1,31 @@ +/** + * Custom emojies helper + * + * @author Pierre Hubert + */ + +import { CustomEmoji } from "../entities/CustomEmoji"; +import { DatabaseHelper } from "./DatabaseHelper"; + +const EMOJIS_TABLE = "comunic_custom_emojis"; + +export class CustomEmojisHelper { + + /** + * Insert a new custom emoji into the database + * + * @param e The emoji to insert + * @return The ID of the created emoji + */ + public static async Insert(e: CustomEmoji) : Promise { + return await DatabaseHelper.InsertRow( + EMOJIS_TABLE, + { + user_id: e.userID, + shorcut: e.shorcut, + path: e.path + } + ) + } + +} \ No newline at end of file diff --git a/src/utils/StringUtils.ts b/src/utils/StringUtils.ts index 064d5b9..91aca4e 100644 --- a/src/utils/StringUtils.ts +++ b/src/utils/StringUtils.ts @@ -75,4 +75,13 @@ export function check_youtube_id(s: string) : boolean { && !s.includes(".") && !s.includes("'") && !s.includes("\"") +} + +/** + * Check an emoji code + * + * @param s The emoji code to check + */ +export function checkEmojiCode(s: string) : boolean { + return s.match(/^:[a-zA-Z0-9]*:$/) != null } \ No newline at end of file