diff --git a/CHANGES.md b/CHANGES.md index a82d7e5d..a6e344b3 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -6,6 +6,16 @@ Version 0.8.18 To be released. + - Fixed Unicode emoji reactions rejecting valid sequences such as `â¤ī¸` + and `đŸŗī¸â€đŸŒˆ`, while accepting multiple emoji or digits as one reaction. + Bare forms such as `❤` are now normalized to `â¤ī¸` when adding and + removing reactions, including federated reactions. Previously stored + reaction values remain removable even if they fail the new validation. + [[#699], [#700]] + +[#699]: https://github.com/fedify-dev/hollo/issues/699 +[#700]: https://github.com/fedify-dev/hollo/pull/700 + Version 0.8.17 -------------- diff --git a/src/api/v1/statuses.reactions.test.ts b/src/api/v1/statuses.reactions.test.ts new file mode 100644 index 00000000..ec33fe31 --- /dev/null +++ b/src/api/v1/statuses.reactions.test.ts @@ -0,0 +1,272 @@ +import { EmojiReact, Undo } from "@fedify/vocab"; +import { eq } from "drizzle-orm"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; + +import { cleanDatabase } from "../../../tests/helpers"; +import { + bearerAuthorization, + createAccount, + createOAuthApplication, + getAccessToken, +} from "../../../tests/helpers/oauth"; +import db from "../../db"; +import federation from "../../federation"; +import { accounts, customEmojis, posts, reactions } from "../../schema"; +import { type Uuid, uuidv7 } from "../../uuid"; +import app from "./statuses"; + +const routes = [ + { add: "react", addMethod: "POST", remove: "unreact", removeMethod: "POST" }, + { + add: "emoji_reactions", + addMethod: "PUT", + remove: "emoji_reactions", + removeMethod: "DELETE", + }, +]; + +describe.each(routes)("emoji reaction API ($addMethod $add)", (route) => { + let accountId: Uuid; + let postId: Uuid; + let authorization: string; + const context = federation.createContext( + new Request("https://hollo.test/"), + undefined, + ); + const sendActivity = vi.spyOn(context, "sendActivity"); + + beforeEach(async () => { + await cleanDatabase(); + const account = await createAccount(); + accountId = account.id as Uuid; + const client = await createOAuthApplication({ scopes: ["write"] }); + authorization = bearerAuthorization( + await getAccessToken(client, account, ["write"]), + ); + postId = uuidv7(); + await db.insert(posts).values({ + id: postId, + iri: `https://hollo.test/@hollo/${postId}`, + type: "Note", + accountId, + visibility: "public", + tags: {}, + emojis: {}, + sensitive: false, + updated: new Date(), + }); + sendActivity.mockReset().mockResolvedValue(undefined); + vi.spyOn(federation, "createContext").mockReturnValue(context); + }); + + afterEach(() => { + vi.mocked(federation.createContext).mockRestore(); + }); + + function request(emoji: string, remove = false) { + return app.request( + `https://hollo.test/${postId}/${remove ? route.remove : route.add}/${encodeURIComponent(emoji)}`, + { + method: remove ? route.removeMethod : route.addMethod, + headers: { authorization }, + }, + ); + } + + it.each([ + ["❤", "â¤ī¸"], + ["â¤ī¸", "❤"], + ["✌", "âœŒī¸"], + ["☚", "â˜šī¸"], + ["☃", "â˜ƒī¸"], + ["đŸŗī¸â€đŸŒˆ", "đŸŗī¸â€đŸŒˆ"], + ["1ī¸âƒŖ", "1ī¸âƒŖ"], + ["đŸ‘¨đŸžâ€âš•ī¸", "đŸ‘¨đŸžâ€âš•ī¸"], + ])("adds %s and removes it using %s", async (input, removal) => { + const canonical = input === "â¤ī¸" ? input : removal; + const added = await request(input); + expect(added.status).toBe(200); + expect((await added.json()).emoji_reactions[0].name).toBe(canonical); + const stored = await db.query.reactions.findMany(); + expect(stored.map((r) => r.emoji)).toEqual([canonical]); + expect(sendActivity).toHaveBeenCalledTimes(2); + const activity = sendActivity.mock.calls[0][2] as EmojiReact; + expect(activity).toBeInstanceOf(EmojiReact); + expect(activity.content).toBe(canonical); + expect(activity.id?.href).toBe( + new URL( + `/#react/${accountId}/${postId}/${canonical}`, + "https://hollo.test", + ).href, + ); + const orderingKey = `react:https://hollo.test/@hollo:https://hollo.test/@hollo/${postId}:${canonical}`; + expect( + sendActivity.mock.calls.slice(0, 2).map((c) => c[3]?.orderingKey), + ).toEqual([orderingKey, orderingKey]); + + const removed = await request(removal, true); + expect(removed.status).toBe(200); + expect((await removed.json()).emoji_reactions).toEqual([]); + expect(await db.query.reactions.findMany()).toEqual([]); + expect(sendActivity).toHaveBeenCalledTimes(4); + const undo = sendActivity.mock.calls[2][2] as Undo; + expect(undo).toBeInstanceOf(Undo); + expect(undo.id?.href).toBe( + new URL( + `/#react/undo/${accountId}/${postId}/${canonical}`, + "https://hollo.test", + ).href, + ); + const object = await undo.getObject(); + expect(object).toBeInstanceOf(EmojiReact); + expect(object?.id?.href).toBe(activity.id?.href); + expect(object?.content).toBe(canonical); + expect( + sendActivity.mock.calls.slice(2).map((c) => c[3]?.orderingKey), + ).toEqual([orderingKey, orderingKey]); + }); + + it.each(["3*14159", "👍🎉🙏🍎", "❤❤❤", "hello"])( + "rejects invalid reaction %s without insertion or delivery", + async (emoji) => { + expect((await request(emoji)).status).toBe(404); + expect((await request(emoji, true)).status).toBe(404); + expect(await db.query.reactions.findMany()).toEqual([]); + expect(sendActivity).not.toHaveBeenCalled(); + }, + ); + + it.each(["blobcat", "1", "â¤ī¸"])( + "preserves local custom shortcode %s and its activity identity", + async (shortcode) => { + await db.insert(customEmojis).values({ + shortcode, + url: "https://hollo.test/emoji.png", + }); + expect((await request(shortcode)).status).toBe(200); + const [stored] = await db.query.reactions.findMany(); + expect(stored.emoji).toBe(`:${shortcode}:`); + expect(stored.customEmoji).toBe("https://hollo.test/emoji.png"); + const activity = sendActivity.mock.calls[0][2] as EmojiReact; + expect(activity.content).toBe(`:${shortcode}:`); + expect(activity.id?.href).toBe( + new URL( + `/#react/${accountId}/${postId}/${shortcode}`, + "https://hollo.test", + ).href, + ); + const tags = []; + for await (const tag of activity.getTags()) tags.push(tag); + expect(tags).toHaveLength(1); + expect(tags[0].id?.href).toBe(stored.emojiIri); + expect((await request(`${shortcode}@hollo.test`, true)).status).toBe(200); + const undo = sendActivity.mock.calls[2][2] as Undo; + expect((await undo.getObject())?.id?.href).toBe(activity.id?.href); + expect(await db.query.reactions.findMany()).toEqual([]); + }, + ); + + it("keeps legacy bare rows removable, preferring the canonical row", async () => { + await db.insert(reactions).values([ + { postId, accountId, emoji: "❤" }, + { postId, accountId, emoji: "â¤ī¸" }, + ]); + expect((await request("❤", true)).status).toBe(200); + expect((await db.query.reactions.findMany()).map((r) => r.emoji)).toEqual([ + "❤", + ]); + expect((await request("❤", true)).status).toBe(200); + const undo = sendActivity.mock.calls[2][2] as Undo; + const object = await undo.getObject(); + expect(object?.content).toBe("❤"); + expect(object?.id?.href).toBe( + new URL(`/#react/${accountId}/${postId}/❤`, "https://hollo.test").href, + ); + expect(await db.query.reactions.findMany()).toEqual([]); + }); + + it.each(["👍🎉", "3*14159", "❤❤❤"])( + "removes legacy invalid Unicode %s only for the current owner and post", + async (emoji) => { + expect((await request(emoji)).status).toBe(404); + const other = await createAccount({ username: "other" }); + const otherPostId = uuidv7(); + await db.insert(posts).values({ + id: otherPostId, + iri: `https://hollo.test/@hollo/${otherPostId}`, + type: "Note", + accountId, + visibility: "public", + tags: {}, + emojis: {}, + sensitive: false, + updated: new Date(), + }); + await db.insert(reactions).values([ + { postId, accountId, emoji }, + { postId, accountId: other.id as Uuid, emoji }, + { postId: otherPostId, accountId, emoji }, + ]); + expect((await request(emoji, true)).status).toBe(200); + const undo = sendActivity.mock.calls[0][2] as Undo; + const object = await undo.getObject(); + expect(object?.content).toBe(emoji); + expect(object?.id?.href).toBe( + new URL(`/#react/${accountId}/${postId}/${emoji}`, "https://hollo.test") + .href, + ); + const remaining = await db.query.reactions.findMany(); + expect(remaining).toHaveLength(2); + expect(remaining).toEqual( + expect.arrayContaining([ + expect.objectContaining({ postId, accountId: other.id, emoji }), + expect.objectContaining({ postId: otherPostId, accountId, emoji }), + ]), + ); + expect((await request(emoji, true)).status).toBe(404); + expect(sendActivity).toHaveBeenCalledTimes(2); + }, + ); + + it("keeps custom shortcode precedence over legacy raw values", async () => { + await db.insert(customEmojis).values({ + shortcode: "1", + url: "https://hollo.test/emoji.png", + }); + expect((await request("1")).status).toBe(200); + await db.insert(reactions).values({ postId, accountId, emoji: "1" }); + expect((await request("1", true)).status).toBe(200); + expect((await db.query.reactions.findMany()).map((r) => r.emoji)).toEqual([ + "1", + ]); + expect((await request("1", true)).status).toBe(404); + }); + + it("preserves remote custom emoji metadata when copying a reaction", async () => { + const remote = await createAccount({ username: "remote" }); + // The remote-custom path identifies the source by its handle's domain. + await db.insert(reactions).values({ + postId, + accountId: remote.id as Uuid, + emoji: ":blobcat:", + customEmoji: "https://remote.test/blobcat.png", + emojiIri: "https://remote.test/emoji/blobcat", + }); + expect((await request("blobcat@hollo.test")).status).toBe(404); + // A domain other than the request host exercises the remote-copy path. + await db + .update(accounts) + .set({ handle: "@remote@remote.test" }) + .where(eq(accounts.id, remote.id as Uuid)); + expect((await request("blobcat@remote.test")).status).toBe(200); + const stored = await db.query.reactions.findFirst({ + where: eq(reactions.accountId, accountId), + }); + expect(stored?.emoji).toBe(":blobcat:"); + expect(stored?.customEmoji).toBe("https://remote.test/blobcat.png"); + expect(stored?.emojiIri).toBe("https://remote.test/emoji/blobcat"); + expect((sendActivity.mock.calls[0][2] as EmojiReact).content).toBe( + ":blobcat:", + ); + }); +}); diff --git a/src/api/v1/statuses.ts b/src/api/v1/statuses.ts index 4aebeffa..76fc7d2f 100644 --- a/src/api/v1/statuses.ts +++ b/src/api/v1/statuses.ts @@ -27,6 +27,7 @@ import type { TypedResponse } from "hono/types"; import { z } from "zod"; import { db } from "../../db"; +import { normalizeUnicodeEmoji } from "../../emoji"; import { serializeAccount, serializeAccountOwner, @@ -1294,7 +1295,9 @@ async function addEmojiReaction( where: eq(customEmojis.shortcode, emoji), }); if (customEmoji == null) { - if (!/^[\p{Emoji}]+$/u.test(emoji)) return c.notFound(); + const normalized = normalizeUnicodeEmoji(emoji); + if (normalized == null) return c.notFound(); + emoji = normalized; // Unicode emoji: await db.insert(reactions).values({ postId, @@ -1396,19 +1399,38 @@ async function removeEmojiReaction( let emoji = c.req.param("emoji"); const url = new URL(c.req.url); if (emoji.endsWith(`@${url.host}`)) emoji = emoji.replace(/@[^@]+$/, ""); - const unicode = /^[\p{Emoji}]+$/u.test(emoji); - const deleted = await db - .delete(reactions) - .where( - and( - eq(reactions.postId, postId), - eq(reactions.accountId, owner.id), - eq(reactions.emoji, unicode ? emoji : `:${emoji}:`), - ), - ) - .returning(); + const customEmoji = emoji.includes("@") + ? null + : await db.query.customEmojis.findFirst({ + where: eq(customEmojis.shortcode, emoji), + }); + const unicode = + customEmoji == null ? normalizeUnicodeEmoji(emoji) : undefined; + const deleteReaction = (emojiCode: string) => + db + .delete(reactions) + .where( + and( + eq(reactions.postId, postId), + eq(reactions.accountId, owner.id), + eq(reactions.emoji, emojiCode), + ), + ) + .returning(); + let deleted = await deleteReaction(unicode ?? `:${emoji}:`); + // The old validator also accepted digits and runs of emoji. Keep those + // existing raw values removable, without accepting them for new reactions. + if ( + deleted.length < 1 && + customEmoji == null && + unicode !== emoji && + /^[\p{Emoji}]+$/u.test(emoji) + ) { + deleted = await deleteReaction(emoji); + } if (deleted.length < 1) return c.notFound(); const [reaction] = deleted; + if (unicode != null) emoji = reaction.emoji; const post = await db.query.posts.findFirst({ where: eq(posts.id, postId), with: getPostRelations(owner.id), diff --git a/src/emoji.test.ts b/src/emoji.test.ts new file mode 100644 index 00000000..67d13cc2 --- /dev/null +++ b/src/emoji.test.ts @@ -0,0 +1,58 @@ +import { describe, expect, it } from "vitest"; + +import { normalizeUnicodeEmoji } from "./emoji"; + +describe("normalizeUnicodeEmoji", () => { + it.each([ + "👍", + "🎉", + "🙏", + "🍎", + "â¤ī¸", + "âœŒī¸", + "â˜šī¸", + "đŸŗī¸â€đŸŒˆ", + "👨‍👩‍👧", + "đŸ‘¨đŸžâ€âš•ī¸", + "👍🏾", + "🏾", + "🇰🇷", + "1ī¸âƒŖ", + "#ī¸âƒŖ", + "*ī¸âƒŖ", + "đŸ´ķ §ķ ĸķ Ĩķ Žķ §ķ ŋ", + ])("preserves a single RGI emoji: %s", (emoji) => { + expect(normalizeUnicodeEmoji(emoji)).toBe(emoji); + }); + + it.each([ + ["❤", "â¤ī¸"], + ["✌", "âœŒī¸"], + ["☚", "â˜šī¸"], + ["☃", "â˜ƒī¸"], + ])("repairs %s to %s idempotently", (bare, qualified) => { + expect(normalizeUnicodeEmoji(bare)).toBe(qualified); + expect(normalizeUnicodeEmoji(normalizeUnicodeEmoji(bare)!)).toBe(qualified); + }); + + it.each([ + "", + "hello", + "3*14159", + "1", + "#", + "*", + "👍🎉🙏🍎", + "❤❤❤", + ":custom:", + "\uFE0F", + "\u200D", + "👍\n", + " 👍", + "1\u20E3", + "đŸŗ\u200D🌈", + "🇰", + ])("rejects non-RGI content: %j", (emoji) => { + expect(normalizeUnicodeEmoji(emoji)).toBeUndefined(); + }); +}); diff --git a/src/emoji.ts b/src/emoji.ts new file mode 100644 index 00000000..0fcfd6fe --- /dev/null +++ b/src/emoji.ts @@ -0,0 +1,11 @@ +const RGI_EMOJI = /^\p{RGI_Emoji}$/v; + +/** + * Accepts one RGI emoji, repairing bare forms such as ❤ with U+FE0F. + * The accepted Unicode version is determined by the JavaScript runtime. + */ +export function normalizeUnicodeEmoji(emoji: string): string | undefined { + if (RGI_EMOJI.test(emoji)) return emoji; + const qualified = `${emoji}\uFE0F`; + return RGI_EMOJI.test(qualified) ? qualified : undefined; +} diff --git a/src/federation/inbox.reactions.test.ts b/src/federation/inbox.reactions.test.ts index 1ecfa1e8..e6b252af 100644 --- a/src/federation/inbox.reactions.test.ts +++ b/src/federation/inbox.reactions.test.ts @@ -1,5 +1,5 @@ import type { InboxContext } from "@fedify/fedify"; -import { EmojiReact, Like, Person } from "@fedify/fedify/vocab"; +import { Emoji, EmojiReact, Image, Like, Person, Undo } from "@fedify/vocab"; import { eq } from "drizzle-orm"; import { beforeEach, describe, expect, it, vi } from "vitest"; @@ -8,7 +8,12 @@ import { createAccount } from "../../tests/helpers/oauth"; import db from "../db"; import { accounts, instances, likes, posts, reactions } from "../schema"; import type { Uuid } from "../uuid"; -import { onEmojiReactionAdded, onLiked } from "./inbox"; +import { + onEmojiReactionAdded, + onEmojiReactionRemoved, + onLiked, + onUnliked, +} from "./inbox"; async function seedRemoteTargetPost(): Promise<{ id: Uuid; iri: string }> { await db @@ -136,3 +141,225 @@ describe("federation inbox reaction target fallback", () => { expect(forwardActivity).not.toHaveBeenCalled(); }); }); + +describe.each([EmojiReact, Like])( + "Unicode normalization for inbound %s", + (Activity) => { + beforeEach(async () => { + await cleanDatabase(); + }); + + async function setup() { + const owner = await createAccount({ username: "author" }); + const id = crypto.randomUUID() as Uuid; + const iri = `https://hollo.test/@author/${id}`; + await db.insert(posts).values({ + id, + iri, + type: "Note", + accountId: owner.id as Uuid, + visibility: "public", + tags: {}, + emojis: {}, + sensitive: false, + updated: new Date(), + }); + const liker = await seedLocalLiker(); + return { + target: { id, iri }, + person: createLocalPerson(liker.iri), + ...createCtx(), + }; + } + + const add = (ctx: InboxContext, activity: EmojiReact | Like) => + activity instanceof Like + ? onLiked(ctx, activity) + : onEmojiReactionAdded(ctx, activity); + const remove = Activity === Like ? onUnliked : onEmojiReactionRemoved; + + it.each([ + ["❤", "â¤ī¸"], + ["â¤ī¸", "❤"], + ["✌", "âœŒī¸"], + ["đŸŗī¸â€đŸŒˆ", "đŸŗī¸â€đŸŒˆ"], + ])( + "normalizes and deduplicates %s, then handles Undo with %s", + async (content, undoContent) => { + const { target, person, ctx, forwardActivity } = await setup(); + const activity = new Activity({ + actor: person, + object: new URL(target.iri), + content: ` ${content} `, + }); + await add(ctx, activity); + await add( + ctx, + new Activity({ + actor: person, + object: new URL(target.iri), + content: undoContent, + }), + ); + const found = await db.query.reactions.findMany(); + expect(found.map((r) => r.emoji)).toEqual([ + content === "â¤ī¸" ? content : undoContent, + ]); + expect(activity.content).toBe(` ${content} `); + expect(forwardActivity).toHaveBeenCalledTimes(2); + await remove( + ctx, + new Undo({ + actor: person, + object: new Activity({ + actor: person, + object: new URL(target.iri), + content: undoContent, + }), + }), + ); + expect(await db.query.reactions.findMany()).toEqual([]); + expect(await db.query.likes.findMany()).toEqual([]); + expect(forwardActivity).toHaveBeenCalledTimes(3); + }, + ); + + it.each(["3*14159", "👍🎉", "❤❤❤", "text", ""])( + "rejects invalid additions %j and ignores Undo without a legacy row", + async (content) => { + const { target, person, ctx, forwardActivity } = await setup(); + const activity = new Activity({ + actor: person, + object: new URL(target.iri), + content, + }); + const getActor = vi.spyOn(activity, "getActor"); + const undo = new Undo({ actor: person, object: activity }); + await add(ctx, activity); + await remove(ctx, undo); + expect(getActor).not.toHaveBeenCalled(); + expect(await db.query.reactions.findMany()).toEqual([]); + expect(await db.query.likes.findMany()).toEqual([]); + expect(await db.query.notifications.findMany()).toEqual([]); + expect(forwardActivity).not.toHaveBeenCalled(); + }, + ); + + it("preserves custom emoji tags and allows Undo", async () => { + const { target, person, ctx } = await setup(); + const activity = new Activity({ + actor: person, + object: new URL(target.iri), + content: ":blobcat:", + tags: [ + new Emoji({ + id: new URL("https://remote.test/emoji/blobcat"), + name: ":blobcat:", + icon: new Image({ + url: new URL("https://remote.test/blobcat.png"), + }), + }), + ], + }); + await add(ctx, activity); + const [stored] = await db.query.reactions.findMany(); + expect(stored.emoji).toBe(":blobcat:"); + expect(stored.customEmoji).toBe("https://remote.test/blobcat.png"); + expect(stored.emojiIri).toBe("https://remote.test/emoji/blobcat"); + await remove(ctx, new Undo({ actor: person, object: activity })); + expect(await db.query.reactions.findMany()).toEqual([]); + }); + + it("removes canonical before legacy bare rows without merging them", async () => { + const { target, person, ctx } = await setup(); + const account = await db.query.accounts.findFirst({ + where: eq(accounts.iri, person.id!.href), + }); + await db.insert(reactions).values([ + { postId: target.id, accountId: account!.id, emoji: "❤" }, + { postId: target.id, accountId: account!.id, emoji: "â¤ī¸" }, + ]); + const undo = new Undo({ + actor: person, + object: new Activity({ + actor: person, + object: new URL(target.iri), + content: "❤", + }), + }); + await remove(ctx, undo); + expect((await db.query.reactions.findMany()).map((r) => r.emoji)).toEqual( + ["❤"], + ); + await remove(ctx, undo); + expect(await db.query.reactions.findMany()).toEqual([]); + }); + + it.each(["👍🎉", "3*14159", "text"])( + "allows scoped Undo of legacy content %j without accepting new additions", + async (content) => { + const { target, person, ctx, forwardActivity } = await setup(); + const account = await db.query.accounts.findFirst({ + where: eq(accounts.iri, person.id!.href), + }); + const other = await createAccount({ username: "other" }); + const otherPostId = crypto.randomUUID() as Uuid; + await db.insert(posts).values({ + id: otherPostId, + iri: `https://hollo.test/@other/${otherPostId}`, + type: "Note", + accountId: other.id as Uuid, + visibility: "public", + tags: {}, + emojis: {}, + sensitive: false, + updated: new Date(), + }); + const activity = new Activity({ + actor: person, + object: new URL(target.iri), + content, + }); + await add(ctx, activity); + expect(await db.query.reactions.findMany()).toEqual([]); + expect(forwardActivity).not.toHaveBeenCalled(); + await db.insert(reactions).values([ + { postId: target.id, accountId: account!.id, emoji: content }, + { postId: target.id, accountId: other.id as Uuid, emoji: content }, + { postId: otherPostId, accountId: account!.id, emoji: content }, + ]); + const undo = new Undo({ actor: person, object: activity }); + await remove( + ctx, + new Undo({ + actor: createLocalPerson("https://hollo.test/@other"), + object: activity, + }), + ); + expect(await db.query.reactions.findMany()).toHaveLength(3); + expect(forwardActivity).not.toHaveBeenCalled(); + await remove(ctx, undo); + const remaining = await db.query.reactions.findMany(); + expect(remaining).toHaveLength(2); + expect(remaining).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + postId: target.id, + accountId: other.id, + emoji: content, + }), + expect.objectContaining({ + postId: otherPostId, + accountId: account!.id, + emoji: content, + }), + ]), + ); + expect(forwardActivity).toHaveBeenCalledTimes(1); + expect(activity.content).toBe(content); + await remove(ctx, undo); + expect(forwardActivity).toHaveBeenCalledTimes(1); + }, + ); + }, +); diff --git a/src/federation/inbox.ts b/src/federation/inbox.ts index b3f052aa..608c2d80 100644 --- a/src/federation/inbox.ts +++ b/src/federation/inbox.ts @@ -27,6 +27,7 @@ import { getLogger } from "@logtape/logtape"; import { and, eq, inArray } from "drizzle-orm"; import { db } from "../db"; +import { normalizeUnicodeEmoji } from "../emoji"; import { createEmojiReactionNotification, createFavouriteNotification, @@ -992,10 +993,14 @@ export async function onEmojiReactionAdded( react: EmojiReact | Like, ): Promise { if (react.content == null || react.objectId == null) return; + const content = react.content.toString().trim(); + const emoji = + content.startsWith(":") && content.endsWith(":") + ? content + : normalizeUnicodeEmoji(content); + if (emoji == null || emoji === "") return; const target = await resolveReactionTarget(ctx, react.objectId); if (target == null) return; - const emoji = react.content.toString().trim(); - if (emoji === "") return; const actor = await react.getActor(); if (actor == null) return; const account = await persistAccount( @@ -1076,6 +1081,12 @@ export async function onEmojiReactionRemoved( ) { return; } + const content = object.content.toString().trim(); + const emoji = + content.startsWith(":") && content.endsWith(":") + ? content + : normalizeUnicodeEmoji(content); + if (content === "") return; const actor = await undo.getActor(); if (actor == null) return; const account = await persistAccount( @@ -1087,15 +1098,23 @@ export async function onEmojiReactionRemoved( if (account == null) return; const target = await resolveReactionTarget(ctx, object.objectId); if (target == null) return; - await db - .delete(reactions) - .where( - and( - eq(reactions.postId, target.post.id), - eq(reactions.accountId, account.id), - eq(reactions.emoji, object.content.toString().trim()), - ), - ); + const deleteReaction = (emojiCode: string) => + db + .delete(reactions) + .where( + and( + eq(reactions.postId, target.post.id), + eq(reactions.accountId, account.id), + eq(reactions.emoji, emojiCode), + ), + ) + .returning({ emoji: reactions.emoji }); + // Undo must also remove raw content accepted before Unicode validation. + const deleted = await deleteReaction(emoji ?? content); + if (emoji == null && deleted.length < 1) return; + if (deleted.length < 1 && emoji !== content) { + await deleteReaction(content); + } if (target.localRecipientHandle != null) { await ctx.forwardActivity( { username: target.localRecipientHandle },