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.
nodebb/src/user/follow.js

90 lines
2.0 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) {
if (!parseInt(uid, 10) || !parseInt(theiruid, 10)) {
return callback(new Error('[[error:invalid-uid]]'));
}
if (parseInt(uid, 10) === parseInt(theiruid, 10)) {
return callback(new Error('[[error:you-cant-follow-yourself]]'));
}
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(uid, 'following:' + uid, callback);
};
User.getFollowers = function(uid, callback) {
getFollow(uid, 'followers:' + uid, callback);
};
function getFollow(uid, set, callback) {
if (!parseInt(uid, 10)) {
return callback(null, []);
}
db.getSetMembers(set, function(err, uids) {
if (err) {
return callback(err);
}
User.getUsers(uids, callback);
});
}
User.getFollowingCount = function(uid, callback) {
if (!parseInt(uid, 10)) {
return callback(null, 0);
}
db.setCount('following:' + uid, callback);
};
User.getFollowerCount = function(uid, callback) {
if (!parseInt(uid, 10)) {
return callback(null, 0);
}
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) {
if (!parseInt(uid, 10) || !parseInt(theirid, 10)) {
return callback(null, false);
}
db.isSetMember('following:' + uid, theirid, callback);
};
};