shadowfacts.net/lib/generate/activitypub.ts

59 lines
1.5 KiB
TypeScript

import path from "path";
import { Page, PostMetadata } from "../metadata";
import * as util from "../util";
const baseURL = process.env.BASE_URL || "shadowfacts.net";
export default async function activitypub(posts: Page[]) {
for (const post of posts) {
const object = postObject(post);
const dir = path.dirname(post.metadata.permalink);
const base = path.basename(post.metadata.permalink);
const dest = path.format({
dir,
name: base,
ext: ".json"
});
const data = JSON.stringify(object);
util.write(dest, data);
util.write(path.join(post.metadata.permalink, "index.html.json"), data);
}
util.write(".well-known/webfinger", JSON.stringify(webfingerObject()));
}
function postObject(post: Page): object {
const postMeta = <PostMetadata>post.metadata;
return {
"@context": [
"https://www.w3.org/ns/activitystreams"
],
"type": "Article",
"id": `https://${baseURL}${post.metadata.permalink}`,
"published": (<Date>postMeta.date).toISOString(),
"inReplyTo": null,
"url": `https://${baseURL}${postMeta.permalink}`,
"attributedTo": `https://${baseURL}/ap/actor`,
"to": [
"https://www.w3.org/ns/activitystreams#Public"
],
"cc": [
`https://${baseURL}/ap/followers`
],
"name": postMeta.title,
"content": post.text
};
}
function webfingerObject() {
return {
"subject": `acct:shadowfacts@${baseURL}`,
"links": [
{
"rel": "self",
"type": "application/activity+json",
"href": `https://${baseURL}/ap/actor`
}
]
};
}