mirror of
https://github.com/Akif9748/akf-forum.git
synced 2024-12-22 23:59:08 +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 });
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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