Advertisement
AllenYuan

implement users following

May 16th, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //...
  2.  
  3. // follow the given user
  4. router.post('/:username/follow', auth.required, function(req, res, next){
  5.   var profileId = req.profile._id;
  6.   // grab the user from the request and call the follow method.
  7.   User.findById(req.payload.id).then(function(user){
  8.     if (!user) { return res.sendStatus(401); }
  9.  
  10.     return user.follow(profileId).then(function(){
  11.       return res.json({profile: req.profile.toProfileJSONFor(user)});
  12.     });
  13.   }).catch(next);
  14. });
  15.  
  16. // unfollow the given user
  17. router.delete('/:username/follow', auth.required, function(req, res, next){
  18.   var profileId = req.profile._id;
  19.   // find user, call unfollow request
  20.   User.findById(req.payload.id).then(function(user){
  21.     if (!user) { return res.sendStatus(401); }
  22.  
  23.     return user.unfollow(profileId).then(function(){
  24.       return res.json({profile: req.profile.toProfileJSONFor(user)});
  25.     });
  26.   }).catch(next);
  27. });
  28.  
  29. module.exports = router;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement