Avoid unneeded group fetches (#7482)

This commit is contained in:
Daniel Espino García 2023-08-07 09:43:46 +02:00 committed by GitHub
parent ff601982b9
commit 4767c28ae4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -294,14 +294,19 @@ const debouncedFetchUserOrGroupsByMentionNames = debounce(
},
);
const notFoundMentions: {[serverUrl: string]: Set<string>} = {};
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) {