Update activitypub.ts

This commit is contained in:
浪子
2026-05-16 01:08:41 +08:00
parent 52c437311a
commit 3065049aaf
+25 -4
View File
@@ -217,8 +217,11 @@ export async function activityObject(env: Env, objectId: string): Promise<Respon
if (status.visibility !== "public" && status.visibility !== "unlisted") return json({ error: "not_found" }, 404);
const user = await env.DB.prepare("SELECT * FROM users WHERE id = ?").bind(status.user_id).first<User>();
if (!user) return json({ error: "not_found" }, 404);
const attachments = await loadStatusAttachments(env, status.id);
return activityJson(noteObject(env, user, status, { attachments }));
const [attachments, tag] = await Promise.all([
loadStatusAttachments(env, status.id),
loadStatusTags(env, status.id)
]);
return activityJson(noteObject(env, user, status, { attachments, tag }));
}
const tomb = await env.DB.prepare("SELECT * FROM deleted_statuses WHERE id = ?").bind(objectId).first<{ id: string; deleted_at: string }>();
if (tomb) {
@@ -602,7 +605,10 @@ export async function createActivity(env: Env, user: User, status: Status, extra
const audience = statusAudience(env, user, status);
const to = extra.to ?? audience.to;
const cc = extra.cc ?? audience.cc;
const attachments = await loadStatusAttachments(env, status.id);
const [attachments, tag] = await Promise.all([
loadStatusAttachments(env, status.id),
loadStatusTags(env, status.id)
]);
return {
"@context": [ACTIVITY_CONTEXT, SECURITY_CONTEXT],
id: status.activity_id,
@@ -611,7 +617,7 @@ export async function createActivity(env: Env, user: User, status: Status, extra
published: status.created_at,
to,
cc,
object: noteObject(env, user, status, { to, cc, attachments })
object: noteObject(env, user, status, { to, cc, attachments, tag })
};
}
@@ -629,6 +635,21 @@ export async function loadStatusAttachments(env: Env, statusId: string): Promise
return media.map((item) => attachmentObject(env, item));
}
export async function loadStatusTags(env: Env, statusId: string): Promise<Json[]> {
const [mentionRows, hashtagRows] = await Promise.all([
env.DB.prepare("SELECT actor, acct FROM mentions WHERE status_id = ?").bind(statusId).all<{ actor: string; acct: string }>(),
env.DB.prepare("SELECT tag FROM hashtags WHERE status_id = ?").bind(statusId).all<{ tag: string }>()
]);
const tags: Json[] = [];
for (const mention of mentionRows.results) {
tags.push({ type: "Mention", href: mention.actor, name: `@${mention.acct}` });
}
for (const row of hashtagRows.results) {
tags.push({ type: "Hashtag", href: `${baseUrl(env)}/tags/${encodeURIComponent(row.tag)}`, name: `#${row.tag}` });
}
return tags;
}
function statusAudience(env: Env, user: User, status: Status): { to: string[]; cc: string[] } {
if (status.visibility === "unlisted") {
return { to: [`${actorUrl(env, user)}/followers`], cc: [PUBLIC_COLLECTION] };