|
| 1 | +const { HDSLibError } = require('../errors'); |
| 2 | +const pryv = require('../patchedPryv'); |
1 | 3 | const Application = require('./Application'); |
| 4 | +const CollectorClient = require('./CollectorClient'); |
| 5 | + |
2 | 6 | /** |
3 | 7 | * - applications |
4 | 8 | * - [baseStreamId] "Root" stream from this app |
5 | 9 | */ |
| 10 | + |
| 11 | +const MAX_COLLECTORS = 1000; |
6 | 12 | class AppClientAccount extends Application { |
| 13 | + constructor (baseStreamId, connection, appName) { |
| 14 | + super(...arguments); |
| 15 | + this.cache.collectorClientsMap = {}; |
| 16 | + } |
| 17 | + |
7 | 18 | get appSettings () { |
8 | 19 | return { |
9 | 20 | canBePersonnal: true, |
10 | 21 | mustBeMaster: true |
11 | 22 | }; |
12 | 23 | } |
13 | 24 |
|
| 25 | + /** |
| 26 | + * When the app receives a new request for data sharing |
| 27 | + * @param {string} apiEndpoint |
| 28 | + * @param {string} [incomingEventId] - Information for the recipient |
| 29 | + */ |
| 30 | + async handleIncomingRequest (apiEndpoint, incomingEventId) { |
| 31 | + const requesterConnection = new pryv.Connection(apiEndpoint); |
| 32 | + const accessInfos = await requesterConnection.accessInfo(); |
| 33 | + // check if request is known |
| 34 | + if (this.cache.collectorClientsMap[CollectorClient.keyFromInfo(accessInfos)]) { |
| 35 | + const collectorClient = this.collectoClientsMap[accessInfos.name]; |
| 36 | + if (incomingEventId && collectorClient.requesterEventId !== incomingEventId) { |
| 37 | + throw new HDSLibError('Found existing collectorClient with a different eventId', { actual: collectorClient.requesterEventId, incoming: incomingEventId }); |
| 38 | + } |
| 39 | + return collectorClient; |
| 40 | + } |
| 41 | + // check if comming form hdsCollector |
| 42 | + if (!accessInfos?.clientData?.hdsCollector) { |
| 43 | + throw new HDSLibError('Invalid collector request, cannot find clientData.hdsCollector', { clientData: accessInfos?.clientData }); |
| 44 | + } |
| 45 | + // else create it |
| 46 | + |
| 47 | + const collectorClient = await CollectorClient.create(this, apiEndpoint, incomingEventId, accessInfos); |
| 48 | + this.cache.collectorClientsMap[collectorClient.key] = collectorClient; |
| 49 | + return collectorClient; |
| 50 | + } |
| 51 | + |
| 52 | + async getCollectorClients () { |
| 53 | + const apiCalls = [{ |
| 54 | + method: 'accesses.get', |
| 55 | + params: { includeDeletions: true } |
| 56 | + }, { |
| 57 | + method: 'events.get', |
| 58 | + params: { types: ['request/collector-client-v1'], streamIds: [this.baseStreamId], limit: MAX_COLLECTORS } |
| 59 | + } |
| 60 | + ]; |
| 61 | + const [accessesRes, eventRes] = await this.connection.api(apiCalls); |
| 62 | + } |
| 63 | + |
14 | 64 | /** |
15 | 65 | * - Check connection validity |
16 | 66 | * - Make sure stream structure exists |
|
0 commit comments