Skip to content

Commit

Permalink
Add logic to load focused group members
Browse files Browse the repository at this point in the history
  • Loading branch information
acelaya committed Jan 15, 2025
1 parent 8ed943d commit 1054a69
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/sidebar/services/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
RouteMap,
RouteMetadata,
Profile,
GroupMembers,
} from '../../types/api';
import { stripInternalProperties } from '../helpers/strip-internal-properties';
import type { SidebarStore } from '../store';
Expand Down Expand Up @@ -218,6 +219,17 @@ export class APIService {
member: {
delete: APICall<{ pubid: string; userid: string }>;
};
members: {
read: APICall<
{
pubid: string;
'page[number]'?: number;
'page[size]'?: number;
},
void,
GroupMembers
>;
};
read: APICall<{ id: string; expand: string[] }, void, Group>;
};
groups: {
Expand Down Expand Up @@ -287,6 +299,17 @@ export class APIService {
userid: string;
}>,
},
members: {
read: apiCall('group.members.read') as APICall<
{
pubid: string;
'page[number]'?: number;
'page[size]'?: number;
},
void,
GroupMembers
>,
},
read: apiCall('group.read') as APICall<
{ id: string; expand: string[] },
void,
Expand Down
46 changes: 45 additions & 1 deletion src/sidebar/services/groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -478,4 +478,48 @@ export class GroupsService {
userid: 'me',
});
}

/**
* Fetch members for focused group from the API and load them into the store.
*/
async loadMembers(): Promise<GroupMember[]> {
const groupId = this._store.focusedGroupId();
if (!groupId) {
throw new Error('A group is not focused yet');

Check warning on line 488 in src/sidebar/services/groups.ts

View check run for this annotation

Codecov / codecov/patch

src/sidebar/services/groups.ts#L485-L488

Added lines #L485 - L488 were not covered by tests
}

const members = await this._fetchAllMembers(groupId);

Check warning on line 491 in src/sidebar/services/groups.ts

View check run for this annotation

Codecov / codecov/patch

src/sidebar/services/groups.ts#L491

Added line #L491 was not covered by tests

// TODO Load members into the store

return members;

Check warning on line 495 in src/sidebar/services/groups.ts

View check run for this annotation

Codecov / codecov/patch

src/sidebar/services/groups.ts#L495

Added line #L495 was not covered by tests
}

private async _fetchAllMembers(groupId: string): Promise<GroupMember[]> {

Check warning on line 498 in src/sidebar/services/groups.ts

View check run for this annotation

Codecov / codecov/patch

src/sidebar/services/groups.ts#L498

Added line #L498 was not covered by tests
// Fetch first page of members, to determine how many more pages there are
const firstPage = await this._fetchMembers(groupId);
const remainingMembers = firstPage.meta.page.total - 100;
let members = firstPage.data;

Check warning on line 502 in src/sidebar/services/groups.ts

View check run for this annotation

Codecov / codecov/patch

src/sidebar/services/groups.ts#L500-L502

Added lines #L500 - L502 were not covered by tests

if (remainingMembers <= 0) {
return members;

Check warning on line 505 in src/sidebar/services/groups.ts

View check run for this annotation

Codecov / codecov/patch

src/sidebar/services/groups.ts#L504-L505

Added lines #L504 - L505 were not covered by tests
}

const pages = Math.ceil(remainingMembers / 100);
for (let i = 1; i < pages; i++) {

Check warning on line 509 in src/sidebar/services/groups.ts

View check run for this annotation

Codecov / codecov/patch

src/sidebar/services/groups.ts#L508-L509

Added lines #L508 - L509 were not covered by tests
// TODO Consider parallelizing requests
const groupMembers = await this._fetchMembers(groupId, i + 1);
members = members.concat(groupMembers.data);

Check warning on line 512 in src/sidebar/services/groups.ts

View check run for this annotation

Codecov / codecov/patch

src/sidebar/services/groups.ts#L511-L512

Added lines #L511 - L512 were not covered by tests
}

return members;

Check warning on line 515 in src/sidebar/services/groups.ts

View check run for this annotation

Codecov / codecov/patch

src/sidebar/services/groups.ts#L515

Added line #L515 was not covered by tests
}

private _fetchMembers(groupId: string, page = 1): Promise<GroupMembers> {
return this._api.group.members.read({

Check warning on line 519 in src/sidebar/services/groups.ts

View check run for this annotation

Codecov / codecov/patch

src/sidebar/services/groups.ts#L518-L519

Added lines #L518 - L519 were not covered by tests
pubid: groupId,
'page[number]': page,
'page[size]': 100,
});
}
}
20 changes: 20 additions & 0 deletions src/types/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,26 @@ export type Group = {
*/
export type GroupIdentifier = NonNullable<Group['id'] | Group['groupid']>;

export type GroupMember = {
authority: string;
userid: string;
username: string;
display_name: string;
roles: string[];
actions: string[];
created: string;
updated: string;
};

export type GroupMembers = {
meta: {
page: {
total: number;
};
};
data: GroupMember[];
};

/**
* Query parameters for an `/api/search` API call.
*
Expand Down

0 comments on commit 1054a69

Please sign in to comment.