shadowfacts.net/lib/activitypub/comments.ts

64 lines
1.8 KiB
TypeScript
Raw Normal View History

2019-03-01 23:42:28 +00:00
import { Router } from "express";
import { Database } from "sqlite3";
import { Note, Actor } from "./activity";
import { getCachedActor } from "./federate";
const domain = process.env.DOMAIN;
interface Comment extends Note {
author: Actor;
}
async function getConversationComments(conversation: string, db: Database): Promise<Comment[]> {
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 Actor
} as Comment;
});
resolve(comments);
}
})
});
}
export default function comments(router: Router) {
router.get("/ap/conversation/:id", async (req, res) => {
const db = req.app.get("db") as Database;
const comments = await getConversationComments(`https://${domain}/ap/conversation/${req.params.id}`, db);
res.json(comments).end();
});
router.get("/comments", (req, res) => {
const id = req.query.id;
if (!id) {
res.sendStatus(400).end();
return;
}
const db = req.app.get("db") as Database;
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();
});
});
}