|
| 1 | +const pryv = require('../patchedPryv'); |
| 2 | +class Application { |
| 3 | + /** @type {Pryv.Connection} */ |
| 4 | + connection; |
| 5 | + /** @type {string} */ |
| 6 | + baseStreamId; |
| 7 | + /** @type {string} */ |
| 8 | + appName; |
| 9 | + |
| 10 | + cache; |
| 11 | + |
| 12 | + get appSettings () { |
| 13 | + throw new Error('appSettings must be implemneted'); |
| 14 | + // possible return values: |
| 15 | + /** |
| 16 | + * return { |
| 17 | + * canBePersonnal: true, |
| 18 | + * mustBeMaster: true |
| 19 | + * appNameFromAccessInfo: true // application name will be taken from Access-Info Name |
| 20 | + * }; |
| 21 | + */ |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Create with an apiEnpoint |
| 26 | + * @param {string} apiEndpoint |
| 27 | + * @param {string} baseStreamId - application base Strem ID |
| 28 | + * @param {string} [appName] - optional if appSettings.appNameFromAccessInfo is set to true |
| 29 | + * @returns {AppClientAccount} |
| 30 | + */ |
| 31 | + static async newFromApiEndpoint (baseStreamId, apiEndpoint, appName) { |
| 32 | + const connection = new pryv.Connection(apiEndpoint); |
| 33 | + // in a static method "this" is the Class (here the extending class) |
| 34 | + return await this.newFromConnection(baseStreamId, connection, appName); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Create with an apiEnpoint |
| 39 | + * @param {Pryv.connection} connection - must be a connection with personnalToken or masterToken |
| 40 | + * @param {string} baseStreamId - application base Strem ID |
| 41 | + * @param {string} [appName] - optional if appSettings.appNameFromAccessInfo is set to true |
| 42 | + * @returns {AppClientAccount} |
| 43 | + */ |
| 44 | + static async newFromConnection (baseStreamId, connection, appName) { |
| 45 | + // in a static method "this" is the Class (here the extending class) |
| 46 | + const app = new this(baseStreamId, connection, appName); |
| 47 | + await app.init(); |
| 48 | + return app; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @private |
| 53 | + * use .newFrom...() to create new AppManagingAccount |
| 54 | + * @param {string} baseStreamId |
| 55 | + * @param {Pryv.Connection} connection |
| 56 | + * @param {string} [appName] - optional if appSettings.appNameFromAccessInfo is set to true |
| 57 | + */ |
| 58 | + constructor (baseStreamId, connection, appName) { |
| 59 | + if (!baseStreamId || baseStreamId.length < 2) throw new Error('Missing or too short baseStreamId'); |
| 60 | + this.baseStreamId = baseStreamId; |
| 61 | + if (appName == null && !this.appSettings.appNameFromAccessInfo) { |
| 62 | + throw new Error('appName must be given unless appSettings.appNameFromAccessInfo = true'); |
| 63 | + } |
| 64 | + this.appName = appName; |
| 65 | + this.connection = connection; |
| 66 | + this.cache = { }; |
| 67 | + } |
| 68 | + |
| 69 | + async init () { |
| 70 | + await createAppStreams(this); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +module.exports = Application; |
| 75 | + |
| 76 | +// create app Streams |
| 77 | + |
| 78 | +async function createAppStreams (app) { |
| 79 | + // check that connection has a personal or master token or has "manage" rights on baseStream |
| 80 | + const infos = await app.connection.accessInfo(); |
| 81 | + if (app.appSettings.appNameFromAccessInfo) { |
| 82 | + app.appName = infos.name; |
| 83 | + } |
| 84 | + let isPersonalOrMaster = infos.type === 'personal'; |
| 85 | + if (!app.appSettings.canBePersonnal && infos.type === 'personal') { |
| 86 | + throw new Error('Application should not use a personal token'); |
| 87 | + } |
| 88 | + if (!isPersonalOrMaster) { |
| 89 | + const allowPersonalStr = app.appSettings.canBePersonnal ? '"personal" or ' : ''; |
| 90 | + |
| 91 | + if (infos.type !== 'app') throw new Error(`Application requires a ${allowPersonalStr} "app" type of access`); |
| 92 | + const masterFound = infos.permissions.find(p => (p.streamId === '*' && p.level === 'manage')); |
| 93 | + isPersonalOrMaster = true; |
| 94 | + if (app.appSettings.mustBemaster && !masterFound) { |
| 95 | + throw new Error('Application with "app" type of access requires "master" token (streamId = "*", level = "manage")'); |
| 96 | + } else { // check that app has "manage" level on baseStreamId |
| 97 | + const baseStreamFound = infos.permissions.find(p => (p.streamId === app.baseStreamId && p.level === 'manage')); |
| 98 | + if (!baseStreamFound) throw new Error(`Application with "app" type of access requires (streamId = '${app.baseStreamId}', level = "manage") or master access`); |
| 99 | + } |
| 100 | + } |
| 101 | + // get streamStructure |
| 102 | + let found = false; |
| 103 | + try { |
| 104 | + const stream = (await app.connection.apiOne('streams.get', { id: app.baseStreamId }, 'streams'))[0]; |
| 105 | + if (stream) { |
| 106 | + app.cache.streamData = stream; |
| 107 | + } |
| 108 | + found = true; |
| 109 | + } catch (e) { |
| 110 | + if (e.innerObject?.id !== 'unknown-referenced-resource' || e.innerObject?.data?.id !== 'test-app-template-client') { |
| 111 | + throw e; |
| 112 | + } |
| 113 | + } |
| 114 | + // not found create streams |
| 115 | + if (!found) { |
| 116 | + if (app.appName == null) { |
| 117 | + throw new Error('Cannot create app stream if not "appName" has been given'); |
| 118 | + } |
| 119 | + if (!isPersonalOrMaster) { |
| 120 | + throw new Error('Token has not sufficient right to create App streams. Create them upfront'); |
| 121 | + } |
| 122 | + const apiCalls = [ |
| 123 | + { method: 'streams.create', params: { id: 'applications', name: 'Applications' } }, |
| 124 | + { method: 'streams.create', params: { id: app.baseStreamId, name: app.appName, parentId: 'applications' } } |
| 125 | + ]; |
| 126 | + const streamCreateResult = await app.connection.api(apiCalls); |
| 127 | + const stream = streamCreateResult[1].stream; |
| 128 | + app.cache.streamData = stream; |
| 129 | + } |
| 130 | +} |
0 commit comments