mirror of
https://github.com/Akif9748/akf-forum.git
synced 2024-11-26 05:10:41 +03:00
Added thread deleting
This commit is contained in:
parent
6fd8302166
commit
f4933e44cf
3 changed files with 30 additions and 7 deletions
|
@ -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 })
|
||||
|
@ -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;
|
|
@ -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 })
|
||||
|
||||
});
|
||||
|
|
|
@ -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){ %>
|
||||
|
|
Loading…
Reference in a new issue