diff --git a/app/components/EmojiCreateDialog.tsx b/app/components/EmojiCreateDialog.tsx index 62068f0805..cd9f16dbee 100644 --- a/app/components/EmojiCreateDialog.tsx +++ b/app/components/EmojiCreateDialog.tsx @@ -64,7 +64,7 @@ export function EmojiCreateDialog({ onSubmit }: Props) { const emoji = await emojis.create({ name: name.trim(), - url: attachment.url, + attachmentId: attachment.id, }); emojis.add(emoji); diff --git a/app/models/Emoji.ts b/app/models/Emoji.ts index 03e33a2e57..d0b7db065d 100644 --- a/app/models/Emoji.ts +++ b/app/models/Emoji.ts @@ -17,6 +17,11 @@ class Emoji extends Model { @observable url: string; + /** The ID */ + @Field + @observable + attachmentId: string; + /** The user who created this emoji */ @Relation(() => User) @observable diff --git a/server/migrations/20251020204139-create-emojis.js b/server/migrations/20251020204139-create-emojis.js index f388d90cca..05637afdfa 100644 --- a/server/migrations/20251020204139-create-emojis.js +++ b/server/migrations/20251020204139-create-emojis.js @@ -16,9 +16,14 @@ module.exports = { type: Sequelize.STRING, allowNull: false, }, - url: { - type: Sequelize.STRING, + attachmentId: { + type: Sequelize.UUID, allowNull: false, + references: { + model: "attachments", + key: "id", + }, + onDelete: "CASCADE", }, teamId: { type: Sequelize.UUID, @@ -52,6 +57,9 @@ module.exports = { await queryInterface.addIndex("emojis", ["teamId"], { transaction }); await queryInterface.addIndex("emojis", ["createdById"], { transaction }); + await queryInterface.addIndex("emojis", ["attachmentId"], { + transaction, + }); await queryInterface.addIndex("emojis", ["teamId", "name"], { unique: true, transaction, diff --git a/server/models/Emoji.ts b/server/models/Emoji.ts index 230deed2af..075aa84d4b 100644 --- a/server/models/Emoji.ts +++ b/server/models/Emoji.ts @@ -18,7 +18,6 @@ import Team from "./Team"; import User from "./User"; 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"; @@ -32,7 +31,7 @@ class Emoji extends IdModel< > { @Length({ max: EmojiValidation.maxNameLength, - msg: `emoji name must be less than ${EmojiValidation.maxNameLength} characters`, + msg: `emoji name must be less than ${EmojiValidation.maxNameLength} characters`, }) @Matches(EmojiValidation.allowedNameCharacters, { message: @@ -41,11 +40,14 @@ class Emoji extends IdModel< @Column(DataType.STRING) name: string; - @IsUrlOrRelativePath - @Column(DataType.STRING) - url: string; - // associations + @BelongsTo(() => Attachment, "attachmentId") + attachment: Attachment; + + @ForeignKey(() => Attachment) + @Column(DataType.UUID) + attachmentId: string; + @BelongsTo(() => Team, "teamId") team: Team; @@ -81,12 +83,7 @@ class Emoji extends IdModel< @BeforeDestroy static async deleteAttachmentFromS3(model: Emoji) { - const attachment = await Attachment.findOne({ - where: { - key: model.url, - }, - }); - + const attachment = await Attachment.findByPk(model.attachmentId); if (attachment) { await FileStorage.deleteFile(attachment.key); } diff --git a/server/presenters/emoji.ts b/server/presenters/emoji.ts index b423b99636..09a8c0a1d0 100644 --- a/server/presenters/emoji.ts +++ b/server/presenters/emoji.ts @@ -5,8 +5,8 @@ export default function present(emoji: Emoji) { return { id: emoji.id, name: emoji.name, - url: emoji.url, teamId: emoji.teamId, + url: emoji.attachment?.url, createdBy: emoji.createdBy ? presentUser(emoji.createdBy) : undefined, createdById: emoji.createdById, createdAt: emoji.createdAt, diff --git a/server/routes/api/emojis/emojis.ts b/server/routes/api/emojis/emojis.ts index 25b3082e65..97a9dcc3db 100644 --- a/server/routes/api/emojis/emojis.ts +++ b/server/routes/api/emojis/emojis.ts @@ -4,7 +4,7 @@ import auth from "@server/middlewares/authentication"; import { rateLimiter } from "@server/middlewares/rateLimiter"; import { transaction } from "@server/middlewares/transaction"; import validate from "@server/middlewares/validate"; -import { Emoji, User } from "@server/models"; +import { Emoji, User, Attachment } from "@server/models"; import { authorize } from "@server/policies"; import { presentEmoji, presentPolicies } from "@server/presenters"; import { APIContext } from "@server/types"; @@ -28,6 +28,11 @@ router.post( as: "createdBy", paranoid: false, }, + { + model: Attachment, + as: "attachment", + paranoid: false, + }, ]; let emoji; @@ -87,6 +92,11 @@ router.post( as: "createdBy", paranoid: false, }, + { + model: Attachment, + as: "attachment", + paranoid: false, + }, ], order: [["createdAt", "DESC"]], offset: ctx.state.pagination.offset, @@ -112,17 +122,25 @@ router.post( validate(T.EmojisCreateSchema), transaction(), async (ctx: APIContext) => { - const { name, url } = ctx.input.body; + const { name, attachmentId } = ctx.input.body; const { user } = ctx.state.auth; + const { transaction } = ctx.state; + + const attachment = await Attachment.findByPk(attachmentId, { + transaction, + rejectOnEmpty: true, + }); + authorize(user, "read", attachment); const emoji = await Emoji.createWithCtx(ctx, { name, - url, + attachmentId, teamId: user.teamId, createdById: user.id, createdBy: user, }); emoji.createdBy = user; + emoji.attachment = attachment; ctx.body = { data: presentEmoji(emoji), diff --git a/server/routes/api/emojis/schema.ts b/server/routes/api/emojis/schema.ts index f9a0d0f680..75e9b51f33 100644 --- a/server/routes/api/emojis/schema.ts +++ b/server/routes/api/emojis/schema.ts @@ -27,7 +27,7 @@ export const EmojisCreateSchema = BaseSchema.extend({ /** Name/shortcode for the emoji (e.g., "awesome") */ name: z.string().min(1).max(EmojiValidation.maxNameLength), /** URL to the emoji image */ - url: z.string(), + attachmentId: z.string().uuid(), }), });