From 3378388e8c1cc146e3d536a051acaf487f94edc5 Mon Sep 17 00:00:00 2001 From: Akif9748 Date: Sun, 28 Aug 2022 21:14:02 +0300 Subject: [PATCH] reactions are fixed --- APIDOCS.md | 6 ++--- models/Message.js | 11 ++++----- routes/api/index.js | 3 +-- routes/api/routes/messages.js | 44 +++++++++++++++++++++++------------ routes/login.js | 2 +- 5 files changed, 39 insertions(+), 27 deletions(-) diff --git a/APIDOCS.md b/APIDOCS.md index b5b9c9b..79bcc1f 100644 --- a/APIDOCS.md +++ b/APIDOCS.md @@ -58,9 +58,9 @@ GET ```/api/messages/0``` "id": "0", "__v": 0, "react": { - "0": true + "like": [0], + "dislike":[] }, - "authorID": "0", - "reactCount": 1 + "authorID": "0" } ``` \ No newline at end of file diff --git a/models/Message.js b/models/Message.js index 8501fc2..2808503 100644 --- a/models/Message.js +++ b/models/Message.js @@ -11,15 +11,14 @@ const schema = new mongoose.Schema({ time: { type: Date, default: Date.now }, deleted: { type: Boolean, default: false }, edited: { type: Boolean, default: false }, - react: { type:Object, default: {} } + react: { + like: [Number], + dislike: [Number] + } }) -schema.virtual('authorID').get(function() { return this.author?.id; }); -schema.virtual('reactCount').get(function() { - const arr = Object.values(this.react) - return arr.filter(Boolean).length - arr.filter(x => !x).length; -}); +schema.virtual('authorID').get(function () { return this.author?.id; }); schema.methods.takeId = async function () { this.id = String(await model.count() || 0); diff --git a/routes/api/index.js b/routes/api/index.js index b24e072..51ed703 100644 --- a/routes/api/index.js +++ b/routes/api/index.js @@ -25,8 +25,7 @@ app.use(async (req, res, next) => { if (!user) return res.error(401, "We have not got any user has got this name") - - if (!bcrypt.compare(password, user.password)) return res.error(401, 'Incorrect Password!'); + if (!await bcrypt.compare(password, user.password)) return res.error(401, 'Incorrect Password!'); req.user = await UserModel.findOne({ name: req.headers.username }); diff --git a/routes/api/routes/messages.js b/routes/api/routes/messages.js index 00ea108..6c194c0 100644 --- a/routes/api/routes/messages.js +++ b/routes/api/routes/messages.js @@ -10,7 +10,7 @@ app.get("/:id", async (req, res) => { const message = await MessageModel.get(req.params.id); - if (!message || (message.deleted && req.user && !req.user.admin)) return res.error(404, `We don't have any message with id ${req.params.id}.`); + if (!message || (message.deleted && req.user && !req.user.admin)) return res.error(404, `We don't have any message with id ${req.params.id}.`); res.complate(message.toObject({ virtuals: true })); @@ -41,26 +41,40 @@ app.post("/", rateLimit({ app.post("/:id/react/:type", async (req, res) => { const message = await MessageModel.get(req.params.id); - if (message) { + if (!message) return error(res, 404, `We don't have any message with id ${req.params.id}.`); - if (req.user.id in message.react) - delete message.react[req.session.userid]; - else - message.react[req.session.userid] = req.params.type === "like"; - message.markModified("react"); - await message.save(); + if (req.params.type == "like") { + if (message.react.like.includes(req.user.id)) + message.react.like.pull(req.user.id); + else { + message.react.like.push(req.user.id); + message.react.dislike.pull(req.user.id); + } - - res.complate(message.toObject({ virtuals: true })); - } else error(res, 404, `We don't have any message with id ${req.params.id}.`); + } else if (req.params.type == "dislike") { + + if (message.react.dislike.includes(req.user.id)) + message.react.dislike.pull(req.user.id); + else { + message.react.dislike.push(req.user.id); + message.react.like.pull(req.user.id); + } + + } else { + return res.error(400, `We don't have any react type with name ${req.params.type}.`); + } + + await message.save(); + + res.complate(message.toObject({ virtuals: true })); }); app.post("/:id/delete", async (req, res) => { const message = await MessageModel.get(req.params.id); - if (!message || (message.deleted && req.user && !req.user.admin)) - return res.error(404, `We don't have any message with id ${req.params.id}.`); + if (!message || (message.deleted && req.user && !req.user.admin)) + return res.error(404, `We don't have any message with id ${req.params.id}.`); const user = req.user; if (user.id != message.authorID && !user.admin) return res.error(403, "You have not got permission for this."); @@ -76,8 +90,8 @@ app.post("/:id/undelete", async (req, res) => { const message = await MessageModel.get(req.params.id); - if (!message ) return res.error(404, `We don't have any message with id ${req.params.id}.`); - + if (!message) return res.error(404, `We don't have any message with id ${req.params.id}.`); + if (!message.deleted) return res.error(404, "This message is not deleted, first, delete it."); message.deleted = false; diff --git a/routes/login.js b/routes/login.js index f187091..abb073e 100644 --- a/routes/login.js +++ b/routes/login.js @@ -14,7 +14,7 @@ app.post("/", async (req, res) => { const user = await SecretModel.findOne({ username }); if (user) { - if (!bcrypt.compare(password, user.password)) return res.error( 403, 'Incorrect Password!') + if (!await bcrypt.compare(password, user.password)) return res.error( 403, 'Incorrect Password!') const member = await UserModel.findOne({ name: username }); if (!member || member.deleted) return res.error( 403, 'Incorrect Username and/or Password!')