From 3449cf321bc43f145842c0345578fccc8c871675 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Tue, 3 Jul 2018 18:43:29 -0400 Subject: [PATCH] add back cache and pubsub --- src/user/blocks.js | 62 ++++++++++++++++++++++++++++++++------------- test/controllers.js | 2 -- test/user.js | 8 ++++++ 3 files changed, 53 insertions(+), 19 deletions(-) diff --git a/src/user/blocks.js b/src/user/blocks.js index 483fae53f7..c390d41679 100644 --- a/src/user/blocks.js +++ b/src/user/blocks.js @@ -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); }); }; diff --git a/test/controllers.js b/test/controllers.js index 2346be5df2..2104fc3230 100644 --- a/test/controllers.js +++ b/test/controllers.js @@ -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(); diff --git a/test/user.js b/test/user.js index 6d8abfa98e..96b47921c6 100644 --- a/test/user.js +++ b/test/user.js @@ -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(); + }); + }); }); });