Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------------
Expand Down
272 changes: 272 additions & 0 deletions src/api/v1/statuses.reactions.test.ts
Original file line number Diff line number Diff line change
@@ -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:",
);
});
});
46 changes: 34 additions & 12 deletions src/api/v1/statuses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down
Loading
Loading