diff --git a/README.md b/README.md index 456da19..c4044c2 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/models/Thread.js b/models/Thread.js index a5f3407..4525a71 100644 --- a/models/Thread.js +++ b/models/Thread.js @@ -12,7 +12,7 @@ const schema = new mongoose.Schema({ title: { type: String, maxlength: limits.title }, oldTitles: [String], - + time: { type: Date, default: Date.now }, edited: { type: Boolean, default: false }, state: { type: String, default: defaultThreadState, enum: threadEnum }, @@ -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; \ No newline at end of file diff --git a/routes/api/routes/threads.js b/routes/api/routes/threads.js index f30d511..10fe377 100644 --- a/routes/api/routes/threads.js +++ b/routes/api/routes/threads.js @@ -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 }); diff --git a/views/thread.ejs b/views/thread.ejs index e8ff01f..e8c85b5 100644 --- a/views/thread.ejs +++ b/views/thread.ejs @@ -28,13 +28,13 @@
- <% if (user && (user.id === thread.authorID || user.admin ) && thread.state !== "DELETED"){ %> + <% if (user && (user.id === thread.authorID || user.admin ) && !thread.deleted){ %> DELETE EDIT - <% } else if (thread.state == "DELETED") { %> + <% } else if (thread.deleted) { %>

This thread has been deleted

- UNDELETE - <% }; %> + UNDELETE + <% }; %>
diff --git a/views/threads.ejs b/views/threads.ejs index 288619e..1e6418f 100644 --- a/views/threads.ejs +++ b/views/threads.ejs @@ -17,11 +17,11 @@
- <% if (thread.state == "DELETED") { %> [DELETED]<% } %> + <% if (thread.deleted) { %> [DELETED]<% } %> <%= thread.title %>