perf: resolve group membership once per folder listing instead of once per entry (#28810)

Listing a user's folders re-checks which entries they may still see, and it resolved their group membership again for every folder, then again inside the collection and note branches for every entry. A comment in that helper claims one membership fetch for the whole listing, but the caller invokes it once per folder, so the claim never held.

The listing now resolves membership once, and only when some folder actually carries entries, then threads it through the file, collection and note checks. Callers that do not supply it are unchanged and still resolve for themselves.

Measured with twenty folders holding six files, two knowledge bases and two notes each: 245 queries and ~145 ms before, 186 and ~117 ms after. The folders returned, and the entries the integrity pass writes back, are unchanged. That was checked against entries the caller owns, entries shared through a group, entries shared with nobody, another user's files, and an unrecognised entry type.
This commit is contained in:
Classic298
2026-08-19 11:16:29 -07:00
committed by GitHub
parent 767a1157f1
commit 7cf6051a74
3 changed files with 25 additions and 7 deletions
+7 -1
View File
@@ -99,6 +99,10 @@ async def get_folders(
folders = await Folders.get_folders_by_user_id(user.id, db=db)
folder_ids = {folder.id for folder in folders}
user_group_ids = None
if user.role != 'admin' and any(folder.data and 'files' in folder.data for folder in folders):
user_group_ids = {group.id for group in await Groups.get_groups_by_member_id(user.id, db=db)}
# Verify folder data integrity
folder_list = []
for folder in folders:
@@ -106,7 +110,9 @@ async def get_folders(
folder = await Folders.update_folder_parent_id_by_id_and_user_id(folder.id, user.id, None, db=db)
if folder.data and 'files' in folder.data:
accessible_files = await get_accessible_folder_files(folder.data['files'], user, db=db)
accessible_files = await get_accessible_folder_files(
folder.data['files'], user, db=db, user_group_ids=user_group_ids
)
if len(accessible_files) != len(folder.data.get('files', [])):
folder.data['files'] = accessible_files
await Folders.update_folder_by_id_and_user_id(