|
|
@ -1,13 +1,25 @@
|
|
|
|
'use strict';
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
|
|
var async = require('async');
|
|
|
|
var async = require('async');
|
|
|
|
|
|
|
|
var LRU = require('lru-cache');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var db = require('../database');
|
|
|
|
var db = require('../database');
|
|
|
|
|
|
|
|
var pubsub = require('../pubsub');
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = function (User) {
|
|
|
|
module.exports = function (User) {
|
|
|
|
User.blocks = {};
|
|
|
|
User.blocks = {
|
|
|
|
|
|
|
|
_cache: LRU({
|
|
|
|
|
|
|
|
max: 100,
|
|
|
|
|
|
|
|
length: function () { return 1; },
|
|
|
|
|
|
|
|
maxAge: 0,
|
|
|
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
User.blocks.is = function (targetUid, uid, callback) {
|
|
|
|
User.blocks.is = function (targetUid, uid, callback) {
|
|
|
|
db.isSortedSetMember('uid:' + uid + ':blocked_uids', String(targetUid), callback);
|
|
|
|
User.blocks.list(uid, function (err, blocks) {
|
|
|
|
|
|
|
|
callback(err, blocks.includes(parseInt(targetUid, 10)));
|
|
|
|
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
User.blocks.can = function (callerUid, blockerUid, blockeeUid, callback) {
|
|
|
|
User.blocks.can = function (callerUid, blockerUid, blockeeUid, callback) {
|
|
|
@ -36,21 +48,35 @@ module.exports = function (User) {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
User.blocks.list = function (uid, callback) {
|
|
|
|
User.blocks.list = function (uid, callback) {
|
|
|
|
|
|
|
|
if (User.blocks._cache.has(uid)) {
|
|
|
|
|
|
|
|
return setImmediate(callback, null, User.blocks._cache.get(uid));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
db.getSortedSetRange('uid:' + uid + ':blocked_uids', 0, -1, function (err, blocked) {
|
|
|
|
db.getSortedSetRange('uid:' + uid + ':blocked_uids', 0, -1, function (err, blocked) {
|
|
|
|
if (err) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
blocked = blocked.map(uid => parseInt(uid, 10)).filter(Boolean);
|
|
|
|
blocked = blocked.map(uid => parseInt(uid, 10)).filter(Boolean);
|
|
|
|
|
|
|
|
User.blocks._cache.set(uid, blocked);
|
|
|
|
callback(null, blocked);
|
|
|
|
callback(null, blocked);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pubsub.on('user:blocks:cache:del', function (uid) {
|
|
|
|
|
|
|
|
User.blocks._cache.del(uid);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
User.blocks.add = function (targetUid, uid, callback) {
|
|
|
|
User.blocks.add = function (targetUid, uid, callback) {
|
|
|
|
async.waterfall([
|
|
|
|
async.waterfall([
|
|
|
|
async.apply(this.applyChecks, true, targetUid, uid),
|
|
|
|
async.apply(this.applyChecks, true, targetUid, uid),
|
|
|
|
async.apply(db.sortedSetAdd.bind(db), 'uid:' + uid + ':blocked_uids', Date.now(), targetUid),
|
|
|
|
async.apply(db.sortedSetAdd.bind(db), 'uid:' + uid + ':blocked_uids', Date.now(), targetUid),
|
|
|
|
async.apply(User.incrementUserFieldBy, uid, 'blocksCount', 1),
|
|
|
|
async.apply(User.incrementUserFieldBy, uid, 'blocksCount', 1),
|
|
|
|
|
|
|
|
function (_blank, next) {
|
|
|
|
|
|
|
|
User.blocks._cache.del(uid);
|
|
|
|
|
|
|
|
pubsub.publish('user:blocks:cache:del', uid);
|
|
|
|
|
|
|
|
setImmediate(next);
|
|
|
|
|
|
|
|
},
|
|
|
|
], callback);
|
|
|
|
], callback);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
@ -59,6 +85,11 @@ module.exports = function (User) {
|
|
|
|
async.apply(this.applyChecks, false, targetUid, uid),
|
|
|
|
async.apply(this.applyChecks, false, targetUid, uid),
|
|
|
|
async.apply(db.sortedSetRemove.bind(db), 'uid:' + uid + ':blocked_uids', targetUid),
|
|
|
|
async.apply(db.sortedSetRemove.bind(db), 'uid:' + uid + ':blocked_uids', targetUid),
|
|
|
|
async.apply(User.decrementUserFieldBy, uid, 'blocksCount', 1),
|
|
|
|
async.apply(User.decrementUserFieldBy, uid, 'blocksCount', 1),
|
|
|
|
|
|
|
|
function (_blank, next) {
|
|
|
|
|
|
|
|
User.blocks._cache.del(uid);
|
|
|
|
|
|
|
|
pubsub.publish('user:blocks:cache:del', uid);
|
|
|
|
|
|
|
|
setImmediate(next);
|
|
|
|
|
|
|
|
},
|
|
|
|
], callback);
|
|
|
|
], callback);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
@ -73,14 +104,11 @@ module.exports = function (User) {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
User.blocks.filterUids = function (targetUid, uids, callback) {
|
|
|
|
User.blocks.filterUids = function (targetUid, uids, callback) {
|
|
|
|
const sets = uids.map(uid => 'uid:' + uid + ':blocked_uids');
|
|
|
|
async.filter(uids, function (uid, next) {
|
|
|
|
db.isMemberOfSortedSets(sets, targetUid, function (err, isMembers) {
|
|
|
|
User.blocks.is(targetUid, uid, function (err, blocked) {
|
|
|
|
if (err) {
|
|
|
|
next(err, !blocked);
|
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, callback);
|
|
|
|
uids = uids.filter((uid, index) => isMembers[index]);
|
|
|
|
|
|
|
|
callback(null, uids);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
User.blocks.filter = function (uid, property, set, callback) {
|
|
|
|
User.blocks.filter = function (uid, property, set, callback) {
|
|
|
@ -91,7 +119,7 @@ module.exports = function (User) {
|
|
|
|
set = property;
|
|
|
|
set = property;
|
|
|
|
property = 'uid';
|
|
|
|
property = 'uid';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
console.log('derp')
|
|
|
|
|
|
|
|
if (!Array.isArray(set) || !set.length || !set.every((item) => {
|
|
|
|
if (!Array.isArray(set) || !set.length || !set.every((item) => {
|
|
|
|
if (!item) {
|
|
|
|
if (!item) {
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
@ -104,15 +132,15 @@ console.log('derp')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const isPlain = typeof set[0] !== 'object';
|
|
|
|
const isPlain = typeof set[0] !== 'object';
|
|
|
|
const values = set.map(function (item) {
|
|
|
|
User.blocks.list(uid, function (err, blocked_uids) {
|
|
|
|
return parseInt(isPlain ? item : item[property], 10);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db.isSortedSetMembers('uid:' + uid + ':blocked_uids', values, function (err, isMembers) {
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
set = set.filter((item, index) => !isMembers[index]);
|
|
|
|
|
|
|
|
|
|
|
|
set = set.filter(function (item) {
|
|
|
|
|
|
|
|
return !blocked_uids.includes(parseInt(isPlain ? item : item[property], 10));
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
callback(null, set);
|
|
|
|
callback(null, set);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|