shadowfacts.net/lib/activitypub/comments.ts

96 lines
2.7 KiB
TypeScript
Raw Normal View History

2019-03-01 23:42:28 +00:00
import { Router } from "express";
import { NoteObject, ActorObject } from "./activity";
2019-03-01 23:42:28 +00:00
import { getCachedActor } from "./federate";
import { getConnection } from "typeorm";
import Note from "../entity/Note";
import Article from "../entity/Article";
2019-03-01 23:42:28 +00:00
const domain = process.env.DOMAIN;
interface Comment {
id: string;
content: string;
published: string;
inReplyTo: string;
author: ActorObject;
2019-03-01 23:42:28 +00:00
}
async function getConversationComments(conversation: string): Promise<Comment[]> {
try {
const notes = await getConnection().getRepository(Note).find({ where: { conversation }, relations: ["actor"] });
return notes.map(it => {
return {
id: it.id,
content: it.content,
published: it.published,
inReplyTo: it.inReplyTo,
author: {
id: it.actor.id,
name: it.actor.displayName,
icon: it.actor.iconURL
} as ActorObject
} as Comment;
});
} catch (err) {
console.log("Couldn't load comments: ", err);
return [];
}
//return new Promise((resolve, reject) => {
//db.all("SELECT notes.id AS comment_id, notes.content, notes.published, notes.in_reply_to, actors.id AS actor_id, actors.display_name, actors.icon_url FROM notes INNER JOIN actors ON actors.id = notes.attributed_to WHERE notes.conversation = $conversation", {
//$conversation: conversation
//}, (err, rows) => {
//if (err) {
//reject(err);
//} else {
//const comments = rows.map(row => {
//return {
//id: row.comment_id,
//content: row.content,
//published: row.published,
//inReplyTo: row.in_reply_to,
//author: {
//id: row.actor_id,
//name: row.display_name,
//icon: row.icon_url
//} as ActorObject
//} as Comment;
//});
//resolve(comments);
//}
//})
//});
2019-03-01 23:42:28 +00:00
}
export default function comments(router: Router) {
router.get("/ap/conversation/:id", async (req, res) => {
const comments = await getConversationComments(`https://${domain}/ap/conversation/${req.params.id}`);
2019-03-01 23:42:28 +00:00
res.json(comments).end();
});
router.get("/comments", async (req, res) => {
2019-03-01 23:42:28 +00:00
const id = req.query.id;
if (!id) {
res.sendStatus(400).end();
return;
}
try {
const article = await getConnection().getRepository(Article).findOne(id);
const comments = await getConversationComments(article.conversation);
2019-03-01 23:42:28 +00:00
res.json(comments).end();
} catch (err) {
console.error("Couldn't retrieve conversation: ", err);
res.json([]).end();
}
//db.get("SELECT conversation FROM articles WHERE id = $id", {
//$id: id
//}, async (err, result) => {
//if (!result || !result.conversation) {
//res.json([]).end();
//return;
//}
//const comments = await getConversationComments(result.conversation, db);
//res.json(comments).end();
//});
2019-03-01 23:42:28 +00:00
});
}