) => {
- const value = event.target.value
- .toLowerCase()
- .replace(/[^a-z0-9_]/g, "")
- .slice(0, 32);
+ const { value } = event.target;
setName(value);
};
- const isValid = name.trim().length > 0 && file;
+ const isValidName = EmojiValidation.allowedNameCharacters.test(name);
+ const isValid = name.trim().length > 0 && file && isValidName;
return (
@@ -115,17 +121,19 @@ export function EmojiCreateDialog({ onSubmit }: Props) {
{file ? (
<>
- {file.name}
- {t("Click or drag to replace")}
+ {file.name}
+
+ {t("Click or drag to replace")}
+
>
) : (
<>
-
+
{isDragActive
? t("Drop the image here")
: t("Click or drag an image here")}
-
+
{t("PNG, JPG, GIF, or WebP up to 1MB")}
>
@@ -133,10 +141,12 @@ export function EmojiCreateDialog({ onSubmit }: Props) {
- {name && (
-
- {t("This emoji will be available as")} :{name}:
-
+ {name.trim() && isValidName && (
+
+
+ {t("This emoji will be available as")} :{name}:
+
+
)}
);
diff --git a/app/hooks/useSettingsConfig.ts b/app/hooks/useSettingsConfig.ts
index c6d1213002..ca8ea82578 100644
--- a/app/hooks/useSettingsConfig.ts
+++ b/app/hooks/useSettingsConfig.ts
@@ -165,8 +165,8 @@ const useSettingsConfig = () => {
icon: ShapesIcon,
},
{
- name: t("Custom Emojis"),
- path: settingsPath("custom-emojis"),
+ name: t("Emojis"),
+ path: settingsPath("emojis"),
component: CustomEmojis.Component,
preload: CustomEmojis.preload,
enabled: can.update,
diff --git a/app/models/Emoji.ts b/app/models/Emoji.ts
index 2fbabae3fa..e5f44200e7 100644
--- a/app/models/Emoji.ts
+++ b/app/models/Emoji.ts
@@ -1,4 +1,4 @@
-import { observable } from "mobx";
+import { computed, observable } from "mobx";
import User from "./User";
import Model from "./base/Model";
import Field from "./decorators/Field";
@@ -10,7 +10,7 @@ class Emoji extends Model {
/** The name of the emoji */
@Field
@observable
- name: string;
+ private _name: string;
/** The URL of the emoji image */
@Field
@@ -26,6 +26,18 @@ class Emoji extends Model {
@Field
@observable
createdById: string;
+
+ /**
+ * emoji name
+ */
+ @computed
+ get name() {
+ return this._name;
+ }
+
+ set name(value: string) {
+ this._name = value;
+ }
}
export default Emoji;
diff --git a/app/scenes/Settings/CustomEmojis.tsx b/app/scenes/Settings/CustomEmojis.tsx
index c73ae41e63..3045281f42 100644
--- a/app/scenes/Settings/CustomEmojis.tsx
+++ b/app/scenes/Settings/CustomEmojis.tsx
@@ -37,7 +37,7 @@ function Emojis() {
const reqParams = useMemo(
() => ({
query: params.get("query") || undefined,
- sort: params.get("sort") || "name",
+ sort: params.get("sort") || "createdAt",
direction: (params.get("direction") || "asc").toUpperCase() as
| "ASC"
| "DESC",
@@ -86,7 +86,7 @@ function Emojis() {
useEffect(() => {
if (error) {
- toast.error(t("Could not load emojis"));
+ toast.error(t("Could not load emoji"));
}
}, [t, error]);
@@ -112,7 +112,7 @@ function Emojis() {
context={context}
icon={}
>
- {t("Add emoji")}…
+ {t("New emoji")}…
)}
diff --git a/app/scenes/Settings/components/EmojisTable.tsx b/app/scenes/Settings/components/EmojisTable.tsx
index 5b7d874ca7..7f77782331 100644
--- a/app/scenes/Settings/components/EmojisTable.tsx
+++ b/app/scenes/Settings/components/EmojisTable.tsx
@@ -33,7 +33,7 @@ const EmojisTable = observer(function EmojisTable({
compact([
{
type: "data",
- id: "emoji",
+ id: "name",
header: t("Emoji"),
accessor: (emoji) => emoji.url,
component: (emoji) => (
@@ -67,10 +67,7 @@ const EmojisTable = observer(function EmojisTable({
id: "createdAt",
header: t("Date added"),
accessor: (emoji) => emoji.createdAt,
- component: (emoji) =>
- emoji.createdAt ? (
-
- ) : null,
+ component: (emoji) => ,
width: "1fr",
},
// to do: emoji menu
diff --git a/server/models/Emoji.ts b/server/models/Emoji.ts
index 72f30786a2..230deed2af 100644
--- a/server/models/Emoji.ts
+++ b/server/models/Emoji.ts
@@ -5,6 +5,7 @@ import {
} from "sequelize";
import {
BeforeCreate,
+ BeforeDestroy,
BelongsTo,
Column,
DataType,
@@ -19,6 +20,9 @@ import IdModel from "./base/IdModel";
import Fix from "./decorators/Fix";
import IsUrlOrRelativePath from "./validators/IsUrlOrRelativePath";
import Length from "./validators/Length";
+import { Matches } from "class-validator";
+import FileStorage from "@server/storage/files";
+import Attachment from "./Attachment";
@Table({ tableName: "emojis", modelName: "emoji" })
@Fix
@@ -30,6 +34,10 @@ class Emoji extends IdModel<
max: EmojiValidation.maxNameLength,
msg: `emoji name must be less than ${EmojiValidation.maxNameLength} characters`,
})
+ @Matches(EmojiValidation.allowedNameCharacters, {
+ message:
+ "emoji name can only contain lowercase letters, numbers, and underscores",
+ })
@Column(DataType.STRING)
name: string;
@@ -70,6 +78,19 @@ class Emoji extends IdModel<
throw ValidationError(`Emoji with name "${model.name}" already exists.`);
}
}
+
+ @BeforeDestroy
+ static async deleteAttachmentFromS3(model: Emoji) {
+ const attachment = await Attachment.findOne({
+ where: {
+ key: model.url,
+ },
+ });
+
+ if (attachment) {
+ await FileStorage.deleteFile(attachment.key);
+ }
+ }
}
export default Emoji;
diff --git a/server/routes/api/emojis/emojis.ts b/server/routes/api/emojis/emojis.ts
index a895522cc6..25b3082e65 100644
--- a/server/routes/api/emojis/emojis.ts
+++ b/server/routes/api/emojis/emojis.ts
@@ -122,10 +122,11 @@ router.post(
createdById: user.id,
createdBy: user,
});
+ emoji.createdBy = user;
ctx.body = {
- data: presentEmoji(emoji!),
- policies: presentPolicies(user, [emoji!]),
+ data: presentEmoji(emoji),
+ policies: presentPolicies(user, [emoji]),
};
}
);
@@ -138,10 +139,12 @@ router.post(
async (ctx: APIContext) => {
const { id } = ctx.input.body;
const { user } = ctx.state.auth;
+ const { transaction } = ctx.state;
const emoji = await Emoji.findByPk(id, {
transaction: ctx.state.transaction,
rejectOnEmpty: true,
+ lock: transaction.LOCK.UPDATE,
});
authorize(user, "delete", emoji);
diff --git a/shared/i18n/locales/en_US/translation.json b/shared/i18n/locales/en_US/translation.json
index 3c6d290e40..0012dbb026 100644
--- a/shared/i18n/locales/en_US/translation.json
+++ b/shared/i18n/locales/en_US/translation.json
@@ -113,8 +113,8 @@
"You have left the shared document": "You have left the shared document",
"Could not leave document": "Could not leave document",
"Apply template": "Apply template",
- "Add emoji": "Add emoji",
- "Add custom emoji": "Add custom emoji",
+ "New emoji": "New emoji",
+ "Upload emoji": "Upload emoji",
"Disconnect analytics": "Disconnect analytics",
"Home": "Home",
"Drafts": "Drafts",
@@ -263,8 +263,10 @@
"Emoji created successfully": "Emoji created successfully",
"Failed to create emoji": "Failed to create emoji",
"Uploading": "Uploading",
+ "Add emoji": "Add emoji",
"Upload an image to create a custom emoji. The name should be unique and contain only lowercase letters, numbers, and underscores.": "Upload an image to create a custom emoji. The name should be unique and contain only lowercase letters, numbers, and underscores.",
"Emoji name": "Emoji name",
+ "name can only should lowercase letters, numbers, and underscores.": "name can only should lowercase letters, numbers, and underscores.",
"Click or drag to replace": "Click or drag to replace",
"Drop the image here": "Drop the image here",
"Click or drag an image here": "Click or drag an image here",
@@ -599,7 +601,6 @@
"Security": "Security",
"Features": "Features",
"Members": "Members",
- "Custom Emojis": "Custom Emojis",
"API Keys": "API Keys",
"Applications": "Applications",
"Shared Links": "Shared Links",
@@ -1071,7 +1072,7 @@
"Editors": "Editors",
"All status": "All status",
"Active": "Active",
- "Could not load emojis": "Could not load emojis",
+ "Could not load emoji": "Could not load emoji",
"Custom emojis can be used throughout your workspace in documents, comments, and reactions.": "Custom emojis can be used throughout your workspace in documents, comments, and reactions.",
"Left": "Left",
"Right": "Right",
diff --git a/shared/validations.ts b/shared/validations.ts
index 708b215196..ded7a4c943 100644
--- a/shared/validations.ts
+++ b/shared/validations.ts
@@ -133,4 +133,6 @@ export const WebhookSubscriptionValidation = {
export const EmojiValidation = {
/** The maximum length of the emoji name */
maxNameLength: 25,
+ /* the allow characters in the name */
+ allowedNameCharacters: /^[a-z0-9_]*$/,
};