2022-03-21 23:53:22 +03:00
|
|
|
const { Thread, Message, User } = require("../classes");
|
|
|
|
const error = require("../errors/error")
|
|
|
|
|
|
|
|
const { Router } = require("express");
|
|
|
|
|
|
|
|
const app = Router();
|
|
|
|
|
2022-04-06 21:14:46 +03:00
|
|
|
app.get("/:id", async (req, res) => {
|
|
|
|
const message = await new Message().getById(req.params.id);
|
2022-03-21 23:53:22 +03:00
|
|
|
|
|
|
|
if (!message || message.deleted) return error(res, 404, "We have not got any message declared as this id.");
|
2022-04-06 21:14:46 +03:00
|
|
|
res.redirect("/threads/" + message.threadID);
|
2022-03-21 23:53:22 +03:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2022-04-03 21:01:55 +03:00
|
|
|
app.use(require("../middlewares/login"));
|
2022-03-21 23:53:22 +03:00
|
|
|
|
|
|
|
|
2022-04-06 21:14:46 +03:00
|
|
|
app.post("/", async (req, res) => {
|
|
|
|
|
|
|
|
const thread = await new Thread().getById(req.body.threadID);
|
2022-03-21 23:53:22 +03:00
|
|
|
if (thread) {
|
2022-04-06 21:14:46 +03:00
|
|
|
const message = await new Message(req.body.content, req.user, thread.id).takeId()
|
|
|
|
await message.write();
|
|
|
|
thread.push(message.id);
|
2022-03-21 23:53:22 +03:00
|
|
|
thread.write();
|
2022-04-06 21:14:46 +03:00
|
|
|
|
2022-03-21 23:53:22 +03:00
|
|
|
res.redirect('/threads/' + req.body.threadID);
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
error(res, 404, "We have not got this thread.");
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2022-04-06 21:14:46 +03:00
|
|
|
app.post("/:id/delete", async (req, res) => {
|
|
|
|
const message = await new Message().getById(req.params.id)
|
2022-03-21 23:53:22 +03:00
|
|
|
if (!message || message.deleted) return error(res, 404, "We have not got any message declared as this id.");
|
2022-04-06 21:14:46 +03:00
|
|
|
const user = req.user;
|
|
|
|
if (user.id != message.authorID && !user.admin)
|
2022-03-21 23:53:22 +03:00
|
|
|
return error(res, 403, "You have not got permission for this.");
|
|
|
|
message.deleted = true;
|
|
|
|
message.write();
|
|
|
|
|
|
|
|
|
2022-04-06 21:14:46 +03:00
|
|
|
res.status(200).redirect("/threads/" + message.threadID);
|
2022-03-21 23:53:22 +03:00
|
|
|
|
|
|
|
})
|
2022-04-06 21:14:46 +03:00
|
|
|
app.post("/:id/react", async (req, res) => {
|
2022-03-21 23:53:22 +03:00
|
|
|
const { id = null } = req.params;
|
|
|
|
const info = req.body;
|
2022-04-06 21:14:46 +03:00
|
|
|
const message = await new Message().getById(id);
|
2022-03-21 23:53:22 +03:00
|
|
|
if (message) {
|
|
|
|
if (!(req.session.userid in message.react))
|
|
|
|
message.react[req.session.userid] = "like" in info;
|
|
|
|
else
|
|
|
|
delete message.react[req.session.userid];
|
|
|
|
|
|
|
|
message.write();
|
2022-04-06 21:14:46 +03:00
|
|
|
res.redirect("/threads/" + message.threadID);
|
2022-03-21 23:53:22 +03:00
|
|
|
} else error(res, 404, "We have not got this Message for reacting.");
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2022-04-06 21:14:46 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
2022-03-21 23:53:22 +03:00
|
|
|
module.exports = app;
|