2019-02-20 22:12:29 +00:00
|
|
|
import express, { Router, Request, Response } from "express";
|
|
|
|
import { Page, PostMetadata } from "../metadata";
|
2019-08-17 18:50:18 +00:00
|
|
|
import { ArticleObject } from "./activity";
|
|
|
|
import Article from "../entity/Article";
|
|
|
|
import { getConnection } from "typeorm";
|
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;
|
|
|
|
|
2019-08-17 18:50:18 +00:00
|
|
|
export async function setup(posts: Page[]) {
|
|
|
|
const repository = getConnection().getRepository(Article);
|
2019-02-20 22:12:29 +00:00
|
|
|
for (const post of posts) {
|
|
|
|
const postMeta = <PostMetadata>post.metadata;
|
2019-08-17 18:50:18 +00:00
|
|
|
if (await repository.findOne(postMeta.permalink)) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-02-20 22:12:29 +00:00
|
|
|
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-08-17 18:50:18 +00:00
|
|
|
const article = new Article();
|
|
|
|
article.id = postMeta.permalink;
|
|
|
|
article.articleObject = articleObject;
|
|
|
|
article.conversation = articleObject.conversation;
|
|
|
|
article.hasFederated = false;
|
|
|
|
await getConnection().manager.save(article);
|
2019-02-20 22:12:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-17 18:50:18 +00:00
|
|
|
export async function toFederate(): Promise<[string, ArticleObject][]> {
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
const articles: Article[] = await getConnection().createQueryBuilder().select("article").from(Article, "article").where("article.hasFederated = :hasFederated", { hasFederated: false }).getMany();
|
|
|
|
|
|
|
|
let result: [string, ArticleObject][] = [];
|
|
|
|
articles.forEach(it => {
|
|
|
|
result.push([it.id, it.articleObject]);
|
2019-02-20 22:12:29 +00:00
|
|
|
});
|
2019-08-17 18:50:18 +00:00
|
|
|
resolve(result);
|
2019-02-20 22:12:29 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-20 23:07:29 +00:00
|
|
|
export function route(router: Router) {
|
2019-08-17 18:50:18 +00:00
|
|
|
router.use("/:category/:year/:slug/", async (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-08-17 18:50:18 +00:00
|
|
|
const id = `/${req.params.category}/${req.params.year}/${req.params.slug}/`;
|
|
|
|
const repository = getConnection().getRepository(Article);
|
|
|
|
try {
|
|
|
|
const article = await repository.findOne(id);
|
|
|
|
res.type("application/activity+json").json(article.articleObject).end();
|
|
|
|
} catch (err) {
|
|
|
|
res.status(500).end(err);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
});
|
2019-08-17 18:50:18 +00:00
|
|
|
}
|