Added virtuals for delete

This commit is contained in:
Akif9748 2022-10-09 20:10:19 +03:00
parent e824b844f6
commit 1de709c632
5 changed files with 20 additions and 15 deletions

View file

@ -63,7 +63,7 @@ Akf-forum has got an API for AJAX (fetch), other clients etc. And, you can learn
- MODALS'S CSS & JS
- change category name
- click to user message count to view message W/search
- add deleted => this.state=="DELETED"
## Major Version History
- V4: Caching
- V3: New Theme

View file

@ -22,6 +22,14 @@ const schema = new mongoose.Schema({
schema.methods.get_author = cache.getAuthor;
schema.virtual("deleted").get(function () {
return this.state === "DELETED";
}).set(function (value) {
this.set({ state: value ? "DELETED" : "OPEN" });
});
schema.methods.get_category = async function () {
return await require("./Category").findOne({ id: this.categoryID }) || { id: this.categoryID, name: "Unknown" };
}
@ -47,9 +55,6 @@ schema.methods.getLink = function (id = this.id) {
const model = mongoose.model('thread', schema);
model.get = async id => {
const thread = await model.findOne({ id })
return await thread.get_author();
};
model.get = id => model.findOne({ id }).then(thread => thread.get_author());
module.exports = model;

View file

@ -83,7 +83,7 @@ app.patch("/:id/", async (req, res) => {
if (thread.state === state) return res.error(400, "You can't change thread state to same state.");
if (!threadEnum.includes(state)) return res.error(400, "Invalid thread state.");
if (thread.state === "DELETED")
if (thread.deleted)
await MessageModel.updateMany({ threadID: thread.id }, { deleted: false });
thread.state = state;
}
@ -99,8 +99,8 @@ app.delete("/:id/", async (req, res) => {
if (user.id != thread.authorID && !user.admin)
return res.error(403, "You have not got permission for this.");
if (thread.state == "DELETED") return res.error(404, "This thread is already deleted.");
thread.state = "DELETED";
if (thread.deleted) return res.error(404, "This thread is already deleted.");
thread.deleted= true;
await thread.save();
await MessageModel.updateMany({ threadID: thread.id }, { deleted: true });

View file

@ -28,13 +28,13 @@
</div>
<div style="text-align:center;padding:8px">
<% if (user && (user.id === thread.authorID || user.admin ) && thread.state !== "DELETED"){ %>
<% if (user && (user.id === thread.authorID || user.admin ) && !thread.deleted){ %>
<a onclick="delete_thread('<%= thread.id %>')" class="btn-outline-primary">DELETE</a>
<a onclick="edit_thread('<%= thread.id %>')" class="btn-outline-primary">EDIT</a>
<% } else if (thread.state == "DELETED") { %>
<% } else if (thread.deleted) { %>
<h4 class="title" style="display:inline; font-size: 20px;">This thread has been deleted</h3>
<a onclick="undelete_thread('<%= thread.id %>')" class="btn-primary">UNDELETE</a>
<% }; %>
<a onclick="undelete_thread('<%= thread.id %>')" class="btn-primary">UNDELETE</a>
<% }; %>
</div>
<div id="messages">

View file

@ -17,11 +17,11 @@
<a href="<%= thread.getLink() %>" class="">
<div class="threads-box">
<div class="thread-box-title">
<% if (thread.state == "DELETED") { %> <span>[DELETED]</span><% } %>
<% if (thread.deleted) { %> <span>[DELETED]</span><% } %>
<%= thread.title %>
</div>
<div class="box-username">
<% if (user?.admin && thread.state !== "DELETED"){ %>
<% if (user?.admin && !thread.deleted){ %>
<a class="btn-danger" onclick="fetch('/api/threads/<%= thread.id %>/',{method:'DELETE'})"><i class="bx bx-trash bx-sm"></i></a>
<% } %>
<%= thread.author.name %> <div class="avatar"><img src="<%=thread.author.avatar %>"> </div>