reactions are fixed

This commit is contained in:
Akif9748 2022-08-28 21:14:02 +03:00
parent 68e4a92b8a
commit 3378388e8c
5 changed files with 39 additions and 27 deletions

View file

@ -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
} }
``` ```

View file

@ -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);

View file

@ -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 });

View file

@ -41,19 +41,33 @@ 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.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(); await message.save();
res.complate(message.toObject({ virtuals: true })); 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); 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.");

View file

@ -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!')