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 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 }); return res.render("threads", { threads, user });
}); });
@ -29,12 +29,12 @@ app.get("/:id", async (req, res) => {
const thread = await ThreadModel.get(id); const thread = await ThreadModel.get(id);
if (thread) { if (thread && !thread.deleted) {
const user = req.user; const user = req.user;
const messages = await Promise.all(thread.messages.map(async id => { const messages = await Promise.all(thread.messages.map(async id => {
const message = await MessageModel.get(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 }) res.render("thread", { thread, messages, user })
@ -67,5 +67,19 @@ app.post("/", rateLimit({
res.redirect('/threads/' + thread.id); 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; module.exports = app;

View file

@ -5,7 +5,7 @@ const error = require("../errors/error");
const { UserModel, MessageModel, ThreadModel } = require("../models"); const { UserModel, MessageModel, ThreadModel } = require("../models");
app.get("/", async ({ user }, res) => { 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 }) return res.render("users", { users, user })
}); });

View file

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