From c9b56e55c41e5419de0758ff52d2668b4eb8253c Mon Sep 17 00:00:00 2001 From: Elias Nahum Date: Tue, 31 Jan 2023 22:01:57 +0200 Subject: [PATCH] Only fetchMissingDirectChannelsInfo when no display name is set (#7060) --- app/actions/remote/channel.ts | 55 +++++++++++++++++------------------ 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/app/actions/remote/channel.ts b/app/actions/remote/channel.ts index dfcdd98521..9d3a92003e 100644 --- a/app/actions/remote/channel.ts +++ b/app/actions/remote/channel.ts @@ -15,7 +15,7 @@ import {getTeammateNameDisplaySetting} from '@helpers/api/preference'; import AppsManager from '@managers/apps_manager'; import NetworkManager from '@managers/network_manager'; import {getActiveServer} from '@queries/app/servers'; -import {prepareMyChannelsForTeam, getChannelById, getChannelByName, getMyChannel, getChannelInfo, queryMyChannelSettingsByIds, getMembersCountByChannelsId} from '@queries/servers/channel'; +import {prepareMyChannelsForTeam, getChannelById, getChannelByName, getMyChannel, getChannelInfo, queryMyChannelSettingsByIds, getMembersCountByChannelsId, queryChannelsById} from '@queries/servers/channel'; import {queryDisplayNamePreferences} from '@queries/servers/preference'; import {getCommonSystemValues, getConfig, getCurrentChannelId, getCurrentTeamId, getCurrentUserId, getLicense, setCurrentChannelId, setCurrentTeamAndChannelId} from '@queries/servers/system'; import {getNthLastChannelFromTeam, getMyTeamById, getTeamByName, queryMyTeams, removeChannelFromTeamHistory} from '@queries/servers/team'; @@ -438,38 +438,37 @@ export async function fetchMyChannel(serverUrl: string, teamId: string, channelI } export async function fetchMissingDirectChannelsInfo(serverUrl: string, directChannels: Channel[], locale?: string, teammateDisplayNameSetting?: string, currentUserId?: string, fetchOnly = false) { - const operator = DatabaseManager.serverDatabases[serverUrl]?.operator; - if (!operator) { - return {error: `${serverUrl} database not found`}; - } - - const {database} = operator; - const displayNameByChannel: Record = {}; - const users: UserProfile[] = []; - const updatedChannels = new Set(); - - const dms: Channel[] = []; - const dmIds: string[] = []; - const dmWithoutDisplayName = new Set(); - const gms: Channel[] = []; - for (const c of directChannels) { - if (c.type === General.DM_CHANNEL) { - dms.push(c); - dmIds.push(c.id); - if (!c.display_name) { - dmWithoutDisplayName.add(c.id); - } - continue; - } - gms.push(c); - } - try { + const {database, operator} = DatabaseManager.getServerDatabaseAndOperator(serverUrl); + const displayNameByChannel: Record = {}; + const users: UserProfile[] = []; + const updatedChannels = new Set(); + + const dms: Channel[] = []; + const dmIds: string[] = []; + const dmWithoutDisplayName = new Set(); + const gms: Channel[] = []; + const channelIds = new Set(directChannels.map((c) => c.id)); + const storedChannels = await queryChannelsById(database, Array.from(channelIds)).fetch(); + const storedChannelsMap = new Map(storedChannels.map((c) => [c.id, c])); + + for (const c of directChannels) { + if (c.type === General.DM_CHANNEL) { + dms.push(c); + dmIds.push(c.id); + if (!c.display_name && !storedChannelsMap.get(c.id)?.displayName) { + dmWithoutDisplayName.add(c.id); + } + continue; + } + gms.push(c); + } + const currentUser = await getCurrentUser(database); // let's filter those channels that we already have the users const membersCount = await getMembersCountByChannelsId(database, dmIds); - const profileChannelsToFetch = dmIds.filter((id) => membersCount[id] <= 1 || dmWithoutDisplayName.has(id)); + const profileChannelsToFetch = dmIds.filter((id) => membersCount[id] <= 1 && dmWithoutDisplayName.has(id)); const results = await Promise.all([ profileChannelsToFetch.length ? fetchProfilesPerChannels(serverUrl, profileChannelsToFetch, currentUserId, false) : Promise.resolve({data: undefined}), fetchProfilesInGroupChannels(serverUrl, gms.map((c) => c.id), false),