@@ -6,7 +6,6 @@ import { once } from '../../system/function';
66import { Logger } from '../../system/logger' ;
77import { getLogScope } from '../../system/logger.scope' ;
88import type {
9- FullOrganization ,
109 Organization ,
1110 OrganizationMember ,
1211 OrganizationSettings ,
@@ -20,8 +19,8 @@ const organizationsCacheExpiration = 24 * 60 * 60 * 1000; // 1 day
2019export class OrganizationService implements Disposable {
2120 private _disposable : Disposable ;
2221 private _organizations : Organization [ ] | null | undefined ;
23- private _fullOrganizations : Map < FullOrganization [ 'id' ] , FullOrganization > | undefined ;
24- private _organizationSettings : Map < FullOrganization [ 'id' ] , OrganizationSettings > | undefined ;
22+ private _organizationSettings : Map < Organization [ 'id' ] , OrganizationSettings > | undefined ;
23+ private _organizationMembers : Map < Organization [ 'id' ] , OrganizationMember [ ] > | undefined ;
2524
2625 constructor (
2726 private readonly container : Container ,
@@ -147,43 +146,34 @@ export class OrganizationService implements Disposable {
147146 }
148147
149148 @gate ( )
150- private async getFullOrganization (
151- id : string ,
152- options ?: { force ?: boolean } ,
153- ) : Promise < FullOrganization | undefined > {
154- if ( ! this . _fullOrganizations ?. has ( id ) || options ?. force === true ) {
155- const rsp = await this . connection . fetchGkApi ( `organization/${ id } ` , { method : 'GET' } ) ;
149+ async getMembers ( id ?: string | undefined , options ?: { force ?: boolean } ) : Promise < OrganizationMember [ ] > {
150+ if ( id == null ) {
151+ id = await this . getActiveOrganizationId ( ) ;
152+ if ( id == null ) return [ ] ;
153+ }
154+
155+ if ( ! this . _organizationMembers ?. has ( id ) || options ?. force === true ) {
156+ type MemberResponse = {
157+ members : OrganizationMember [ ] ;
158+ } ;
159+ const rsp = await this . connection . fetchGkApi ( `organization/${ id } /members` , { method : 'GET' } ) ;
156160 if ( ! rsp . ok ) {
157161 Logger . error (
158162 '' ,
159163 getLogScope ( ) ,
160- `Unable to get organization; status=(${ rsp . status } ): ${ rsp . statusText } ` ,
164+ `Unable to get organization members ; status=(${ rsp . status } ): ${ rsp . statusText } ` ,
161165 ) ;
162- return undefined ;
166+ return [ ] ;
163167 }
164168
165- const organization = ( await rsp . json ( ) ) as FullOrganization ;
166- if ( this . _fullOrganizations == null ) {
167- this . _fullOrganizations = new Map ( ) ;
168- }
169- organization . members . sort ( ( a , b ) => ( a . name ?? a . username ) . localeCompare ( b . name ?? b . username ) ) ;
170- this . _fullOrganizations . set ( id , organization ) ;
171- }
172- return this . _fullOrganizations . get ( id ) ;
173- }
169+ const members : OrganizationMember [ ] = ( ( await rsp . json ( ) ) as MemberResponse ) . members ;
170+ sortOrgMembers ( members ) ;
174171
175- @gate ( )
176- async getMembers (
177- organizationId ?: string | undefined ,
178- options ?: { force ?: boolean } ,
179- ) : Promise < OrganizationMember [ ] > {
180- if ( organizationId == null ) {
181- organizationId = await this . getActiveOrganizationId ( ) ;
182- if ( organizationId == null ) return [ ] ;
172+ this . _organizationMembers ??= new Map ( ) ;
173+ this . _organizationMembers . set ( id , members ) ;
183174 }
184175
185- const organization = await this . getFullOrganization ( organizationId , options ) ;
186- return organization ?. members ?? [ ] ;
176+ return this . _organizationMembers . get ( id ) ?? [ ] ;
187177 }
188178
189179 async getMemberById ( id : string , organizationId : string ) : Promise < OrganizationMember | undefined > {
@@ -245,3 +235,7 @@ export class OrganizationService implements Disposable {
245235 return this . _organizationSettings . get ( id ) ;
246236 }
247237}
238+
239+ function sortOrgMembers ( members : OrganizationMember [ ] ) : OrganizationMember [ ] {
240+ return members . sort ( ( a , b ) => ( a . name ?? a . username ) . localeCompare ( b . name ?? b . username ) ) ;
241+ }
0 commit comments