feat: #7743, user/digest, user/email, user/follow
parent
13aaf07bf5
commit
c610eb1430
@ -1,103 +1,85 @@
|
|||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
|
||||||
var plugins = require('../plugins');
|
var plugins = require('../plugins');
|
||||||
var db = require('../database');
|
var db = require('../database');
|
||||||
|
|
||||||
module.exports = function (User) {
|
module.exports = function (User) {
|
||||||
User.follow = function (uid, followuid, callback) {
|
User.follow = async function (uid, followuid) {
|
||||||
toggleFollow('follow', uid, followuid, callback);
|
await toggleFollow('follow', uid, followuid);
|
||||||
};
|
};
|
||||||
|
|
||||||
User.unfollow = function (uid, unfollowuid, callback) {
|
User.unfollow = async function (uid, unfollowuid) {
|
||||||
toggleFollow('unfollow', uid, unfollowuid, callback);
|
await toggleFollow('unfollow', uid, unfollowuid);
|
||||||
};
|
};
|
||||||
|
|
||||||
function toggleFollow(type, uid, theiruid, callback) {
|
async function toggleFollow(type, uid, theiruid) {
|
||||||
if (parseInt(uid, 10) <= 0 || parseInt(theiruid, 10) <= 0) {
|
if (parseInt(uid, 10) <= 0 || parseInt(theiruid, 10) <= 0) {
|
||||||
return callback(new Error('[[error:invalid-uid]]'));
|
throw new Error('[[error:invalid-uid]]');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (parseInt(uid, 10) === parseInt(theiruid, 10)) {
|
if (parseInt(uid, 10) === parseInt(theiruid, 10)) {
|
||||||
return callback(new Error('[[error:you-cant-follow-yourself]]'));
|
throw new Error('[[error:you-cant-follow-yourself]]');
|
||||||
|
}
|
||||||
|
const exists = await User.exists(theiruid);
|
||||||
|
if (!exists) {
|
||||||
|
throw new Error('[[error:no-user]]');
|
||||||
|
}
|
||||||
|
const isFollowing = await User.isFollowing(uid, theiruid);
|
||||||
|
if (type === 'follow') {
|
||||||
|
if (isFollowing) {
|
||||||
|
throw new Error('[[error:already-following]]');
|
||||||
|
}
|
||||||
|
const now = Date.now();
|
||||||
|
await Promise.all([
|
||||||
|
db.sortedSetAddBulk([
|
||||||
|
['following:' + uid, now, theiruid],
|
||||||
|
['followers:' + theiruid, now, uid],
|
||||||
|
]),
|
||||||
|
User.incrementUserFieldBy(uid, 'followingCount', 1),
|
||||||
|
User.incrementUserFieldBy(theiruid, 'followerCount', 1),
|
||||||
|
]);
|
||||||
|
} else {
|
||||||
|
if (!isFollowing) {
|
||||||
|
throw new Error('[[error:not-following]]');
|
||||||
|
}
|
||||||
|
await Promise.all([
|
||||||
|
db.sortedSetRemoveBulk([
|
||||||
|
['following:' + uid, theiruid],
|
||||||
|
['followers:' + theiruid, uid],
|
||||||
|
]),
|
||||||
|
User.decrementUserFieldBy(uid, 'followingCount', 1),
|
||||||
|
User.decrementUserFieldBy(theiruid, 'followerCount', 1),
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
async.waterfall([
|
|
||||||
function (next) {
|
|
||||||
User.exists(theiruid, next);
|
|
||||||
},
|
|
||||||
function (exists, next) {
|
|
||||||
if (!exists) {
|
|
||||||
return next(new Error('[[error:no-user]]'));
|
|
||||||
}
|
|
||||||
User.isFollowing(uid, theiruid, next);
|
|
||||||
},
|
|
||||||
function (isFollowing, next) {
|
|
||||||
if (type === 'follow') {
|
|
||||||
if (isFollowing) {
|
|
||||||
return next(new Error('[[error:already-following]]'));
|
|
||||||
}
|
|
||||||
var now = Date.now();
|
|
||||||
async.parallel([
|
|
||||||
async.apply(db.sortedSetAddBulk, [
|
|
||||||
['following:' + uid, now, theiruid],
|
|
||||||
['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);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
], function (err) {
|
|
||||||
callback(err);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
User.getFollowing = function (uid, start, stop, callback) {
|
User.getFollowing = async function (uid, start, stop) {
|
||||||
getFollow(uid, 'following', start, stop, callback);
|
return await getFollow(uid, 'following', start, stop);
|
||||||
};
|
};
|
||||||
|
|
||||||
User.getFollowers = function (uid, start, stop, callback) {
|
User.getFollowers = async function (uid, start, stop) {
|
||||||
getFollow(uid, 'followers', start, stop, callback);
|
return await getFollow(uid, 'followers', start, stop);
|
||||||
};
|
};
|
||||||
|
|
||||||
function getFollow(uid, type, start, stop, callback) {
|
async function getFollow(uid, type, start, stop) {
|
||||||
if (parseInt(uid, 10) <= 0) {
|
if (parseInt(uid, 10) <= 0) {
|
||||||
return setImmediate(callback, null, []);
|
return [];
|
||||||
}
|
}
|
||||||
async.waterfall([
|
const uids = await db.getSortedSetRevRange(type + ':' + uid, start, stop);
|
||||||
function (next) {
|
const data = await plugins.fireHook('filter:user.' + type, {
|
||||||
db.getSortedSetRevRange(type + ':' + uid, start, stop, next);
|
uids: uids,
|
||||||
},
|
uid: uid,
|
||||||
function (uids, next) {
|
start: start,
|
||||||
plugins.fireHook('filter:user.' + type, {
|
stop: stop,
|
||||||
uids: uids,
|
});
|
||||||
uid: uid,
|
return await User.getUsers(data.uids, uid);
|
||||||
start: start,
|
|
||||||
stop: stop,
|
|
||||||
}, next);
|
|
||||||
},
|
|
||||||
function (data, next) {
|
|
||||||
User.getUsers(data.uids, uid, next);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
User.isFollowing = function (uid, theirid, callback) {
|
User.isFollowing = async function (uid, theirid) {
|
||||||
if (parseInt(uid, 10) <= 0 || parseInt(theirid, 10) <= 0) {
|
if (parseInt(uid, 10) <= 0 || parseInt(theirid, 10) <= 0) {
|
||||||
return setImmediate(callback, null, false);
|
return false;
|
||||||
}
|
}
|
||||||
db.isSortedSetMember('following:' + uid, theirid, callback);
|
return await db.isSortedSetMember('following:' + uid, theirid);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue