-
Notifications
You must be signed in to change notification settings - Fork 20
/
Account.ts
714 lines (632 loc) · 27.8 KB
/
Account.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
/*
* Wire
* Copyright (C) 2018 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*
*/
import {
RegisterData,
AUTH_COOKIE_KEY,
AUTH_TABLE_NAME,
Context,
Cookie,
CookieStore,
LoginData,
} from '@wireapp/api-client/lib/auth';
import {ClientClassification, ClientType, RegisteredClient} from '@wireapp/api-client/lib/client/';
import * as Events from '@wireapp/api-client/lib/event';
import {CONVERSATION_EVENT} from '@wireapp/api-client/lib/event';
import {Notification} from '@wireapp/api-client/lib/notification/';
import {AbortHandler, WebSocketClient} from '@wireapp/api-client/lib/tcp/';
import {WEBSOCKET_STATE} from '@wireapp/api-client/lib/tcp/ReconnectingWebsocket';
import axios from 'axios';
import {Encoder} from 'bazinga64';
import {StatusCodes as HTTP_STATUS} from 'http-status-codes';
import logdown from 'logdown';
import {EventEmitter} from 'events';
import {APIClient, BackendFeatures} from '@wireapp/api-client';
import {CoreCrypto} from '@wireapp/core-crypto';
import * as cryptobox from '@wireapp/cryptobox';
import {CRUDEngine, error as StoreEngineError, MemoryEngine} from '@wireapp/store-engine';
import {AccountService} from './account/';
import {LoginSanitizer} from './auth/';
import {BroadcastService} from './broadcast/';
import {ClientInfo, ClientService} from './client/';
import {ConnectionService} from './connection/';
import {AssetService, ConversationService} from './conversation/';
import {getQueueLength, pauseMessageSending, resumeMessageSending} from './conversation/message/messageSender';
import {CoreError} from './CoreError';
import {CryptographyService, SessionId} from './cryptography/';
import {GiphyService} from './giphy/';
import {LinkPreviewService} from './linkPreview';
import {MLSService} from './messagingProtocols/mls';
import {MLSCallbacks, CryptoProtocolConfig} from './messagingProtocols/mls/types';
import {ProteusService} from './messagingProtocols/proteus';
import {HandledEventPayload, NotificationService, NotificationSource} from './notification/';
import {SelfService} from './self/';
import {TeamService} from './team/';
import {UserService} from './user/';
import {createCustomEncryptedStore, createEncryptedStore, deleteEncryptedStore} from './util/encryptedStore';
export type ProcessedEventPayload = HandledEventPayload;
enum TOPIC {
ERROR = 'Account.TOPIC.ERROR',
}
export enum ConnectionState {
/** The websocket is closed and notifications stream is not being processed */
CLOSED = 'closed',
/** The websocket is being opened */
CONNECTING = 'connecting',
/** The websocket is open but locked and notifications stream is being processed */
PROCESSING_NOTIFICATIONS = 'processing_notifications',
/** The websocket is open and message will go through and notifications stream is fully processed */
LIVE = 'live',
}
export interface Account {
on(event: TOPIC.ERROR, listener: (payload: CoreError) => void): this;
}
export type CreateStoreFn = (storeName: string, context: Context) => undefined | Promise<CRUDEngine | undefined>;
interface AccountOptions<T> {
/** Used to store info in the database (will create a inMemory engine if returns undefined) */
createStore?: CreateStoreFn;
/** Number of prekeys to generate when creating a new device (defaults to 2)
* Prekeys are Diffie-Hellmann public keys which allow offline initiation of a secure Proteus session between two devices.
* Having a high value will:
* - make creating a new device consuming more CPU resources
* - make it less likely that all prekeys get consumed while the device is offline and the last resort prekey will not be used to create new session
* Having a low value will:
* - make creating a new device fast
* - make it likely that all prekeys get consumed while the device is offline and the last resort prekey will be used to create new session
*/
nbPrekeys?: number;
/**
* Config for MLS and proteus devices. Will fallback to the old proteus logic if not provided
*/
cryptoProtocolConfig?: CryptoProtocolConfig<T>;
}
type InitOptions = {
/** cookie used to identify the current user. Will use the browser cookie if not defined */
cookie?: Cookie;
/** fully initiate the client and register periodic checks */
initClient?: boolean;
/**
* callback triggered when a message from an unknown client is received.
* An unknown client is a client we don't yet have a session with
*/
onNewClient?: (sessionId: SessionId) => void;
};
const coreDefaultClient: ClientInfo = {
classification: ClientClassification.DESKTOP,
cookieLabel: 'default',
model: '@wireapp/core',
};
export class Account<T = any> extends EventEmitter {
private readonly apiClient: APIClient;
private readonly logger: logdown.Logger;
private readonly createStore: CreateStoreFn;
private storeEngine?: CRUDEngine;
private readonly nbPrekeys: number;
private readonly cryptoProtocolConfig?: CryptoProtocolConfig<T>;
private coreCryptoClient?: CoreCrypto;
public static readonly TOPIC = TOPIC;
public service?: {
mls: MLSService;
account: AccountService;
asset: AssetService;
broadcast: BroadcastService;
client: ClientService;
connection: ConnectionService;
conversation: ConversationService;
cryptography: CryptographyService;
giphy: GiphyService;
linkPreview: LinkPreviewService;
notification: NotificationService;
self: SelfService;
team: TeamService;
user: UserService;
};
public backendFeatures: BackendFeatures;
/**
* @param apiClient The apiClient instance to use in the core (will create a new new one if undefined)
* @param accountOptions
*/
constructor(
apiClient: APIClient = new APIClient(),
{createStore = () => undefined, nbPrekeys = 2, cryptoProtocolConfig}: AccountOptions<T> = {},
) {
super();
this.apiClient = apiClient;
this.backendFeatures = this.apiClient.backendFeatures;
this.cryptoProtocolConfig = cryptoProtocolConfig;
this.nbPrekeys = nbPrekeys;
this.createStore = createStore;
apiClient.on(APIClient.TOPIC.COOKIE_REFRESH, async (cookie?: Cookie) => {
if (cookie && this.storeEngine) {
try {
await this.persistCookie(this.storeEngine, cookie);
} catch (error) {
this.logger.error(`Failed to save cookie: ${(error as Error).message}`, error);
}
}
});
this.logger = logdown('@wireapp/core/Account', {
logger: console,
markdown: false,
});
}
/**
* Will set the APIClient to use a specific version of the API (by default uses version 0)
* It will fetch the API Config and use the highest possible version
* @param acceptedVersions Which version the consumer supports
* @param useDevVersion allow the api-client to use development version of the api (if present). The dev version also need to be listed on the supportedVersions given as parameters
* If we have version 2 that is a dev version, this is going to be the output of those calls
* - useVersion([0, 1, 2], true) > version 2 is used
* - useVersion([0, 1, 2], false) > version 1 is used
* - useVersion([0, 1], true) > version 1 is used
* @return The highest version that is both supported by client and backend
*/
public async useAPIVersion(supportedVersions: number[], useDevVersion?: boolean) {
const features = await this.apiClient.useVersion(supportedVersions, useDevVersion);
this.backendFeatures = features;
return features;
}
private persistCookie(storeEngine: CRUDEngine, cookie: Cookie): Promise<string> {
const entity = {expiration: cookie.expiration, zuid: cookie.zuid};
return storeEngine.updateOrCreate(AUTH_TABLE_NAME, AUTH_COOKIE_KEY, entity);
}
get clientId(): string {
return this.apiClient.validatedClientId;
}
get userId(): string {
return this.apiClient.validatedUserId;
}
/**
* Will register a new user to the backend
*
* @param registration The user's data
* @param clientType Type of client to create (temporary or permanent)
*/
public async register(registration: RegisterData, clientType: ClientType): Promise<Context> {
const context = await this.apiClient.register(registration, clientType);
await this.initServices(context);
return context;
}
/**
* Will init the core with an already existing client (both on backend and local)
* Will fail if local client cannot be found
*
* @param clientType The type of client the user is using (temporary or permanent)
*/
public async init(
clientType: ClientType,
{cookie, initClient = true, onNewClient}: InitOptions = {},
): Promise<Context> {
const context = await this.apiClient.init(clientType, cookie);
await this.initServices(context);
/** @fixme
* When we will start migrating to CoreCrypto encryption/decryption, those hooks won't be available anymore
* We will need to implement
* - the mechanism to handle messages from an unknown sender
* - the mechanism to generate new prekeys when we reach a certain threshold of prekeys
*/
this.service!.cryptography.setCryptoboxHooks({
onNewPrekeys: async prekeys => {
this.logger.debug(`Received '${prekeys.length}' new PreKeys.`);
await this.apiClient.api.client.putClient(context.clientId!, {prekeys});
this.logger.debug(`Successfully uploaded '${prekeys.length}' PreKeys.`);
},
onNewSession: onNewClient,
});
// Assumption: client gets only initialized once
if (initClient) {
const {localClient} = await this.initClient({clientType});
//call /access endpoint with client_id after client initialisation
await this.apiClient.transport.http.associateClientWithSession(localClient.id);
if (this.cryptoProtocolConfig?.mls && this.backendFeatures.supportsMLS) {
// initialize schedulers for pending mls proposals once client is initialized
await this.service?.mls.checkExistingPendingProposals();
// initialize schedulers for renewing key materials
this.service?.mls.checkForKeyMaterialsUpdate();
// initialize scheduler for syncing key packages with backend
this.service?.mls.checkForKeyPackagesBackendSync();
}
}
return context;
}
/**
* Will log the user in with the given credential.
* Will also create the local client and store it in DB
*
* @param loginData The credentials of the user
* @param initClient Should the call also create the local client
* @param clientInfo Info about the client to create (name, type...)
*/
public async login(
loginData: LoginData,
initClient: boolean = true,
clientInfo: ClientInfo = coreDefaultClient,
): Promise<Context> {
this.resetContext();
LoginSanitizer.removeNonPrintableCharacters(loginData);
const context = await this.apiClient.login(loginData);
await this.initServices(context);
if (initClient) {
await this.initClient(loginData, clientInfo);
}
return context;
}
/**
* Will try to get the load the local client from local DB.
* If clientInfo are provided, will also create the client on backend and DB
* If clientInfo are not provided, the method will fail if local client cannot be found
*
* @param loginData User's credentials
* @param clientInfo Will allow creating the client if the local client cannot be found (else will fail if local client is not found)
* @param entropyData Additional entropy data
* @returns The local existing client or newly created client
*/
public async initClient(
loginData: LoginData,
clientInfo?: ClientInfo,
entropyData?: Uint8Array,
): Promise<{isNewClient: boolean; localClient: RegisteredClient}> {
if (!this.service) {
throw new Error('Services are not set.');
}
try {
const localClient = await this.loadAndValidateLocalClient(entropyData);
return {isNewClient: false, localClient};
} catch (error) {
if (!clientInfo) {
// If no client info provided, the client should not be created
throw error;
}
// There was no client so we need to "create" and "register" a client
const notFoundInDatabase =
error instanceof cryptobox.error.CryptoboxError ||
(error as Error).constructor.name === 'CryptoboxError' ||
error instanceof StoreEngineError.RecordNotFoundError ||
(error as Error).constructor.name === StoreEngineError.RecordNotFoundError.name;
const notFoundOnBackend = axios.isAxiosError(error) ? error.response?.status === HTTP_STATUS.NOT_FOUND : false;
if (notFoundInDatabase) {
this.logger.log(`Could not find valid client in database "${this.storeEngine?.storeName}".`);
return this.registerClient(loginData, clientInfo, entropyData);
}
if (notFoundOnBackend) {
this.logger.log('Could not find valid client on backend');
const client = await this.service!.client.getLocalClient();
const shouldDeleteWholeDatabase = client.type === ClientType.TEMPORARY;
if (shouldDeleteWholeDatabase) {
this.logger.log('Last client was temporary - Deleting database');
if (this.storeEngine) {
await this.storeEngine.clearTables();
}
const context = await this.apiClient.init(loginData.clientType);
await this.initEngine(context);
return this.registerClient(loginData, clientInfo, entropyData);
}
this.logger.log('Last client was permanent - Deleting cryptography stores');
await this.service!.cryptography.deleteCryptographyStores();
return this.registerClient(loginData, clientInfo, entropyData);
}
throw error;
}
}
/**
* In order to be able to send MLS messages, the core needs a few information from the consumer.
* Namely:
* - is the current user allowed to administrate a specific conversation
* - what is the groupId of a conversation
* @param mlsCallbacks
*/
configureMLSCallbacks(mlsCallbacks: MLSCallbacks) {
this.service?.mls.configureMLSCallbacks(mlsCallbacks);
}
public async initServices(context: Context): Promise<void> {
this.storeEngine = await this.initEngine(context);
const accountService = new AccountService(this.apiClient);
const assetService = new AssetService(this.apiClient);
const cryptographyService = new CryptographyService(this.apiClient, this.storeEngine, {
// We want to encrypt with fully qualified session ids, only if the backend is federated with other backends
useQualifiedIds: this.backendFeatures.isFederated,
nbPrekeys: this.nbPrekeys,
});
const clientService = new ClientService(this.apiClient, this.storeEngine, cryptographyService);
const mlsService = new MLSService(this.apiClient, () => this.coreCryptoClient, {
...this.cryptoProtocolConfig?.mls,
nbKeyPackages: this.nbPrekeys,
});
const proteusService = new ProteusService(this.apiClient, cryptographyService, {
// We can use qualified ids to send messages as long as the backend supports federated endpoints
useQualifiedIds: this.backendFeatures.federationEndpoints,
});
const connectionService = new ConnectionService(this.apiClient);
const giphyService = new GiphyService(this.apiClient);
const linkPreviewService = new LinkPreviewService(assetService);
const notificationService = new NotificationService(this.apiClient, mlsService, proteusService, this.storeEngine);
const conversationService = new ConversationService(
this.apiClient,
cryptographyService,
{
// We can use qualified ids to send messages as long as the backend supports federated endpoints
useQualifiedIds: this.backendFeatures.federationEndpoints,
},
mlsService,
proteusService,
);
const selfService = new SelfService(this.apiClient);
const teamService = new TeamService(this.apiClient);
const broadcastService = new BroadcastService(this.apiClient, cryptographyService);
const userService = new UserService(this.apiClient, broadcastService, connectionService);
this.service = {
mls: mlsService,
account: accountService,
asset: assetService,
broadcast: broadcastService,
client: clientService,
connection: connectionService,
conversation: conversationService,
cryptography: cryptographyService,
giphy: giphyService,
linkPreview: linkPreviewService,
notification: notificationService,
self: selfService,
team: teamService,
user: userService,
};
}
public async loadAndValidateLocalClient(entropyData?: Uint8Array): Promise<RegisteredClient> {
const loadedClient = await this.service!.client.getLocalClient();
await this.apiClient.api.client.getClient(loadedClient.id);
this.apiClient.context!.clientId = loadedClient.id;
await this.service!.cryptography.initCryptobox();
if (this.cryptoProtocolConfig?.mls && this.backendFeatures.supportsMLS) {
this.coreCryptoClient = await this.createMLSClient(
loadedClient,
this.apiClient.context!,
this.cryptoProtocolConfig,
entropyData,
);
}
return loadedClient;
}
private async createMLSClient(
client: RegisteredClient,
context: Context,
cryptoProtocolConfig: CryptoProtocolConfig,
entropyData?: Uint8Array,
) {
if (!this.service) {
throw new Error('Services are not set.');
}
const coreCryptoKeyId = 'corecrypto-key';
const dbName = this.generateSecretsDbName(context);
const secretStore = cryptoProtocolConfig.systemCrypto
? await createCustomEncryptedStore(dbName, cryptoProtocolConfig.systemCrypto)
: await createEncryptedStore(dbName);
let key = await secretStore.getsecretValue(coreCryptoKeyId);
let isNewMLSDevice = false;
if (!key) {
key = window.crypto.getRandomValues(new Uint8Array(16));
await secretStore.saveSecretValue(coreCryptoKeyId, key);
// Keeping track that this device is a new MLS device (but can be an old proteus device)
isNewMLSDevice = true;
}
const {userId, domain} = this.apiClient.context!;
const mlsClient = await CoreCrypto.init({
databaseName: `corecrypto-${this.generateDbName(context)}`,
key: Encoder.toBase64(key).asString,
clientId: `${userId}:${client.id}@${domain}`,
wasmFilePath: cryptoProtocolConfig.coreCrypoWasmFilePath,
entropySeed: entropyData,
});
if (isNewMLSDevice) {
// If the device is new, we need to upload keypackages and public key to the backend
await this.service.mls.uploadMLSPublicKeys(await mlsClient.clientPublicKey(), client.id);
await this.service.mls.uploadMLSKeyPackages(await mlsClient.clientKeypackages(this.nbPrekeys), client.id);
}
return mlsClient;
}
private async registerClient(
loginData: LoginData,
clientInfo: ClientInfo = coreDefaultClient,
entropyData?: Uint8Array,
): Promise<{isNewClient: boolean; localClient: RegisteredClient}> {
if (!this.service) {
throw new Error('Services are not set.');
}
this.logger.info(`Creating new client {mls: ${!!this.cryptoProtocolConfig}}`);
const registeredClient = await this.service.client.register(loginData, clientInfo, entropyData);
if (this.cryptoProtocolConfig && this.backendFeatures.supportsMLS) {
this.coreCryptoClient = await this.createMLSClient(
registeredClient,
this.apiClient.context!,
this.cryptoProtocolConfig,
entropyData,
);
}
this.apiClient.context!.clientId = registeredClient.id;
this.logger.info('Client is created');
await this.service!.notification.initializeNotificationStream();
await this.service!.client.synchronizeClients();
await this.service!.cryptography.initCryptobox();
return {isNewClient: true, localClient: registeredClient};
}
private resetContext(): void {
delete this.apiClient.context;
delete this.service;
}
/**
* Will logout the current user
* @param clearData if set to `true` will completely wipe any database that was created by the Account
*/
public async logout(clearData: boolean = false): Promise<void> {
if (clearData && this.coreCryptoClient) {
await this.coreCryptoClient.wipe();
await deleteEncryptedStore(this.generateSecretsDbName(this.apiClient.context!));
}
await this.apiClient.logout();
this.resetContext();
}
/**
* Will download and handle the notification stream since last stored notification id.
* Once the notification stream has been handled from backend, will then connect to the websocket and start listening to incoming events
*
* @param callbacks callbacks that will be called to handle different events
* @returns close a function that will disconnect from the websocket
*/
public listen({
onEvent = () => {},
onConnectionStateChanged = () => {},
onNotificationStreamProgress = () => {},
onMissedNotifications = () => {},
dryRun = false,
}: {
/**
* Called when a new event arrives from backend
* @param payload the payload of the event. Contains the raw event received and the decrypted data (if event was encrypted)
* @param source where the message comes from (either websocket or notification stream)
*/
onEvent?: (payload: HandledEventPayload, source: NotificationSource) => void;
/**
* During the notification stream processing, this function will be called whenever a new notification has been processed
*/
onNotificationStreamProgress?: ({done, total}: {done: number; total: number}) => void;
/**
* called when the connection state with the backend has changed
*/
onConnectionStateChanged?: (state: ConnectionState) => void;
/**
* called when we detect lost notification from backend.
* When a client doesn't log in for a while (28 days, as of now) notifications that are older than 28 days will be deleted from backend.
* If the client query the backend for the notifications since a particular notification ID and this ID doesn't exist anymore on the backend, we deduce that some messages were not sync before they were removed from backend.
* We can then detect that something was wrong and warn the consumer that there might be some missing old messages
* @param {string} notificationId
*/
onMissedNotifications?: (notificationId: string) => void;
/**
* When set will not decrypt and not store the last notification ID. This is useful if you only want to subscribe to unencrypted backend events
*/
dryRun?: boolean;
} = {}): () => void {
if (!this.apiClient.context) {
throw new Error('Context is not set - please login first');
}
const handleEvent = async (payload: HandledEventPayload, source: NotificationSource) => {
const {event} = payload;
switch (event?.type) {
case CONVERSATION_EVENT.MESSAGE_TIMER_UPDATE: {
const {
data: {message_timer},
conversation,
} = event as Events.ConversationMessageTimerUpdateEvent;
const expireAfterMillis = Number(message_timer);
this.service!.conversation.messageTimer.setConversationLevelTimer(conversation, expireAfterMillis);
break;
}
}
onEvent(payload, source);
};
const handleNotification = async (notification: Notification, source: NotificationSource): Promise<void> => {
try {
const messages = this.service!.notification.handleNotification(notification, source, dryRun);
for await (const message of messages) {
await handleEvent(message, source);
}
} catch (error) {
this.logger.error(`Failed to handle notification ID "${notification.id}": ${(error as any).message}`, error);
}
};
this.apiClient.transport.ws.removeAllListeners(WebSocketClient.TOPIC.ON_MESSAGE);
this.apiClient.transport.ws.on(WebSocketClient.TOPIC.ON_MESSAGE, notification =>
handleNotification(notification, NotificationSource.WEBSOCKET),
);
this.apiClient.transport.ws.on(WebSocketClient.TOPIC.ON_STATE_CHANGE, wsState => {
const mapping: Partial<Record<WEBSOCKET_STATE, ConnectionState>> = {
[WEBSOCKET_STATE.CLOSED]: ConnectionState.CLOSED,
[WEBSOCKET_STATE.CONNECTING]: ConnectionState.CONNECTING,
};
const connectionState = mapping[wsState];
if (connectionState) {
onConnectionStateChanged(connectionState);
}
});
const handleMissedNotifications = async (notificationId: string) => {
if (this.cryptoProtocolConfig?.mls && this.backendFeatures.supportsMLS) {
await this.service?.conversation.handleEpochMismatch();
}
return onMissedNotifications(notificationId);
};
const processNotificationStream = async (abortHandler: AbortHandler) => {
// Lock websocket in order to buffer any message that arrives while we handle the notification stream
this.apiClient.transport.ws.lock();
pauseMessageSending();
onConnectionStateChanged(ConnectionState.PROCESSING_NOTIFICATIONS);
const results = await this.service!.notification.processNotificationStream(
async (notification, source, progress) => {
await handleNotification(notification, source);
onNotificationStreamProgress(progress);
},
handleMissedNotifications,
abortHandler,
);
this.logger.log(`Finished processing notifications ${JSON.stringify(results)}`, results);
if (abortHandler.isAborted()) {
this.logger.warn('Ending connection process as websocket was closed');
return;
}
onConnectionStateChanged(ConnectionState.LIVE);
// We can now unlock the websocket and let the new messages being handled and decrypted
this.apiClient.transport.ws.unlock();
// We need to wait for the notification stream to be fully handled before releasing the message sending queue.
// This is due to the nature of how message are encrypted, any change in mls epoch needs to happen before we start encrypting any kind of messages
this.logger.info(`Resuming message sending. ${getQueueLength()} messages to be sent`);
resumeMessageSending();
};
this.apiClient.connect(processNotificationStream);
return () => {
this.apiClient.disconnect();
onConnectionStateChanged(ConnectionState.CLOSED);
this.apiClient.transport.ws.removeAllListeners();
};
}
private generateDbName(context: Context) {
const clientType = context.clientType === ClientType.NONE ? '' : `@${context.clientType}`;
return `wire@${this.apiClient.config.urls.name}@${context.userId}${clientType}`;
}
private generateSecretsDbName(context: Context) {
return `secrets-${this.generateDbName(context)}`;
}
private async initEngine(context: Context): Promise<CRUDEngine> {
const dbName = this.generateDbName(context);
this.logger.log(`Initialising store with name "${dbName}"...`);
const openDb = async () => {
const initializedDb = await this.createStore(dbName, context);
if (initializedDb) {
this.logger.log(`Initialized store with existing engine "${dbName}".`);
return initializedDb;
}
this.logger.log(`Initialized store with new memory engine "${dbName}".`);
const memoryEngine = new MemoryEngine();
await memoryEngine.init(dbName);
return memoryEngine;
};
const storeEngine = await openDb();
const cookie = CookieStore.getCookie();
if (cookie) {
await this.persistCookie(storeEngine, cookie);
}
return storeEngine;
}
}