mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
perf: get rid of async call in user load for expiring bans
unban all expires in one call
This commit is contained in:
@@ -42,7 +42,7 @@ module.exports = function (User) {
|
||||
await db.sortedSetAdd('users:banned', now, uid);
|
||||
await db.sortedSetAdd(`uid:${uid}:bans:timestamp`, now, banKey);
|
||||
await db.setObject(banKey, banData);
|
||||
await User.setUserField(uid, 'banned:expire', banData.expire);
|
||||
await User.setUserFields(uid, { banned: 1, 'banned:expire': banData.expire });
|
||||
if (until > now) {
|
||||
await db.sortedSetAdd('users:banned:expire', until, uid);
|
||||
} else {
|
||||
@@ -69,7 +69,7 @@ module.exports = function (User) {
|
||||
uids = isArray ? uids : [uids];
|
||||
const userData = await User.getUsersFields(uids, ['email:confirmed']);
|
||||
|
||||
await db.setObject(uids.map(uid => `user:${uid}`), { 'banned:expire': 0 });
|
||||
await db.setObject(uids.map(uid => `user:${uid}`), { banned: 0, 'banned:expire': 0 });
|
||||
const now = Date.now();
|
||||
const unbanDataArray = [];
|
||||
/* eslint-disable no-await-in-loop */
|
||||
@@ -124,16 +124,15 @@ module.exports = function (User) {
|
||||
|
||||
User.bans.unbanIfExpired = async function (uids) {
|
||||
// loading user data will unban if it has expired -barisu
|
||||
const userData = await User.getUsersFields(uids, ['banned:expire']);
|
||||
const userData = await User.getUsersFields(uids, ['banned', 'banned:expire']);
|
||||
return User.bans.calcExpiredFromUserData(userData);
|
||||
};
|
||||
|
||||
User.bans.calcExpiredFromUserData = async function (userData) {
|
||||
User.bans.calcExpiredFromUserData = function (userData) {
|
||||
const isArray = Array.isArray(userData);
|
||||
userData = isArray ? userData : [userData];
|
||||
const banned = await groups.isMembers(userData.map(u => u.uid), groups.BANNED_USERS);
|
||||
userData = userData.map((userData, index) => ({
|
||||
banned: banned[index],
|
||||
userData = userData.map(userData => ({
|
||||
banned: !!(userData && userData.banned),
|
||||
'banned:expire': userData && userData['banned:expire'],
|
||||
banExpired: userData && userData['banned:expire'] <= Date.now() && userData['banned:expire'] !== 0,
|
||||
}));
|
||||
|
||||
@@ -193,6 +193,7 @@ module.exports = function (User) {
|
||||
iconBackgrounds = await User.getIconBackgrounds();
|
||||
}
|
||||
|
||||
const unbanUids = [];
|
||||
users.forEach((user) => {
|
||||
if (!user) {
|
||||
return;
|
||||
@@ -258,23 +259,22 @@ module.exports = function (User) {
|
||||
if (user.hasOwnProperty('mutedUntil')) {
|
||||
user.muted = user.mutedUntil > Date.now();
|
||||
}
|
||||
});
|
||||
|
||||
// TODO get rid of single calls
|
||||
// dont do anything if user is not banned?
|
||||
await Promise.all(users.map(async (user) => {
|
||||
if (user.hasOwnProperty('banned') || user.hasOwnProperty('banned:expire')) {
|
||||
const result = await User.bans.calcExpiredFromUserData(user);
|
||||
const result = User.bans.calcExpiredFromUserData(user);
|
||||
user.banned = result.banned;
|
||||
const unban = result.banned && result.banExpired;
|
||||
user.banned_until = unban ? 0 : user['banned:expire'];
|
||||
user.banned_until_readable = user.banned_until && !unban ? utils.toISOString(user.banned_until) : 'Not Banned';
|
||||
if (unban) {
|
||||
await User.bans.unban(user.uid, '[[user:info.ban-expired]]');
|
||||
unbanUids.push(user.uid);
|
||||
user.banned = false;
|
||||
}
|
||||
}
|
||||
}));
|
||||
});
|
||||
if (unbanUids.length) {
|
||||
await User.bans.unban(unbanUids, '[[user:info.ban-expired]]');
|
||||
}
|
||||
|
||||
return await plugins.hooks.fire('filter:users.get', users);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user