[Gekidou] Add performance and code improvements around post_list (#6113)

* Add performance and code improvements around post_list

* Fix test

* Move observers from utils to queries

* remove Flipper on iOS to fix CI build

* Fix observePermissionForChannel for DM/GM

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
This commit is contained in:
Daniel Espino García
2022-04-04 14:14:55 +02:00
committed by GitHub
parent c9d73d4512
commit d1322e84ce
22 changed files with 740 additions and 325 deletions

View File

@@ -1,16 +1,7 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {General, Permissions} from '@constants';
import {queryRolesByNames} from '@queries/servers/role';
import type ChannelModel from '@typings/database/models/servers/channel';
import type MyChannelModel from '@typings/database/models/servers/my_channel';
import type MyTeamModel from '@typings/database/models/servers/my_team';
import type PostModel from '@typings/database/models/servers/post';
import type RoleModel from '@typings/database/models/servers/role';
import type TeamModel from '@typings/database/models/servers/team';
import type UserModel from '@typings/database/models/servers/user';
export function hasPermission(roles: RoleModel[] | Role[], permission: string, defaultValue: boolean) {
const permissions = new Set<string>();
@@ -21,83 +12,3 @@ export function hasPermission(roles: RoleModel[] | Role[], permission: string, d
const exists = permissions.has(permission);
return defaultValue === true || exists;
}
export async function hasPermissionForChannel(channel: ChannelModel, user: UserModel, permission: string, defaultValue: boolean) {
const rolesArray = [...user.roles.split(' ')];
const myChannel = await channel.membership.fetch() as MyChannelModel | undefined;
if (myChannel) {
rolesArray.push(...myChannel.roles.split(' '));
}
const team = await channel.team.fetch() as TeamModel | undefined;
if (team) {
const myTeam = await team.myTeam.fetch() as MyTeamModel | undefined;
if (myTeam) {
rolesArray.push(...myTeam.roles.split(' '));
}
}
if (rolesArray.length) {
const roles = await queryRolesByNames(user.database, rolesArray).fetch();
return hasPermission(roles, permission, defaultValue);
}
return defaultValue;
}
export async function hasPermissionForTeam(team: TeamModel, user: UserModel, permission: string, defaultValue: boolean) {
const rolesArray = [...user.roles.split(' ')];
const myTeam = await team.myTeam.fetch() as MyTeamModel | undefined;
if (myTeam) {
rolesArray.push(...myTeam.roles.split(' '));
}
if (rolesArray.length) {
const roles = await queryRolesByNames(user.database, rolesArray).fetch();
return hasPermission(roles, permission, defaultValue);
}
return defaultValue;
}
export async function hasPermissionForPost(post: PostModel, user: UserModel, permission: string, defaultValue: boolean) {
const channel = await post.channel.fetch() as ChannelModel | undefined;
if (channel) {
return hasPermissionForChannel(channel, user, permission, defaultValue);
}
return defaultValue;
}
export async function canManageChannelMembers(post: PostModel, user: UserModel) {
const rolesArray = [...user.roles.split(' ')];
const channel = await post.channel.fetch() as ChannelModel | undefined;
const directTypes: string[] = [General.DM_CHANNEL, General.GM_CHANNEL];
if (!channel || channel.deleteAt !== 0 || directTypes.includes(channel.type) || channel.name === General.DEFAULT_CHANNEL) {
return false;
}
const myChannel = await channel.membership.fetch() as MyChannelModel | undefined;
if (myChannel) {
rolesArray.push(...myChannel.roles.split(' '));
}
const team = await channel.team.fetch() as TeamModel | undefined;
if (team) {
const myTeam = await team.myTeam.fetch() as MyTeamModel | undefined;
if (myTeam) {
rolesArray.push(...myTeam.roles.split(' '));
}
}
if (rolesArray.length) {
const roles = await queryRolesByNames(post.database, rolesArray).fetch() as RoleModel[];
const permission = channel.type === General.OPEN_CHANNEL ? Permissions.MANAGE_PUBLIC_CHANNEL_MEMBERS : Permissions.MANAGE_PRIVATE_CHANNEL_MEMBERS;
return hasPermission(roles, permission, true);
}
return true;
}