mirror of
https://github.com/Akif9748/akf-forum.git
synced 2024-11-26 13:20:41 +03:00
38 lines
No EOL
1.2 KiB
JavaScript
38 lines
No EOL
1.2 KiB
JavaScript
const { Router } = require("express");
|
|
const app = Router();
|
|
|
|
const { ThreadModel,MessageModel } = require("../models")
|
|
|
|
|
|
app.get("/", async (req, res) => {
|
|
|
|
const threads = await ThreadModel.find(req.user?.admin ? {} : { deleted: false })//.limit(10);
|
|
|
|
return res.reply("threads", { threads });
|
|
});
|
|
|
|
|
|
app.get("/create*", (req, res) => res.reply("create_thread"));
|
|
|
|
app.get("/:id", async (req, res) => {
|
|
|
|
const { id } = req.params;
|
|
|
|
const thread = await ThreadModel.get(id);
|
|
thread.views++;
|
|
|
|
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
|
|
res.error(404, "We have not got this thread.");
|
|
thread.save();
|
|
});
|
|
|
|
|
|
module.exports = app; |