@@ -12,6 +12,7 @@ const collectorIdGenerator = new ShortUniqueId({ dictionary: 'alphanum_lower', l
1212 * Stream structure
1313 * - [baseStreamId] "Root" stream for this app
1414 * - [baseStreamId]-[collectorsId] Each "questionnary" or "request for a set of data" has it's own stream
15+ * - [baseStreamId]-[collectorsId]-internal Private stuff not to be shared
1516 * - [baseStreamId]-[collectorsId]-public Contains events with the current settings of this app (this stream will be shared in "read" with the request)
1617 * - [baseStreamId]-[collectorsId]-pending Contains events with "pending" requests
1718 * - [baseStreamId]-[collectorsId]-inbox Contains events with "inbox" requests Will be shared in createOnly
@@ -99,6 +100,7 @@ class AppManagingAccount {
99100}
100101
101102const COLLECTOR_STREAMID_SUFFIXES = {
103+ internal : 'internal' ,
102104 public : 'public' ,
103105 pending : 'pending' ,
104106 inbox : 'inbox' ,
@@ -108,6 +110,12 @@ const COLLECTOR_STREAMID_SUFFIXES = {
108110Object . freeze ( COLLECTOR_STREAMID_SUFFIXES ) ;
109111class Collector {
110112 static STREAMID_SUFFIXES = COLLECTOR_STREAMID_SUFFIXES ;
113+ static STATUSES = Object . freeze ( {
114+ draft : 'draft' ,
115+ active : 'active' ,
116+ deactivated : 'deactivated'
117+ } ) ;
118+
111119 appManaging ;
112120 streamId ;
113121 name ;
@@ -126,6 +134,76 @@ class Collector {
126134 this . #cache = { } ;
127135 }
128136
137+ /**
138+ * @property {string } one of 'draft', 'active', 'deactivated'
139+ */
140+ get statusCode ( ) {
141+ if ( this . #cache. status == null ) throw new Error ( 'Init Collector first' ) ;
142+ return this . #cache. status . content . status ;
143+ }
144+
145+ /**
146+ * Fetch online data
147+ */
148+ async init ( ) {
149+ await this . checkStreamStructure ( ) ;
150+ await this . getStatus ( ) ;
151+ }
152+
153+ /**
154+ * @type {StatusEvent } - extends PryvEvent with a specific content
155+ * @property {Object } content - content
156+ * @property {String } content.status - one of 'draft', 'active', 'deactivated'
157+ * @property {Data } content.data - app specific data
158+ */
159+
160+ /**
161+ * Get Collector status,
162+ * @param {boolean } forceRefresh - if true, forces fetching the status from the server
163+ * @returns {StatusEvent }
164+ */
165+ async getStatus ( forceRefresh = false ) {
166+ if ( ! forceRefresh && this . #cache. status ) return this . #cache. status ;
167+ const params = { types : [ 'status/collector-v1' ] , limit : 1 , streams : [ this . streamIdFor ( Collector . STREAMID_SUFFIXES . internal ) ] } ;
168+ const statusEvents = await this . appManaging . connection . apiOne ( 'events.get' , params , 'events' ) ;
169+ if ( statusEvents . length === 0 ) { // non exsitent set "draft" status
170+ return this . setStatus ( Collector . STATUSES . draft , { } ) ;
171+ }
172+ this . #cache. status = statusEvents [ 0 ] ;
173+ return this . #cache. status ;
174+ }
175+
176+ /**
177+ * Change the status
178+ * @param {string } status one of of 'draft', 'active', 'deactivated'
179+ * @param {object } data - custom data
180+ * @returns {StatusEvent }
181+ */
182+ async setStatus ( status , data ) {
183+ if ( ! Collector . STATUSES [ status ] ) throw new HDSLibError ( 'Unkown status key' , { status, data } ) ;
184+ const event = {
185+ type : 'status/collector-v1' ,
186+ streamIds : [ this . streamIdFor ( Collector . STREAMID_SUFFIXES . internal ) ] ,
187+ content : {
188+ status,
189+ data
190+ }
191+ } ;
192+ const statusEvent = await this . appManaging . connection . apiOne ( 'events.create' , event , 'event' ) ;
193+ this . #cache. status = statusEvent ;
194+ return this . #cache. status ;
195+ }
196+
197+ /**
198+ * Create a "pending" invite to be sent to an app usin AppSharingAccount
199+ * @param {string } name a default display name for this request
200+ * @param {Object } [options]
201+ * @param {Object } [options.customData] any data to be used by the client app
202+ */
203+ async createInvite ( name , options ) {
204+
205+ }
206+
129207 /**
130208 * Get sharing api endpoint
131209 */
0 commit comments