11const { HDSLibError } = require ( '../errors' ) ;
2+ const CollectorInvite = require ( './CollectorInvite' ) ;
23
34const COLLECTOR_STREAMID_SUFFIXES = {
5+ archive : 'archive' ,
46 internal : 'internal' ,
57 public : 'public' ,
68 pending : 'pending' ,
@@ -38,7 +40,11 @@ class Collector {
3840 this . name = streamData . name ;
3941 this . appManaging = appManaging ;
4042 this . #streamData = streamData ;
41- this . #cache = { } ;
43+ this . #cache = {
44+ invites : { } ,
45+ invitesInitialized : false ,
46+ invitesInitializing : false
47+ } ;
4248 }
4349
4450 /**
@@ -143,6 +149,76 @@ class Collector {
143149 return await this . #setStatus( Collector . STATUSES . active ) ;
144150 }
145151
152+ #addOrUpdateInvite ( eventData ) {
153+ const key = CollectorInvite . getKeyForEvent ( eventData ) ;
154+ if ( this . #cache. invites [ key ] ) {
155+ this . #cache. invites [ key ] . setEventData ( eventData ) ;
156+ } else {
157+ this . #cache. invites [ key ] = new CollectorInvite ( this , eventData ) ;
158+ }
159+ return this . #cache. invites [ key ] ;
160+ }
161+
162+ async getInvites ( forceRefresh = false ) {
163+ while ( this . #cache. invitesInitializing ) ( await new Promise ( ( resolve ) => { setTimeout ( resolve , 100 ) ; } ) ) ;
164+ this . #cache. invitesInitializing = true ;
165+ if ( ! forceRefresh && this . #cache. invitesInitialized ) return Object . values ( this . #cache. invites ) ;
166+ const queryParams = { types : [ 'invite/collector-v1' ] , streams : [ this . streamId ] , fromTime : 0 , toTime : 8640000000000000 , limit : 10000 } ;
167+ try {
168+ await this . appManaging . connection . getEventsStreamed ( queryParams , ( eventData ) => {
169+ this . #addOrUpdateInvite( eventData ) ;
170+ } ) ;
171+ } catch ( e ) {
172+ this . #cache. invitesInitialized = true ;
173+ this . #cache. invitesInitializing = false ;
174+ throw e ;
175+ }
176+ this . #cache. invitesInitialized = true ;
177+ this . #cache. invitesInitializing = false ;
178+ return Object . values ( this . #cache. invites ) ;
179+ }
180+
181+ async checkInbox ( ) {
182+ const tempr = [ ] ;
183+
184+ const params = { types : [ 'credentials/collector-v1' ] , limit : 1 , streams : [ this . streamIdFor ( Collector . STREAMID_SUFFIXES . inbox ) ] } ;
185+ const incomingCredentials = await this . appManaging . connection . apiOne ( 'events.get' , params , 'events' ) ;
186+ for ( const incomingCredential of incomingCredentials ) {
187+ // fetch corresponding invite
188+ const inviteEvent = await this . appManaging . connection . apiOne ( 'events.getOne' , { id : incomingCredential . content . eventId } , 'event' ) ;
189+ if ( inviteEvent == null ) throw new HDSLibError ( `Cannot find invite event matching id: ${ incomingCredential . content . eventId } ` , incomingCredential ) ;
190+ // update inviteEvent and archive inbox message
191+ const apiCalls = [
192+ {
193+ method : 'events.update' ,
194+ params : {
195+ id : inviteEvent . id ,
196+ update : {
197+ streamIds : [ this . streamIdFor ( Collector . STREAMID_SUFFIXES . active ) ] ,
198+ content : Object . assign ( inviteEvent . content , { apiEndpoint : incomingCredential . content . apiEndpoint } )
199+ }
200+ }
201+ } ,
202+ {
203+ method : 'events.update' ,
204+ params : {
205+ id : incomingCredential . id ,
206+ update : {
207+ streamIds : [ this . streamIdFor ( Collector . STREAMID_SUFFIXES . archive ) ]
208+ }
209+ }
210+ }
211+ ] ;
212+ const results = await this . appManaging . connection . api ( apiCalls ) ;
213+ const errors = results . filter ( ( r ) => ( ! r . event ) ) ;
214+ if ( errors . length > 0 ) throw new HDSLibError ( 'Error activating incoming request' , errors ) ;
215+ const eventUpdated = results [ 0 ] . event ;
216+ const inviteUpdated = this . #addOrUpdateInvite( eventUpdated ) ;
217+ tempr . push ( inviteUpdated ) ;
218+ }
219+ return tempr ;
220+ }
221+
146222 /**
147223 * Create a "pending" invite to be sent to an app using AppSharingAccount
148224 * @param {string } name a default display name for this request
@@ -160,11 +236,8 @@ class Collector {
160236 }
161237 } ;
162238 const newInvite = await this . appManaging . connection . apiOne ( 'events.create' , eventParams , 'event' ) ;
163- const result = {
164- apiEndpoint : await this . sharingApiEndpoint ( ) ,
165- eventId : newInvite . id
166- } ;
167- return result ;
239+ const invite = this . #addOrUpdateInvite( newInvite ) ;
240+ return invite ;
168241 }
169242
170243 /**
@@ -260,6 +333,22 @@ class Collector {
260333 streamIdFor ( suffix ) {
261334 return this . streamId + '-' + suffix ;
262335 }
336+
337+ /**
338+ * Invite Status for streamId
339+ * reverse of streamIdFor
340+ */
341+ inviteStatusForStreamId ( streamId ) {
342+ if ( ! this . #cache. inviteStatusForStreamId ) {
343+ this . #cache. inviteStatusForStreamId = { } ;
344+ for ( const status of [ COLLECTOR_STREAMID_SUFFIXES . pending , COLLECTOR_STREAMID_SUFFIXES . active , COLLECTOR_STREAMID_SUFFIXES . error ] ) {
345+ this . #cache. inviteStatusForStreamId [ this . streamIdFor ( status ) ] = status ;
346+ }
347+ }
348+ const status = this . #cache. inviteStatusForStreamId [ streamId ] ;
349+ if ( status == null ) throw new HDSLibError ( `Cannot find status for streamId: ${ streamId } ` ) ;
350+ return status ;
351+ }
263352}
264353
265354module . exports = Collector ;
0 commit comments