@wireapp/core 30.13.0 → 31.0.0

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 (45) hide show
  1. package/package.json +28 -22
  2. package/src/main/Account.d.ts +3 -3
  3. package/src/main/CoreError.d.ts +1 -1
  4. package/src/main/account/AccountService.d.ts +2 -2
  5. package/src/main/account/AccountService.ts +2 -2
  6. package/src/main/auth/LoginSanitizer.d.ts +1 -1
  7. package/src/main/broadcast/AvailabilityType.d.ts +1 -1
  8. package/src/main/broadcast/BroadcastService.d.ts +3 -3
  9. package/src/main/client/ClientBackendRepository.d.ts +2 -2
  10. package/src/main/client/ClientDatabaseRepository.d.ts +3 -3
  11. package/src/main/client/ClientInfo.d.ts +1 -1
  12. package/src/main/client/ClientService.d.ts +4 -4
  13. package/src/main/connection/ConnectionService.d.ts +1 -1
  14. package/src/main/conversation/AbortReason.d.ts +1 -1
  15. package/src/main/conversation/AssetService/AssetService.d.ts +4 -4
  16. package/src/main/conversation/ConversationService/ConversationService.d.ts +9 -28
  17. package/src/main/conversation/ConversationService/ConversationService.js +2 -309
  18. package/src/main/conversation/ConversationService/ConversationService.types.d.ts +2 -0
  19. package/src/main/conversation/ConversationService/messageGenerator.d.ts +9 -0
  20. package/src/main/conversation/ConversationService/messageGenerator.js +301 -0
  21. package/src/main/conversation/message/CompositeContentBuilder.d.ts +1 -1
  22. package/src/main/conversation/message/Message.d.ts +3 -3
  23. package/src/main/conversation/message/MessageBuilder.d.ts +6 -2
  24. package/src/main/conversation/message/MessageBuilder.js +3 -0
  25. package/src/main/conversation/message/MessageToProtoMapper.d.ts +2 -2
  26. package/src/main/conversation/message/OtrMessage.d.ts +2 -2
  27. package/src/main/conversation/message/PayloadBundle.d.ts +3 -3
  28. package/src/main/conversation/message/TeamMessage.d.ts +2 -2
  29. package/src/main/conversation/message/TextContentBuilder.d.ts +1 -1
  30. package/src/main/conversation/message/UserMessage.d.ts +2 -2
  31. package/src/main/cryptography/AssetCryptography/index.d.ts +1 -1
  32. package/src/main/cryptography/CryptographyDatabaseRepository.d.ts +1 -1
  33. package/src/main/cryptography/CryptographyService.d.ts +7 -7
  34. package/src/main/cryptography/GenericMessageMapper.d.ts +1 -1
  35. package/src/main/giphy/GiphyService.d.ts +2 -2
  36. package/src/main/mls/MLSService/MLSService.d.ts +1 -1
  37. package/src/main/notification/NotificationBackendRepository.d.ts +2 -2
  38. package/src/main/notification/NotificationDatabaseRepository.d.ts +3 -3
  39. package/src/main/notification/NotificationService.d.ts +4 -4
  40. package/src/main/self/SelfService.d.ts +2 -2
  41. package/src/main/team/TeamService.d.ts +2 -2
  42. package/src/main/test/CryptographyHelper.d.ts +1 -1
  43. package/src/main/user/UserService.d.ts +3 -3
  44. package/src/main/util/TypePredicateUtil.d.ts +2 -2
  45. package/CHANGELOG.md +0 -12419
@@ -28,12 +28,12 @@ const AssetCryptography_1 = require("../../cryptography/AssetCryptography");
28
28
  const TypePredicateUtil_1 = require("../../util/TypePredicateUtil");
29
29
  const MessageBuilder_1 = require("../message/MessageBuilder");
30
30
  const MessageService_1 = require("../message/MessageService");
31
- const MessageToProtoMapper_1 = require("../message/MessageToProtoMapper");
32
31
  const ConversationService_types_1 = require("./ConversationService.types");
33
32
  const bazinga64_1 = require("bazinga64");
34
33
  const fullyQualifiedClientIdUtils_1 = require("../../util/fullyQualifiedClientIdUtils");
35
34
  const mls_1 = require("../../mls");
36
35
  const messageSender_1 = require("../message/messageSender");
36
+ const messageGenerator_1 = require("./messageGenerator");
37
37
  class ConversationService {
38
38
  constructor(apiClient, cryptographyService, config, notificationService, mlsService) {
39
39
  this.apiClient = apiClient;
@@ -43,17 +43,6 @@ class ConversationService {
43
43
  this.messageTimer = new conversation_2.MessageTimer();
44
44
  this.messageService = new MessageService_1.MessageService(this.apiClient, cryptographyService);
45
45
  }
46
- createEphemeral(originalGenericMessage, expireAfterMillis) {
47
- const ephemeralMessage = protocol_messaging_1.Ephemeral.create({
48
- expireAfterMillis,
49
- [originalGenericMessage.content]: originalGenericMessage[originalGenericMessage.content],
50
- });
51
- const genericMessage = protocol_messaging_1.GenericMessage.create({
52
- [conversation_2.GenericMessageType.EPHEMERAL]: ephemeralMessage,
53
- messageId: originalGenericMessage.messageId,
54
- });
55
- return genericMessage;
56
- }
57
46
  async getConversationQualifiedMembers(conversationId) {
58
47
  const conversation = await this.apiClient.api.conversation.getConversation(conversationId);
59
48
  /*
@@ -231,214 +220,6 @@ class ConversationService {
231
220
  }
232
221
  return userIds;
233
222
  }
234
- generateButtonActionGenericMessage(payloadBundle) {
235
- return protocol_messaging_1.GenericMessage.create({
236
- [conversation_2.GenericMessageType.BUTTON_ACTION]: protocol_messaging_1.ButtonAction.create(payloadBundle.content),
237
- messageId: payloadBundle.id,
238
- });
239
- }
240
- generateButtonActionConfirmationGenericMessage(payloadBundle) {
241
- return protocol_messaging_1.GenericMessage.create({
242
- [conversation_2.GenericMessageType.BUTTON_ACTION_CONFIRMATION]: protocol_messaging_1.ButtonActionConfirmation.create(payloadBundle.content),
243
- messageId: payloadBundle.id,
244
- });
245
- }
246
- generateCompositeGenericMessage(payloadBundle) {
247
- return protocol_messaging_1.GenericMessage.create({
248
- [conversation_2.GenericMessageType.COMPOSITE]: protocol_messaging_1.Composite.create(payloadBundle.content),
249
- messageId: payloadBundle.id,
250
- });
251
- }
252
- generateConfirmationGenericMessage(payloadBundle) {
253
- const content = protocol_messaging_1.Confirmation.create(payloadBundle.content);
254
- return protocol_messaging_1.GenericMessage.create({
255
- [conversation_2.GenericMessageType.CONFIRMATION]: content,
256
- messageId: payloadBundle.id,
257
- });
258
- }
259
- generateEditedTextGenericMessage(payloadBundle) {
260
- const editedMessage = protocol_messaging_1.MessageEdit.create({
261
- replacingMessageId: payloadBundle.content.originalMessageId,
262
- text: MessageToProtoMapper_1.MessageToProtoMapper.mapText(payloadBundle),
263
- });
264
- return protocol_messaging_1.GenericMessage.create({
265
- [conversation_2.GenericMessageType.EDITED]: editedMessage,
266
- messageId: payloadBundle.id,
267
- });
268
- }
269
- generateFileDataGenericMessage(payloadBundle) {
270
- if (!payloadBundle.content) {
271
- throw new Error('No content for sendFileData provided.');
272
- }
273
- const { asset, expectsReadConfirmation, legalHoldStatus } = payloadBundle.content;
274
- const remoteData = protocol_messaging_1.Asset.RemoteData.create({
275
- assetId: asset.key,
276
- assetToken: asset.token,
277
- otrKey: asset.keyBytes,
278
- sha256: asset.sha256,
279
- assetDomain: asset.domain,
280
- });
281
- const assetMessage = protocol_messaging_1.Asset.create({
282
- expectsReadConfirmation,
283
- legalHoldStatus,
284
- uploaded: remoteData,
285
- });
286
- assetMessage.status = conversation_2.AssetTransferState.UPLOADED;
287
- const genericMessage = protocol_messaging_1.GenericMessage.create({
288
- [conversation_2.GenericMessageType.ASSET]: assetMessage,
289
- messageId: payloadBundle.id,
290
- });
291
- const expireAfterMillis = this.messageTimer.getMessageTimer(payloadBundle.conversation);
292
- return expireAfterMillis > 0 ? this.createEphemeral(genericMessage, expireAfterMillis) : genericMessage;
293
- }
294
- generateFileMetaDataGenericMessage(payloadBundle) {
295
- if (!payloadBundle.content) {
296
- throw new Error('No content for sendFileMetaData provided.');
297
- }
298
- const { expectsReadConfirmation, legalHoldStatus, metaData } = payloadBundle.content;
299
- const original = protocol_messaging_1.Asset.Original.create({
300
- audio: metaData.audio,
301
- mimeType: metaData.type,
302
- name: metaData.name,
303
- size: metaData.length,
304
- video: metaData.video,
305
- image: metaData.image,
306
- });
307
- const assetMessage = protocol_messaging_1.Asset.create({
308
- expectsReadConfirmation,
309
- legalHoldStatus,
310
- original,
311
- });
312
- const genericMessage = protocol_messaging_1.GenericMessage.create({
313
- [conversation_2.GenericMessageType.ASSET]: assetMessage,
314
- messageId: payloadBundle.id,
315
- });
316
- const expireAfterMillis = this.messageTimer.getMessageTimer(payloadBundle.conversation);
317
- return expireAfterMillis > 0 ? this.createEphemeral(genericMessage, expireAfterMillis) : genericMessage;
318
- }
319
- generateFileAbortGenericMessage(payloadBundle) {
320
- if (!payloadBundle.content) {
321
- throw new Error('No content for sendFileAbort provided.');
322
- }
323
- const { expectsReadConfirmation, legalHoldStatus, reason } = payloadBundle.content;
324
- const assetMessage = protocol_messaging_1.Asset.create({
325
- expectsReadConfirmation,
326
- legalHoldStatus,
327
- notUploaded: reason,
328
- });
329
- assetMessage.status = conversation_2.AssetTransferState.NOT_UPLOADED;
330
- const genericMessage = protocol_messaging_1.GenericMessage.create({
331
- [conversation_2.GenericMessageType.ASSET]: assetMessage,
332
- messageId: payloadBundle.id,
333
- });
334
- const expireAfterMillis = this.messageTimer.getMessageTimer(payloadBundle.conversation);
335
- return expireAfterMillis > 0 ? this.createEphemeral(genericMessage, expireAfterMillis) : genericMessage;
336
- }
337
- generateAsset(payloadBundle) {
338
- if (!payloadBundle.content) {
339
- throw new Error('No content for sendImage provided.');
340
- }
341
- const { asset, expectsReadConfirmation, image, legalHoldStatus } = payloadBundle.content;
342
- const imageMetadata = protocol_messaging_1.Asset.ImageMetaData.create({
343
- height: image.height,
344
- width: image.width,
345
- });
346
- const original = protocol_messaging_1.Asset.Original.create({
347
- [conversation_2.GenericMessageType.IMAGE]: imageMetadata,
348
- mimeType: image.type,
349
- name: null,
350
- size: image.data.length,
351
- });
352
- const remoteData = protocol_messaging_1.Asset.RemoteData.create({
353
- assetId: asset.key,
354
- assetToken: asset.token,
355
- assetDomain: asset.domain,
356
- otrKey: asset.keyBytes,
357
- sha256: asset.sha256,
358
- });
359
- const assetMessage = protocol_messaging_1.Asset.create({
360
- expectsReadConfirmation,
361
- legalHoldStatus,
362
- original,
363
- uploaded: remoteData,
364
- });
365
- assetMessage.status = conversation_2.AssetTransferState.UPLOADED;
366
- return assetMessage;
367
- }
368
- generateImageGenericMessage(payloadBundle) {
369
- const imageAsset = this.generateAsset(payloadBundle);
370
- let genericMessage = protocol_messaging_1.GenericMessage.create({
371
- [conversation_2.GenericMessageType.ASSET]: imageAsset,
372
- messageId: payloadBundle.id,
373
- });
374
- const expireAfterMillis = this.messageTimer.getMessageTimer(payloadBundle.conversation);
375
- if (expireAfterMillis) {
376
- genericMessage = this.createEphemeral(genericMessage, expireAfterMillis);
377
- }
378
- return { genericMessage, content: imageAsset };
379
- }
380
- generateLocationGenericMessage(payloadBundle) {
381
- const { expectsReadConfirmation, latitude, legalHoldStatus, longitude, name, zoom } = payloadBundle.content;
382
- const locationMessage = protocol_messaging_1.Location.create({
383
- expectsReadConfirmation,
384
- latitude,
385
- legalHoldStatus,
386
- longitude,
387
- name,
388
- zoom,
389
- });
390
- const genericMessage = protocol_messaging_1.GenericMessage.create({
391
- [conversation_2.GenericMessageType.LOCATION]: locationMessage,
392
- messageId: payloadBundle.id,
393
- });
394
- const expireAfterMillis = this.messageTimer.getMessageTimer(payloadBundle.conversation);
395
- return expireAfterMillis > 0 ? this.createEphemeral(genericMessage, expireAfterMillis) : genericMessage;
396
- }
397
- generatePingGenericMessage(payloadBundle) {
398
- const content = protocol_messaging_1.Knock.create(payloadBundle.content);
399
- const genericMessage = protocol_messaging_1.GenericMessage.create({
400
- [conversation_2.GenericMessageType.KNOCK]: content,
401
- messageId: payloadBundle.id,
402
- });
403
- const expireAfterMillis = this.messageTimer.getMessageTimer(payloadBundle.conversation);
404
- return expireAfterMillis > 0 ? this.createEphemeral(genericMessage, expireAfterMillis) : genericMessage;
405
- }
406
- generateReactionGenericMessage(payloadBundle) {
407
- const { legalHoldStatus, originalMessageId, type } = payloadBundle.content;
408
- const reaction = protocol_messaging_1.Reaction.create({
409
- emoji: type,
410
- legalHoldStatus,
411
- messageId: originalMessageId,
412
- });
413
- const genericMessage = protocol_messaging_1.GenericMessage.create({
414
- [conversation_2.GenericMessageType.REACTION]: reaction,
415
- messageId: payloadBundle.id,
416
- });
417
- return genericMessage;
418
- }
419
- generateSessionResetGenericMessage(payloadBundle) {
420
- return protocol_messaging_1.GenericMessage.create({
421
- [conversation_2.GenericMessageType.CLIENT_ACTION]: protocol_messaging_1.ClientAction.RESET_SESSION,
422
- messageId: payloadBundle.id,
423
- });
424
- }
425
- generateCallGenericMessage(payloadBundle) {
426
- const callMessage = protocol_messaging_1.Calling.create({
427
- content: payloadBundle.content,
428
- });
429
- return protocol_messaging_1.GenericMessage.create({
430
- [conversation_2.GenericMessageType.CALLING]: callMessage,
431
- messageId: payloadBundle.id,
432
- });
433
- }
434
- generateTextGenericMessage(payloadBundle) {
435
- const genericMessage = protocol_messaging_1.GenericMessage.create({
436
- messageId: payloadBundle.id,
437
- [conversation_2.GenericMessageType.TEXT]: MessageToProtoMapper_1.MessageToProtoMapper.mapText(payloadBundle),
438
- });
439
- const expireAfterMillis = this.messageTimer.getMessageTimer(payloadBundle.conversation);
440
- return expireAfterMillis > 0 ? this.createEphemeral(genericMessage, expireAfterMillis) : genericMessage;
441
- }
442
223
  async clearConversation(conversationId, timestamp = new Date(), messageId = MessageBuilder_1.MessageBuilder.createId(), sendAsProtobuf) {
443
224
  if (timestamp instanceof Date) {
444
225
  timestamp = timestamp.getTime();
@@ -588,35 +369,6 @@ class ConversationService {
588
369
  type: conversation_2.PayloadBundleType.MESSAGE_HIDE,
589
370
  };
590
371
  }
591
- async deleteMessageEveryone(conversationId, messageIdToDelete, userIds, sendAsProtobuf, conversationDomain, callbacks) {
592
- var _a, _b;
593
- const messageId = MessageBuilder_1.MessageBuilder.createId();
594
- const content = protocol_messaging_1.MessageDelete.create({
595
- messageId: messageIdToDelete,
596
- });
597
- const genericMessage = protocol_messaging_1.GenericMessage.create({
598
- [conversation_2.GenericMessageType.DELETED]: content,
599
- messageId,
600
- });
601
- (_a = callbacks === null || callbacks === void 0 ? void 0 : callbacks.onStart) === null || _a === void 0 ? void 0 : _a.call(callbacks, genericMessage);
602
- const response = await this.sendGenericMessage(this.apiClient.validatedClientId, conversationId, genericMessage, {
603
- userIds,
604
- sendAsProtobuf,
605
- conversationDomain,
606
- });
607
- (_b = callbacks === null || callbacks === void 0 ? void 0 : callbacks.onSuccess) === null || _b === void 0 ? void 0 : _b.call(callbacks, genericMessage, response === null || response === void 0 ? void 0 : response.time);
608
- return {
609
- content,
610
- conversation: conversationId,
611
- from: this.apiClient.context.userId,
612
- id: messageId,
613
- messageTimer: this.messageTimer.getMessageTimer(conversationId),
614
- source: conversation_2.PayloadBundleSource.LOCAL,
615
- state: conversation_2.PayloadBundleState.OUTGOING_SENT,
616
- timestamp: Date.now(),
617
- type: conversation_2.PayloadBundleType.MESSAGE_DELETE,
618
- };
619
- }
620
372
  createProteusConversation(conversationData, otherUserIds) {
621
373
  let payload;
622
374
  if (typeof conversationData === 'string') {
@@ -689,7 +441,7 @@ class ConversationService {
689
441
  return params.protocol === conversation_1.ConversationProtocol.MLS;
690
442
  }
691
443
  const { payload, onStart } = params;
692
- const { genericMessage, content } = this.generateGenericMessage(payload);
444
+ const { genericMessage, content } = (0, messageGenerator_1.generateGenericMessage)(payload, this.messageTimer);
693
445
  if ((await (onStart === null || onStart === void 0 ? void 0 : onStart(genericMessage))) === false) {
694
446
  // If the onStart call returns false, it means the consumer wants to cancel the message sending
695
447
  return Object.assign(Object.assign({}, payload), { state: conversation_2.PayloadBundleState.CANCELLED });
@@ -736,65 +488,6 @@ class ConversationService {
736
488
  const hasFailed = Object.keys(mismatch.failed_to_send || {}).length > 0;
737
489
  return !hasMissing && !hasDeleted && !hasRedundant && !hasFailed;
738
490
  }
739
- generateGenericMessage(payload) {
740
- let genericMessage;
741
- const content = payload.content;
742
- switch (payload.type) {
743
- case conversation_2.PayloadBundleType.ASSET:
744
- genericMessage = this.generateFileDataGenericMessage(payload);
745
- return { genericMessage, content };
746
- case conversation_2.PayloadBundleType.ASSET_ABORT:
747
- genericMessage = this.generateFileAbortGenericMessage(payload);
748
- return { genericMessage, content };
749
- case conversation_2.PayloadBundleType.ASSET_META:
750
- genericMessage = this.generateFileMetaDataGenericMessage(payload);
751
- return { genericMessage, content };
752
- case conversation_2.PayloadBundleType.ASSET_IMAGE:
753
- return this.generateImageGenericMessage(payload);
754
- case conversation_2.PayloadBundleType.BUTTON_ACTION:
755
- genericMessage = this.generateButtonActionGenericMessage(payload);
756
- return { genericMessage, content };
757
- case conversation_2.PayloadBundleType.BUTTON_ACTION_CONFIRMATION:
758
- genericMessage = this.generateButtonActionConfirmationGenericMessage(payload);
759
- return { genericMessage, content };
760
- case conversation_2.PayloadBundleType.CALL:
761
- genericMessage = this.generateCallGenericMessage(payload);
762
- return { genericMessage, content };
763
- case conversation_2.PayloadBundleType.CLIENT_ACTION: {
764
- if (payload.content.clientAction !== protocol_messaging_1.ClientAction.RESET_SESSION) {
765
- throw new Error(`No send method implemented for "${payload.type}" and ClientAction "${payload.content}".`);
766
- }
767
- genericMessage = this.generateSessionResetGenericMessage(payload);
768
- return { genericMessage, content };
769
- }
770
- case conversation_2.PayloadBundleType.COMPOSITE:
771
- genericMessage = this.generateCompositeGenericMessage(payload);
772
- return { genericMessage, content };
773
- case conversation_2.PayloadBundleType.CONFIRMATION:
774
- genericMessage = this.generateConfirmationGenericMessage(payload);
775
- return { genericMessage, content };
776
- case conversation_2.PayloadBundleType.LOCATION:
777
- genericMessage = this.generateLocationGenericMessage(payload);
778
- return { genericMessage, content };
779
- case conversation_2.PayloadBundleType.MESSAGE_EDIT:
780
- genericMessage = this.generateEditedTextGenericMessage(payload);
781
- return { genericMessage, content };
782
- case conversation_2.PayloadBundleType.PING:
783
- genericMessage = this.generatePingGenericMessage(payload);
784
- return { genericMessage, content };
785
- case conversation_2.PayloadBundleType.REACTION:
786
- genericMessage = this.generateReactionGenericMessage(payload);
787
- return { genericMessage, content };
788
- case conversation_2.PayloadBundleType.TEXT:
789
- genericMessage = this.generateTextGenericMessage(payload);
790
- return { genericMessage, content };
791
- /**
792
- * ToDo: Create Generic implementation for everything else
793
- */
794
- default:
795
- throw new Error(`No send method implemented for "${payload['type']}".`);
796
- }
797
- }
798
491
  /**
799
492
  * ###############################################
800
493
  * ################ MLS Functions ################
@@ -82,12 +82,14 @@ export declare type SendProteusMessageParams<T> = SendCommonParams<T> & MessageS
82
82
  */
83
83
  userIds?: string[] | QualifiedId[] | UserClients | QualifiedUserClients;
84
84
  onClientMismatch?: (status: ClientMismatch | MessageSendingStatus, wasSent: boolean) => void | boolean | Promise<boolean>;
85
+ protocol: ConversationProtocol.PROTEUS;
85
86
  };
86
87
  export declare type SendMlsMessageParams<T> = SendCommonParams<T> & {
87
88
  /**
88
89
  * The groupId of the conversation to send the message to (Needed only for MLS)
89
90
  */
90
91
  groupId: string;
92
+ protocol: ConversationProtocol.MLS;
91
93
  };
92
94
  export declare type QualifiedUsers = QualifiedId & {
93
95
  skipOwn?: string;
@@ -0,0 +1,9 @@
1
+ import { OtrMessage } from '../message/OtrMessage';
2
+ import { GenericMessage } from '@wireapp/protocol-messaging';
3
+ import { MessageTimer } from '../MessageTimer';
4
+ interface GeneratedMessage<T extends OtrMessage> {
5
+ genericMessage: GenericMessage;
6
+ content: T['content'];
7
+ }
8
+ export declare function generateGenericMessage<T extends OtrMessage>(payload: T, messageTimer: MessageTimer): GeneratedMessage<T>;
9
+ export {};
@@ -0,0 +1,301 @@
1
+ "use strict";
2
+ /*
3
+ * Wire
4
+ * Copyright (C) 2022 Wire Swiss GmbH
5
+ *
6
+ * This program is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation, either version 3 of the License, or
9
+ * (at your option) any later version.
10
+ *
11
+ * This program is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ * GNU General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU General Public License
17
+ * along with this program. If not, see http://www.gnu.org/licenses/.
18
+ *
19
+ */
20
+ Object.defineProperty(exports, "__esModule", { value: true });
21
+ exports.generateGenericMessage = void 0;
22
+ const protocol_messaging_1 = require("@wireapp/protocol-messaging");
23
+ const PayloadBundle_1 = require("../message/PayloadBundle");
24
+ const MessageToProtoMapper_1 = require("../message/MessageToProtoMapper");
25
+ const GenericMessageType_1 = require("../GenericMessageType");
26
+ const AssetTransferState_1 = require("../AssetTransferState");
27
+ function generateGenericMessage(payload, messageTimer) {
28
+ const content = payload.content;
29
+ switch (payload.type) {
30
+ case PayloadBundle_1.PayloadBundleType.ASSET:
31
+ return { genericMessage: generateFileDataGenericMessage(payload, messageTimer), content };
32
+ case PayloadBundle_1.PayloadBundleType.ASSET_ABORT:
33
+ return { genericMessage: generateFileAbortGenericMessage(payload, messageTimer), content };
34
+ case PayloadBundle_1.PayloadBundleType.ASSET_META:
35
+ return { genericMessage: generateFileMetaDataGenericMessage(payload, messageTimer), content };
36
+ case PayloadBundle_1.PayloadBundleType.ASSET_IMAGE:
37
+ return generateImageGenericMessage(payload, messageTimer);
38
+ case PayloadBundle_1.PayloadBundleType.BUTTON_ACTION:
39
+ return { genericMessage: generateButtonActionGenericMessage(payload), content };
40
+ case PayloadBundle_1.PayloadBundleType.BUTTON_ACTION_CONFIRMATION:
41
+ return { genericMessage: generateButtonActionConfirmationGenericMessage(payload), content };
42
+ case PayloadBundle_1.PayloadBundleType.CALL:
43
+ return { genericMessage: generateCallGenericMessage(payload), content };
44
+ case PayloadBundle_1.PayloadBundleType.CLIENT_ACTION:
45
+ return { genericMessage: generateSessionResetGenericMessage(payload), content };
46
+ case PayloadBundle_1.PayloadBundleType.COMPOSITE:
47
+ return { genericMessage: generateCompositeGenericMessage(payload), content };
48
+ case PayloadBundle_1.PayloadBundleType.CONFIRMATION:
49
+ return { genericMessage: generateConfirmationGenericMessage(payload), content };
50
+ case PayloadBundle_1.PayloadBundleType.LOCATION:
51
+ return { genericMessage: generateLocationGenericMessage(payload, messageTimer), content };
52
+ case PayloadBundle_1.PayloadBundleType.MESSAGE_EDIT:
53
+ return { genericMessage: generateEditedTextGenericMessage(payload), content };
54
+ case PayloadBundle_1.PayloadBundleType.PING:
55
+ return { genericMessage: generatePingGenericMessage(payload, messageTimer), content };
56
+ case PayloadBundle_1.PayloadBundleType.REACTION:
57
+ return { genericMessage: generateReactionGenericMessage(payload), content };
58
+ case PayloadBundle_1.PayloadBundleType.TEXT:
59
+ return { genericMessage: generateTextGenericMessage(payload, messageTimer), content };
60
+ case PayloadBundle_1.PayloadBundleType.MESSAGE_DELETE:
61
+ return { genericMessage: generateDeleteMessage(payload), content };
62
+ /**
63
+ * ToDo: Create Generic implementation for everything else
64
+ */
65
+ default:
66
+ throw new Error(`No send method implemented for "${payload['type']}".`);
67
+ }
68
+ }
69
+ exports.generateGenericMessage = generateGenericMessage;
70
+ function generateButtonActionGenericMessage(payloadBundle) {
71
+ return protocol_messaging_1.GenericMessage.create({
72
+ [GenericMessageType_1.GenericMessageType.BUTTON_ACTION]: protocol_messaging_1.ButtonAction.create(payloadBundle.content),
73
+ messageId: payloadBundle.id,
74
+ });
75
+ }
76
+ function generateButtonActionConfirmationGenericMessage(payloadBundle) {
77
+ return protocol_messaging_1.GenericMessage.create({
78
+ [GenericMessageType_1.GenericMessageType.BUTTON_ACTION_CONFIRMATION]: protocol_messaging_1.ButtonActionConfirmation.create(payloadBundle.content),
79
+ messageId: payloadBundle.id,
80
+ });
81
+ }
82
+ function generateCompositeGenericMessage(payloadBundle) {
83
+ return protocol_messaging_1.GenericMessage.create({
84
+ [GenericMessageType_1.GenericMessageType.COMPOSITE]: protocol_messaging_1.Composite.create(payloadBundle.content),
85
+ messageId: payloadBundle.id,
86
+ });
87
+ }
88
+ function generateConfirmationGenericMessage(payloadBundle) {
89
+ const content = protocol_messaging_1.Confirmation.create(payloadBundle.content);
90
+ return protocol_messaging_1.GenericMessage.create({
91
+ [GenericMessageType_1.GenericMessageType.CONFIRMATION]: content,
92
+ messageId: payloadBundle.id,
93
+ });
94
+ }
95
+ function generateEditedTextGenericMessage(payloadBundle) {
96
+ const editedMessage = protocol_messaging_1.MessageEdit.create({
97
+ replacingMessageId: payloadBundle.content.originalMessageId,
98
+ text: MessageToProtoMapper_1.MessageToProtoMapper.mapText(payloadBundle),
99
+ });
100
+ return protocol_messaging_1.GenericMessage.create({
101
+ [GenericMessageType_1.GenericMessageType.EDITED]: editedMessage,
102
+ messageId: payloadBundle.id,
103
+ });
104
+ }
105
+ function generateFileDataGenericMessage(payloadBundle, messageTimer) {
106
+ if (!payloadBundle.content) {
107
+ throw new Error('No content for sendFileData provided.');
108
+ }
109
+ const { asset, expectsReadConfirmation, legalHoldStatus } = payloadBundle.content;
110
+ const remoteData = protocol_messaging_1.Asset.RemoteData.create({
111
+ assetId: asset.key,
112
+ assetToken: asset.token,
113
+ otrKey: asset.keyBytes,
114
+ sha256: asset.sha256,
115
+ assetDomain: asset.domain,
116
+ });
117
+ const assetMessage = protocol_messaging_1.Asset.create({
118
+ expectsReadConfirmation,
119
+ legalHoldStatus,
120
+ uploaded: remoteData,
121
+ });
122
+ assetMessage.status = AssetTransferState_1.AssetTransferState.UPLOADED;
123
+ const genericMessage = protocol_messaging_1.GenericMessage.create({
124
+ [GenericMessageType_1.GenericMessageType.ASSET]: assetMessage,
125
+ messageId: payloadBundle.id,
126
+ });
127
+ const expireAfterMillis = messageTimer.getMessageTimer(payloadBundle.conversation);
128
+ return expireAfterMillis > 0 ? createEphemeral(genericMessage, expireAfterMillis) : genericMessage;
129
+ }
130
+ function generateFileMetaDataGenericMessage(payloadBundle, messageTimer) {
131
+ if (!payloadBundle.content) {
132
+ throw new Error('No content for sendFileMetaData provided.');
133
+ }
134
+ const { expectsReadConfirmation, legalHoldStatus, metaData } = payloadBundle.content;
135
+ const original = protocol_messaging_1.Asset.Original.create({
136
+ audio: metaData.audio,
137
+ mimeType: metaData.type,
138
+ name: metaData.name,
139
+ size: metaData.length,
140
+ video: metaData.video,
141
+ image: metaData.image,
142
+ });
143
+ const assetMessage = protocol_messaging_1.Asset.create({
144
+ expectsReadConfirmation,
145
+ legalHoldStatus,
146
+ original,
147
+ });
148
+ const genericMessage = protocol_messaging_1.GenericMessage.create({
149
+ [GenericMessageType_1.GenericMessageType.ASSET]: assetMessage,
150
+ messageId: payloadBundle.id,
151
+ });
152
+ const expireAfterMillis = messageTimer.getMessageTimer(payloadBundle.conversation);
153
+ return expireAfterMillis > 0 ? createEphemeral(genericMessage, expireAfterMillis) : genericMessage;
154
+ }
155
+ function generateFileAbortGenericMessage(payloadBundle, messageTimer) {
156
+ if (!payloadBundle.content) {
157
+ throw new Error('No content for sendFileAbort provided.');
158
+ }
159
+ const { expectsReadConfirmation, legalHoldStatus, reason } = payloadBundle.content;
160
+ const assetMessage = protocol_messaging_1.Asset.create({
161
+ expectsReadConfirmation,
162
+ legalHoldStatus,
163
+ notUploaded: reason,
164
+ });
165
+ assetMessage.status = AssetTransferState_1.AssetTransferState.NOT_UPLOADED;
166
+ const genericMessage = protocol_messaging_1.GenericMessage.create({
167
+ [GenericMessageType_1.GenericMessageType.ASSET]: assetMessage,
168
+ messageId: payloadBundle.id,
169
+ });
170
+ const expireAfterMillis = messageTimer.getMessageTimer(payloadBundle.conversation);
171
+ return expireAfterMillis > 0 ? createEphemeral(genericMessage, expireAfterMillis) : genericMessage;
172
+ }
173
+ function generateImageGenericMessage(payloadBundle, messageTimer) {
174
+ const imageAsset = generateAsset(payloadBundle);
175
+ let genericMessage = protocol_messaging_1.GenericMessage.create({
176
+ [GenericMessageType_1.GenericMessageType.ASSET]: imageAsset,
177
+ messageId: payloadBundle.id,
178
+ });
179
+ const expireAfterMillis = messageTimer.getMessageTimer(payloadBundle.conversation);
180
+ if (expireAfterMillis) {
181
+ genericMessage = createEphemeral(genericMessage, expireAfterMillis);
182
+ }
183
+ return { genericMessage, content: imageAsset };
184
+ }
185
+ function generateLocationGenericMessage(payloadBundle, messageTimer) {
186
+ const { expectsReadConfirmation, latitude, legalHoldStatus, longitude, name, zoom } = payloadBundle.content;
187
+ const locationMessage = protocol_messaging_1.Location.create({
188
+ expectsReadConfirmation,
189
+ latitude,
190
+ legalHoldStatus,
191
+ longitude,
192
+ name,
193
+ zoom,
194
+ });
195
+ const genericMessage = protocol_messaging_1.GenericMessage.create({
196
+ [GenericMessageType_1.GenericMessageType.LOCATION]: locationMessage,
197
+ messageId: payloadBundle.id,
198
+ });
199
+ const expireAfterMillis = messageTimer.getMessageTimer(payloadBundle.conversation);
200
+ return expireAfterMillis > 0 ? createEphemeral(genericMessage, expireAfterMillis) : genericMessage;
201
+ }
202
+ function generatePingGenericMessage(payloadBundle, messageTimer) {
203
+ const content = protocol_messaging_1.Knock.create(payloadBundle.content);
204
+ const genericMessage = protocol_messaging_1.GenericMessage.create({
205
+ [GenericMessageType_1.GenericMessageType.KNOCK]: content,
206
+ messageId: payloadBundle.id,
207
+ });
208
+ const expireAfterMillis = messageTimer.getMessageTimer(payloadBundle.conversation);
209
+ return expireAfterMillis > 0 ? createEphemeral(genericMessage, expireAfterMillis) : genericMessage;
210
+ }
211
+ function generateReactionGenericMessage(payloadBundle) {
212
+ const { legalHoldStatus, originalMessageId, type } = payloadBundle.content;
213
+ const reaction = protocol_messaging_1.Reaction.create({
214
+ emoji: type,
215
+ legalHoldStatus,
216
+ messageId: originalMessageId,
217
+ });
218
+ const genericMessage = protocol_messaging_1.GenericMessage.create({
219
+ [GenericMessageType_1.GenericMessageType.REACTION]: reaction,
220
+ messageId: payloadBundle.id,
221
+ });
222
+ return genericMessage;
223
+ }
224
+ function generateSessionResetGenericMessage(payload) {
225
+ if (payload.content.clientAction !== protocol_messaging_1.ClientAction.RESET_SESSION) {
226
+ throw new Error(`No send method implemented for "${payload.type}" and ClientAction "${payload.content}".`);
227
+ }
228
+ return protocol_messaging_1.GenericMessage.create({
229
+ [GenericMessageType_1.GenericMessageType.CLIENT_ACTION]: protocol_messaging_1.ClientAction.RESET_SESSION,
230
+ messageId: payload.id,
231
+ });
232
+ }
233
+ function generateCallGenericMessage(payloadBundle) {
234
+ const callMessage = protocol_messaging_1.Calling.create({
235
+ content: payloadBundle.content,
236
+ });
237
+ return protocol_messaging_1.GenericMessage.create({
238
+ [GenericMessageType_1.GenericMessageType.CALLING]: callMessage,
239
+ messageId: payloadBundle.id,
240
+ });
241
+ }
242
+ function generateDeleteMessage(payload) {
243
+ const content = protocol_messaging_1.MessageDelete.create({
244
+ messageId: payload.content.messageId,
245
+ });
246
+ return protocol_messaging_1.GenericMessage.create({
247
+ [GenericMessageType_1.GenericMessageType.DELETED]: content,
248
+ messageId: payload.id,
249
+ });
250
+ }
251
+ function generateTextGenericMessage(payloadBundle, messageTimer) {
252
+ const genericMessage = protocol_messaging_1.GenericMessage.create({
253
+ messageId: payloadBundle.id,
254
+ [GenericMessageType_1.GenericMessageType.TEXT]: MessageToProtoMapper_1.MessageToProtoMapper.mapText(payloadBundle),
255
+ });
256
+ const expireAfterMillis = messageTimer.getMessageTimer(payloadBundle.conversation);
257
+ return expireAfterMillis > 0 ? createEphemeral(genericMessage, expireAfterMillis) : genericMessage;
258
+ }
259
+ function generateAsset(payloadBundle) {
260
+ if (!payloadBundle.content) {
261
+ throw new Error('No content for sendImage provided.');
262
+ }
263
+ const { asset, expectsReadConfirmation, image, legalHoldStatus } = payloadBundle.content;
264
+ const imageMetadata = protocol_messaging_1.Asset.ImageMetaData.create({
265
+ height: image.height,
266
+ width: image.width,
267
+ });
268
+ const original = protocol_messaging_1.Asset.Original.create({
269
+ [GenericMessageType_1.GenericMessageType.IMAGE]: imageMetadata,
270
+ mimeType: image.type,
271
+ name: null,
272
+ size: image.data.length,
273
+ });
274
+ const remoteData = protocol_messaging_1.Asset.RemoteData.create({
275
+ assetId: asset.key,
276
+ assetToken: asset.token,
277
+ assetDomain: asset.domain,
278
+ otrKey: asset.keyBytes,
279
+ sha256: asset.sha256,
280
+ });
281
+ const assetMessage = protocol_messaging_1.Asset.create({
282
+ expectsReadConfirmation,
283
+ legalHoldStatus,
284
+ original,
285
+ uploaded: remoteData,
286
+ });
287
+ assetMessage.status = AssetTransferState_1.AssetTransferState.UPLOADED;
288
+ return assetMessage;
289
+ }
290
+ function createEphemeral(originalGenericMessage, expireAfterMillis) {
291
+ const ephemeralMessage = protocol_messaging_1.Ephemeral.create({
292
+ expireAfterMillis,
293
+ [originalGenericMessage.content]: originalGenericMessage[originalGenericMessage.content],
294
+ });
295
+ const genericMessage = protocol_messaging_1.GenericMessage.create({
296
+ [GenericMessageType_1.GenericMessageType.EPHEMERAL]: ephemeralMessage,
297
+ messageId: originalGenericMessage.messageId,
298
+ });
299
+ return genericMessage;
300
+ }
301
+ //# sourceMappingURL=messageGenerator.js.map