-
Notifications
You must be signed in to change notification settings - Fork 201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add logic to load focused group members #6756
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ import shallowEqual from 'shallowequal'; | |
|
||
// @ts-ignore - TS doesn't know about SVG files. | ||
import { default as logo } from '../../images/icons/logo.svg'; | ||
import type { Group } from '../../types/api'; | ||
import type { Group, GroupMember, GroupMembers } from '../../types/api'; | ||
import type { SidebarSettings } from '../../types/config'; | ||
import type { Service } from '../../types/config'; | ||
import { serviceConfig } from '../config/service-config'; | ||
|
@@ -23,6 +23,8 @@ const DEFAULT_ORGANIZATION = { | |
logo: 'data:image/svg+xml;utf8,' + encodeURIComponent(logo), | ||
}; | ||
|
||
export const MEMBERS_PAGE_SIZE = 100; | ||
|
||
/** | ||
* For any group that does not have an associated organization, populate with | ||
* the default Hypothesis organization. | ||
|
@@ -478,4 +480,69 @@ export class GroupsService { | |
userid: 'me', | ||
}); | ||
} | ||
|
||
/** | ||
* Fetch members for a group from the API and load them into the store as the | ||
* members of the focused group. | ||
* | ||
* @param [signal] - If provided, it can be used to cancel the loading of | ||
* group members when aborted | ||
*/ | ||
async loadFocusedGroupMembers( | ||
groupId: string, | ||
signal?: AbortSignal, | ||
): Promise<void> { | ||
const members = await this._fetchAllMembers(groupId, signal); | ||
this._store.loadFocusedGroupMembers(members); | ||
} | ||
|
||
private async _fetchAllMembers( | ||
groupId: string, | ||
signal?: AbortSignal, | ||
): Promise<GroupMember[]> { | ||
// Fetch first page of members, to determine how many more pages there are | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a race condition here that if group members are added while we are paging through, we will end up with duplicates. Unlikely, but possible in principle. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True. Something similar could happen if a member that's already fetched in a previous page is removed. That could cause a member from a subsequent page to not be fetched since it has moved a position "up" and is now part of a page which is supposedly fetched already. None of these issues are easy to handle with current API contract though, where pagination is not done via cursor, but good to have in mind. |
||
const firstPage = await this._fetchMembers({ groupId, signal }); | ||
const pages = Math.min( | ||
Math.ceil(firstPage.meta.page.total / MEMBERS_PAGE_SIZE), | ||
// Do not try to load more than 10 pages, to avoid long loading times and | ||
// hitting the server too much | ||
10, | ||
); | ||
let members = firstPage.data; | ||
|
||
// Fetch remaining pages. Start at 2 to skip first one which was already | ||
// loaded. | ||
for (let page = 2; page <= pages; page++) { | ||
// Do not attempt any further request once the signal has been aborted | ||
if (signal?.aborted) { | ||
break; | ||
} | ||
|
||
// TODO Consider parallelizing requests | ||
const membersPage = await this._fetchMembers({ groupId, page, signal }); | ||
members = members.concat(membersPage.data); | ||
} | ||
|
||
return members; | ||
} | ||
acelaya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private _fetchMembers({ | ||
groupId, | ||
page = 1, | ||
signal, | ||
}: { | ||
groupId: string; | ||
page?: number; | ||
signal?: AbortSignal; | ||
}): Promise<GroupMembers> { | ||
return this._api.group.members.read( | ||
{ | ||
pubid: groupId, | ||
'page[number]': page, | ||
'page[size]': MEMBERS_PAGE_SIZE, | ||
}, | ||
undefined, | ||
signal, | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code still has the issue that the focused group could change in between the call to
loadFocusedGroupMembers
andstore.loadFocusedGroupMembers
being called. At a minimum you need to ensure that members loaded for group A don't get saved as the members of group B if the focused group changes from A to B while fetching. Ideally you also want to stop fetching any further pages of members for group A.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, sorry. This is not ready for review yet. I haven't finished applying all the changes from your previous review, but I forgot to move it to draft.