Update activitypub.ts
This commit is contained in:
+25
-4
@@ -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);
|
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>();
|
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);
|
if (!user) return json({ error: "not_found" }, 404);
|
||||||
const attachments = await loadStatusAttachments(env, status.id);
|
const [attachments, tag] = await Promise.all([
|
||||||
return activityJson(noteObject(env, user, status, { attachments }));
|
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 }>();
|
const tomb = await env.DB.prepare("SELECT * FROM deleted_statuses WHERE id = ?").bind(objectId).first<{ id: string; deleted_at: string }>();
|
||||||
if (tomb) {
|
if (tomb) {
|
||||||
@@ -602,7 +605,10 @@ export async function createActivity(env: Env, user: User, status: Status, extra
|
|||||||
const audience = statusAudience(env, user, status);
|
const audience = statusAudience(env, user, status);
|
||||||
const to = extra.to ?? audience.to;
|
const to = extra.to ?? audience.to;
|
||||||
const cc = extra.cc ?? audience.cc;
|
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 {
|
return {
|
||||||
"@context": [ACTIVITY_CONTEXT, SECURITY_CONTEXT],
|
"@context": [ACTIVITY_CONTEXT, SECURITY_CONTEXT],
|
||||||
id: status.activity_id,
|
id: status.activity_id,
|
||||||
@@ -611,7 +617,7 @@ export async function createActivity(env: Env, user: User, status: Status, extra
|
|||||||
published: status.created_at,
|
published: status.created_at,
|
||||||
to,
|
to,
|
||||||
cc,
|
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));
|
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[] } {
|
function statusAudience(env: Env, user: User, status: Status): { to: string[]; cc: string[] } {
|
||||||
if (status.visibility === "unlisted") {
|
if (status.visibility === "unlisted") {
|
||||||
return { to: [`${actorUrl(env, user)}/followers`], cc: [PUBLIC_COLLECTION] };
|
return { to: [`${actorUrl(env, user)}/followers`], cc: [PUBLIC_COLLECTION] };
|
||||||
|
|||||||
Reference in New Issue
Block a user