feature: Add Friends

Now other user can be added as friends in Settings > Friends
This commit is contained in:
Moon Patel
2023-07-13 23:12:01 +05:30
parent 03fd3a0692
commit e8775ff8e1
5 changed files with 54 additions and 26 deletions
+2
View File
@@ -62,5 +62,7 @@ const userSchema = new Schema(
}
);
userSchema.index({ username: "text" });
const User = mongoose.model("User", userSchema);
module.exports = { User };
+6 -4
View File
@@ -75,11 +75,13 @@ router.get(
// TO BE TESTED
// add a friend
router.post(
"/:userid/friends/:friendid",
"/:userid/friends/:friendusername",
checkAuth,
catchAsync(async (req, res, next) => {
let { friendid } = req.params;
let friendData = await User.findById(friendid);
let { friendusername } = req.params;
if (req.userData.username === friendusername)
res.json({ success: false, error: { message: "Cannot add yourself as friend" } });
let friendData = await User.findOne({ username: friendusername });
if (friendData) {
if (friendData.friends.includes(req.userData._id)) {
res.json({ success: false, error: { message: "User is already added as a friend" } });
@@ -91,7 +93,7 @@ router.post(
res.json({ success: true });
}
} else {
res.status(404).json({ success: false, error: { message: "No user with the given username found" } });
res.status(404).json({ success: false, error: { message: "username does not exist" } });
}
})
);