From 8693c1e5568de1d2fd493e09b2cb7765933caaa6 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 3 Apr 2020 18:51:00 +0200 Subject: [PATCH] Can get information about an emoji included in a POST request --- src/entities/BaseRequestsHandler.ts | 24 +++++++++++++++++++++--- src/helpers/CustomEmojisHelper.ts | 19 +++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/entities/BaseRequestsHandler.ts b/src/entities/BaseRequestsHandler.ts index 91c7b96..06339ca 100644 --- a/src/entities/BaseRequestsHandler.ts +++ b/src/entities/BaseRequestsHandler.ts @@ -16,6 +16,8 @@ import { CommentsHelper } from "../helpers/CommentsHelper"; import { checkVirtualDirectory } from "../utils/VirtualDirsUtils"; import { ConversationsHelper } from "../helpers/ConversationsHelper"; import { AbstractUserConnectionContainer } from "./UserConnectionContainer"; +import { CustomEmoji } from "./CustomEmoji"; +import { CustomEmojisHelper } from "../helpers/CustomEmojisHelper"; export abstract class BaseRequestsHandler implements AbstractUserConnectionContainer { @@ -412,11 +414,27 @@ export abstract class BaseRequestsHandler implements AbstractUserConnectionConta * @param name The name of the POST field containing the id of the emoji */ public postEmojiShorcut(name: string) : string { - const emojiID = this.postString(name); + const emojiShorcut = this.postString(name); - if(!checkEmojiCode(emojiID)) + if(!checkEmojiCode(emojiShorcut)) this.error(401, "Invalid emoji specified at " + name + "!") - return emojiID; + return emojiShorcut; + } + + /** + * Get information about an emoji included in a POST request + * + * @param name The name of the post field containing emoji ID + */ + public async postEmojiID(name: string) : Promise { + const eomjiID = this.postInt(name); + + const info = await CustomEmojisHelper.GetSingle(eomjiID); + + if(info.userID != this.getUserId()) + this.error(401, "You are not allowed to modify this emoji!"); + + return info; } } \ No newline at end of file diff --git a/src/helpers/CustomEmojisHelper.ts b/src/helpers/CustomEmojisHelper.ts index 0a95b4b..5b6d48f 100644 --- a/src/helpers/CustomEmojisHelper.ts +++ b/src/helpers/CustomEmojisHelper.ts @@ -42,6 +42,25 @@ export class CustomEmojisHelper { })).map((e) => this.DbToCustomEmoji(e)); } + /** + * Get information about a single emoji + * + * @param emojiID The ID of the emoji to get + * @throws An error if the emoji was not found + */ + public static async GetSingle(emojiID: number) : Promise { + const row = await DatabaseHelper.QueryRow({ + table: EMOJIS_TABLE, + where: { + id: emojiID + } + }); + + if(row == null) + throw new Error("Requested emoji was not found ! (emoji id " + emojiID + ")") + + return this.DbToCustomEmoji(row); + } /** * Turn a database entry into a custom emoji