fix(api): 🐛 Fix regression in emoji parsing caused by incorrectly changed regex

This commit is contained in:
Jesse Wierzbinski 2024-05-12 15:07:55 -10:00
parent c6c92e716f
commit 8e5d68144c
No known key found for this signature in database
3 changed files with 49 additions and 3 deletions

View file

@ -1,4 +1,4 @@
import { emojiValidator } from "@api"; import { emojiValidator, emojiValidatorWithColons } from "@api";
import { proxyUrl } from "@response"; import { proxyUrl } from "@response";
import { type InferSelectModel, and, eq } from "drizzle-orm"; import { type InferSelectModel, and, eq } from "drizzle-orm";
import type * as Lysand from "lysand-types"; import type * as Lysand from "lysand-types";
@ -17,7 +17,7 @@ export type EmojiWithInstance = InferSelectModel<typeof Emojis> & {
* @returns An array of emojis * @returns An array of emojis
*/ */
export const parseEmojis = async (text: string) => { export const parseEmojis = async (text: string) => {
const matches = text.match(emojiValidator); const matches = text.match(emojiValidatorWithColons);
if (!matches) return []; if (!matches) return [];
const emojis = await db.query.Emojis.findMany({ const emojis = await db.query.Emojis.findMany({
where: (emoji, { eq, or }) => where: (emoji, { eq, or }) =>

View file

@ -1,5 +1,8 @@
import { afterAll, describe, expect, test } from "bun:test"; import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import { config } from "config-manager"; import { config } from "config-manager";
import { eq } from "drizzle-orm";
import { db } from "~drizzle/db";
import { Emojis } from "~drizzle/schema";
import { import {
deleteOldTestUsers, deleteOldTestUsers,
getTestUsers, getTestUsers,
@ -14,6 +17,16 @@ const { users, tokens, deleteUsers } = await getTestUsers(5);
afterAll(async () => { afterAll(async () => {
await deleteUsers(); await deleteUsers();
await db.delete(Emojis).where(eq(Emojis.shortcode, "test"));
});
beforeAll(async () => {
await db.insert(Emojis).values({
contentType: "image/png",
shortcode: "test",
url: "https://example.com/test.png",
visibleInPicker: true,
});
}); });
describe(meta.route, () => { describe(meta.route, () => {
@ -278,6 +291,32 @@ describe(meta.route, () => {
expect(object2.quote?.id).toBe(object.id); expect(object2.quote?.id).toBe(object.id);
}); });
test("should correctly parse emojis", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: new URLSearchParams({
status: "Hello, :test:!",
federate: "false",
}),
}),
);
expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toBe("application/json");
const object = (await response.json()) as APIStatus;
expect(object.emojis).toBeArrayOfSize(1);
expect(object.emojis[0]).toMatchObject({
shortcode: "test",
url: expect.stringContaining("/media/proxy/"),
});
});
describe("mentions testing", () => { describe("mentions testing", () => {
test("should correctly parse @mentions", async () => { test("should correctly parse @mentions", async () => {
const response = await sendTestRequest( const response = await sendTestRequest(

View file

@ -57,6 +57,13 @@ export const emojiValidator = createRegExp(
[caseInsensitive], [caseInsensitive],
); );
export const emojiValidatorWithColons = createRegExp(
exactly(":"),
oneOrMore(letter.or(digit).or(exactly("_")).or(exactly("-"))),
exactly(":"),
[caseInsensitive],
);
export const handleZodError = ( export const handleZodError = (
result: result:
| { success: true; data?: object } | { success: true; data?: object }