2019-02-20 22:12:29 +00:00
|
|
|
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";
|
2019-02-20 22:12:29 +00:00
|
|
|
|
|
|
|
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()}`,
|
2019-02-20 22:12:29 +00:00
|
|
|
"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)", {
|
2019-02-20 22:12:29 +00:00
|
|
|
$id: postMeta.permalink,
|
|
|
|
$article_doc: JSON.stringify(articleObject),
|
2019-02-22 00:17:59 +00:00
|
|
|
$conversation: articleObject.conversation,
|
2019-02-20 22:12:29 +00:00
|
|
|
$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) {
|
2019-02-20 22:12:29 +00:00
|
|
|
router.use("/:category/:year/:slug/", (req, res, next) => {
|
2019-06-30 19:16:08 +00:00
|
|
|
const best = req.accepts(["text/html", "application/activity+json"]);
|
|
|
|
console.log(best);
|
|
|
|
if (best === "text/html") {
|
2019-02-20 22:12:29 +00:00
|
|
|
next();
|
2019-06-30 19:16:08 +00:00
|
|
|
} else if (best === "application/activity+json") {
|
2019-02-20 22:12:29 +00:00
|
|
|
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;
|
|
|
|
}
|
2019-06-30 19:16:08 +00:00
|
|
|
res.type("application/activity+json");
|
2019-02-20 22:12:29 +00:00
|
|
|
res.end(result.article_doc);
|
|
|
|
});
|
2019-06-30 19:16:08 +00:00
|
|
|
} else {
|
|
|
|
res.status(415).end("No acceptable content-type given. text/html or application/activity+json are supported");
|
2019-02-20 22:12:29 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|