From 8f73cd81069e26f3dc4c6967c03d6a5e99be4888 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 18 Feb 2019 13:47:43 -0500 Subject: [PATCH] Start ActivityPub stuff --- lib/generate/activitypub.ts | 78 +++++++++++++++++++++++++++++++++++++ lib/generate/index.ts | 2 + lib/index.ts | 4 +- lib/metadata.ts | 1 + package.json | 6 ++- 5 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 lib/generate/activitypub.ts diff --git a/lib/generate/activitypub.ts b/lib/generate/activitypub.ts new file mode 100644 index 0000000..87a931a --- /dev/null +++ b/lib/generate/activitypub.ts @@ -0,0 +1,78 @@ +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); + util.write(`ap/posts/${post.metadata.permalink}.json`, JSON.stringify(object)); + } + + util.write("ap/actor", JSON.stringify(actorObject())) + + util.write(".well-known/webfinger", JSON.stringify(webfingerObject())); +} + +function postObject(post: Page): object { + const postMeta = post.metadata; + return { + "@context": [ + "https://www.w3.org/ns/activitystreams" + ], + "type": "Article", + "id": `https://${baseURL}${postMeta.permalink}.json`, + "published": (postMeta.date).toISOString(), + "inReplyTo": null, + "url": `https://${baseURL}${postMeta.permalink}`, + "attributedTo": `https://${baseURL}/ap/actor`, + "to": [ + "https://www.w3.org/ns/activitystreams#Public" + ], + "cc": [], + "name": postMeta.title, + "content": post.text + }; +} + +function actorObject() { + return { + "@context": [ + "https://www.w3.org/ns/activitystreams", + "https://w3id.org/security/v1" + ], + "type": "Person", + "id": `https://${baseURL}/ap/actor`, + "preferredUsername": "shadowfacts", + "inbox": `https://${baseURL}/ap/inbox`, + "publicKey": { + "id": `https://${baseURL}/ap/actor#main-key`, + "owner": `https://${baseURL}/ap/actor`, + "publicKeyPem": +` +-----BEGIN PUBLIC KEY----- +MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxsePhOEq6EqqgGe+5vKa +ti8cnvLoP3eJDTHENxX4M9epMIeo48cI214wkAERoNufZLtYeGOHO6YJcVydVSYN +Ud4lQxgaPwAlh0DB0BUrXR1yfcSAhHS86XBSUq86O3fY5V+vEdZiN5PfUCG5rg5t +5V/TrcbVQngXqDmwVZ/OcJ+5vUo+B62Mul7EmiNkZHKzp1VZGVtZ6nZ/4TxsdeSN +XT8Vb0f/GJQOQLzW6dV0CBupts6x7o+9oRiYiAH+cXLFyQdTQ+1TrqMSMscdfX9I +Kos5Ih7oKdj6wQt0pDEOGx090c9JzQC25RzxRk7jc1Jt1UzDi/a1U/BbucxIvPr3 +PQIDAQAB +-----END PUBLIC KEY----- +` + } + }; +} + +function webfingerObject() { + return { + "subject": `acct:shadowfacts@${baseURL}`, + "links": [ + { + "rel": "self", + "type": "application/activity+json", + "href": `https://${baseURL}/ap/actor` + } + ] + }; +} \ No newline at end of file diff --git a/lib/generate/index.ts b/lib/generate/index.ts index c46aebf..d36ac41 100644 --- a/lib/generate/index.ts +++ b/lib/generate/index.ts @@ -1,3 +1,4 @@ +import activitypub from "./activitypub"; import categories from "./categories"; import copy from "./copy"; import css from "./css"; @@ -9,6 +10,7 @@ import rss from "./rss"; import tutorials from "./tutorials"; export = { + activitypub, categories, copy, css, diff --git a/lib/index.ts b/lib/index.ts index c06643d..135f626 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -11,7 +11,9 @@ async function generate() { generators.homepage(posts); generators.redirects(posts); const categories = await generators.categories(posts); - generators.rss(posts, categories, tutorials); + await generators.rss(posts, categories, tutorials); + + generators.activitypub(posts); } generate(); \ No newline at end of file diff --git a/lib/metadata.ts b/lib/metadata.ts index 4e522ff..ad7d29b 100644 --- a/lib/metadata.ts +++ b/lib/metadata.ts @@ -18,6 +18,7 @@ export interface PostMetadata extends Metadata { category: string; date: string | Date; excerpt?: string; + uuid: string; } export async function get(path: string): Promise { diff --git a/package.json b/package.json index f109745..1be324d 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,8 @@ "license": "UNLICENSED", "scripts": { "generate": "tsc -p . && node built/index.js", - "serve": "npm run generate && http-server out" + "serve": "npm run generate && http-server out", + "generate-post": "node bin/generate-post.js" }, "dependencies": { "@sindresorhus/slugify": "^0.6.0", @@ -14,7 +15,8 @@ "highlight.js": "^9.13.1", "markdown-it": "^8.4.2", "node-sass": "^4.11.0", - "typescript": "^3.2.2" + "typescript": "^3.2.2", + "uuid": "^3.3.2" }, "devDependencies": { "@types/ejs": "^2.6.1",