@xmtp/browser-sdk 0.0.21 → 0.0.22

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.
@@ -1,4 +1,10 @@
1
- import type { Conversation, Message, StreamCloser } from "@xmtp/wasm-bindings";
1
+ import type {
2
+ Consent,
3
+ Conversation,
4
+ Message,
5
+ StreamCloser,
6
+ UserPreference,
7
+ } from "@xmtp/wasm-bindings";
2
8
  import type {
3
9
  ClientEventsActions,
4
10
  ClientEventsClientMessageData,
@@ -13,6 +19,7 @@ import type {
13
19
  import {
14
20
  fromEncodedContent,
15
21
  fromSafeEncodedContent,
22
+ toSafeConsent,
16
23
  toSafeConversation,
17
24
  toSafeHmacKey,
18
25
  toSafeInboxState,
@@ -22,7 +29,7 @@ import {
22
29
  import { WorkerClient } from "@/WorkerClient";
23
30
  import { WorkerConversation } from "@/WorkerConversation";
24
31
 
25
- let client: WorkerClient;
32
+ let maybeClient: WorkerClient | undefined;
26
33
  let enableLogging = false;
27
34
 
28
35
  const streamClosers = new Map<string, StreamCloser>();
@@ -66,18 +73,47 @@ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
66
73
  console.log("client worker received event data", event.data);
67
74
  }
68
75
 
69
- // a client is required for all actions except init
70
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
71
- if (action !== "init" && !client) {
72
- postMessageError({
73
- id,
74
- action,
75
- error: "Client not initialized",
76
- });
77
- return;
78
- }
79
-
80
76
  try {
77
+ // init is a special action that initializes the client
78
+ if (action === "init" && !maybeClient) {
79
+ maybeClient = await WorkerClient.create(
80
+ data.address,
81
+ data.encryptionKey,
82
+ data.options,
83
+ );
84
+ enableLogging =
85
+ data.options?.loggingLevel !== undefined &&
86
+ data.options.loggingLevel !== "off";
87
+ postMessage({
88
+ id,
89
+ action,
90
+ result: {
91
+ inboxId: maybeClient.inboxId,
92
+ installationId: maybeClient.installationId,
93
+ installationIdBytes: maybeClient.installationIdBytes,
94
+ },
95
+ });
96
+ return;
97
+ }
98
+
99
+ // a client is required for all other actions
100
+ if (!maybeClient) {
101
+ throw new Error("Client not initialized");
102
+ }
103
+
104
+ // let typescript know that a client will be available for the rest
105
+ // of this code block
106
+ const client = maybeClient;
107
+
108
+ // helper function that throws an error if the group is not found
109
+ const getGroup = (groupId: string) => {
110
+ const group = client.conversations.getConversationById(groupId);
111
+ if (!group) {
112
+ throw new Error(`Group "${groupId}" not found`);
113
+ }
114
+ return group;
115
+ };
116
+
81
117
  switch (action) {
82
118
  /**
83
119
  * Stream actions
@@ -87,100 +123,49 @@ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
87
123
  if (streamCloser) {
88
124
  streamCloser.end();
89
125
  streamClosers.delete(data.streamId);
90
- postMessage({
91
- id,
92
- action,
93
- result: undefined,
94
- });
126
+ postMessage({ id, action, result: undefined });
95
127
  } else {
96
- postMessageError({
97
- id,
98
- action,
99
- error: `Stream "${data.streamId}" not found`,
100
- });
128
+ throw new Error(`Stream "${data.streamId}" not found`);
101
129
  }
102
130
  break;
103
131
  }
104
132
  /**
105
133
  * Client actions
106
134
  */
107
- case "init":
108
- client = await WorkerClient.create(
109
- data.address,
110
- data.encryptionKey,
111
- data.options,
112
- );
113
- enableLogging =
114
- data.options?.loggingLevel !== undefined &&
115
- data.options.loggingLevel !== "off";
116
- postMessage({
117
- id,
118
- action,
119
- result: {
120
- inboxId: client.inboxId,
121
- installationId: client.installationId,
122
- installationIdBytes: client.installationIdBytes,
123
- },
124
- });
125
- break;
126
135
  case "createInboxSignatureText": {
127
136
  const result = client.createInboxSignatureText();
128
- postMessage({
129
- id,
130
- action,
131
- result,
132
- });
137
+ postMessage({ id, action, result });
133
138
  break;
134
139
  }
135
140
  case "addAccountSignatureText": {
136
141
  const result = await client.addAccountSignatureText(
137
142
  data.newAccountAddress,
138
143
  );
139
- postMessage({
140
- id,
141
- action,
142
- result,
143
- });
144
+ postMessage({ id, action, result });
144
145
  break;
145
146
  }
146
147
  case "removeAccountSignatureText": {
147
148
  const result = await client.removeAccountSignatureText(
148
149
  data.accountAddress,
149
150
  );
150
- postMessage({
151
- id,
152
- action,
153
- result,
154
- });
151
+ postMessage({ id, action, result });
155
152
  break;
156
153
  }
157
154
  case "revokeAllOtherInstallationsSignatureText": {
158
155
  const result = await client.revokeAllAOtherInstallationsSignatureText();
159
- postMessage({
160
- id,
161
- action,
162
- result,
163
- });
156
+ postMessage({ id, action, result });
164
157
  break;
165
158
  }
166
159
  case "revokeInstallationsSignatureText": {
167
160
  const result = await client.revokeInstallationsSignatureText(
168
161
  data.installationIds,
169
162
  );
170
- postMessage({
171
- id,
172
- action,
173
- result,
174
- });
163
+ postMessage({ id, action, result });
175
164
  break;
176
165
  }
177
166
  case "addSignature":
178
167
  await client.addSignature(data.type, data.bytes);
179
- postMessage({
180
- id,
181
- action,
182
- result: undefined,
183
- });
168
+ postMessage({ id, action, result: undefined });
184
169
  break;
185
170
  case "addScwSignature":
186
171
  await client.addScwSignature(
@@ -189,71 +174,41 @@ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
189
174
  data.chainId,
190
175
  data.blockNumber,
191
176
  );
192
- postMessage({
193
- id,
194
- action,
195
- result: undefined,
196
- });
177
+ postMessage({ id, action, result: undefined });
197
178
  break;
198
179
  case "applySignatures":
199
180
  await client.applySignatures();
200
- postMessage({
201
- id,
202
- action,
203
- result: undefined,
204
- });
181
+ postMessage({ id, action, result: undefined });
205
182
  break;
206
183
  case "registerIdentity":
207
184
  await client.registerIdentity();
208
- postMessage({
209
- id,
210
- action,
211
- result: undefined,
212
- });
185
+ postMessage({ id, action, result: undefined });
213
186
  break;
214
187
  case "isRegistered": {
215
188
  const result = client.isRegistered;
216
- postMessage({
217
- id,
218
- action,
219
- result,
220
- });
189
+ postMessage({ id, action, result });
221
190
  break;
222
191
  }
223
192
  case "canMessage": {
224
193
  const result = await client.canMessage(data.accountAddresses);
225
- postMessage({
226
- id,
227
- action,
228
- result,
229
- });
194
+ postMessage({ id, action, result });
230
195
  break;
231
196
  }
232
197
  case "inboxState": {
233
- const result = await client.inboxState(data.refreshFromNetwork);
234
- postMessage({
235
- id,
236
- action,
237
- result: toSafeInboxState(result),
238
- });
198
+ const inboxState = await client.inboxState(data.refreshFromNetwork);
199
+ const result = toSafeInboxState(inboxState);
200
+ postMessage({ id, action, result });
239
201
  break;
240
202
  }
241
203
  case "getLatestInboxState": {
242
- const result = await client.getLatestInboxState(data.inboxId);
243
- postMessage({
244
- id,
245
- action,
246
- result: toSafeInboxState(result),
247
- });
204
+ const inboxState = await client.getLatestInboxState(data.inboxId);
205
+ const result = toSafeInboxState(inboxState);
206
+ postMessage({ id, action, result });
248
207
  break;
249
208
  }
250
209
  case "setConsentStates": {
251
210
  await client.setConsentStates(data.records);
252
- postMessage({
253
- id,
254
- action,
255
- result: undefined,
256
- });
211
+ postMessage({ id, action, result: undefined });
257
212
  break;
258
213
  }
259
214
  case "getConsentState": {
@@ -261,29 +216,17 @@ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
261
216
  data.entityType,
262
217
  data.entity,
263
218
  );
264
- postMessage({
265
- id,
266
- action,
267
- result,
268
- });
219
+ postMessage({ id, action, result });
269
220
  break;
270
221
  }
271
222
  case "findInboxIdByAddress": {
272
223
  const result = await client.findInboxIdByAddress(data.address);
273
- postMessage({
274
- id,
275
- action,
276
- result,
277
- });
224
+ postMessage({ id, action, result });
278
225
  break;
279
226
  }
280
227
  case "signWithInstallationKey": {
281
228
  const result = client.signWithInstallationKey(data.signatureText);
282
- postMessage({
283
- id,
284
- action,
285
- result,
286
- });
229
+ postMessage({ id, action, result });
287
230
  break;
288
231
  }
289
232
  case "verifySignedWithInstallationKey": {
@@ -291,11 +234,7 @@ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
291
234
  data.signatureText,
292
235
  data.signatureBytes,
293
236
  );
294
- postMessage({
295
- id,
296
- action,
297
- result,
298
- });
237
+ postMessage({ id, action, result });
299
238
  break;
300
239
  }
301
240
  case "verifySignedWithPublicKey": {
@@ -304,11 +243,7 @@ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
304
243
  data.signatureBytes,
305
244
  data.publicKey,
306
245
  );
307
- postMessage({
308
- id,
309
- action,
310
- result,
311
- });
246
+ postMessage({ id, action, result });
312
247
  break;
313
248
  }
314
249
  /**
@@ -342,11 +277,7 @@ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
342
277
  data.conversationType,
343
278
  );
344
279
  streamClosers.set(data.streamId, streamCloser);
345
- postMessage({
346
- id,
347
- action,
348
- result: undefined,
349
- });
280
+ postMessage({ id, action, result: undefined });
350
281
  break;
351
282
  }
352
283
  case "streamAllMessages": {
@@ -373,6 +304,30 @@ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
373
304
  data.conversationType,
374
305
  );
375
306
  streamClosers.set(data.streamId, streamCloser);
307
+ postMessage({ id, action, result: undefined });
308
+ break;
309
+ }
310
+ case "streamConsent": {
311
+ const streamCallback = (
312
+ error: Error | null,
313
+ value: Consent[] | undefined,
314
+ ) => {
315
+ if (error) {
316
+ postStreamMessageError({
317
+ type: "consent",
318
+ streamId: data.streamId,
319
+ error: error.message,
320
+ });
321
+ } else {
322
+ postStreamMessage({
323
+ type: "consent",
324
+ streamId: data.streamId,
325
+ result: value?.map(toSafeConsent) ?? [],
326
+ });
327
+ }
328
+ };
329
+ const streamCloser = client.conversations.streamConsent(streamCallback);
330
+ streamClosers.set(data.streamId, streamCloser);
376
331
  postMessage({
377
332
  id,
378
333
  action,
@@ -380,43 +335,57 @@ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
380
335
  });
381
336
  break;
382
337
  }
383
- case "getConversations": {
384
- const conversations = client.conversations.list(data.options);
338
+ case "streamPreferences": {
339
+ const streamCallback = (
340
+ error: Error | null,
341
+ value: UserPreference[] | undefined,
342
+ ) => {
343
+ if (error) {
344
+ postStreamMessageError({
345
+ type: "preferences",
346
+ streamId: data.streamId,
347
+ error: error.message,
348
+ });
349
+ } else {
350
+ postStreamMessage({
351
+ type: "preferences",
352
+ streamId: data.streamId,
353
+ result: value ?? undefined,
354
+ });
355
+ }
356
+ };
357
+ const streamCloser =
358
+ client.conversations.streamPreferences(streamCallback);
359
+ streamClosers.set(data.streamId, streamCloser);
385
360
  postMessage({
386
361
  id,
387
362
  action,
388
- result: await Promise.all(
389
- conversations.map((conversation) =>
390
- toSafeConversation(conversation),
391
- ),
392
- ),
363
+ result: undefined,
393
364
  });
394
365
  break;
395
366
  }
367
+ case "getConversations": {
368
+ const conversations = client.conversations.list(data.options);
369
+ const result = await Promise.all(
370
+ conversations.map((conversation) => toSafeConversation(conversation)),
371
+ );
372
+ postMessage({ id, action, result });
373
+ break;
374
+ }
396
375
  case "getGroups": {
397
376
  const conversations = client.conversations.listGroups(data.options);
398
- postMessage({
399
- id,
400
- action,
401
- result: await Promise.all(
402
- conversations.map((conversation) =>
403
- toSafeConversation(conversation),
404
- ),
405
- ),
406
- });
377
+ const result = await Promise.all(
378
+ conversations.map((conversation) => toSafeConversation(conversation)),
379
+ );
380
+ postMessage({ id, action, result });
407
381
  break;
408
382
  }
409
383
  case "getDms": {
410
384
  const conversations = client.conversations.listDms(data.options);
411
- postMessage({
412
- id,
413
- action,
414
- result: await Promise.all(
415
- conversations.map((conversation) =>
416
- toSafeConversation(conversation),
417
- ),
418
- ),
419
- });
385
+ const result = await Promise.all(
386
+ conversations.map((conversation) => toSafeConversation(conversation)),
387
+ );
388
+ postMessage({ id, action, result });
420
389
  break;
421
390
  }
422
391
  case "newGroup": {
@@ -424,11 +393,8 @@ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
424
393
  data.accountAddresses,
425
394
  data.options,
426
395
  );
427
- postMessage({
428
- id,
429
- action,
430
- result: await toSafeConversation(conversation),
431
- });
396
+ const result = await toSafeConversation(conversation);
397
+ postMessage({ id, action, result });
432
398
  break;
433
399
  }
434
400
  case "newGroupByInboxIds": {
@@ -436,11 +402,8 @@ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
436
402
  data.inboxIds,
437
403
  data.options,
438
404
  );
439
- postMessage({
440
- id,
441
- action,
442
- result: await toSafeConversation(conversation),
443
- });
405
+ const result = await toSafeConversation(conversation);
406
+ postMessage({ id, action, result });
444
407
  break;
445
408
  }
446
409
  case "newDm": {
@@ -448,11 +411,8 @@ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
448
411
  data.accountAddress,
449
412
  data.options,
450
413
  );
451
- postMessage({
452
- id,
453
- action,
454
- result: await toSafeConversation(conversation),
455
- });
414
+ const result = await toSafeConversation(conversation);
415
+ postMessage({ id, action, result });
456
416
  break;
457
417
  }
458
418
  case "newDmByInboxId": {
@@ -460,11 +420,8 @@ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
460
420
  data.inboxId,
461
421
  data.options,
462
422
  );
463
- postMessage({
464
- id,
465
- action,
466
- result: await toSafeConversation(conversation),
467
- });
423
+ const result = await toSafeConversation(conversation);
424
+ postMessage({ id, action, result });
468
425
  break;
469
426
  }
470
427
  case "syncConversations": {
@@ -487,634 +444,257 @@ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
487
444
  }
488
445
  case "getConversationById": {
489
446
  const conversation = client.conversations.getConversationById(data.id);
490
- postMessage({
491
- id,
492
- action,
493
- result: conversation
494
- ? await toSafeConversation(conversation)
495
- : undefined,
496
- });
447
+ const result = conversation
448
+ ? await toSafeConversation(conversation)
449
+ : undefined;
450
+ postMessage({ id, action, result });
497
451
  break;
498
452
  }
499
453
  case "getMessageById": {
500
454
  const message = client.conversations.getMessageById(data.id);
501
- postMessage({
502
- id,
503
- action,
504
- result: message ? toSafeMessage(message) : undefined,
505
- });
455
+ const result = message ? toSafeMessage(message) : undefined;
456
+ postMessage({ id, action, result });
506
457
  break;
507
458
  }
508
459
  case "getDmByInboxId": {
509
460
  const conversation = client.conversations.getDmByInboxId(data.inboxId);
510
- postMessage({
511
- id,
512
- action,
513
- result: conversation
514
- ? await toSafeConversation(conversation)
515
- : undefined,
516
- });
461
+ const result = conversation
462
+ ? await toSafeConversation(conversation)
463
+ : undefined;
464
+ postMessage({ id, action, result });
517
465
  break;
518
466
  }
519
467
  case "getHmacKeys": {
520
- const result = client.conversations.getHmacKeys();
521
- postMessage({
522
- id,
523
- action,
524
- result: Object.fromEntries(
525
- Array.from(result.entries()).map(([groupId, hmacKeys]) => [
526
- groupId,
527
- hmacKeys.map(toSafeHmacKey),
528
- ]),
529
- ),
530
- });
468
+ const hmacKeys = client.conversations.getHmacKeys();
469
+ const result = Object.fromEntries(
470
+ Array.from(hmacKeys.entries()).map(([groupId, hmacKeys]) => [
471
+ groupId,
472
+ hmacKeys.map(toSafeHmacKey),
473
+ ]),
474
+ );
475
+ postMessage({ id, action, result });
531
476
  break;
532
477
  }
533
478
  /**
534
479
  * Group actions
535
480
  */
536
481
  case "syncGroup": {
537
- const group = client.conversations.getConversationById(data.id);
538
- if (group) {
539
- await group.sync();
540
- postMessage({
541
- id,
542
- action,
543
- result: await toSafeConversation(group),
544
- });
545
- } else {
546
- postMessageError({
547
- id,
548
- action,
549
- error: "Group not found",
550
- });
551
- }
482
+ const group = getGroup(data.id);
483
+ await group.sync();
484
+ const result = await toSafeConversation(group);
485
+ postMessage({ id, action, result });
552
486
  break;
553
487
  }
554
488
  case "updateGroupName": {
555
- const group = client.conversations.getConversationById(data.id);
556
- if (group) {
557
- await group.updateName(data.name);
558
- postMessage({
559
- id,
560
- action,
561
- result: undefined,
562
- });
563
- } else {
564
- postMessageError({
565
- id,
566
- action,
567
- error: "Group not found",
568
- });
569
- }
489
+ const group = getGroup(data.id);
490
+ await group.updateName(data.name);
491
+ postMessage({ id, action, result: undefined });
570
492
  break;
571
493
  }
572
494
  case "updateGroupDescription": {
573
- const group = client.conversations.getConversationById(data.id);
574
- if (group) {
575
- await group.updateDescription(data.description);
576
- postMessage({
577
- id,
578
- action,
579
- result: undefined,
580
- });
581
- } else {
582
- postMessageError({
583
- id,
584
- action,
585
- error: "Group not found",
586
- });
587
- }
495
+ const group = getGroup(data.id);
496
+ await group.updateDescription(data.description);
497
+ postMessage({ id, action, result: undefined });
588
498
  break;
589
499
  }
590
500
  case "updateGroupImageUrlSquare": {
591
- const group = client.conversations.getConversationById(data.id);
592
- if (group) {
593
- await group.updateImageUrl(data.imageUrl);
594
- postMessage({
595
- id,
596
- action,
597
- result: undefined,
598
- });
599
- } else {
600
- postMessageError({
601
- id,
602
- action,
603
- error: "Group not found",
604
- });
605
- }
501
+ const group = getGroup(data.id);
502
+ await group.updateImageUrl(data.imageUrl);
503
+ postMessage({ id, action, result: undefined });
606
504
  break;
607
505
  }
608
506
  case "sendGroupMessage": {
609
- const group = client.conversations.getConversationById(data.id);
610
- if (group) {
611
- const result = await group.send(
612
- fromEncodedContent(fromSafeEncodedContent(data.content)),
613
- );
614
- postMessage({
615
- id,
616
- action,
617
- result,
618
- });
619
- } else {
620
- postMessageError({
621
- id,
622
- action,
623
- error: "Group not found",
624
- });
625
- }
507
+ const group = getGroup(data.id);
508
+ const result = await group.send(
509
+ fromEncodedContent(fromSafeEncodedContent(data.content)),
510
+ );
511
+ postMessage({ id, action, result });
626
512
  break;
627
513
  }
628
514
  case "sendOptimisticGroupMessage": {
629
- const group = client.conversations.getConversationById(data.id);
630
- if (group) {
631
- const result = group.sendOptimistic(
632
- fromEncodedContent(fromSafeEncodedContent(data.content)),
633
- );
634
- postMessage({
635
- id,
636
- action,
637
- result,
638
- });
639
- } else {
640
- postMessageError({
641
- id,
642
- action,
643
- error: "Group not found",
644
- });
645
- }
515
+ const group = getGroup(data.id);
516
+ const result = group.sendOptimistic(
517
+ fromEncodedContent(fromSafeEncodedContent(data.content)),
518
+ );
519
+ postMessage({ id, action, result });
646
520
  break;
647
521
  }
648
522
  case "publishGroupMessages": {
649
- const group = client.conversations.getConversationById(data.id);
650
- if (group) {
651
- await group.publishMessages();
652
- postMessage({
653
- id,
654
- action,
655
- result: undefined,
656
- });
657
- } else {
658
- postMessageError({
659
- id,
660
- action,
661
- error: "Group not found",
662
- });
663
- }
523
+ const group = getGroup(data.id);
524
+ await group.publishMessages();
525
+ postMessage({ id, action, result: undefined });
664
526
  break;
665
527
  }
666
528
  case "getGroupMessages": {
667
- const group = client.conversations.getConversationById(data.id);
668
- if (group) {
669
- const messages = await group.messages(data.options);
670
- postMessage({
671
- id,
672
- action,
673
- result: messages.map((message) => toSafeMessage(message)),
674
- });
675
- } else {
676
- postMessageError({
677
- id,
678
- action,
679
- error: "Group not found",
680
- });
681
- }
529
+ const group = getGroup(data.id);
530
+ const messages = await group.messages(data.options);
531
+ const result = messages.map((message) => toSafeMessage(message));
532
+ postMessage({ id, action, result });
682
533
  break;
683
534
  }
684
535
  case "getGroupMembers": {
685
- const group = client.conversations.getConversationById(data.id);
686
- if (group) {
687
- const result = await group.members();
688
- postMessage({
689
- id,
690
- action,
691
- result,
692
- });
693
- } else {
694
- postMessageError({
695
- id,
696
- action,
697
- error: "Group not found",
698
- });
699
- }
536
+ const group = getGroup(data.id);
537
+ const result = await group.members();
538
+ postMessage({ id, action, result });
700
539
  break;
701
540
  }
702
541
  case "getGroupAdmins": {
703
- const group = client.conversations.getConversationById(data.id);
704
- if (group) {
705
- postMessage({
706
- id,
707
- action,
708
- result: group.admins,
709
- });
710
- } else {
711
- postMessageError({
712
- id,
713
- action,
714
- error: "Group not found",
715
- });
716
- }
542
+ const group = getGroup(data.id);
543
+ const result = group.admins;
544
+ postMessage({ id, action, result });
717
545
  break;
718
546
  }
719
547
  case "getGroupSuperAdmins": {
720
- const group = client.conversations.getConversationById(data.id);
721
- if (group) {
722
- postMessage({
723
- id,
724
- action,
725
- result: group.superAdmins,
726
- });
727
- } else {
728
- postMessageError({
729
- id,
730
- action,
731
- error: "Group not found",
732
- });
733
- }
548
+ const group = getGroup(data.id);
549
+ const result = group.superAdmins;
550
+ postMessage({ id, action, result });
734
551
  break;
735
552
  }
736
553
  case "getGroupConsentState": {
737
- const group = client.conversations.getConversationById(data.id);
738
- if (group) {
739
- postMessage({
740
- id,
741
- action,
742
- result: group.consentState,
743
- });
744
- } else {
745
- postMessageError({
746
- id,
747
- action,
748
- error: "Group not found",
749
- });
750
- }
554
+ const group = getGroup(data.id);
555
+ const result = group.consentState;
556
+ postMessage({ id, action, result });
751
557
  break;
752
558
  }
753
559
  case "updateGroupConsentState": {
754
- const group = client.conversations.getConversationById(data.id);
755
- if (group) {
756
- group.updateConsentState(data.state);
757
- postMessage({
758
- id,
759
- action,
760
- result: undefined,
761
- });
762
- } else {
763
- postMessageError({
764
- id,
765
- action,
766
- error: "Group not found",
767
- });
768
- }
560
+ const group = getGroup(data.id);
561
+ group.updateConsentState(data.state);
562
+ postMessage({ id, action, result: undefined });
769
563
  break;
770
564
  }
771
565
  case "addGroupAdmin": {
772
- const group = client.conversations.getConversationById(data.id);
773
- if (group) {
774
- await group.addAdmin(data.inboxId);
775
- postMessage({
776
- id,
777
- action,
778
- result: undefined,
779
- });
780
- } else {
781
- postMessageError({
782
- id,
783
- action,
784
- error: "Group not found",
785
- });
786
- }
566
+ const group = getGroup(data.id);
567
+ await group.addAdmin(data.inboxId);
568
+ postMessage({ id, action, result: undefined });
787
569
  break;
788
570
  }
789
571
  case "removeGroupAdmin": {
790
- const group = client.conversations.getConversationById(data.id);
791
- if (group) {
792
- await group.removeAdmin(data.inboxId);
793
- postMessage({
794
- id,
795
- action,
796
- result: undefined,
797
- });
798
- } else {
799
- postMessageError({
800
- id,
801
- action,
802
- error: "Group not found",
803
- });
804
- }
572
+ const group = getGroup(data.id);
573
+ await group.removeAdmin(data.inboxId);
574
+ postMessage({ id, action, result: undefined });
805
575
  break;
806
576
  }
807
577
  case "addGroupSuperAdmin": {
808
- const group = client.conversations.getConversationById(data.id);
809
- if (group) {
810
- await group.addSuperAdmin(data.inboxId);
811
- postMessage({
812
- id,
813
- action,
814
- result: undefined,
815
- });
816
- } else {
817
- postMessageError({
818
- id,
819
- action,
820
- error: "Group not found",
821
- });
822
- }
578
+ const group = getGroup(data.id);
579
+ await group.addSuperAdmin(data.inboxId);
580
+ postMessage({ id, action, result: undefined });
823
581
  break;
824
582
  }
825
583
  case "removeGroupSuperAdmin": {
826
- const group = client.conversations.getConversationById(data.id);
827
- if (group) {
828
- await group.removeSuperAdmin(data.inboxId);
829
- postMessage({
830
- id,
831
- action,
832
- result: undefined,
833
- });
834
- } else {
835
- postMessageError({
836
- id,
837
- action,
838
- error: "Group not found",
839
- });
840
- }
584
+ const group = getGroup(data.id);
585
+ await group.removeSuperAdmin(data.inboxId);
586
+ postMessage({ id, action, result: undefined });
841
587
  break;
842
588
  }
843
589
  case "addGroupMembers": {
844
- const group = client.conversations.getConversationById(data.id);
845
- if (group) {
846
- await group.addMembers(data.accountAddresses);
847
- postMessage({
848
- id,
849
- action,
850
- result: undefined,
851
- });
852
- } else {
853
- postMessageError({
854
- id,
855
- action,
856
- error: "Group not found",
857
- });
858
- }
590
+ const group = getGroup(data.id);
591
+ await group.addMembers(data.accountAddresses);
592
+ postMessage({ id, action, result: undefined });
859
593
  break;
860
594
  }
861
595
  case "removeGroupMembers": {
862
- const group = client.conversations.getConversationById(data.id);
863
- if (group) {
864
- await group.removeMembers(data.accountAddresses);
865
- postMessage({
866
- id,
867
- action,
868
- result: undefined,
869
- });
870
- } else {
871
- postMessageError({
872
- id,
873
- action,
874
- error: "Group not found",
875
- });
876
- }
596
+ const group = getGroup(data.id);
597
+ await group.removeMembers(data.accountAddresses);
598
+ postMessage({ id, action, result: undefined });
877
599
  break;
878
600
  }
879
601
  case "addGroupMembersByInboxId": {
880
- const group = client.conversations.getConversationById(data.id);
881
- if (group) {
882
- await group.addMembersByInboxId(data.inboxIds);
883
- postMessage({
884
- id,
885
- action,
886
- result: undefined,
887
- });
888
- } else {
889
- postMessageError({
890
- id,
891
- action,
892
- error: "Group not found",
893
- });
894
- }
602
+ const group = getGroup(data.id);
603
+ await group.addMembersByInboxId(data.inboxIds);
604
+ postMessage({ id, action, result: undefined });
895
605
  break;
896
606
  }
897
607
  case "removeGroupMembersByInboxId": {
898
- const group = client.conversations.getConversationById(data.id);
899
- if (group) {
900
- await group.removeMembersByInboxId(data.inboxIds);
901
- postMessage({
902
- id,
903
- action,
904
- result: undefined,
905
- });
906
- } else {
907
- postMessageError({
908
- id,
909
- action,
910
- error: "Group not found",
911
- });
912
- }
608
+ const group = getGroup(data.id);
609
+ await group.removeMembersByInboxId(data.inboxIds);
610
+ postMessage({ id, action, result: undefined });
913
611
  break;
914
612
  }
915
613
  case "isGroupAdmin": {
916
- const group = client.conversations.getConversationById(data.id);
917
- if (group) {
918
- const result = group.isAdmin(data.inboxId);
919
- postMessage({
920
- id,
921
- action,
922
- result,
923
- });
924
- } else {
925
- postMessageError({
926
- id,
927
- action,
928
- error: "Group not found",
929
- });
930
- }
614
+ const group = getGroup(data.id);
615
+ const result = group.isAdmin(data.inboxId);
616
+ postMessage({ id, action, result });
931
617
  break;
932
618
  }
933
619
  case "isGroupSuperAdmin": {
934
- const group = client.conversations.getConversationById(data.id);
935
- if (group) {
936
- const result = group.isSuperAdmin(data.inboxId);
937
- postMessage({
938
- id,
939
- action,
940
- result,
941
- });
942
- } else {
943
- postMessageError({
944
- id,
945
- action,
946
- error: "Group not found",
947
- });
948
- }
620
+ const group = getGroup(data.id);
621
+ const result = group.isSuperAdmin(data.inboxId);
622
+ postMessage({ id, action, result });
949
623
  break;
950
624
  }
951
625
  case "getDmPeerInboxId": {
952
- const group = client.conversations.getConversationById(data.id);
953
- if (group) {
954
- const result = group.dmPeerInboxId();
955
- postMessage({
956
- id,
957
- action,
958
- result,
959
- });
960
- } else {
961
- postMessageError({
962
- id,
963
- action,
964
- error: "Group not found",
965
- });
966
- }
626
+ const group = getGroup(data.id);
627
+ const result = group.dmPeerInboxId();
628
+ postMessage({ id, action, result });
967
629
  break;
968
630
  }
969
631
  case "updateGroupPermissionPolicy": {
970
- const group = client.conversations.getConversationById(data.id);
971
- if (group) {
972
- await group.updatePermission(
973
- data.permissionType,
974
- data.policy,
975
- data.metadataField,
976
- );
977
- postMessage({
978
- id,
979
- action,
980
- result: undefined,
981
- });
982
- } else {
983
- postMessageError({
984
- id,
985
- action,
986
- error: "Group not found",
987
- });
988
- }
632
+ const group = getGroup(data.id);
633
+ await group.updatePermission(
634
+ data.permissionType,
635
+ data.policy,
636
+ data.metadataField,
637
+ );
638
+ postMessage({ id, action, result: undefined });
989
639
  break;
990
640
  }
991
641
  case "getGroupPermissions": {
992
- const group = client.conversations.getConversationById(data.id);
993
- if (group) {
994
- const safeConversation = await toSafeConversation(group);
995
- postMessage({
996
- id,
997
- action,
998
- result: safeConversation.permissions,
999
- });
1000
- } else {
1001
- postMessageError({
1002
- id,
1003
- action,
1004
- error: "Group not found",
1005
- });
1006
- }
642
+ const group = getGroup(data.id);
643
+ const safeConversation = await toSafeConversation(group);
644
+ const result = safeConversation.permissions;
645
+ postMessage({ id, action, result });
1007
646
  break;
1008
647
  }
1009
648
  case "getGroupMessageDisappearingSettings": {
1010
- const group = client.conversations.getConversationById(data.id);
1011
- if (group) {
1012
- const result = group.messageDisappearingSettings();
1013
- postMessage({
1014
- id,
1015
- action,
1016
- result: result
1017
- ? toSafeMessageDisappearingSettings(result)
1018
- : undefined,
1019
- });
1020
- } else {
1021
- postMessageError({
1022
- id,
1023
- action,
1024
- error: "Group not found",
1025
- });
1026
- }
649
+ const group = getGroup(data.id);
650
+ const settings = group.messageDisappearingSettings();
651
+ const result = settings
652
+ ? toSafeMessageDisappearingSettings(settings)
653
+ : undefined;
654
+ postMessage({ id, action, result });
1027
655
  break;
1028
656
  }
1029
657
  case "updateGroupMessageDisappearingSettings": {
1030
- const group = client.conversations.getConversationById(data.id);
1031
- if (group) {
1032
- await group.updateMessageDisappearingSettings(data.fromNs, data.inNs);
1033
- postMessage({
1034
- id,
1035
- action,
1036
- result: undefined,
1037
- });
1038
- } else {
1039
- postMessageError({
1040
- id,
1041
- action,
1042
- error: "Group not found",
1043
- });
1044
- }
658
+ const group = getGroup(data.id);
659
+ await group.updateMessageDisappearingSettings(data.fromNs, data.inNs);
660
+ postMessage({ id, action, result: undefined });
1045
661
  break;
1046
662
  }
1047
663
  case "removeGroupMessageDisappearingSettings": {
1048
- const group = client.conversations.getConversationById(data.id);
1049
- if (group) {
1050
- await group.removeMessageDisappearingSettings();
1051
- postMessage({
1052
- id,
1053
- action,
1054
- result: undefined,
1055
- });
1056
- } else {
1057
- postMessageError({
1058
- id,
1059
- action,
1060
- error: "Group not found",
1061
- });
1062
- }
664
+ const group = getGroup(data.id);
665
+ await group.removeMessageDisappearingSettings();
666
+ postMessage({ id, action, result: undefined });
1063
667
  break;
1064
668
  }
1065
669
  case "isGroupMessageDisappearingEnabled": {
1066
- const group = client.conversations.getConversationById(data.id);
1067
- if (group) {
1068
- const result = group.isMessageDisappearingEnabled();
1069
- postMessage({
1070
- id,
1071
- action,
1072
- result,
1073
- });
1074
- } else {
1075
- postMessageError({
1076
- id,
1077
- action,
1078
- error: "Group not found",
1079
- });
1080
- }
670
+ const group = getGroup(data.id);
671
+ const result = group.isMessageDisappearingEnabled();
672
+ postMessage({ id, action, result });
1081
673
  break;
1082
674
  }
1083
675
  case "streamGroupMessages": {
1084
- const group = client.conversations.getConversationById(data.groupId);
1085
- if (group) {
1086
- const streamCallback = (
1087
- error: Error | null,
1088
- value: Message | undefined,
1089
- ) => {
1090
- if (error) {
1091
- postStreamMessageError({
1092
- type: "message",
1093
- streamId: data.streamId,
1094
- error: error.message,
1095
- });
1096
- } else {
1097
- postStreamMessage({
1098
- type: "message",
1099
- streamId: data.streamId,
1100
- result: value ? toSafeMessage(value) : undefined,
1101
- });
1102
- }
1103
- };
1104
- const streamCloser = group.stream(streamCallback);
1105
- streamClosers.set(data.streamId, streamCloser);
1106
- postMessage({
1107
- id,
1108
- action,
1109
- result: undefined,
1110
- });
1111
- } else {
1112
- postMessageError({
1113
- id,
1114
- action,
1115
- error: "Group not found",
1116
- });
1117
- }
676
+ const group = getGroup(data.groupId);
677
+ const streamCallback = (
678
+ error: Error | null,
679
+ value: Message | undefined,
680
+ ) => {
681
+ if (error) {
682
+ postStreamMessageError({
683
+ type: "message",
684
+ streamId: data.streamId,
685
+ error: error.message,
686
+ });
687
+ } else {
688
+ postStreamMessage({
689
+ type: "message",
690
+ streamId: data.streamId,
691
+ result: value ? toSafeMessage(value) : undefined,
692
+ });
693
+ }
694
+ };
695
+ const streamCloser = group.stream(streamCallback);
696
+ streamClosers.set(data.streamId, streamCloser);
697
+ postMessage({ id, action, result: undefined });
1118
698
  break;
1119
699
  }
1120
700
  }