You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.5 KiB
JavaScript
69 lines
1.5 KiB
JavaScript
|
|
'use strict';
|
|
|
|
var async = require('async'),
|
|
db = require('./../database');
|
|
|
|
module.exports = function(User) {
|
|
|
|
User.follow = function(uid, followuid, callback) {
|
|
toggleFollow('follow', uid, followuid, callback);
|
|
};
|
|
|
|
User.unfollow = function(uid, unfollowuid, callback) {
|
|
toggleFollow('unfollow', uid, unfollowuid, callback);
|
|
};
|
|
|
|
function toggleFollow(type, uid, theiruid, callback) {
|
|
var command = type === 'follow' ? 'setAdd' : 'setRemove';
|
|
db[command]('following:' + uid, theiruid, function(err) {
|
|
if(err) {
|
|
return callback(err);
|
|
}
|
|
db[command]('followers:' + theiruid, uid, callback);
|
|
});
|
|
}
|
|
|
|
User.getFollowing = function(uid, callback) {
|
|
getFollow('following:' + uid, callback);
|
|
};
|
|
|
|
User.getFollowers = function(uid, callback) {
|
|
getFollow('followers:' + uid, callback);
|
|
};
|
|
|
|
function getFollow(set, callback) {
|
|
db.getSetMembers(set, function(err, uids) {
|
|
if(err) {
|
|
return callback(err);
|
|
}
|
|
|
|
User.getUsers(uids, callback);
|
|
});
|
|
}
|
|
|
|
User.getFollowingCount = function(uid, callback) {
|
|
db.setCount('following:' + uid, callback);
|
|
};
|
|
|
|
User.getFollowerCount = function(uid, callback) {
|
|
db.setCount('followers:' + uid, callback);
|
|
};
|
|
|
|
User.getFollowStats = function (uid, callback) {
|
|
async.parallel({
|
|
followingCount: function(next) {
|
|
User.getFollowingCount(uid, next);
|
|
},
|
|
followerCount : function(next) {
|
|
User.getFollowerCount(uid, next);
|
|
}
|
|
}, callback);
|
|
};
|
|
|
|
User.isFollowing = function(uid, theirid, callback) {
|
|
db.isSetMember('following:' + uid, theirid, callback);
|
|
};
|
|
|
|
};
|