Improves validation in getRolesByName endpoint (#25215)

* Improves validation in getRolesByName endpoint

* Updates the max constant and fixes linter

* Adds a mechanism to split roles in chunks in the webapp client

---------

Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
Miguel de la Cruz
2023-12-22 10:29:51 +01:00
коммит произвёл GitHub
родитель be24f108e1
Коммит 0e60f3d542
5 изменённых файлов: 45 добавлений и 1 удалений

Просмотреть файл

@@ -10,6 +10,8 @@ import type {DispatchFunc, GetStateFunc, ActionFunc} from 'mattermost-redux/type
import {bindClientFunc} from './helpers';
import {General} from '../constants';
export function getRolesByNames(rolesNames: string[]) {
return bindClientFunc({
clientFunc: Client4.getRolesByNames,
@@ -98,8 +100,24 @@ export function loadRolesIfNeeded(roles: Iterable<string>): ActionFunc {
if (state.entities.roles.pending) {
await dispatch(setPendingRoles([]));
}
if (newRoles.size > 0) {
return getRolesByNames(Array.from(newRoles))(dispatch, getState);
const newRolesArray = Array.from(newRoles);
const getRolesRequests = [];
for (let i = 0; i < newRolesArray.length; i += General.MAX_GET_ROLES_BY_NAMES) {
const chunk = newRolesArray.slice(i, i + General.MAX_GET_ROLES_BY_NAMES);
getRolesRequests.push(getRolesByNames(chunk)(dispatch, getState));
}
const result = await Promise.all(getRolesRequests);
return result.reduce(
(acc: Record<string, any>, val: Record<string, any>): Record<string, any> => {
acc.data = acc.data.concat(val.data);
return acc;
},
{data: []},
);
}
return {data: state.entities.roles.roles};
};

Просмотреть файл

@@ -76,4 +76,5 @@ export default {
ALWAYS_ON: 'always_on',
DEFAULT_GROUP: 'board',
CUSTOM_GROUP_USER_ROLE: 'custom_group_user',
MAX_GET_ROLES_BY_NAMES: 100,
};