2019-03-01 23:42:28 +00:00
|
|
|
import { Router } from "express";
|
2019-08-17 18:50:18 +00:00
|
|
|
import { NoteObject, ActorObject } from "./activity";
|
2019-03-01 23:42:28 +00:00
|
|
|
import { getCachedActor } from "./federate";
|
2019-08-17 18:50:18 +00:00
|
|
|
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;
|
|
|
|
|
2019-08-17 18:50:18 +00:00
|
|
|
interface Comment {
|
|
|
|
id: string;
|
|
|
|
content: string;
|
|
|
|
published: string;
|
|
|
|
inReplyTo: string;
|
|
|
|
author: ActorObject;
|
2019-03-01 23:42:28 +00:00
|
|
|
}
|
|
|
|
|
2019-08-17 18:50:18 +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 [];
|
|
|
|
}
|
2019-03-01 23:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default function comments(router: Router) {
|
|
|
|
router.get("/ap/conversation/:id", async (req, res) => {
|
2019-08-17 18:50:18 +00:00
|
|
|
const comments = await getConversationComments(`https://${domain}/ap/conversation/${req.params.id}`);
|
2019-03-01 23:42:28 +00:00
|
|
|
res.json(comments).end();
|
|
|
|
});
|
|
|
|
|
2019-08-17 18:50:18 +00:00
|
|
|
router.get("/comments", async (req, res) => {
|
2021-04-16 22:36:41 +00:00
|
|
|
const id = req.query.id as string;
|
2019-03-01 23:42:28 +00:00
|
|
|
if (!id) {
|
|
|
|
res.sendStatus(400).end();
|
|
|
|
return;
|
|
|
|
}
|
2019-08-17 18:50:18 +00:00
|
|
|
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();
|
2019-08-17 18:50:18 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.error("Couldn't retrieve conversation: ", err);
|
|
|
|
res.json([]).end();
|
|
|
|
}
|
2019-03-01 23:42:28 +00:00
|
|
|
});
|
2019-08-17 18:50:18 +00:00
|
|
|
}
|