@nmshd/app-runtime 2.0.0-beta.1 → 2.0.0-beta.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. package/dist/AppRuntime.d.ts +11 -19
  2. package/dist/AppRuntime.js +64 -112
  3. package/dist/AppRuntime.js.map +1 -1
  4. package/dist/AppRuntimeErrors.d.ts +0 -1
  5. package/dist/AppRuntimeErrors.js +0 -3
  6. package/dist/AppRuntimeErrors.js.map +1 -1
  7. package/dist/SessionStorage.d.ts +10 -0
  8. package/dist/SessionStorage.js +31 -0
  9. package/dist/SessionStorage.js.map +1 -0
  10. package/dist/buildInformation.js +5 -5
  11. package/dist/events/index.d.ts +0 -1
  12. package/dist/events/index.js +0 -1
  13. package/dist/events/index.js.map +1 -1
  14. package/dist/modules/appEvents/MailReceivedModule.js +1 -5
  15. package/dist/modules/appEvents/MailReceivedModule.js.map +1 -1
  16. package/dist/modules/appEvents/OnboardingChangeReceivedModule.js +1 -5
  17. package/dist/modules/appEvents/OnboardingChangeReceivedModule.js.map +1 -1
  18. package/dist/modules/appSync/AppSyncModule.js +1 -2
  19. package/dist/modules/appSync/AppSyncModule.js.map +1 -1
  20. package/dist/modules/pushNotifications/PushNotificationModule.d.ts +1 -1
  21. package/dist/modules/pushNotifications/PushNotificationModule.js +18 -34
  22. package/dist/modules/pushNotifications/PushNotificationModule.js.map +1 -1
  23. package/dist/modules/runtimeEvents/MessageReceivedModule.js +2 -10
  24. package/dist/modules/runtimeEvents/MessageReceivedModule.js.map +1 -1
  25. package/dist/modules/runtimeEvents/RelationshipChangedModule.js +12 -14
  26. package/dist/modules/runtimeEvents/RelationshipChangedModule.js.map +1 -1
  27. package/lib-web/nmshd.app-runtime.js +143 -214
  28. package/lib-web/nmshd.app-runtime.min.js +1 -1
  29. package/package.json +10 -11
  30. package/dist/events/RequestReceivedEvent.d.ts +0 -8
  31. package/dist/events/RequestReceivedEvent.js +0 -15
  32. package/dist/events/RequestReceivedEvent.js.map +0 -1
@@ -100,44 +100,38 @@ const events_1 = __webpack_require__(/*! ./events */ "./dist/events/index.js");
100
100
  const extensibility_1 = __webpack_require__(/*! ./extensibility */ "./dist/extensibility/index.js");
101
101
  const modules_1 = __webpack_require__(/*! ./modules */ "./dist/modules/index.js");
102
102
  const multiAccount_1 = __webpack_require__(/*! ./multiAccount */ "./dist/multiAccount/index.js");
103
+ const SessionStorage_1 = __webpack_require__(/*! ./SessionStorage */ "./dist/SessionStorage.js");
103
104
  const UserfriendlyResult_1 = __webpack_require__(/*! ./UserfriendlyResult */ "./dist/UserfriendlyResult.js");
104
105
  class AppRuntime extends runtime_1.Runtime {
105
106
  constructor(_nativeEnvironment, appConfig) {
106
107
  super(appConfig, _nativeEnvironment.loggerFactory);
107
108
  this._nativeEnvironment = _nativeEnvironment;
108
- this._availableSessions = [];
109
+ this.sessionStorage = new SessionStorage_1.SessionStorage();
109
110
  this.translationProvider = {
110
111
  translate: (key) => Promise.resolve(ts_utils_1.Result.ok(key))
111
112
  };
112
113
  }
113
- uiBridge() {
114
+ async uiBridge() {
114
115
  if (this._uiBridge)
115
- return Promise.resolve(this._uiBridge);
116
- if (this._uiBridgePromise)
117
- return this._uiBridgePromise;
118
- this._uiBridgePromise = new Promise((resolve) => {
119
- this._uiBridgeResolveFunction = resolve;
120
- });
121
- this._uiBridgePromise
122
- .then(() => {
123
- this._uiBridgePromise = undefined;
124
- this._uiBridgeResolveFunction = undefined;
125
- })
126
- .catch((e) => {
127
- this._uiBridgePromise = undefined;
128
- this._uiBridgeResolveFunction = undefined;
129
- this.logger.error(e);
130
- });
131
- return this._uiBridgePromise;
116
+ return this._uiBridge;
117
+ if (this._uiBridgeResolver)
118
+ return await this._uiBridgeResolver.promise;
119
+ let resolve = () => "";
120
+ const promise = new Promise((r) => (resolve = r));
121
+ this._uiBridgeResolver = { promise, resolve };
122
+ try {
123
+ return await this._uiBridgeResolver.promise;
124
+ }
125
+ finally {
126
+ this._uiBridgeResolver = undefined;
127
+ }
132
128
  }
133
129
  registerUIBridge(uiBridge) {
134
- if (this._uiBridge) {
130
+ var _a;
131
+ if (this._uiBridge)
135
132
  return UserfriendlyResult_1.UserfriendlyResult.fail(AppRuntimeErrors_1.AppRuntimeErrors.startup.uiBridgeAlreadyRegistered());
136
- }
137
133
  this._uiBridge = uiBridge;
138
- if (this._uiBridgePromise && this._uiBridgeResolveFunction) {
139
- this._uiBridgeResolveFunction();
140
- }
134
+ (_a = this._uiBridgeResolver) === null || _a === void 0 ? void 0 : _a.resolve(uiBridge);
141
135
  return UserfriendlyResult_1.UserfriendlyResult.ok(undefined);
142
136
  }
143
137
  get multiAccountController() {
@@ -150,28 +144,22 @@ class AppRuntime extends runtime_1.Runtime {
150
144
  return this._nativeEnvironment;
151
145
  }
152
146
  get currentAccount() {
153
- if (!this._currentSession) {
154
- throw AppRuntimeErrors_1.AppRuntimeErrors.general.currentSessionUnavailable();
155
- }
156
- return this._currentSession.account;
147
+ return this.sessionStorage.currentSession.account;
148
+ }
149
+ get currentSession() {
150
+ return this.sessionStorage.currentSession;
151
+ }
152
+ getSessions() {
153
+ return this.sessionStorage.getSessions();
157
154
  }
158
155
  async login(accountController, consumptionController) {
159
156
  const services = await super.login(accountController, consumptionController);
160
157
  const appServices = new extensibility_1.AppServices(this, services.transportServices, services.consumptionServices, services.dataViewExpander);
161
158
  return { ...services, appServices };
162
159
  }
163
- get currentSession() {
164
- if (!this._currentSession) {
165
- throw AppRuntimeErrors_1.AppRuntimeErrors.general.currentSessionUnavailable();
166
- }
167
- return this._currentSession;
168
- }
169
- getServices(address) {
160
+ async getServices(address) {
170
161
  const addressString = typeof address === "string" ? address : address.toString();
171
- const session = this._availableSessions.find((session) => session.address === addressString);
172
- if (!session) {
173
- throw new Error(`Account ${addressString} not logged in.`);
174
- }
162
+ const session = await this.getOrCreateSession(addressString);
175
163
  return {
176
164
  transportServices: session.transportServices,
177
165
  consumptionServices: session.consumptionServices,
@@ -179,70 +167,43 @@ class AppRuntime extends runtime_1.Runtime {
179
167
  dataViewExpander: session.expander
180
168
  };
181
169
  }
182
- getSessions() {
183
- return this._availableSessions.map((session) => session.account);
184
- }
185
- getSession(accountId) {
186
- const session = this.findSession(accountId);
187
- if (session)
188
- return session.account;
189
- return undefined;
190
- }
191
- findSession(accountId) {
192
- return this._availableSessions.find((item) => item.account.id === accountId);
193
- }
194
- findSessionByAddress(address) {
195
- return this._availableSessions.find((item) => item.address === address);
170
+ async selectAccount(accountAddress, _password) {
171
+ const session = await this.getOrCreateSession(accountAddress);
172
+ this.sessionStorage.currentSession = session;
173
+ this.eventBus.publish(new events_1.AccountSelectedEvent(session.address, session.account.id));
174
+ return session;
196
175
  }
197
- async selectAccountByAddress(address, password) {
198
- if (this._currentSession && this._currentSession.address === address) {
199
- return this._currentSession.account;
176
+ async getOrCreateSession(accountReference) {
177
+ const existingSession = this.sessionStorage.findSession(accountReference);
178
+ if (existingSession) {
179
+ return existingSession;
200
180
  }
201
- const availableSession = this.findSessionByAddress(address);
202
- let accountId = availableSession === null || availableSession === void 0 ? void 0 : availableSession.account.id;
203
- if (!accountId) {
204
- accountId = (await this.multiAccountController.getAccountByAddress(address)).id.toString();
205
- }
206
- return await this.selectAccount(accountId, password);
181
+ return await this.createSession(accountReference);
207
182
  }
208
- async selectAccount(accountId, password) {
209
- if (this._currentSession && this._currentSession.account.id === accountId) {
210
- return this._currentSession.account;
211
- }
212
- const availableSession = this.findSession(accountId);
213
- // If there is any select promise, we have to await it, even before returning an available session
214
- if (this._accountIdToPromise && this._accountIdToPromise !== accountId) {
215
- if (!availableSession) {
216
- // Another account is currently logging in and we also need to log in -> error
217
- throw AppRuntimeErrors_1.AppRuntimeErrors.multiAccount.concurrentLoginOfDifferentAccounts();
218
- }
219
- else {
220
- // Another account is currently logging in and we already have an open session -> await login of the other account but do not return
221
- await this._selectAccountPromise;
222
- }
223
- }
224
- else if (this._selectAccountPromise) {
225
- // Same account is currently logging in -> await login and return
226
- return await this._selectAccountPromise;
227
- }
228
- if (availableSession) {
229
- await this.login(availableSession.accountController, availableSession.consumptionController);
230
- this._currentSession = availableSession;
231
- this.eventBus.publish(new events_1.AccountSelectedEvent(availableSession.address, availableSession.account.id));
232
- return availableSession.account;
183
+ async createSession(accountReference, masterPassword = "") {
184
+ var _a;
185
+ const accountId = accountReference.length === 20
186
+ ? accountReference
187
+ : (await this.multiAccountController.getAccountByAddress(accountReference)).id.toString();
188
+ if (((_a = this.currentSessionPromise) === null || _a === void 0 ? void 0 : _a.accountId) === accountId) {
189
+ return await this.currentSessionPromise.promise;
190
+ }
191
+ if (this.currentSessionPromise) {
192
+ await this.currentSessionPromise.promise.catch(() => {
193
+ // ignore
194
+ });
195
+ return await this.createSession(accountId, masterPassword);
233
196
  }
234
- this._selectAccountPromise = this._selectAccount(accountId, password);
197
+ this.currentSessionPromise = { promise: this._createSession(accountId, masterPassword), accountId };
235
198
  try {
236
- return await this._selectAccountPromise;
199
+ return await this.currentSessionPromise.promise;
237
200
  }
238
201
  finally {
239
- this._selectAccountPromise = undefined;
240
- this._accountIdToPromise = undefined;
202
+ this.currentSessionPromise = undefined;
241
203
  }
242
204
  }
243
- async _selectAccount(accountId, password) {
244
- this._accountIdToPromise = accountId;
245
- const [localAccount, accountController] = await this._multiAccountController.selectAccount(transport_1.CoreId.from(accountId), password);
205
+ async _createSession(accountId, masterPassword) {
206
+ const [localAccount, accountController] = await this._multiAccountController.selectAccount(transport_1.CoreId.from(accountId), masterPassword);
246
207
  if (!localAccount.address) {
247
208
  throw AppRuntimeErrors_1.AppRuntimeErrors.general.addressUnavailable().logWith(this.logger);
248
209
  }
@@ -259,10 +220,8 @@ class AppRuntime extends runtime_1.Runtime {
259
220
  accountController,
260
221
  consumptionController
261
222
  };
262
- this._availableSessions.push(session);
263
- this._currentSession = session;
264
- this.eventBus.publish(new events_1.AccountSelectedEvent(session.address, session.account.id));
265
- return session.account;
223
+ this.sessionStorage.addSession(session);
224
+ return session;
266
225
  }
267
226
  async queryAccount(title = "i18n://uibridge.accountSelection.title", description = "i18n://uibridge.accountSelection.description") {
268
227
  let selectedAccount;
@@ -284,20 +243,16 @@ class AppRuntime extends runtime_1.Runtime {
284
243
  return UserfriendlyResult_1.UserfriendlyResult.ok(selectedAccount);
285
244
  }
286
245
  async selectRelationship(id) {
287
- if (!this._currentSession) {
288
- throw AppRuntimeErrors_1.AppRuntimeErrors.general.currentSessionUnavailable().logWith(this.logger);
289
- }
290
246
  if (!id) {
291
- this._currentSession.selectedRelationship = undefined;
247
+ this.currentSession.selectedRelationship = undefined;
292
248
  return;
293
249
  }
294
250
  const result = await this.currentSession.appServices.relationships.renderRelationship(id);
295
- if (result.isError) {
251
+ if (result.isError)
296
252
  throw result.error;
297
- }
298
253
  const relationship = result.value;
299
- this._currentSession.selectedRelationship = relationship;
300
- this.eventBus.publish(new events_1.RelationshipSelectedEvent(this._currentSession.address, relationship));
254
+ this.currentSession.selectedRelationship = relationship;
255
+ this.eventBus.publish(new events_1.RelationshipSelectedEvent(this.currentSession.address, relationship));
301
256
  }
302
257
  getHealth() {
303
258
  const health = {
@@ -338,9 +293,6 @@ class AppRuntime extends runtime_1.Runtime {
338
293
  runtime.logger.trace("Runtime started");
339
294
  return runtime;
340
295
  }
341
- createLoggerFactory() {
342
- return this.nativeEnvironment.loggerFactory;
343
- }
344
296
  createDatabaseConnection() {
345
297
  this.logger.trace("Creating DatabaseConnection to LokiJS");
346
298
  this.lokiConnection = new docdb_access_loki_1.LokiJsConnection("./data", this.nativeEnvironment.databaseFactory);
@@ -469,9 +421,6 @@ class MultiAccount {
469
421
  wrongRealm() {
470
422
  return new UserfriendlyApplicationError_1.UserfriendlyApplicationError("error.runtime.MultiAccount.WrongRealm", "The given realm is invalid.");
471
423
  }
472
- concurrentLoginOfDifferentAccounts() {
473
- return new UserfriendlyApplicationError_1.UserfriendlyApplicationError("error.runtime.MultiAccount.ParallelLoginOfDifferentAccounts", "You cannot login different accounts concurrently. Please wait until the login is completed.");
474
- }
475
424
  }
476
425
  class Modules {
477
426
  constructor() {
@@ -489,6 +438,47 @@ AppRuntimeErrors.multiAccount = new MultiAccount();
489
438
 
490
439
  /***/ }),
491
440
 
441
+ /***/ "./dist/SessionStorage.js":
442
+ /*!********************************!*\
443
+ !*** ./dist/SessionStorage.js ***!
444
+ \********************************/
445
+ /***/ ((__unused_webpack_module, exports, __webpack_require__) => {
446
+
447
+ "use strict";
448
+
449
+ Object.defineProperty(exports, "__esModule", ({ value: true }));
450
+ exports.SessionStorage = void 0;
451
+ const AppRuntimeErrors_1 = __webpack_require__(/*! ./AppRuntimeErrors */ "./dist/AppRuntimeErrors.js");
452
+ class SessionStorage {
453
+ constructor() {
454
+ this._availableSessions = [];
455
+ }
456
+ set currentSession(session) {
457
+ this._currentSession = session;
458
+ }
459
+ get currentSession() {
460
+ if (!this._currentSession) {
461
+ throw AppRuntimeErrors_1.AppRuntimeErrors.general.currentSessionUnavailable();
462
+ }
463
+ return this._currentSession;
464
+ }
465
+ getSessions() {
466
+ return this._availableSessions;
467
+ }
468
+ findSession(accountReference) {
469
+ return this._availableSessions.find((item) => item.account.address === accountReference || item.account.id === accountReference);
470
+ }
471
+ addSession(session) {
472
+ if (this.findSession(session.account.id))
473
+ throw new Error("Session already exists");
474
+ this._availableSessions.push(session);
475
+ }
476
+ }
477
+ exports.SessionStorage = SessionStorage;
478
+ //# sourceMappingURL=SessionStorage.js.map
479
+
480
+ /***/ }),
481
+
492
482
  /***/ "./dist/UserfriendlyApplicationError.js":
493
483
  /*!**********************************************!*\
494
484
  !*** ./dist/UserfriendlyApplicationError.js ***!
@@ -552,11 +542,11 @@ const crypto_1 = __webpack_require__(/*! @nmshd/crypto */ "@nmshd/crypto");
552
542
  const runtime_1 = __webpack_require__(/*! @nmshd/runtime */ "@nmshd/runtime");
553
543
  const transport_1 = __webpack_require__(/*! @nmshd/transport */ "@nmshd/transport");
554
544
  exports.buildInformation = {
555
- version: "2.0.0-beta.1",
556
- build: "18",
557
- date: "2022-07-26T13:34:15+00:00",
558
- commit: "759a2353d512c735290e3e8853079a029c72fc65",
559
- dependencies: {"@js-soft/docdb-access-loki":"^1.0.3","@js-soft/native-abstractions":"^1.2.0","@nmshd/runtime":"2.0.0-beta.9","lodash":"^4.17.21"},
545
+ version: "2.0.0-beta.10",
546
+ build: "27",
547
+ date: "2022-08-25T12:04:58+00:00",
548
+ commit: "316eb6de4a2f76523d837aefcf0aa520d1d29542",
549
+ dependencies: {"@js-soft/docdb-access-loki":"^1.0.3","@js-soft/native-abstractions":"^1.2.0","@nmshd/runtime":"2.0.0-beta.20","lodash":"^4.17.21"},
560
550
  libraries: {
561
551
  serval: ts_serval_1.buildInformation,
562
552
  crypto: crypto_1.buildInformation,
@@ -713,31 +703,6 @@ RelationshipSelectedEvent.namespace = "app.relationshipSelected";
713
703
 
714
704
  /***/ }),
715
705
 
716
- /***/ "./dist/events/RequestReceivedEvent.js":
717
- /*!*********************************************!*\
718
- !*** ./dist/events/RequestReceivedEvent.js ***!
719
- \*********************************************/
720
- /***/ ((__unused_webpack_module, exports, __webpack_require__) => {
721
-
722
- "use strict";
723
-
724
- Object.defineProperty(exports, "__esModule", ({ value: true }));
725
- exports.RequestReceivedEvent = void 0;
726
- const runtime_1 = __webpack_require__(/*! @nmshd/runtime */ "@nmshd/runtime");
727
- class RequestReceivedEvent extends runtime_1.DataEvent {
728
- constructor(address, request, message) {
729
- super(RequestReceivedEvent.namespace, address, {
730
- request,
731
- message
732
- });
733
- }
734
- }
735
- exports.RequestReceivedEvent = RequestReceivedEvent;
736
- RequestReceivedEvent.namespace = "app.requestReceived";
737
- //# sourceMappingURL=RequestReceivedEvent.js.map
738
-
739
- /***/ }),
740
-
741
706
  /***/ "./dist/events/index.js":
742
707
  /*!******************************!*\
743
708
  !*** ./dist/events/index.js ***!
@@ -767,7 +732,6 @@ __exportStar(__webpack_require__(/*! ./ExternalEventReceivedEvent */ "./dist/eve
767
732
  __exportStar(__webpack_require__(/*! ./MailReceivedEvent */ "./dist/events/MailReceivedEvent.js"), exports);
768
733
  __exportStar(__webpack_require__(/*! ./OnboardingChangeReceivedEvent */ "./dist/events/OnboardingChangeReceivedEvent.js"), exports);
769
734
  __exportStar(__webpack_require__(/*! ./RelationshipSelectedEvent */ "./dist/events/RelationshipSelectedEvent.js"), exports);
770
- __exportStar(__webpack_require__(/*! ./RequestReceivedEvent */ "./dist/events/RequestReceivedEvent.js"), exports);
771
735
  //# sourceMappingURL=index.js.map
772
736
 
773
737
  /***/ }),
@@ -1217,12 +1181,8 @@ class MailReceivedModule extends AppRuntimeModule_1.AppRuntimeModule {
1217
1181
  this.subscribeToEvent(events_1.MailReceivedEvent, this.handleMailReceived.bind(this));
1218
1182
  }
1219
1183
  async handleMailReceived(event) {
1184
+ const session = await this.runtime.getOrCreateSession(event.eventTargetAddress);
1220
1185
  const mail = event.data;
1221
- const session = this.runtime.findSessionByAddress(event.eventTargetAddress);
1222
- if (!session) {
1223
- this.logger.error(`No session found for address ${event.eventTargetAddress}`);
1224
- return;
1225
- }
1226
1186
  const sender = mail.createdBy;
1227
1187
  await this.runtime.nativeEnvironment.notificationAccess.schedule(mail.name, mail.createdBy.name, {
1228
1188
  callback: async () => {
@@ -1268,11 +1228,7 @@ class OnboardingChangeReceivedModule extends AppRuntimeModule_1.AppRuntimeModule
1268
1228
  const identity = event.data.identity;
1269
1229
  let title = "";
1270
1230
  let text = "";
1271
- const session = this.runtime.findSessionByAddress(event.eventTargetAddress);
1272
- if (!session) {
1273
- this.logger.error(`No session found for address ${event.eventTargetAddress}`);
1274
- return;
1275
- }
1231
+ const session = await this.runtime.getOrCreateSession(event.eventTargetAddress);
1276
1232
  switch (change.status) {
1277
1233
  case runtime_1.RelationshipChangeStatus.Accepted:
1278
1234
  title = "Kontaktanfrage genehmigt";
@@ -1366,8 +1322,7 @@ class AppSyncModule extends AppRuntimeModule_1.AppRuntimeModule {
1366
1322
  }
1367
1323
  async sync() {
1368
1324
  for (const session of this.runtime.getSessions()) {
1369
- const services = this.runtime.getServices(session.address);
1370
- const result = await services.transportServices.account.syncEverything();
1325
+ const result = await session.transportServices.account.syncEverything();
1371
1326
  if (result.isError) {
1372
1327
  this.logger.error(result.error);
1373
1328
  }
@@ -1495,28 +1450,23 @@ class PushNotificationModule extends AppRuntimeModule_1.AppRuntimeModule {
1495
1450
  const notification = event.notification;
1496
1451
  const content = notification.content;
1497
1452
  try {
1498
- const account = await this.runtime.selectAccountByAddress(content.accRef, "");
1499
- const session = this.runtime.findSession(account.id.toString());
1500
- if (!session) {
1501
- this.logger.error(`No session found for account ref ${content.accRef}`);
1502
- return;
1503
- }
1453
+ const services = await this.runtime.getServices(content.accRef);
1504
1454
  switch (content.eventName) {
1505
1455
  case IBackboneEventContent_1.BackboneEventName.DatawalletModificationsCreated:
1506
- const walletResult = await session.transportServices.account.syncDatawallet();
1456
+ const walletResult = await services.transportServices.account.syncDatawallet();
1507
1457
  if (walletResult.isError) {
1508
1458
  this.logger.error(walletResult);
1509
1459
  return;
1510
1460
  }
1511
- this.runtime.eventBus.publish(new events_1.DatawalletSynchronizedEvent(session.address));
1461
+ this.runtime.eventBus.publish(new events_1.DatawalletSynchronizedEvent(content.accRef));
1512
1462
  break;
1513
1463
  case IBackboneEventContent_1.BackboneEventName.ExternalEventCreated:
1514
- const syncResult = await session.transportServices.account.syncEverything();
1464
+ const syncResult = await services.transportServices.account.syncEverything();
1515
1465
  if (syncResult.isError) {
1516
1466
  this.logger.error(syncResult);
1517
1467
  return;
1518
1468
  }
1519
- this.runtime.eventBus.publish(new events_1.ExternalEventReceivedEvent(session.address, syncResult.value.messages, syncResult.value.relationships));
1469
+ this.runtime.eventBus.publish(new events_1.ExternalEventReceivedEvent(content.accRef, syncResult.value.messages, syncResult.value.relationships));
1520
1470
  break;
1521
1471
  default:
1522
1472
  break;
@@ -1529,8 +1479,8 @@ class PushNotificationModule extends AppRuntimeModule_1.AppRuntimeModule {
1529
1479
  async handleTokenRegistration(event) {
1530
1480
  try {
1531
1481
  this.logger.trace("PushNotificationModule.handleTokenRegistration", event);
1532
- for (const account of this.runtime.getSessions()) {
1533
- await this.registerPushTokenForLocalAccount(account.id, event.token);
1482
+ for (const session of this.runtime.getSessions()) {
1483
+ await this.registerPushTokenForLocalAccount(session.account.address, event.token);
1534
1484
  }
1535
1485
  }
1536
1486
  catch (e) {
@@ -1538,33 +1488,22 @@ class PushNotificationModule extends AppRuntimeModule_1.AppRuntimeModule {
1538
1488
  }
1539
1489
  }
1540
1490
  async handleAccountSelected(event) {
1541
- try {
1542
- this.logger.trace("PushNotificationModule.handleAccountSelected", event);
1543
- const tokenResult = this.getNotificationTokenFromConfig();
1544
- if (tokenResult.isSuccess) {
1545
- await this.registerPushTokenForLocalAccount(event.data.localAccountId, tokenResult.value);
1546
- }
1547
- else {
1548
- this.logger.error(tokenResult.error);
1549
- }
1550
- }
1551
- catch (e) {
1552
- this.logger.error(e);
1491
+ this.logger.trace("PushNotificationModule.handleAccountSelected", event);
1492
+ const tokenResult = this.getNotificationTokenFromConfig();
1493
+ if (tokenResult.isError) {
1494
+ this.logger.error(tokenResult.error);
1495
+ return;
1553
1496
  }
1497
+ await this.registerPushTokenForLocalAccount(event.data.address, tokenResult.value);
1554
1498
  }
1555
- async registerPushTokenForLocalAccount(id, token) {
1499
+ async registerPushTokenForLocalAccount(address, token) {
1556
1500
  if (!token) {
1557
1501
  throw AppRuntimeErrors_1.AppRuntimeErrors.modules.pushNotificationModule
1558
1502
  .tokenRegistrationNotPossible("The registered token was empty. This might be the case if you did not allow push notifications.")
1559
1503
  .logWith(this.logger);
1560
1504
  }
1561
- const session = this.runtime.findSession(id);
1562
- if (!session) {
1563
- throw AppRuntimeErrors_1.AppRuntimeErrors.modules.pushNotificationModule
1564
- .tokenRegistrationNotPossible("No session for this account found")
1565
- .logWith(this.logger);
1566
- }
1567
- const deviceResult = await session.transportServices.account.getDeviceInfo();
1505
+ const services = await this.runtime.getServices(address);
1506
+ const deviceResult = await services.transportServices.account.getDeviceInfo();
1568
1507
  if (deviceResult.isError) {
1569
1508
  throw AppRuntimeErrors_1.AppRuntimeErrors.modules.pushNotificationModule
1570
1509
  .tokenRegistrationNotPossible("No device for this account found", deviceResult.error)
@@ -1574,7 +1513,7 @@ class PushNotificationModule extends AppRuntimeModule_1.AppRuntimeModule {
1574
1513
  const platform = this.runtime.nativeEnvironment.deviceInfoAccess.deviceInfo.pushService;
1575
1514
  const handle = token;
1576
1515
  const installationId = device.id;
1577
- const result = await session.transportServices.account.registerPushNotificationToken({
1516
+ const result = await services.transportServices.account.registerPushNotificationToken({
1578
1517
  platform,
1579
1518
  handle,
1580
1519
  installationId
@@ -1585,7 +1524,7 @@ class PushNotificationModule extends AppRuntimeModule_1.AppRuntimeModule {
1585
1524
  .logWith(this.logger);
1586
1525
  }
1587
1526
  else {
1588
- this.logger.trace(`PushNotificationModule.registerPushTokenForLocalAccount: Token ${handle} registered for account ${id} on platform ${platform} and installationId ${installationId}`);
1527
+ this.logger.trace(`PushNotificationModule.registerPushTokenForLocalAccount: Token ${handle} registered for account ${address} on platform ${platform} and installationId ${installationId}`);
1589
1528
  }
1590
1529
  }
1591
1530
  getNotificationTokenFromConfig() {
@@ -1658,17 +1597,9 @@ class MessageReceivedModule extends AppRuntimeModule_1.AppRuntimeModule {
1658
1597
  this.subscribeToEvent(runtime_1.MessageReceivedEvent, this.handleMessageReceived.bind(this));
1659
1598
  }
1660
1599
  async handleMessageReceived(event) {
1661
- const message = event.data;
1662
- const session = this.runtime.findSessionByAddress(event.eventTargetAddress);
1663
- if (!session) {
1664
- this.logger.error(`No session found for address ${event.eventTargetAddress}`);
1665
- return;
1666
- }
1667
- const messageDVO = await session.expander.expandMessageDTO(message);
1600
+ const services = await this.runtime.getServices(event.eventTargetAddress);
1601
+ const messageDVO = await services.dataViewExpander.expandMessageDTO(event.data);
1668
1602
  switch (messageDVO.type) {
1669
- case "RequestMessageDVO":
1670
- this.runtime.eventBus.publish(new events_1.RequestReceivedEvent(event.eventTargetAddress, messageDVO.request, messageDVO));
1671
- break;
1672
1603
  case "MailDVO":
1673
1604
  const mail = messageDVO;
1674
1605
  this.runtime.eventBus.publish(new events_1.MailReceivedEvent(event.eventTargetAddress, mail));
@@ -1712,21 +1643,19 @@ class RelationshipChangedModule extends AppRuntimeModule_1.AppRuntimeModule {
1712
1643
  }
1713
1644
  async handleRelationshipChanged(event) {
1714
1645
  const relationship = event.data;
1715
- const session = this.runtime.findSessionByAddress(event.eventTargetAddress);
1716
- if (!session) {
1717
- this.logger.error(`No session found for address ${event.eventTargetAddress}`);
1646
+ // Only listen for the creation change (the first one)
1647
+ if (relationship.changes.length !== 1)
1718
1648
  return;
1719
- }
1720
- // The very first change is the onboarding change
1721
- if (relationship.changes.length === 1) {
1722
- const change = relationship.changes[0];
1723
- // Only fire received events if the current session did not create it
1724
- if ((!change.response && change.request.createdBy !== session.address) ||
1725
- (change.response && change.response.createdBy !== session.address)) {
1726
- const relationshipDVO = await session.expander.expandRelationshipDTO(relationship);
1727
- this.runtime.eventBus.publish(new events_1.OnboardingChangeReceivedEvent(session.address, relationship.changes[0], relationship, relationshipDVO));
1728
- }
1729
- }
1649
+ const change = relationship.changes[0];
1650
+ // response exits and created by the current identity
1651
+ if (!change.response && change.request.createdBy === event.eventTargetAddress)
1652
+ return;
1653
+ // response exits and created by the current identity
1654
+ if (change.response && change.response.createdBy === event.eventTargetAddress)
1655
+ return;
1656
+ const services = await this.runtime.getServices(event.eventTargetAddress);
1657
+ const relationshipDVO = await services.dataViewExpander.expandRelationshipDTO(relationship);
1658
+ this.runtime.eventBus.publish(new events_1.OnboardingChangeReceivedEvent(event.eventTargetAddress, change, relationship, relationshipDVO));
1730
1659
  }
1731
1660
  stop() {
1732
1661
  this.unsubscribeFromAllEvents();
@@ -3537,7 +3466,7 @@ class ApplicationError extends Error {
3537
3466
  return this.code === error.code;
3538
3467
  }
3539
3468
  toString() {
3540
- return JSON.stringify(this, undefined, 2);
3469
+ return JSON.stringify({ code: this.code, message: this.message, data: this.data }, undefined, 2);
3541
3470
  }
3542
3471
  }
3543
3472
  exports.ApplicationError = ApplicationError;