1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-11-21 21:09:22 +00:00

Create first custom emoji

This commit is contained in:
Pierre HUBERT 2020-04-03 16:20:08 +02:00
parent 2465b0c1ac
commit 131735a5f0
6 changed files with 108 additions and 1 deletions

View File

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

View File

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

View File

@ -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;
}
}

View File

@ -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];
}
}
}

View File

@ -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<number> {
return await DatabaseHelper.InsertRow(
EMOJIS_TABLE,
{
user_id: e.userID,
shorcut: e.shorcut,
path: e.path
}
)
}
}

View File

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