akf-forum/routes/api/routes/threads.js

111 lines
3.7 KiB
JavaScript
Raw Normal View History

const { MessageModel, ThreadModel } = require("../../../models");
2022-04-03 22:03:47 +03:00
const { Router } = require("express")
2022-09-21 23:54:48 +03:00
const { RL, threadEnum } = require('../../../lib');
2022-04-03 22:03:47 +03:00
const app = Router();
2022-08-31 14:44:28 +03:00
app.param("id", async (req, res, next, id) => {
req.thread = await ThreadModel.get(id);
2022-04-03 22:03:47 +03:00
2022-08-31 14:44:28 +03:00
if (!req.thread) return res.error(404, `We don't have any thread with id ${id}.`);
2022-04-03 22:03:47 +03:00
2022-09-21 23:54:48 +03:00
if (req.thread.state !== "OPEN" && !req.user?.admin)
2022-08-31 14:44:28 +03:00
return res.error(404, `You do not have permissions to view this thread with id ${id}.`)
2022-04-03 22:03:47 +03:00
2022-08-31 14:44:28 +03:00
next();
2022-04-03 22:03:47 +03:00
});
2022-08-31 14:44:28 +03:00
app.get("/:id", async (req, res) => res.complate(req.thread));
2022-08-11 17:55:48 +03:00
2022-08-31 14:44:28 +03:00
app.get("/:id/messages/", async (req, res) => {
2022-08-11 17:55:48 +03:00
2022-08-11 18:15:27 +03:00
const { id } = req.params;
2022-08-11 17:55:48 +03:00
const limit = Number(req.query.limit);
2022-08-28 17:34:29 +03:00
const skip = Number(req.query.skip);
2022-08-11 17:55:48 +03:00
const query = { threadID: id };
if (!req.user.admin) query.deleted = false;
2022-08-11 17:55:48 +03:00
2022-08-29 16:16:44 +03:00
const options = { sort: { time: -1 } };
2022-08-11 17:55:48 +03:00
if (limit) options.limit = limit;
2022-08-28 17:34:29 +03:00
if (skip) options.skip = skip;
2022-08-11 17:55:48 +03:00
const messages = await MessageModel.find(query, null, options)
2022-08-28 17:34:29 +03:00
if (!messages.length) return res.error(404, "We don't have any messages in this with your query thread.");
2022-08-11 17:55:48 +03:00
2022-08-31 14:44:28 +03:00
res.complate(messages);
2022-08-11 17:55:48 +03:00
})
2022-04-06 21:14:46 +03:00
2022-09-21 23:06:14 +03:00
app.post("/", RL(5 * 60_000, 1), async (req, res) => {
2022-04-06 21:14:46 +03:00
2022-09-09 20:55:05 +03:00
const { title, content, category } = req.body;
2022-08-10 02:08:18 +03:00
if (!content || !title) return res.error(400, "Missing content/title in request body.");
2022-09-17 16:27:01 +03:00
const limits = req.app.get("limits");
2022-09-21 23:54:48 +03:00
if (title.length < 5 || title.length > limits.title) return res.error(400, `title must be between 5 - ${limits.title} characters`);
if (content.length < 5 || content.length > limits.message) return res.error(400, `content must be between 5 - ${limits.message} characters`);
2022-08-31 14:44:28 +03:00
const { user } = req;
2022-08-10 02:08:18 +03:00
const thread = await new ThreadModel({ title, author: user }).takeId()
2022-09-09 20:55:05 +03:00
if (category)
thread.categoryID = category;
2022-08-10 02:08:18 +03:00
const message = await new MessageModel({ content, author: user, threadID: thread.id }).takeId()
await thread.push(message.id).save();
await message.save();
2022-04-06 21:14:46 +03:00
2022-08-31 14:44:28 +03:00
res.complate(thread);
2022-04-06 21:14:46 +03:00
});
2022-09-21 23:54:48 +03:00
app.patch("/:id/", async (req, res) => {
2022-08-31 14:44:28 +03:00
const { user, thread } = req;
2022-08-29 19:31:59 +03:00
2022-08-31 14:44:28 +03:00
if (user.id !== thread.authorID && !user.admin) return res.error(403, "You have not got permission for this.");
2022-09-21 23:54:48 +03:00
if (!Object.values(req.body).some(Boolean)) return res.error(400, "Missing thread informations for update in request body.");
const { title, state } = req.body;
if (title) {
const limits = req.app.get("limits");
if (title.length < 5 || title.length > limits.title) return res.error(400, `title must be between 5 - ${limits.title} characters`);
if (thread.oldTitles.at(-1) == title) return res.error(400, "You can't use the same title as the previous one.");
thread.oldTitles.push(thread.title = title);
}
2022-09-17 16:27:01 +03:00
2022-09-21 23:54:48 +03:00
if (state) {
if (!user.admin)
return res.error(403, "You have not got permission for change state.");
2022-09-17 00:27:38 +03:00
2022-09-21 23:54:48 +03:00
if (thread.state === state) return res.error(400, "You can't change thread state to same state.");
if (!threadEnum.includes(state)) return res.error(400, "Invalid thread state.");
if (thread.state === "DELETED")
await MessageModel.updateMany({ threadID: thread.id }, { deleted: false });
thread.state = state;
}
2022-09-21 23:06:14 +03:00
2022-08-29 19:31:59 +03:00
await thread.save();
2022-08-31 14:44:28 +03:00
res.complate(thread);
2022-08-29 19:31:59 +03:00
})
app.delete("/:id/", async (req, res) => {
2022-08-31 14:44:28 +03:00
const { user, thread } = req;
if (user.id != thread.authorID && !user.admin)
2022-08-11 17:55:48 +03:00
return res.error(403, "You have not got permission for this.");
if (thread.state == "DELETED") return res.error(404, "This thread is already deleted.");
2022-09-21 23:54:48 +03:00
thread.state = "DELETED";
await thread.save();
2022-08-31 16:05:23 +03:00
await MessageModel.updateMany({ threadID: thread.id }, { deleted: true });
2022-08-31 14:44:28 +03:00
res.complate(thread);
})
2022-04-03 22:03:47 +03:00
module.exports = app;