mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-07-06 23:27:56 +02:00
add back cache and pubsub
This commit is contained in:
@@ -1,13 +1,25 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
var LRU = require('lru-cache');
|
||||
|
||||
|
||||
var db = require('../database');
|
||||
var pubsub = require('../pubsub');
|
||||
|
||||
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) {
|
||||
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) {
|
||||
@@ -36,21 +48,35 @@ module.exports = function (User) {
|
||||
};
|
||||
|
||||
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) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
blocked = blocked.map(uid => parseInt(uid, 10)).filter(Boolean);
|
||||
User.blocks._cache.set(uid, blocked);
|
||||
callback(null, blocked);
|
||||
});
|
||||
};
|
||||
|
||||
pubsub.on('user:blocks:cache:del', function (uid) {
|
||||
User.blocks._cache.del(uid);
|
||||
});
|
||||
|
||||
User.blocks.add = function (targetUid, uid, callback) {
|
||||
async.waterfall([
|
||||
async.apply(this.applyChecks, true, targetUid, uid),
|
||||
async.apply(db.sortedSetAdd.bind(db), 'uid:' + uid + ':blocked_uids', Date.now(), targetUid),
|
||||
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);
|
||||
};
|
||||
|
||||
@@ -59,6 +85,11 @@ module.exports = function (User) {
|
||||
async.apply(this.applyChecks, false, targetUid, uid),
|
||||
async.apply(db.sortedSetRemove.bind(db), 'uid:' + uid + ':blocked_uids', targetUid),
|
||||
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);
|
||||
};
|
||||
|
||||
@@ -73,14 +104,11 @@ module.exports = function (User) {
|
||||
};
|
||||
|
||||
User.blocks.filterUids = function (targetUid, uids, callback) {
|
||||
const sets = uids.map(uid => 'uid:' + uid + ':blocked_uids');
|
||||
db.isMemberOfSortedSets(sets, targetUid, function (err, isMembers) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
uids = uids.filter((uid, index) => isMembers[index]);
|
||||
callback(null, uids);
|
||||
});
|
||||
async.filter(uids, function (uid, next) {
|
||||
User.blocks.is(targetUid, uid, function (err, blocked) {
|
||||
next(err, !blocked);
|
||||
});
|
||||
}, callback);
|
||||
};
|
||||
|
||||
User.blocks.filter = function (uid, property, set, callback) {
|
||||
@@ -91,7 +119,7 @@ module.exports = function (User) {
|
||||
set = property;
|
||||
property = 'uid';
|
||||
}
|
||||
console.log('derp')
|
||||
|
||||
if (!Array.isArray(set) || !set.length || !set.every((item) => {
|
||||
if (!item) {
|
||||
return false;
|
||||
@@ -104,15 +132,15 @@ console.log('derp')
|
||||
}
|
||||
|
||||
const isPlain = typeof set[0] !== 'object';
|
||||
const values = set.map(function (item) {
|
||||
return parseInt(isPlain ? item : item[property], 10);
|
||||
});
|
||||
|
||||
db.isSortedSetMembers('uid:' + uid + ':blocked_uids', values, function (err, isMembers) {
|
||||
User.blocks.list(uid, function (err, blocked_uids) {
|
||||
if (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);
|
||||
});
|
||||
};
|
||||
|
||||
@@ -1229,7 +1229,6 @@ describe('Controllers', function () {
|
||||
function (res, body, next) {
|
||||
assert.equal(res.statusCode, 200);
|
||||
assert(body);
|
||||
console.log(body);
|
||||
var notif = body.notifications[0];
|
||||
assert.equal(notif.bodyShort, notifData.bodyShort);
|
||||
assert.equal(notif.bodyLong, notifData.bodyLong);
|
||||
@@ -2152,7 +2151,6 @@ describe('Controllers', function () {
|
||||
request(nconf.get('url') + '/api/compose', { json: true }, function (err, res, body) {
|
||||
assert.ifError(err);
|
||||
assert.equal(res.statusCode, 404);
|
||||
console.log(body);
|
||||
|
||||
plugins.unregisterHook('myTestPlugin', 'filter:composer.build', hookMethod);
|
||||
done();
|
||||
|
||||
@@ -1965,6 +1965,14 @@ describe('User', function () {
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should filter uids that are blocking targetUid', function (done) {
|
||||
User.blocks.filterUids(blockeeUid, [1, 2], function (err, filtered) {
|
||||
assert.ifError(err);
|
||||
assert.deepEqual(filtered, [2]);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user