shadowfacts.net/lib/activitypub/articles.ts

82 lines
2.6 KiB
TypeScript
Raw Normal View History

import express, { Router, Request, Response } from "express";
import { Page, PostMetadata } from "../metadata";
import { Article } from "./activity";
import { Database } from "sqlite3";
2019-02-22 00:17:59 +00:00
import uuidv4 from "uuid/v4";
const domain = process.env.DOMAIN;
export async function setup(posts: Page[], db: Database) {
for (const post of posts) {
const postMeta = <PostMetadata>post.metadata;
const articleObject = {
"@context": [
"https://www.w3.org/ns/activitystreams",
],
"type": "Article",
"id": `https://${domain}${post.metadata.permalink}`,
"published": (<Date>postMeta.date).toISOString(),
"inReplyTo": null,
2019-02-22 00:17:59 +00:00
"conversation": `https://${domain}/ap/conversation/${uuidv4()}`,
"url": `https://${domain}${postMeta.permalink}`,
"attributedTo": `https://${domain}/ap/actor`,
"to": [
"https://www.w3.org/ns/activitystreams#Public"
],
"cc": [
`https://${domain}/ap/actor/followers`
],
"name": postMeta.title,
"content": post.text
};
2019-02-22 00:17:59 +00:00
db.run("INSERT OR IGNORE INTO articles(id, article_doc, conversation, has_federated) VALUES($id, $article_doc, $conversation, $has_federated)", {
$id: postMeta.permalink,
$article_doc: JSON.stringify(articleObject),
2019-02-22 00:17:59 +00:00
$conversation: articleObject.conversation,
$has_federated: 0
}, (err) => {
if (err) console.log(`Encountered error inserting article ${postMeta.permalink}`, err);
});
}
}
export async function toFederate(db: Database): Promise<[string, Article][]> {
return new Promise((resolve, reject) => {
db.all("SELECT id, article_doc FROM articles WHERE has_federated = $has_federated", {
$has_federated: 0
}, (err, rows) => {
if (err) reject(err);
else {
let result: [string, Article][] = [];
for (const row of rows) {
result.push([row.id, JSON.parse(row.article_doc)]);
}
resolve(result);
}
});
});
}
2019-02-20 23:07:29 +00:00
export function route(router: Router) {
router.use("/:category/:year/:slug/", (req, res, next) => {
const best = req.accepts(["text/html", "application/activity+json"]);
console.log(best);
if (best === "text/html") {
next();
} else if (best === "application/activity+json") {
const db = <Database>req.app.get("db")
db.get("SELECT article_doc FROM articles WHERE id = $id", {
$id: `/${req.params.category}/${req.params.year}/${req.params.slug}/`
}, (err, result) => {
if (err) {
res.status(500).end(err);
return;
}
res.type("application/activity+json");
res.end(result.article_doc);
});
} else {
res.status(415).end("No acceptable content-type given. text/html or application/activity+json are supported");
}
});
}