From 4767c28ae43545eabb88ab417349c9c8b8ee81e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Espino=20Garc=C3=ADa?= Date: Mon, 7 Aug 2023 09:43:46 +0200 Subject: [PATCH] Avoid unneeded group fetches (#7482) --- app/actions/remote/user.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/app/actions/remote/user.ts b/app/actions/remote/user.ts index 5c0facba2..f6297db66 100644 --- a/app/actions/remote/user.ts +++ b/app/actions/remote/user.ts @@ -294,14 +294,19 @@ const debouncedFetchUserOrGroupsByMentionNames = debounce( }, ); +const notFoundMentions: {[serverUrl: string]: Set} = {}; const fetchUserOrGroupsByMentionNames = async (serverUrl: string, mentions: string[]) => { try { + if (!notFoundMentions[serverUrl]) { + notFoundMentions[serverUrl] = new Set(); + } + const {database} = DatabaseManager.getServerDatabaseAndOperator(serverUrl); // Get any missing users const usersInDb = await queryUsersByIdsOrUsernames(database, [], mentions).fetch(); const usersMap = new Set(usersInDb.map((u) => u.username)); - const usernamesToFetch = mentions.filter((m) => !usersMap.has(m)); + const usernamesToFetch = mentions.filter((m) => !usersMap.has(m) && !notFoundMentions[serverUrl].has(m)); let fetchedUsers; if (usernamesToFetch.length) { @@ -317,7 +322,15 @@ const fetchUserOrGroupsByMentionNames = async (serverUrl: string, mentions: stri const groupsToFetch = groupsToCheck.filter((g) => !groupsMap.has(g)); if (groupsToFetch.length) { - await fetchGroupsByNames(serverUrl, groupsToFetch, false); + const results = await fetchGroupsByNames(serverUrl, groupsToFetch, false); + if (!('error' in results)) { + const retrievedSet = new Set(results.map((r) => r.name)); + for (const g of groupsToFetch) { + if (!retrievedSet.has(g)) { + notFoundMentions[serverUrl].add(g); + } + } + } } return {}; } catch (error) {