From 9eb1fcd4e5e909c1d2772c6ef5d49d9e430660dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Sun, 14 Jul 2019 16:25:30 -0400 Subject: [PATCH] fix: #7762, allow array for isBanned dont filter banned users one by one in digest --- src/user/bans.js | 70 ++++++++++++++++++++-------------------------- src/user/digest.js | 14 ++++------ 2 files changed, 37 insertions(+), 47 deletions(-) diff --git a/src/user/bans.js b/src/user/bans.js index 28b87d23d7..49f0eff07a 100644 --- a/src/user/bans.js +++ b/src/user/bans.js @@ -1,6 +1,5 @@ 'use strict'; -const util = require('util'); const db = require('../database'); module.exports = function (User) { @@ -42,42 +41,45 @@ module.exports = function (User) { return banData; }; - User.bans.unban = async function (uid) { - await User.setUserFields(uid, { banned: 0, 'banned:expire': 0 }); - await db.sortedSetsRemove(['users:banned', 'users:banned:expire'], uid); + User.bans.unban = async function (uids) { + if (Array.isArray(uids)) { + await db.setObject(uids.map(uid => 'user:' + uid), { banned: 0, 'banned:expire': 0 }); + } else { + await User.setUserFields(uids, { banned: 0, 'banned:expire': 0 }); + } + + await db.sortedSetRemove(['users:banned', 'users:banned:expire'], uids); }; - User.bans.getBannedAndExpired = async function (uid) { - if (parseInt(uid, 10) <= 0) { - return false; - } - const userData = await User.getUserFields(uid, ['banned', 'banned:expire']); - return User.bans.calcExpiredFromUserData(userData); + User.bans.isBanned = async function (uids) { + const isArray = Array.isArray(uids); + uids = isArray ? uids : [uids]; + const result = await User.bans.unbanIfExpired(uids); + return isArray ? result.map(r => r.banned) : result[0].banned; }; - User.bans.calcExpiredFromUserData = function (userData) { - return { - banned: !!userData.banned, - 'banned:expire': userData['banned:expire'], - banExpired: userData['banned:expire'] <= Date.now() && userData['banned:expire'] !== 0, - }; + User.bans.unbanIfExpired = async function (uids) { + // loading user data will unban if it has expired -barisu + const userData = await User.getUsersFields(uids, ['banned', 'banned:expire']); + return User.bans.calcExpiredFromUserData(userData); }; - User.bans.unbanIfExpired = async function (uid) { - const result = await User.bans.getBannedAndExpired(uid); - if (result.banned && result.banExpired) { - await User.bans.unban(uid); - return { banned: false, banExpired: true, 'banned:expire': 0 }; - } - return result; + User.bans.calcExpiredFromUserData = function (userData) { + const isArray = Array.isArray(userData); + userData = isArray ? userData : [userData]; + userData = userData.map(function (userData) { + return { + banned: userData && !!userData.banned, + 'banned:expire': userData && userData['banned:expire'], + banExpired: userData && userData['banned:expire'] <= Date.now() && userData['banned:expire'] !== 0, + }; + }); + return isArray ? userData : userData[0]; }; - User.bans.isBanned = async function (uid) { - if (parseInt(uid, 10) <= 0) { - return false; - } - const result = await User.bans.unbanIfExpired(uid); - return result.banned; + User.bans.filterBanned = async function (uids) { + const isBanned = User.bans.isBanned(uids); + return uids.filter((uid, index) => !isBanned[index]); }; User.bans.getReason = async function (uid) { @@ -91,14 +93,4 @@ module.exports = function (User) { const banObj = await db.getObject(keys[0]); return banObj && banObj.reason ? banObj.reason : ''; }; - - // TODO Remove in v1.13.0 - const deprecatedMessage = (oldPath, newPath) => `function ${oldPath} is deprecated, please use ${newPath} instead`; - User.ban = util.deprecate(User.bans.ban, deprecatedMessage('User.ban', 'User.bans.ban')); - User.unban = util.deprecate(User.bans.unban, deprecatedMessage('User.unban', 'User.bans.unban')); - User.getBannedAndExpired = util.deprecate(User.bans.getBannedAndExpired, deprecatedMessage('User.getBannedAndExpired', 'User.bans.getBannedAndExpired')); - User.calcBanExpiredFromUserData = util.deprecate(User.bans.calcExpiredFromUserData, deprecatedMessage('User.calcBanExpiredFromUserData', 'User.bans.calcExpiredFromUserData')); - User.unbanIfBanExpired = util.deprecate(User.bans.unbanIfExpired, deprecatedMessage('User.unbanIfBanExpired', 'User.bans.unbanIfExpired')); - User.isBanned = util.deprecate(User.bans.isBanned, deprecatedMessage('User.isBanned', 'User.bans.isBanned')); - User.getBannedReason = util.deprecate(User.bans.getReason, deprecatedMessage('User.getBannedReason', 'User.bans.getReason')); }; diff --git a/src/user/digest.js b/src/user/digest.js index 9b01126891..8b6d465c4f 100644 --- a/src/user/digest.js +++ b/src/user/digest.js @@ -65,11 +65,16 @@ Digest.getSubscribers = function (interval, callback) { user.getMultipleUserSettings(uids, next); }, function (settings, next) { + const subUids = []; settings.forEach(function (hash) { if (hash.dailyDigestFreq === interval) { - subs.push(hash.uid); + subUids.push(hash.uid); } }); + user.bans.filterBanned(subUids, next); + }, + function (uids, next) { + subs = subs.concat(uids); next(); }, ], next); @@ -77,13 +82,6 @@ Digest.getSubscribers = function (interval, callback) { next(err, subs); }); }, - function (subscribers, next) { - async.filter(subscribers, function (uid, next) { - user.bans.isBanned(uid, function (err, banned) { - next(err, !banned); - }); - }, next); - }, function (subscribers, next) { plugins.fireHook('filter:digest.subscribers', { interval: interval,