akf-forum/routes/threads.js

48 lines
1.2 KiB
JavaScript
Raw Normal View History

2022-03-21 23:53:22 +03:00
const { Router } = require("express");
const app = Router();
const rateLimit = require('express-rate-limit')
2022-03-21 23:53:22 +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-04-06 21:14:46 +03:00
const user = req.user;
2022-03-21 23:53:22 +03:00
2022-08-10 00:44:13 +03:00
const threads = await ThreadModel.find(user?.admin ? {} : { deleted: false }).limit(10);
2022-03-21 23:53:22 +03:00
2022-04-06 21:14:46 +03:00
return res.render("threads", { threads, user });
2022-03-21 23:53:22 +03:00
});
app.get("/create*", async (req, res) => {
2022-04-03 21:39:26 +03:00
2022-04-06 21:14:46 +03:00
const user = req.user
res.render("createThread", { user })
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;
const thread = await ThreadModel.get(id);
2022-08-10 02:08:18 +03:00
const user = req.user;
2022-03-21 23:53:22 +03:00
2022-08-10 02:08:18 +03:00
if (thread && (user?.admin || !thread.deleted)) {
2022-04-06 21:14:46 +03:00
const messages = await Promise.all(thread.messages.map(async id => {
const message = await MessageModel.get(id)
2022-08-11 00:38:44 +03:00
const arr = Object.values(message.react)
message.reactCount = arr.filter(Boolean).length - arr.filter(x => !x).length;
2022-08-10 00:44:13 +03:00
return user?.admin || !message?.deleted ? message : null;
2022-04-06 21:14:46 +03:00
}));
2022-03-21 23:53:22 +03:00
res.render("thread", { thread, messages, user })
} else
2022-08-11 00:49:44 +03:00
res.error( 404, "We have not got this thread.");
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;