mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-27 00:56:13 +01:00
refactor: allow array of uids for blocks.is/list
remove async.filter use cacheCreate
This commit is contained in:
@@ -1,24 +1,25 @@
|
||||
'use strict';
|
||||
|
||||
const async = require('async');
|
||||
const LRU = require('lru-cache');
|
||||
|
||||
const db = require('../database');
|
||||
const pubsub = require('../pubsub');
|
||||
const plugins = require('../plugins');
|
||||
const cacheCreate = require('../cacheCreate');
|
||||
|
||||
module.exports = function (User) {
|
||||
User.blocks = {
|
||||
_cache: new LRU({
|
||||
_cache: cacheCreate({
|
||||
name: 'user:blocks',
|
||||
max: 100,
|
||||
length: function () { return 1; },
|
||||
maxAge: 0,
|
||||
}),
|
||||
};
|
||||
|
||||
User.blocks.is = async function (targetUid, uid) {
|
||||
const blocks = await User.blocks.list(uid);
|
||||
return blocks.includes(parseInt(targetUid, 10));
|
||||
User.blocks.is = async function (targetUid, uids) {
|
||||
const isArray = Array.isArray(uids);
|
||||
uids = isArray ? uids : [uids];
|
||||
const blocks = await User.blocks.list(uids);
|
||||
const isBlocked = uids.map((uid, index) => blocks[index] && blocks[index].includes(parseInt(targetUid, 10)));
|
||||
return isArray ? isBlocked : isBlocked[0];
|
||||
};
|
||||
|
||||
User.blocks.can = async function (callerUid, blockerUid, blockeeUid) {
|
||||
@@ -43,27 +44,27 @@ module.exports = function (User) {
|
||||
}
|
||||
};
|
||||
|
||||
User.blocks.list = async function (uid) {
|
||||
if (User.blocks._cache.has(parseInt(uid, 10))) {
|
||||
return User.blocks._cache.get(parseInt(uid, 10));
|
||||
User.blocks.list = async function (uids) {
|
||||
const isArray = Array.isArray(uids);
|
||||
uids = (isArray ? uids : [uids]).map(uid => parseInt(uid, 10));
|
||||
const cachedData = {};
|
||||
const unCachedUids = User.blocks._cache.getUnCachedKeys(uids, cachedData);
|
||||
if (unCachedUids.length) {
|
||||
const unCachedData = await db.getSortedSetsMembers(unCachedUids.map(uid => `uid:${uid}:blocked_uids`));
|
||||
unCachedUids.forEach((uid, index) => {
|
||||
cachedData[uid] = (unCachedData[index] || []).map(uid => parseInt(uid, 10));
|
||||
User.blocks._cache.set(uid, cachedData[uid]);
|
||||
});
|
||||
}
|
||||
|
||||
let blocked = await db.getSortedSetRange(`uid:${uid}:blocked_uids`, 0, -1);
|
||||
blocked = blocked.map(uid => parseInt(uid, 10)).filter(Boolean);
|
||||
User.blocks._cache.set(parseInt(uid, 10), blocked);
|
||||
return blocked;
|
||||
const result = uids.map(uid => cachedData[uid] || []);
|
||||
return isArray ? result.slice() : result[0];
|
||||
};
|
||||
|
||||
pubsub.on('user:blocks:cache:del', (uid) => {
|
||||
User.blocks._cache.del(uid);
|
||||
});
|
||||
|
||||
User.blocks.add = async function (targetUid, uid) {
|
||||
await User.blocks.applyChecks('block', targetUid, uid);
|
||||
await db.sortedSetAdd(`uid:${uid}:blocked_uids`, Date.now(), targetUid);
|
||||
await User.incrementUserFieldBy(uid, 'blocksCount', 1);
|
||||
User.blocks._cache.del(parseInt(uid, 10));
|
||||
pubsub.publish('user:blocks:cache:del', parseInt(uid, 10));
|
||||
plugins.hooks.fire('action:user.blocks.add', { uid: uid, targetUid: targetUid });
|
||||
};
|
||||
|
||||
@@ -72,7 +73,6 @@ module.exports = function (User) {
|
||||
await db.sortedSetRemove(`uid:${uid}:blocked_uids`, targetUid);
|
||||
await User.decrementUserFieldBy(uid, 'blocksCount', 1);
|
||||
User.blocks._cache.del(parseInt(uid, 10));
|
||||
pubsub.publish('user:blocks:cache:del', parseInt(uid, 10));
|
||||
plugins.hooks.fire('action:user.blocks.remove', { uid: uid, targetUid: targetUid });
|
||||
};
|
||||
|
||||
@@ -86,10 +86,8 @@ module.exports = function (User) {
|
||||
};
|
||||
|
||||
User.blocks.filterUids = async function (targetUid, uids) {
|
||||
return await async.filter(uids, async (uid) => {
|
||||
const isBlocked = await User.blocks.is(targetUid, uid);
|
||||
return !isBlocked;
|
||||
});
|
||||
const isBlocked = await User.blocks.is(targetUid, uids);
|
||||
return uids.filter((uid, index) => !isBlocked[index]);
|
||||
};
|
||||
|
||||
User.blocks.filter = async function (uid, property, set) {
|
||||
|
||||
Reference in New Issue
Block a user