mirror of
https://github.com/Akif9748/akf-forum.git
synced 2024-11-26 13:20:41 +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",
|
"id": "0",
|
||||||
"__v": 0,
|
"__v": 0,
|
||||||
"react": {
|
"react": {
|
||||||
"0": true
|
"like": [0],
|
||||||
|
"dislike":[]
|
||||||
},
|
},
|
||||||
"authorID": "0",
|
"authorID": "0"
|
||||||
"reactCount": 1
|
|
||||||
}
|
}
|
||||||
```
|
```
|
|
@ -11,15 +11,14 @@ const schema = new mongoose.Schema({
|
||||||
time: { type: Date, default: Date.now },
|
time: { type: Date, default: Date.now },
|
||||||
deleted: { type: Boolean, default: false },
|
deleted: { type: Boolean, default: false },
|
||||||
edited: { 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('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.methods.takeId = async function () {
|
schema.methods.takeId = async function () {
|
||||||
this.id = String(await model.count() || 0);
|
this.id = String(await model.count() || 0);
|
||||||
|
|
|
@ -25,8 +25,7 @@ app.use(async (req, res, next) => {
|
||||||
if (!user)
|
if (!user)
|
||||||
return res.error(401, "We have not got any user has got this name")
|
return res.error(401, "We have not got any user has got this name")
|
||||||
|
|
||||||
|
if (!await bcrypt.compare(password, user.password)) return res.error(401, 'Incorrect Password!');
|
||||||
if (!bcrypt.compare(password, user.password)) return res.error(401, 'Incorrect Password!');
|
|
||||||
|
|
||||||
req.user = await UserModel.findOne({ name: req.headers.username });
|
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);
|
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 }));
|
res.complate(message.toObject({ virtuals: true }));
|
||||||
|
|
||||||
|
@ -41,26 +41,40 @@ app.post("/", rateLimit({
|
||||||
app.post("/:id/react/:type", async (req, res) => {
|
app.post("/:id/react/:type", async (req, res) => {
|
||||||
|
|
||||||
const message = await MessageModel.get(req.params.id);
|
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)
|
if (req.params.type == "like") {
|
||||||
delete message.react[req.session.userid];
|
if (message.react.like.includes(req.user.id))
|
||||||
else
|
message.react.like.pull(req.user.id);
|
||||||
message.react[req.session.userid] = req.params.type === "like";
|
else {
|
||||||
message.markModified("react");
|
message.react.like.push(req.user.id);
|
||||||
await message.save();
|
message.react.dislike.pull(req.user.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
res.complate(message.toObject({ virtuals: true }));
|
} else if (req.params.type == "dislike") {
|
||||||
} else error(res, 404, `We don't have any message with id ${req.params.id}.`);
|
|
||||||
|
|
||||||
|
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) => {
|
app.post("/:id/delete", async (req, res) => {
|
||||||
const message = await MessageModel.get(req.params.id);
|
const message = await MessageModel.get(req.params.id);
|
||||||
if (!message || (message.deleted && req.user && !req.user.admin))
|
if (!message || (message.deleted && req.user && !req.user.admin))
|
||||||
return res.error(404, `We don't have any message with id ${req.params.id}.`);
|
return res.error(404, `We don't have any message with id ${req.params.id}.`);
|
||||||
const user = req.user;
|
const user = req.user;
|
||||||
if (user.id != message.authorID && !user.admin)
|
if (user.id != message.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.");
|
||||||
|
@ -76,7 +90,7 @@ app.post("/:id/undelete", async (req, res) => {
|
||||||
|
|
||||||
const message = await MessageModel.get(req.params.id);
|
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.");
|
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 });
|
const user = await SecretModel.findOne({ username });
|
||||||
if (user) {
|
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 });
|
const member = await UserModel.findOne({ name: username });
|
||||||
if (!member || member.deleted) return res.error( 403, 'Incorrect Username and/or Password!')
|
if (!member || member.deleted) return res.error( 403, 'Incorrect Username and/or Password!')
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue