mirror of
https://github.com/Akif9748/akf-forum.git
synced 2024-11-10 22:45:04 +03:00
reactions are fixed
This commit is contained in:
parent
68e4a92b8a
commit
3378388e8c
5 changed files with 39 additions and 27 deletions
|
@ -58,9 +58,9 @@ GET ```/api/messages/0```
|
|||
"id": "0",
|
||||
"__v": 0,
|
||||
"react": {
|
||||
"0": true
|
||||
"like": [0],
|
||||
"dislike":[]
|
||||
},
|
||||
"authorID": "0",
|
||||
"reactCount": 1
|
||||
"authorID": "0"
|
||||
}
|
||||
```
|
|
@ -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);
|
||||
|
|
|
@ -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 });
|
||||
|
||||
|
|
|
@ -41,19 +41,33 @@ 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.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);
|
||||
}
|
||||
|
||||
|
||||
} 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}.`);
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
|
||||
res.complate(message.toObject({ virtuals: true }));
|
||||
} else error(res, 404, `We don't have any message with id ${req.params.id}.`);
|
||||
|
||||
|
||||
});
|
||||
|
||||
|
@ -76,7 +90,7 @@ 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.");
|
||||
|
||||
|
|
|
@ -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!')
|
||||
|
||||
|
|
Loading…
Reference in a new issue