2022-03-21 23:53:22 +03:00
|
|
|
const { Router } = require("express");
|
|
|
|
const app = Router();
|
|
|
|
|
2022-08-28 18:19:03 +03:00
|
|
|
const { ThreadModel,MessageModel } = require("../models")
|
2022-03-21 23:53:22 +03:00
|
|
|
|
|
|
|
|
2022-04-06 21:14:46 +03:00
|
|
|
app.get("/", async (req, res) => {
|
2022-03-21 23:53:22 +03:00
|
|
|
|
2022-08-28 15:00:53 +03:00
|
|
|
const threads = await ThreadModel.find(req.user?.admin ? {} : { deleted: false })//.limit(10);
|
2022-03-21 23:53:22 +03:00
|
|
|
|
2022-08-27 10:31:16 +03:00
|
|
|
return res.reply("threads", { threads });
|
2022-03-21 23:53:22 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2022-08-28 15:00:53 +03:00
|
|
|
app.get("/create*", (req, res) => res.reply("create_thread"));
|
2022-04-03 21:39:26 +03:00
|
|
|
|
2022-04-06 21:14:46 +03:00
|
|
|
app.get("/:id", async (req, res) => {
|
2022-03-21 23:53:22 +03:00
|
|
|
|
|
|
|
const { id } = req.params;
|
|
|
|
|
2022-08-09 19:16:34 +03:00
|
|
|
const thread = await ThreadModel.get(id);
|
2022-08-28 15:00:53 +03:00
|
|
|
thread.views++;
|
2022-04-06 21:14:46 +03:00
|
|
|
|
2022-08-28 18:19:03 +03:00
|
|
|
if (thread && (req.user?.admin || !thread.deleted)) {
|
|
|
|
const messages = await Promise.all(thread.messages.map(async id => {
|
|
|
|
const message = await MessageModel.get(id)
|
|
|
|
message.content = message.content.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll("\"", """).replaceAll("'", "'").replaceAll("\n", "<br>")
|
|
|
|
return req.user?.admin || !message?.deleted ? message.toObject({ virtuals: true }) : null;
|
|
|
|
}));
|
|
|
|
|
|
|
|
res.reply("thread", { thread, messages, scroll: req.query.scroll || false });
|
|
|
|
} else
|
2022-08-27 10:31:16 +03:00
|
|
|
res.error(404, "We have not got this thread.");
|
2022-08-28 18:19:03 +03:00
|
|
|
thread.save();
|
2022-03-21 23:53:22 +03:00
|
|
|
});
|
|
|
|
|
2022-04-03 21:01:55 +03:00
|
|
|
|
2022-03-21 23:53:22 +03:00
|
|
|
module.exports = app;
|