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

110 lines
3.4 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:06:14 +03:00
const { RL } = 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-08-31 14:44:28 +03:00
if (req.thread.deleted && !req.user?.admin)
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");
if (title.length < 5 || title.length > limits.title) return res.error(400, "title must be between 5 - 128 characters");
if (content.length < 5 || content.length > limits.message) return res.error(400, "content must be between 5 - 1024 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
});
app.patch("/:id/", async (req, res) => {
2022-04-06 21:14:46 +03:00
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.");
const { title } = req.body;
2022-08-29 19:31:59 +03:00
if (!title) return res.error(400, "Missing thread title in request body.");
2022-09-17 16:27:01 +03:00
const limits = req.app.get("limits");
if (title.length < 5 || title.length > limits.title) return res.error(400, "title must be between 5 - 128 characters");
2022-09-17 00:27:38 +03:00
2022-08-29 19:31:59 +03:00
thread.title = title;
2022-09-21 23:06:14 +03:00
if (!thread.oldTitles.includes(title))
thread.oldTitles.push(title);
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.deleted) return res.error(403, "This thread is already deleted.");
thread.deleted = true;
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-08-26 20:12:23 +03:00
app.post("/:id/undelete", async (req, res) => {
2022-08-31 14:44:28 +03:00
const { thread } = req;
2022-08-26 20:12:23 +03:00
if (!thread.deleted) return res.error(404, "This thread is not deleted, first, delete it.");
thread.deleted = false;
2022-08-31 14:44:28 +03:00
thread.edited = true;
2022-08-29 19:31:59 +03:00
2022-08-26 20:12:23 +03:00
await thread.save();
await MessageModel.updateMany({ threadID: thread.id }, { deleted: false });
2022-08-26 20:12:23 +03:00
2022-08-31 14:44:28 +03:00
res.complate(thread);
2022-08-26 20:12:23 +03:00
})
2022-04-03 22:03:47 +03:00
module.exports = app;