Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 06570b1

Browse files
authored
Merge pull request #129 from MassiGy/relationships-review
Following & Blocking Systems Fixed
2 parents d90c2cd + bd57b8a commit 06570b1

File tree

9 files changed

+46
-31
lines changed

9 files changed

+46
-31
lines changed

‎src/controllers/User/Block/blockUser.js‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11

22
/**
33
* @module Block User Controller
4-
*
4+
*
55
* @param {Request} req - HTTP Request from the client
66
* @param {Response} res - HTTP Response for the client
7-
*
7+
*
88
* @description
99
* This controller will allow the user to block a specific user, if all parameters are correct.
10-
*
10+
*
1111
* @todo
1212
* Nothing for now.
1313
*/
@@ -40,11 +40,14 @@ module.exports.blockUser = async (req, res) => {
4040
if (!user) {
4141
return res.status(404).send({ msg: 'Current account not found.' })
4242
}
43-
if (user.hasBlocked(userToBlock)) {
43+
44+
const alreadyBlocked = await user.hasBlockedUser(userToBlock);
45+
46+
if (alreadyBlocked) {
4447
return res.status(400).send({ msg: 'You have already blocked this person.' });
4548
}
4649

47-
user.addBlocked(userToBlock);
50+
user.addBlockedUser(userToBlock);
4851

4952
return res.status(200).send({ msg: 'User blocked.' });
5053

‎src/controllers/User/Block/listBlocked.js‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,19 @@ module.exports.listBlocked = async (req, res) => {
1919

2020
try {
2121

22-
const user = await User.findOne({
22+
const userBlockedList = await User.findOne({
2323
where: {
2424
id: req.user.id,
2525
},
26-
})
26+
include: 'BlockedUser',
27+
});
2728

2829
if (!user) {
2930
return res.status(404).send({ msg: 'User not found.' });
3031
}
3132

32-
constblockedUsers=awaituser.getBlocked();
33-
return res.status(200).json(blockedUsers);
33+
34+
return res.status(200).json(userBlockedList.BlockedUser);
3435

3536
} catch (err) {
3637
console.log(err.message);

‎src/controllers/User/Block/queryBlock.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module.exports.queryBlock = async (req, res) => {
2323
const [blockedUser, user] = await Promise.all([
2424
User.findOne({
2525
where: {
26-
id: req.params.blockId,
26+
id: req.params.blockedId,
2727
},
2828
})
2929
,
@@ -41,7 +41,9 @@ module.exports.queryBlock = async (req, res) => {
4141
return res.status(404).send({ msg: 'Current account not found. Try loggin in.' })
4242
}
4343

44-
if (!user.hasBlocked(blockedUser)) {
44+
const alreadyBlocked = await user.hasBlockedUser(blockedUser);
45+
46+
if (!alreadyBlocked) {
4547
return res.status(401).send({ msg: "You have not blocked this person." });
4648
}
4749

‎src/controllers/User/Block/unblockUser.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,13 @@ module.exports.unblockUser = async (req, res) => {
4343
return res.status(404).send({ msg: 'Current account not found.' });
4444
}
4545

46-
if (!user.hasBlocked(blockedUser)) {
46+
const alreadyBlocked = await user.hasBlockedUser(blockedUser);
47+
48+
if (!alreadyBlocked) {
4749
return res.status(401).send({ msg: "You have not blocked this person." });
4850
}
4951

50-
user.removeBlocked(blockedUser);
52+
user.removeBlockedUser(blockedUser);
5153

5254
return res.status(200).send({ msg: 'User unblocked.' });
5355

‎src/controllers/User/Followers/followUser.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ module.exports.followUser = async (req, res) => {
4545
return res.status(404).send({ msg: 'Current account not found.' });
4646
}
4747

48-
if (userToFollow.hasFollower(user)) {
48+
49+
const alreadyFollower = await userToFollow.hasFollower(user);
50+
51+
if (alreadyFollower) {
4952
return res.status(401).send({ msg: "You are already following this person." });
5053
}
5154

‎src/controllers/User/Followers/listFollowers.js‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,25 @@
1313
*/
1414

1515
module.exports.listFollowers = async (req, res) => {
16-
16+
1717
if (!req.params.id) {
1818
return res.status(400).send({ msg: "Not All Parameters Provided." });
1919
}
2020

2121
try {
2222

23-
const user = await User.findOne({
23+
const userFollowersList = await User.findOne({
2424
where: {
2525
id: req.params.id,
2626
},
27+
include: 'Follower',
2728
});
2829

29-
if (!user) {
30+
if (!userFollowersList) {
3031
return res.status(404).send({ msg: "User not found." });
3132
}
3233

33-
const followers = await user.getFollowers();
34-
return res.status(200).json(followers);
34+
return res.status(200).json(userFollowersList.Follower);
3535

3636
} catch (err) {
3737
console.log(err.message);

‎src/controllers/User/Followers/queryFollower.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ module.exports.queryFollower = async (req, res) => {
3737

3838
])
3939

40-
41-
4240
if (!follower) {
4341
return res.status(404).send({ msg: 'Follower user not found.' });
4442
}
@@ -47,8 +45,10 @@ module.exports.queryFollower = async (req, res) => {
4745
return res.status(404).send({ msg: 'Followee user not found.' });
4846
}
4947

50-
if (!followee.hasFollower(follower)) {
51-
return res.status(401).send({
48+
const alreadyFollower = await followee.hasFollower(follower);
49+
50+
if (!alreadyFollower) {
51+
return res.status(400).send({
5252
msg: `User with id=${req.params.id} is not followed by user with id=${req.params.followerId}`
5353
});
5454
}

‎src/controllers/User/Followers/unfollowUser.js‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ module.exports.unfollowUser = async (req, res) => {
1919
return res.status(400).send({ msg: "Not All Parameters Provided." });
2020
}
2121

22-
2322
try {
24-
2523
const [followee, user] = await Promise.all([
2624

2725
User.findOne({
@@ -47,13 +45,12 @@ module.exports.unfollowUser = async (req, res) => {
4745
return res.status(404).send({ msg: 'Current user not found.' });
4846
}
4947

50-
if (!followee.hasFollower(user)) {
48+
const alreadyFollower = await followee.hasFollower(user);
49+
50+
if (!alreadyFollower) {
5151
return res.status(401).send({ msg: 'Your are not following this person.' });
5252
}
5353

54-
55-
56-
5754
followee.removeFollower(user);
5855

5956
return res.status(200).send({ msg: 'Your are not longer following this person.' });

‎src/routes/userRoutes.js‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ const { verifyEmail } = require('../middelwares/verifyEmail');
77
// User Endpoints
88
router.get('/', userControllers.listUsers);
99
router.get('/count', userControllers.countUsers);
10-
router.get('/:id/', userControllers.queryUser);
11-
router.get('/:id/status', userControllers.getStatus);
10+
1211

1312
router.post('/signup', userControllers.signup);
1413
router.post('/login', userControllers.login);
@@ -21,6 +20,14 @@ router.post('/delete', verifyLogin, userControllers.deleteAccount);
2120
router.get('/blockedUsers', verifyLogin, userControllers.listBlocked);
2221
router.get('/blockedUsers/:blockedId', verifyLogin, userControllers.queryBlock);
2322

23+
24+
// User endpoints -
25+
// Note: since id can be anything the get by id route sould be last to be invoked.
26+
27+
router.get('/:id/', userControllers.queryUser);
28+
router.get('/:id/status', userControllers.getStatus);
29+
30+
2431
router.post('/:toBlockId/block', verifyLogin, verifyEmail, userControllers.blockUser);
2532
router.post('/:toUnblockId/unblock', verifyLogin, verifyEmail, userControllers.unblockUser);
2633

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /