Skip to content

Commit 3daeab1

Browse files
committed
Replace @std/media-types with mime-db
Replace the @std/media-types dependency with mime-db for MIME type handling. Extract quote link detection logic into a reusable isQuoteLink function to eliminate code duplication between bot-impl.ts and message-impl.ts.
1 parent 3364c51 commit 3daeab1

3 files changed

Lines changed: 32 additions & 32 deletions

File tree

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
"@logtape/logtape": "jsr:@logtape/logtape@^1.0.0",
2626
"@phensley/language-tag": "npm:@phensley/language-tag@^1.12.2",
2727
"@std/assert": "jsr:@std/assert@^1.0.12",
28-
"@std/media-types": "jsr:@std/media-types@^1.1.0",
2928
"hono": "jsr:@hono/hono@^4.8.2",
3029
"html-entities": "npm:html-entities@^2.6.0",
3130
"markdown-it": "npm:markdown-it@^14.1.0",
31+
"mime-db": "npm:mime-db@^1.54.0",
3232
"uuid": "npm:uuid@^11.1.0",
3333
"xss": "npm:xss@^1.0.15"
3434
},

src/bot-impl.ts

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ import {
5555
} from "@fedify/fedify/vocab";
5656
import { getXForwardedRequest } from "@hongminhee/x-forwarded-fetch";
5757
import { getLogger } from "@logtape/logtape";
58-
import { extension } from "@std/media-types/extension";
59-
import { parseMediaType } from "@std/media-types/parse-media-type";
58+
import mimeDb from "mime-db";
6059
import metadata from "../deno.json" with { type: "json" };
6160
import type { Bot, CreateBotOptions, PagesOptions } from "./bot.ts";
6261
import {
@@ -85,6 +84,7 @@ import {
8584
createMessage,
8685
getMessageVisibility,
8786
isMessageObject,
87+
isQuoteLink,
8888
messageClasses,
8989
} from "./message-impl.ts";
9090
import type { Message, MessageClass, SharedMessage } from "./message.ts";
@@ -681,19 +681,9 @@ export class BotImpl<TContextData> implements Bot<TContextData> {
681681
let quoteUrl: URL | null = null;
682682
// FIXME: eliminate this duplication
683683
for await (const tag of object.getTags(ctx)) {
684-
if (tag instanceof Link) {
685-
const mediaType = tag.mediaType == null
686-
? null
687-
: parseMediaType(tag.mediaType);
688-
if (
689-
tag.rel === "https://misskey-hub.net/ns#_misskey_quote" ||
690-
mediaType?.[0] === "application/activity+json" ||
691-
mediaType?.[0] === "application/ld+json" &&
692-
mediaType[1]?.profile === "https://www.w3.org/ns/activitystreams"
693-
) {
694-
quoteUrl = tag.href;
695-
break;
696-
}
684+
if (tag instanceof Link && isQuoteLink(tag)) {
685+
quoteUrl = tag.href;
686+
break;
697687
}
698688
}
699689
if (quoteUrl == null) quoteUrl = object.quoteUrl;
@@ -998,9 +988,13 @@ export class BotImpl<TContextData> implements Bot<TContextData> {
998988
if ("url" in data) {
999989
url = new URL(data.url);
1000990
} else {
1001-
const ext = extension(data.type);
991+
const t = mimeDb[data.type];
1002992
url = new URL(
1003-
`/emojis/${name}${ext == null ? "" : `.${ext}`}`,
993+
`/emojis/${name}${
994+
t == null || t.extensions == null || t.extensions.length < 1
995+
? ""
996+
: `.${t.extensions[0]}`
997+
}`,
1004998
ctx.origin,
1005999
);
10061000
}

src/message-impl.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import {
3838
Update,
3939
} from "@fedify/fedify/vocab";
4040
import type { LanguageTag } from "@phensley/language-tag";
41-
import { parseMediaType } from "@std/media-types/parse-media-type";
4241
import { decode } from "html-entities";
4342
import { v7 as uuidv7 } from "uuid";
4443
import { FilterXSS, getDefaultWhiteList } from "xss";
@@ -653,19 +652,8 @@ export async function createMessage<T extends MessageClass, TContextData>(
653652
mentionedActorIds.add(tag.href.href);
654653
} else if (tag instanceof Hashtag) {
655654
hashtags.push(tag);
656-
} else if (tag instanceof Link) {
657-
// FIXME: eliminate this duplication
658-
const mediaType = tag.mediaType == null
659-
? null
660-
: parseMediaType(tag.mediaType);
661-
if (
662-
tag.rel === "https://misskey-hub.net/ns#_misskey_quote" ||
663-
mediaType?.[0] === "application/activity+json" ||
664-
mediaType?.[0] === "application/ld+json" &&
665-
mediaType[1]?.profile === "https://www.w3.org/ns/activitystreams"
666-
) {
667-
quoteLinks.push(tag);
668-
}
655+
} else if (tag instanceof Link && isQuoteLink(tag)) {
656+
quoteLinks.push(tag);
669657
}
670658
}
671659
const attachments: Document[] = [];
@@ -773,3 +761,21 @@ export function getMessageVisibility(
773761
? "direct"
774762
: "unknown";
775763
}
764+
765+
export function isQuoteLink(tag: Link): boolean {
766+
if (tag.rel === "https://misskey-hub.net/ns#_misskey_quote") return true;
767+
else if (tag.mediaType == null) return false;
768+
// FIXME: Properly parse the media type
769+
const parsed = tag.mediaType.split(";");
770+
const type = parsed[0].trim();
771+
if (type === "application/activity+json") return true;
772+
const params: Record<string, string> = {};
773+
for (let i = 1; i < parsed.length; i++) {
774+
const param = parsed[i].trim().split("=");
775+
if (param.length === 2) {
776+
params[param[0]] = param[1].replace(/"/g, "");
777+
}
778+
}
779+
return type === "application/ld+json" &&
780+
params.profile === "https://www.w3.org/ns/activitystreams";
781+
}

0 commit comments

Comments
 (0)