all files / src/user/ follow.js

55% Statements 22/40
45.45% Branches 10/22
53.85% Functions 7/13
55% Lines 22/40
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101                                                                                                                                                             
 
'use strict';
 
var async = require('async'),
	plugins = require('../plugins'),
	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) {
		Iif (!parseInt(uid, 10) || !parseInt(theiruid, 10)) {
			return callback(new Error('[[error:invalid-uid]]'));
		}
 
		Iif (parseInt(uid, 10) === parseInt(theiruid, 10)) {
			return callback(new Error('[[error:you-cant-follow-yourself]]'));
		}
 
		async.waterfall([
			function (next) {
				User.exists(theiruid, next);
			},
			function (exists, next) {
				Iif (!exists) {
					return next(new Error('[[error:no-user]]'));
				}
				User.isFollowing(uid, theiruid, next);
			},
			function (isFollowing, next) {
				Eif (type === 'follow') {
					Iif (isFollowing) {
						return next(new Error('[[error:already-following]]'));
					}
					var now = Date.now();
					async.parallel([
						async.apply(db.sortedSetAdd, 'following:' + uid, now, theiruid),
						async.apply(db.sortedSetAdd, 'followers:' + theiruid, now, uid),
						async.apply(User.incrementUserFieldBy, uid, 'followingCount', 1),
						async.apply(User.incrementUserFieldBy, theiruid, 'followerCount', 1)
					], next);
				} else {
					if (!isFollowing) {
						return next(new Error('[[error:not-following]]'));
					}
					async.parallel([
						async.apply(db.sortedSetRemove, 'following:' + uid, theiruid),
						async.apply(db.sortedSetRemove, 'followers:' + theiruid, uid),
						async.apply(User.decrementUserFieldBy, uid, 'followingCount', 1),
						async.apply(User.decrementUserFieldBy, theiruid, 'followerCount', 1)
					], next);
				}
			}
		], callback);
	}
 
	User.getFollowing = function(uid, start, stop, callback) {
		getFollow(uid, 'following', start, stop, callback);
	};
 
	User.getFollowers = function(uid, start, stop, callback) {
		getFollow(uid, 'followers', start, stop, callback);
	};
 
	function getFollow(uid, type, start, stop, callback) {
		if (!parseInt(uid, 10)) {
			return callback(null, []);
		}
 
		db.getSortedSetRevRange(type + ':' + uid, start, stop, function(err, uids) {
			if (err) {
				return callback(err);
			}
 
			plugins.fireHook('filter:user.' + type, {
				uids: uids,
				uid: uid,
				start: start,
				stop: stop
			}, function(err, data) {
				User.getUsers(data.uids, uid, callback);
			});
		});
	}
 
	User.isFollowing = function(uid, theirid, callback) {
		Iif (!parseInt(uid, 10) || !parseInt(theirid, 10)) {
			return callback(null, false);
		}
		db.isSortedSetMember('following:' + uid, theirid, callback);
	};
 
};