2022-03-21 23:53:22 +03:00
|
|
|
const { Router } = require("express");
|
|
|
|
const app = Router();
|
2022-08-10 00:22:12 +03:00
|
|
|
const rateLimit = require('express-rate-limit')
|
2022-03-21 23:53:22 +03:00
|
|
|
|
2022-04-06 21:14:46 +03:00
|
|
|
const error = require("../errors/error")
|
2022-08-09 19:16:34 +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
|
|
|
});
|
|
|
|
|
|
|
|
|
2022-08-09 19:16:34 +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
|
2022-08-09 19:16:34 +03:00
|
|
|
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;
|
|
|
|
|
2022-08-09 19:16:34 +03:00
|
|
|
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 => {
|
2022-08-09 19:16:34 +03:00
|
|
|
const message = await MessageModel.get(id)
|
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
|
|
|
|
error(res, 404, "We have not got this thread.");
|
|
|
|
});
|
|
|
|
|
2022-04-03 21:01:55 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
2022-04-03 21:39:26 +03:00
|
|
|
app.use(require("../middlewares/login"));
|
2022-04-03 21:01:55 +03:00
|
|
|
|
|
|
|
|
2022-08-10 00:22:12 +03:00
|
|
|
app.post("/", rateLimit({
|
2022-08-10 02:08:18 +03:00
|
|
|
windowMs: 10 * 60_000, max: 1, standardHeaders: true, legacyHeaders: false,
|
|
|
|
handler: (request, response, next, options) =>
|
|
|
|
!request.user.admin ?
|
|
|
|
error(response, options.statusCode, "You are begin ratelimited")
|
|
|
|
: next()
|
2022-08-10 00:22:12 +03:00
|
|
|
}), async (req, res) => {
|
2022-03-21 23:53:22 +03:00
|
|
|
|
|
|
|
const { title = null, content = null } = req.body;
|
|
|
|
|
2022-04-06 21:14:46 +03:00
|
|
|
if (!title || !content) return error(res, 400, "Title and/or content is missing");
|
2022-08-09 19:16:34 +03:00
|
|
|
const user = req.user
|
|
|
|
const thread = await new ThreadModel({ title, author: user }).takeId()
|
|
|
|
|
|
|
|
const message = await new MessageModel({ content, author: user, threadID: thread.id }).takeId()
|
2022-03-21 23:53:22 +03:00
|
|
|
|
2022-08-09 19:16:34 +03:00
|
|
|
await thread.push(message.id).save();
|
2022-03-21 23:53:22 +03:00
|
|
|
|
2022-08-09 19:16:34 +03:00
|
|
|
await message.save();
|
2022-03-21 23:53:22 +03:00
|
|
|
|
2022-04-06 21:14:46 +03:00
|
|
|
res.redirect('/threads/' + thread.id);
|
2022-03-21 23:53:22 +03:00
|
|
|
})
|
|
|
|
|
2022-08-10 00:44:13 +03:00
|
|
|
app.post("/:id/delete", async (req, res) => {
|
|
|
|
const thread = await ThreadModel.get(req.params.id);
|
|
|
|
if (!thread || thread.deleted) return error(res, 404, "We have not got any thread declared as this id.");
|
|
|
|
const user = req.user;
|
|
|
|
if (user.id != thread.authorID && !user.admin)
|
|
|
|
return error(res, 403, "You have not got permission for this.");
|
|
|
|
|
|
|
|
thread.deleted = true;
|
|
|
|
await thread.save();
|
|
|
|
|
|
|
|
|
|
|
|
res.status(200).redirect("/threads/");
|
|
|
|
|
|
|
|
})
|
2022-03-21 23:53:22 +03:00
|
|
|
|
|
|
|
module.exports = app;
|