From 44d07ea543e24fd1bc2d00de90414f6a9aae40e6 Mon Sep 17 00:00:00 2001 From: Baris Soner Usakli Date: Mon, 17 Jun 2013 15:25:09 -0400 Subject: [PATCH] changed friends to following/followers, updated routes and the user.js functions --- public/src/forum/account.js | 23 +++++---- public/src/forum/followers.js | 29 ++++++++++++ public/src/forum/following.js | 44 +++++++++++++++++ public/templates/account.tpl | 5 +- public/templates/accountedit.tpl | 3 +- public/templates/config.json | 3 +- public/templates/followers.tpl | 47 ++++++++++++++++++ public/templates/following.tpl | 48 +++++++++++++++++++ public/templates/friends.tpl | 47 ------------------ src/routes/user.js | 43 ++++++++++++----- src/user.js | 81 +++++++++++++++++++------------- 11 files changed, 266 insertions(+), 107 deletions(-) create mode 100644 public/src/forum/followers.js create mode 100644 public/src/forum/following.js create mode 100644 public/templates/followers.tpl create mode 100644 public/templates/following.tpl delete mode 100644 public/templates/friends.tpl diff --git a/public/src/forum/account.js b/public/src/forum/account.js index 72f59b0cd8..0e8129bae9 100644 --- a/public/src/forum/account.js +++ b/public/src/forum/account.js @@ -1,7 +1,7 @@ (function() { var yourid = templates.get('yourid'), theirid = templates.get('theirid'), - isFriend = templates.get('isFriend'); + isFollowing = templates.get('isFollowing'); $(document).ready(function() { @@ -12,25 +12,24 @@ postcount.html(app.addCommas(postcount.html())); var editLink = $('#editLink'); - var addFriendBtn = $('#add-friend-btn'); - - + var followBtn = $('#follow-btn'); + if( yourid !== theirid) { editLink.hide(); - if(isFriend) - addFriendBtn.hide(); + if(isFollowing) + followBtn.hide(); else - addFriendBtn.show(); + followBtn.show(); } else { - addFriendBtn.hide(); + followBtn.hide(); } - addFriendBtn.on('click', function() { - $.post('/users/addfriend', {uid: theirid}, + followBtn.on('click', function() { + $.post('/users/follow', {uid: theirid}, function(data) { - addFriendBtn.remove(); - $('#user-action-alert').html('Friend Added!').show(); + followBtn.remove(); + $('#user-action-alert').html('You are now following'+ $('.account-username').text() +'!').show(); } ); return false; diff --git a/public/src/forum/followers.js b/public/src/forum/followers.js new file mode 100644 index 0000000000..ee7e019562 --- /dev/null +++ b/public/src/forum/followers.js @@ -0,0 +1,29 @@ +(function() { + + var yourid = templates.get('yourid'), + theirid = templates.get('theirid'), + followersCount = templates.get('followersCount'); + + $(document).ready(function() { + + if(parseInt(followersCount, 10) === 0) { + $('#no-followers-notice').show(); + } + var editLink = $('#editLink'); + + if(yourid !== theirid) { + editLink.hide(); + } + + $('.reputation').each(function(index, element) { + $(element).html(app.addCommas($(element).html())); + }); + + $('.postcount').each(function(index, element) { + $(element).html(app.addCommas($(element).html())); + }); + + }); + + +}()); \ No newline at end of file diff --git a/public/src/forum/following.js b/public/src/forum/following.js new file mode 100644 index 0000000000..6e44908119 --- /dev/null +++ b/public/src/forum/following.js @@ -0,0 +1,44 @@ +(function() { + + var yourid = templates.get('yourid'), + theirid = templates.get('theirid'), + followingCount = templates.get('followingCount'); + + $(document).ready(function() { + + if(parseInt(followingCount, 10) === 0) { + $('#no-following-notice').show(); + } + var editLink = $('#editLink'); + + if(yourid !== theirid) { + editLink.hide(); + $('.unfollow-btn').hide(); + } + else { + $('.unfollow-btn').on('click',function(){ + + var removeBtn = $(this); + var followingUid = $(this).attr('followingUid'); + + $.post('/users/unfollow', {uid: followingUid}, + function(data) { + removeBtn.parent().remove(); + } + ); + return false; + }); + } + + $('.reputation').each(function(index, element) { + $(element).html(app.addCommas($(element).html())); + }); + + $('.postcount').each(function(index, element) { + $(element).html(app.addCommas($(element).html())); + }); + + }); + + +}()); \ No newline at end of file diff --git a/public/templates/account.tpl b/public/templates/account.tpl index 4a288e7bba..a15b9aa9e4 100644 --- a/public/templates/account.tpl +++ b/public/templates/account.tpl @@ -8,7 +8,8 @@ {username} @@ -65,7 +66,7 @@
- Follow + Follow

diff --git a/public/templates/accountedit.tpl b/public/templates/accountedit.tpl index 878b6fe08b..63efebca48 100644 --- a/public/templates/accountedit.tpl +++ b/public/templates/accountedit.tpl @@ -66,7 +66,8 @@ edit diff --git a/public/templates/config.json b/public/templates/config.json index d562f3172e..61efdeef50 100644 --- a/public/templates/config.json +++ b/public/templates/config.json @@ -16,7 +16,8 @@ "install/social/?": "install/social", "install/privileges/?": "install/privileges", "users[^]*edit": "accountedit", - "users[^]*friends": "friends", + "users[^]*following": "following", + "users[^]*followers": "followers", "users/[^]*": "account", "latest": "category", "popular": "category", diff --git a/public/templates/followers.tpl b/public/templates/followers.tpl new file mode 100644 index 0000000000..6e5b9b0f70 --- /dev/null +++ b/public/templates/followers.tpl @@ -0,0 +1,47 @@ + +
+ + + + + +
+ + +
+ + + +
+ {followers.username} +
+
+ {followers.reputation} + +
+
+ {followers.postcount} + +
+
+ + +
+
This user doesn't have any followers :(
+
+ + + + + + \ No newline at end of file diff --git a/public/templates/following.tpl b/public/templates/following.tpl new file mode 100644 index 0000000000..ac0c3ff4b5 --- /dev/null +++ b/public/templates/following.tpl @@ -0,0 +1,48 @@ + +
+ + + + + +
+ + +
+ + + +
+ {following.username} +
+
+ {following.reputation} + +
+
+ {following.postcount} + +
+ Unfollow +
+ + +
+
This user isn't following anyone :(
+
+ + + + + + diff --git a/public/templates/friends.tpl b/public/templates/friends.tpl deleted file mode 100644 index aee731db35..0000000000 --- a/public/templates/friends.tpl +++ /dev/null @@ -1,47 +0,0 @@ - -
- - - - - -
- - -
- - - -
- {friends.username} -
-
- {friends.reputation} - -
-
- {friends.postcount} - -
- Unfollow -
- - -
-
This user doesn't have any friends :(
-
- - - - - - diff --git a/src/routes/user.js b/src/routes/user.js index b40660fef1..c657cf0592 100644 --- a/src/routes/user.js +++ b/src/routes/user.js @@ -202,36 +202,44 @@ var user = require('./../user.js'), res.send({}); }); - app.post('/users/addfriend', function(req, res){ + app.post('/users/follow', function(req, res){ if(!req.user) return res.redirect('/403'); if(req.user.uid == req.body.uid) return res.redirect('/'); - user.addFriend(req.user.uid, req.body.uid, function(data) { + user.follow(req.user.uid, req.body.uid, function(data) { res.send({data:data}); }); }); - app.post('/users/removefriend', function(req, res){ + app.post('/users/unfollow', function(req, res){ if(!req.user) return res.redirect('/403'); if(req.user.uid == req.body.uid) return res.redirect('/'); - user.removeFriend(req.user.uid, req.body.uid, function(data) { + user.unfollow(req.user.uid, req.body.uid, function(data) { res.send({data:data}); }); }); - app.get('/users/:username/friends', function(req, res){ + app.get('/users/:username/following', function(req, res) { if(!req.user) return res.redirect('/403'); - res.send(build_header() + app.create_route('users/'+req.params.username+'/friends','friends') + templates['footer']); + res.send(build_header() + app.create_route('users/'+req.params.username+'/following','following') + templates['footer']); + }); + + app.get('/users/:username/followers', function(req, res) { + + if(!req.user) + return res.redirect('/403'); + + res.send(build_header() + app.create_route('users/'+req.params.username+'/followers','followers') + templates['footer']); }); function api_method(req, res) { @@ -246,13 +254,24 @@ var user = require('./../user.js'), }); } - else if(String(req.params.section).toLowerCase() === 'friends') { + else if(String(req.params.section).toLowerCase() === 'following') { + + getUserDataByUserName(req.params.username, callerUID, function(userData) { + + user.getFollowing(userData.uid, function(followingData){ + userData.following = followingData; + userData.followingCount = followingData.length; + res.send(JSON.stringify(userData)); + }); + }); + } + else if(String(req.params.section).toLowerCase() === 'followers') { getUserDataByUserName(req.params.username, callerUID, function(userData) { - user.getFriends(userData.uid, function(friendsData){ - userData.friends = friendsData; - userData.friendCount = friendsData.length; + user.getFollowers(userData.uid, function(followersData){ + userData.followers = followersData; + userData.followersCount = followersData.length; res.send(JSON.stringify(userData)); }); }); @@ -264,8 +283,8 @@ var user = require('./../user.js'), } else { getUserDataByUserName(req.params.username, callerUID, function(userData) { - user.isFriend(callerUID, userData.theirid, function(isFriend) { - userData.isFriend = isFriend; + user.isFollowing(callerUID, userData.theirid, function(isFollowing) { + userData.isFollowing = isFollowing; userData.signature = marked(userData.signature || ''); diff --git a/src/user.js b/src/user.js index 5c1221ca40..7c1b716bce 100644 --- a/src/user.js +++ b/src/user.js @@ -388,54 +388,71 @@ var utils = require('./../public/src/utils.js'), } } - User.addFriend = function(uid, friendid, callback) { - RDB.sadd('user:'+uid+':friends', friendid, function(err, data){ - if(err === null) - callback(data); + User.follow = function(uid, followid, callback) { + RDB.sadd('user:'+uid+':following', followid, function(err, data) { + if(err === null) { + RDB.sadd('user:'+followid+':followers', uid, function(err, data) { + callback(data); + }); + } else console.log(err); - }) + }); } - User.getFriends = function(uid, callback) { - RDB.smembers('user:'+uid+':friends', function(err, data){ - if(err === null){ - - var friendsData = []; - - if(data.length === 0) { - callback(friendsData); - return; - } - - for(var i=0, ii=data.length; i