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 - MODALS'S CSS & JS
- change category name - change category name
- click to user message count to view message W/search - click to user message count to view message W/search
- add deleted => this.state=="DELETED"
## Major Version History ## Major Version History
- V4: Caching - V4: Caching
- V3: New Theme - V3: New Theme

View file

@ -12,7 +12,7 @@ const schema = new mongoose.Schema({
title: { type: String, maxlength: limits.title }, title: { type: String, maxlength: limits.title },
oldTitles: [String], oldTitles: [String],
time: { type: Date, default: Date.now }, time: { type: Date, default: Date.now },
edited: { type: Boolean, default: false }, edited: { type: Boolean, default: false },
state: { type: String, default: defaultThreadState, enum: threadEnum }, state: { type: String, default: defaultThreadState, enum: threadEnum },
@ -22,6 +22,14 @@ const schema = new mongoose.Schema({
schema.methods.get_author = cache.getAuthor; 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 () { schema.methods.get_category = async function () {
return await require("./Category").findOne({ id: this.categoryID }) || { id: this.categoryID, name: "Unknown" }; 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); const model = mongoose.model('thread', schema);
model.get = async id => { model.get = id => model.findOne({ id }).then(thread => thread.get_author());
const thread = await model.findOne({ id })
return await thread.get_author();
};
module.exports = model; 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 (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 (!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 }); await MessageModel.updateMany({ threadID: thread.id }, { deleted: false });
thread.state = state; thread.state = state;
} }
@ -99,8 +99,8 @@ app.delete("/:id/", async (req, res) => {
if (user.id != thread.authorID && !user.admin) if (user.id != thread.authorID && !user.admin)
return res.error(403, "You have not got permission for this."); return res.error(403, "You have not got permission for this.");
if (thread.state == "DELETED") return res.error(404, "This thread is already deleted."); if (thread.deleted) return res.error(404, "This thread is already deleted.");
thread.state = "DELETED"; thread.deleted= true;
await thread.save(); await thread.save();
await MessageModel.updateMany({ threadID: thread.id }, { deleted: true }); await MessageModel.updateMany({ threadID: thread.id }, { deleted: true });

View file

@ -28,13 +28,13 @@
</div> </div>
<div style="text-align:center;padding:8px"> <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="delete_thread('<%= thread.id %>')" class="btn-outline-primary">DELETE</a>
<a onclick="edit_thread('<%= thread.id %>')" class="btn-outline-primary">EDIT</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> <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>
<div id="messages"> <div id="messages">

View file

@ -17,11 +17,11 @@
<a href="<%= thread.getLink() %>" class=""> <a href="<%= thread.getLink() %>" class="">
<div class="threads-box"> <div class="threads-box">
<div class="thread-box-title"> <div class="thread-box-title">
<% if (thread.state == "DELETED") { %> <span>[DELETED]</span><% } %> <% if (thread.deleted) { %> <span>[DELETED]</span><% } %>
<%= thread.title %> <%= thread.title %>
</div> </div>
<div class="box-username"> <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> <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> <%= thread.author.name %> <div class="avatar"><img src="<%=thread.author.avatar %>"> </div>