Added thread deleting

This commit is contained in:
Akif9748 2022-08-10 00:44:13 +03:00
parent 6fd8302166
commit f4933e44cf
3 changed files with 30 additions and 7 deletions

View file

@ -10,7 +10,7 @@ app.get("/", async (req, res) => {
const user = req.user;
const threads = await ThreadModel.find({}).limit(10);
const threads = await ThreadModel.find(user?.admin ? {} : { deleted: false }).limit(10);
return res.render("threads", { threads, user });
});
@ -29,12 +29,12 @@ app.get("/:id", async (req, res) => {
const thread = await ThreadModel.get(id);
if (thread) {
if (thread && !thread.deleted) {
const user = req.user;
const messages = await Promise.all(thread.messages.map(async id => {
const message = await MessageModel.get(id)
return (user.admin || !message?.deleted) ? message : null;
return user?.admin || !message?.deleted ? message : null;
}));
res.render("thread", { thread, messages, user })
@ -49,7 +49,7 @@ app.use(require("../middlewares/login"));
app.post("/", rateLimit({
windowMs: 10*60_000, max: 1, standardHeaders: true, legacyHeaders: false
windowMs: 10 * 60_000, max: 1, standardHeaders: true, legacyHeaders: false
}), async (req, res) => {
const { title = null, content = null } = req.body;
@ -67,5 +67,19 @@ app.post("/", rateLimit({
res.redirect('/threads/' + thread.id);
})
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/");
})
module.exports = app;

View file

@ -5,7 +5,7 @@ const error = require("../errors/error");
const { UserModel, MessageModel, ThreadModel } = require("../models");
app.get("/", async ({ user }, res) => {
const users = await UserModel.find(user.admin ? {} : { deleted: false });
const users = await UserModel.find(user?.admin ? {} : { deleted: false });
return res.render("users", { users, user })
});

View file

@ -12,9 +12,18 @@
<%= thread.title %>
</h1>
<h2>By <a href=<%="/users/" + thread.author.id %>> <%= thread.author.name %></a>
<h2 style="display:inline;">By <a href=<%="/users/" + thread.author.id %>> <%= thread.author.name %></a>
<img class="yuvarlak" src=<%= thread.author.avatar %> alt=<%= thread.author.name %>>
</h2>
<% if (user){ %>
<form style="display:inline;" action="/threads/<%= thread.id %>/delete/" method="post">
</a><button type="submit">DELETE</button>
</form>
<% }; %>
<hr>
<% messages.forEach(message=>{ %>
<% if (message){ %>