From 4095212d86019c1823bb0d0540de112e216919dd Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Fri, 22 Dec 2023 10:29:49 +0100 Subject: [PATCH] Improves get roles by names requests to batch them following the new endpoint limit (#7650) --- app/actions/remote/role.ts | 9 ++++++++- app/constants/general.ts | 1 + 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/app/actions/remote/role.ts b/app/actions/remote/role.ts index ecbc29bd8..8ffb1ed84 100644 --- a/app/actions/remote/role.ts +++ b/app/actions/remote/role.ts @@ -1,6 +1,7 @@ // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. +import {General} from '@constants'; import DatabaseManager from '@database/manager'; import NetworkManager from '@managers/network_manager'; import {queryRoles} from '@queries/servers/role'; @@ -42,7 +43,13 @@ export const fetchRolesIfNeeded = async (serverUrl: string, updatedRoles: string return {roles: []}; } - const roles = await client.getRolesByNames(newRoles); + const getRolesRequests = []; + for (let i = 0; i < newRoles.length; i += General.MAX_GET_ROLES_BY_NAMES) { + const chunk = newRoles.slice(i, i + General.MAX_GET_ROLES_BY_NAMES); + getRolesRequests.push(client.getRolesByNames(chunk)); + } + + const roles = (await Promise.all(getRolesRequests)).flat(); if (!fetchOnly) { await operator.handleRole({ roles, diff --git a/app/constants/general.ts b/app/constants/general.ts index 1be49a701..c94ce5ceb 100644 --- a/app/constants/general.ts +++ b/app/constants/general.ts @@ -32,6 +32,7 @@ export default { MAX_USERS_IN_GM: 7, MIN_USERS_IN_GM: 3, MAX_GROUP_CHANNELS_FOR_PROFILES: 50, + MAX_GET_ROLES_BY_NAMES: 100, DEFAULT_AUTOLINKED_URL_SCHEMES: ['http', 'https', 'ftp', 'mailto', 'tel', 'mattermost'], PROFILE_CHUNK_SIZE: 100, SEARCH_TIMEOUT_MILLISECONDS: 500,