@wireapp/core 30.5.1 → 30.5.2

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 (218) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/package.json +3 -3
  3. package/src/main/Account.d.ts +0 -204
  4. package/src/main/Account.js +0 -439
  5. package/src/main/CoreError.d.ts +0 -9
  6. package/src/main/CoreError.js +0 -26
  7. package/src/main/account/AccountService.d.ts +0 -7
  8. package/src/main/account/AccountService.js +0 -33
  9. package/src/main/account/AccountService.js.map +0 -1
  10. package/src/main/account/index.d.ts +0 -1
  11. package/src/main/account/index.js +0 -32
  12. package/src/main/account/index.js.map +0 -1
  13. package/src/main/auth/LoginSanitizer.d.ts +0 -5
  14. package/src/main/auth/LoginSanitizer.js +0 -41
  15. package/src/main/auth/index.d.ts +0 -1
  16. package/src/main/auth/index.js +0 -32
  17. package/src/main/broadcast/AvailabilityType.d.ts +0 -2
  18. package/src/main/broadcast/AvailabilityType.js +0 -21
  19. package/src/main/broadcast/BroadcastService.d.ts +0 -24
  20. package/src/main/broadcast/BroadcastService.js +0 -74
  21. package/src/main/broadcast/index.d.ts +0 -2
  22. package/src/main/broadcast/index.js +0 -33
  23. package/src/main/client/ClientBackendRepository.d.ts +0 -11
  24. package/src/main/client/ClientBackendRepository.js +0 -43
  25. package/src/main/client/ClientDatabaseRepository.d.ts +0 -27
  26. package/src/main/client/ClientDatabaseRepository.js +0 -85
  27. package/src/main/client/ClientInfo.d.ts +0 -8
  28. package/src/main/client/ClientInfo.js +0 -21
  29. package/src/main/client/ClientService.d.ts +0 -48
  30. package/src/main/client/ClientService.js +0 -118
  31. package/src/main/client/index.d.ts +0 -4
  32. package/src/main/client/index.js +0 -35
  33. package/src/main/connection/ConnectionService.d.ts +0 -11
  34. package/src/main/connection/ConnectionService.js +0 -45
  35. package/src/main/connection/index.d.ts +0 -1
  36. package/src/main/connection/index.js +0 -32
  37. package/src/main/conversation/AbortReason.d.ts +0 -2
  38. package/src/main/conversation/AbortReason.js +0 -21
  39. package/src/main/conversation/AssetService/AssetService.d.ts +0 -62
  40. package/src/main/conversation/AssetService/AssetService.js +0 -88
  41. package/src/main/conversation/AssetService/index.d.ts +0 -1
  42. package/src/main/conversation/AssetService/index.js +0 -32
  43. package/src/main/conversation/AssetTransferState.d.ts +0 -4
  44. package/src/main/conversation/AssetTransferState.js +0 -27
  45. package/src/main/conversation/ClientActionType.d.ts +0 -1
  46. package/src/main/conversation/ClientActionType.js +0 -24
  47. package/src/main/conversation/ConversationMapper/ConversationMapper.d.ts +0 -6
  48. package/src/main/conversation/ConversationMapper/ConversationMapper.js +0 -57
  49. package/src/main/conversation/ConversationMapper/index.d.ts +0 -1
  50. package/src/main/conversation/ConversationMapper/index.js +0 -32
  51. package/src/main/conversation/ConversationService/ConversationService.d.ts +0 -162
  52. package/src/main/conversation/ConversationService/ConversationService.js +0 -900
  53. package/src/main/conversation/ConversationService/ConversationService.types.d.ts +0 -108
  54. package/src/main/conversation/ConversationService/ConversationService.types.js +0 -28
  55. package/src/main/conversation/ConversationService/index.d.ts +0 -2
  56. package/src/main/conversation/ConversationService/index.js +0 -33
  57. package/src/main/conversation/GenericMessageType.d.ts +0 -25
  58. package/src/main/conversation/GenericMessageType.js +0 -49
  59. package/src/main/conversation/MessageTimer/MessageTimer.d.ts +0 -10
  60. package/src/main/conversation/MessageTimer/MessageTimer.js +0 -54
  61. package/src/main/conversation/MessageTimer/index.d.ts +0 -1
  62. package/src/main/conversation/MessageTimer/index.js +0 -32
  63. package/src/main/conversation/ReactionType.d.ts +0 -4
  64. package/src/main/conversation/ReactionType.js +0 -27
  65. package/src/main/conversation/content/AssetContent.d.ts +0 -42
  66. package/src/main/conversation/content/AssetContent.js +0 -21
  67. package/src/main/conversation/content/ButtonActionConfirmationContent.d.ts +0 -2
  68. package/src/main/conversation/content/ButtonActionConfirmationContent.js +0 -21
  69. package/src/main/conversation/content/ButtonActionContent.d.ts +0 -2
  70. package/src/main/conversation/content/ButtonActionContent.js +0 -21
  71. package/src/main/conversation/content/CallingContent.d.ts +0 -1
  72. package/src/main/conversation/content/CallingContent.js +0 -21
  73. package/src/main/conversation/content/ClearedContent.d.ts +0 -2
  74. package/src/main/conversation/content/ClearedContent.js +0 -21
  75. package/src/main/conversation/content/ClientActionContent.d.ts +0 -2
  76. package/src/main/conversation/content/ClientActionContent.js +0 -21
  77. package/src/main/conversation/content/ClientAddContent.d.ts +0 -4
  78. package/src/main/conversation/content/ClientAddContent.js +0 -21
  79. package/src/main/conversation/content/ClientRemoveContent.d.ts +0 -6
  80. package/src/main/conversation/content/ClientRemoveContent.js +0 -21
  81. package/src/main/conversation/content/CompositeContent.d.ts +0 -2
  82. package/src/main/conversation/content/CompositeContent.js +0 -21
  83. package/src/main/conversation/content/ConfirmationContent.d.ts +0 -2
  84. package/src/main/conversation/content/ConfirmationContent.js +0 -21
  85. package/src/main/conversation/content/ContentType.d.ts +0 -21
  86. package/src/main/conversation/content/ContentType.js +0 -94
  87. package/src/main/conversation/content/ConversationContent.d.ts +0 -3
  88. package/src/main/conversation/content/ConversationContent.js +0 -21
  89. package/src/main/conversation/content/DeletedContent.d.ts +0 -2
  90. package/src/main/conversation/content/DeletedContent.js +0 -21
  91. package/src/main/conversation/content/EditedTextContent.d.ts +0 -11
  92. package/src/main/conversation/content/EditedTextContent.js +0 -21
  93. package/src/main/conversation/content/FileContent.d.ts +0 -13
  94. package/src/main/conversation/content/FileContent.js +0 -21
  95. package/src/main/conversation/content/HiddenContent.d.ts +0 -2
  96. package/src/main/conversation/content/HiddenContent.js +0 -21
  97. package/src/main/conversation/content/ImageContent.d.ts +0 -7
  98. package/src/main/conversation/content/ImageContent.js +0 -21
  99. package/src/main/conversation/content/KnockContent.d.ts +0 -2
  100. package/src/main/conversation/content/KnockContent.js +0 -21
  101. package/src/main/conversation/content/LinkPreviewContent.d.ts +0 -10
  102. package/src/main/conversation/content/LinkPreviewContent.js +0 -21
  103. package/src/main/conversation/content/LocationContent.d.ts +0 -9
  104. package/src/main/conversation/content/LocationContent.js +0 -21
  105. package/src/main/conversation/content/MentionContent.d.ts +0 -2
  106. package/src/main/conversation/content/MentionContent.js +0 -21
  107. package/src/main/conversation/content/QuoteContent.d.ts +0 -7
  108. package/src/main/conversation/content/QuoteContent.js +0 -21
  109. package/src/main/conversation/content/ReactionContent.d.ts +0 -7
  110. package/src/main/conversation/content/ReactionContent.js +0 -21
  111. package/src/main/conversation/content/TextContent.d.ts +0 -9
  112. package/src/main/conversation/content/TextContent.js +0 -21
  113. package/src/main/conversation/content/TweetContent.d.ts +0 -2
  114. package/src/main/conversation/content/TweetContent.js +0 -21
  115. package/src/main/conversation/content/index.d.ts +0 -28
  116. package/src/main/conversation/content/index.js +0 -72
  117. package/src/main/conversation/index.d.ts +0 -9
  118. package/src/main/conversation/index.js +0 -40
  119. package/src/main/conversation/message/CompositeContentBuilder.d.ts +0 -14
  120. package/src/main/conversation/message/CompositeContentBuilder.js +0 -54
  121. package/src/main/conversation/message/Message.d.ts +0 -4
  122. package/src/main/conversation/message/Message.js +0 -21
  123. package/src/main/conversation/message/MessageBuilder.d.ts +0 -87
  124. package/src/main/conversation/message/MessageBuilder.js +0 -119
  125. package/src/main/conversation/message/MessageService.d.ts +0 -67
  126. package/src/main/conversation/message/MessageService.js +0 -293
  127. package/src/main/conversation/message/MessageToProtoMapper.d.ts +0 -7
  128. package/src/main/conversation/message/MessageToProtoMapper.js +0 -99
  129. package/src/main/conversation/message/OtrMessage.d.ts +0 -80
  130. package/src/main/conversation/message/OtrMessage.js +0 -21
  131. package/src/main/conversation/message/PayloadBundle.d.ts +0 -76
  132. package/src/main/conversation/message/PayloadBundle.js +0 -81
  133. package/src/main/conversation/message/TeamMessage.d.ts +0 -31
  134. package/src/main/conversation/message/TeamMessage.js +0 -21
  135. package/src/main/conversation/message/TextContentBuilder.d.ts +0 -13
  136. package/src/main/conversation/message/TextContentBuilder.js +0 -75
  137. package/src/main/conversation/message/UserClientsUtil.d.ts +0 -22
  138. package/src/main/conversation/message/UserClientsUtil.js +0 -38
  139. package/src/main/conversation/message/UserMessage.d.ts +0 -43
  140. package/src/main/conversation/message/UserMessage.js +0 -21
  141. package/src/main/cryptography/AssetCryptography/EncryptedAsset.d.ts +0 -11
  142. package/src/main/cryptography/AssetCryptography/EncryptedAsset.js +0 -21
  143. package/src/main/cryptography/AssetCryptography/crypto.browser.d.ts +0 -2
  144. package/src/main/cryptography/AssetCryptography/crypto.browser.js +0 -46
  145. package/src/main/cryptography/AssetCryptography/crypto.node.d.ts +0 -2
  146. package/src/main/cryptography/AssetCryptography/crypto.node.js +0 -68
  147. package/src/main/cryptography/AssetCryptography/index.d.ts +0 -8
  148. package/src/main/cryptography/AssetCryptography/index.js +0 -53
  149. package/src/main/cryptography/AssetCryptography/interfaces.d.ts +0 -9
  150. package/src/main/cryptography/AssetCryptography/interfaces.js +0 -21
  151. package/src/main/cryptography/CryptographyDatabaseRepository.d.ts +0 -17
  152. package/src/main/cryptography/CryptographyDatabaseRepository.js +0 -45
  153. package/src/main/cryptography/CryptographyService.d.ts +0 -52
  154. package/src/main/cryptography/CryptographyService.js +0 -205
  155. package/src/main/cryptography/GenericMessageMapper.d.ts +0 -6
  156. package/src/main/cryptography/GenericMessageMapper.js +0 -160
  157. package/src/main/cryptography/MessageHashService.d.ts +0 -16
  158. package/src/main/cryptography/MessageHashService.js +0 -114
  159. package/src/main/cryptography/SessionPayloadBundle.d.ts +0 -4
  160. package/src/main/cryptography/SessionPayloadBundle.js +0 -21
  161. package/src/main/cryptography/index.d.ts +0 -4
  162. package/src/main/cryptography/index.js +0 -35
  163. package/src/main/giphy/GiphyService.d.ts +0 -9
  164. package/src/main/giphy/GiphyService.js +0 -37
  165. package/src/main/giphy/index.d.ts +0 -1
  166. package/src/main/giphy/index.js +0 -32
  167. package/src/main/index.d.ts +0 -17
  168. package/src/main/index.js +0 -55
  169. package/src/main/linkPreview/LinkPreviewService.d.ts +0 -7
  170. package/src/main/linkPreview/LinkPreviewService.js +0 -52
  171. package/src/main/linkPreview/index.d.ts +0 -1
  172. package/src/main/linkPreview/index.js +0 -32
  173. package/src/main/mls/MLSService/MLSService.d.ts +0 -26
  174. package/src/main/mls/MLSService/MLSService.js +0 -131
  175. package/src/main/mls/index.d.ts +0 -1
  176. package/src/main/mls/index.js +0 -32
  177. package/src/main/mls/types.d.ts +0 -22
  178. package/src/main/mls/types.js +0 -21
  179. package/src/main/notification/NotificationBackendRepository.d.ts +0 -11
  180. package/src/main/notification/NotificationBackendRepository.js +0 -34
  181. package/src/main/notification/NotificationDatabaseRepository.d.ts +0 -66
  182. package/src/main/notification/NotificationDatabaseRepository.js +0 -136
  183. package/src/main/notification/NotificationService.d.ts +0 -131
  184. package/src/main/notification/NotificationService.js +0 -468
  185. package/src/main/notification/index.d.ts +0 -1
  186. package/src/main/notification/index.js +0 -32
  187. package/src/main/notification/types.d.ts +0 -20
  188. package/src/main/notification/types.js +0 -21
  189. package/src/main/self/SelfService.d.ts +0 -13
  190. package/src/main/self/SelfService.js +0 -55
  191. package/src/main/self/index.d.ts +0 -1
  192. package/src/main/self/index.js +0 -32
  193. package/src/main/team/TeamService.d.ts +0 -15
  194. package/src/main/team/TeamService.js +0 -55
  195. package/src/main/team/index.d.ts +0 -1
  196. package/src/main/team/index.js +0 -32
  197. package/src/main/test/CryptographyHelper.d.ts +0 -4
  198. package/src/main/test/CryptographyHelper.js +0 -65
  199. package/src/main/test/PayloadHelper.d.ts +0 -3
  200. package/src/main/test/PayloadHelper.js +0 -66
  201. package/src/main/user/UserMapper.d.ts +0 -5
  202. package/src/main/user/UserMapper.js +0 -88
  203. package/src/main/user/UserService.d.ts +0 -25
  204. package/src/main/user/UserService.js +0 -81
  205. package/src/main/user/index.d.ts +0 -1
  206. package/src/main/user/index.js +0 -32
  207. package/src/main/util/LowPrecisionTaskScheduler/LowPrecisionTaskScheduler.d.ts +0 -21
  208. package/src/main/util/LowPrecisionTaskScheduler/LowPrecisionTaskScheduler.js +0 -62
  209. package/src/main/util/TaskScheduler/TaskScheduler.d.ts +0 -10
  210. package/src/main/util/TaskScheduler/TaskScheduler.js +0 -70
  211. package/src/main/util/TypePredicateUtil.d.ts +0 -7
  212. package/src/main/util/TypePredicateUtil.js +0 -55
  213. package/src/main/util/encryptedStore.d.ts +0 -45
  214. package/src/main/util/encryptedStore.js +0 -116
  215. package/src/main/util/index.d.ts +0 -1
  216. package/src/main/util/index.js +0 -32
  217. package/src/main/util/mapQualifiedUserClientIdsToFullyQualifiedClientIds.d.ts +0 -8
  218. package/src/main/util/mapQualifiedUserClientIdsToFullyQualifiedClientIds.js +0 -32
@@ -1,900 +0,0 @@
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.ConversationService = void 0;
22
- const conversation_1 = require("@wireapp/api-client/src/conversation");
23
- const data_1 = require("@wireapp/api-client/src/conversation/data");
24
- const protocol_messaging_1 = require("@wireapp/protocol-messaging");
25
- const conversation_2 = require("../../conversation/");
26
- const AssetCryptography_1 = require("../../cryptography/AssetCryptography");
27
- const TypePredicateUtil_1 = require("../../util/TypePredicateUtil");
28
- const MessageBuilder_1 = require("../message/MessageBuilder");
29
- const MessageService_1 = require("../message/MessageService");
30
- const MessageToProtoMapper_1 = require("../message/MessageToProtoMapper");
31
- const ConversationService_types_1 = require("./ConversationService.types");
32
- const bazinga64_1 = require("bazinga64");
33
- const mapQualifiedUserClientIdsToFullyQualifiedClientIds_1 = require("../../util/mapQualifiedUserClientIdsToFullyQualifiedClientIds");
34
- const mls_1 = require("../../mls");
35
- class ConversationService {
36
- constructor(apiClient, cryptographyService, config, notificationService, mlsService) {
37
- this.apiClient = apiClient;
38
- this.config = config;
39
- this.notificationService = notificationService;
40
- this.mlsService = mlsService;
41
- this.messageTimer = new conversation_2.MessageTimer();
42
- this.messageService = new MessageService_1.MessageService(this.apiClient, cryptographyService);
43
- }
44
- createEphemeral(originalGenericMessage, expireAfterMillis) {
45
- const ephemeralMessage = protocol_messaging_1.Ephemeral.create({
46
- expireAfterMillis,
47
- [originalGenericMessage.content]: originalGenericMessage[originalGenericMessage.content],
48
- });
49
- const genericMessage = protocol_messaging_1.GenericMessage.create({
50
- [conversation_2.GenericMessageType.EPHEMERAL]: ephemeralMessage,
51
- messageId: originalGenericMessage.messageId,
52
- });
53
- return genericMessage;
54
- }
55
- async getConversationQualifiedMembers(conversationId) {
56
- const conversation = await this.apiClient.api.conversation.getConversation(conversationId);
57
- /*
58
- * If you are sending a message to a conversation, you have to include
59
- * yourself in the list of users if you want to sync a message also to your
60
- * other clients.
61
- */
62
- return conversation.members.others
63
- .filter(member => !!member.qualified_id)
64
- .map(member => member.qualified_id)
65
- .concat(conversation.members.self.qualified_id);
66
- }
67
- /**
68
- * Will generate a prekey bundle for specific users.
69
- * If a QualifiedId array is given the bundle will contain all the clients from those users fetched from the server.
70
- * If a QualifiedUserClients is provided then only the clients in the payload will be targeted (which could generate a ClientMismatch when sending messages)
71
- *
72
- * @param {QualifiedId[]|QualifiedUserClients} userIds - Targeted users.
73
- * @returns {Promise<QualifiedUserPreKeyBundleMap}
74
- */
75
- async getQualifiedPreKeyBundle(userIds) {
76
- let targets = [];
77
- if (userIds) {
78
- if ((0, TypePredicateUtil_1.isQualifiedIdArray)(userIds)) {
79
- targets = userIds.map(id => ({ id }));
80
- }
81
- else {
82
- targets = Object.entries(userIds).reduce((accumulator, [domain, userClients]) => {
83
- for (const userId in userClients) {
84
- accumulator.push({ id: { id: userId, domain }, clients: userClients[userId] });
85
- }
86
- return accumulator;
87
- }, []);
88
- }
89
- }
90
- const preKeys = await Promise.all(targets.map(async ({ id: userId, clients }) => {
91
- const prekeyBundle = await this.apiClient.api.user.getUserPreKeys(userId);
92
- // We filter the clients that should not receive the message (if a QualifiedUserClients was given as parameter)
93
- const userClients = clients
94
- ? prekeyBundle.clients.filter(client => clients.includes(client.client))
95
- : prekeyBundle.clients;
96
- return { user: userId, clients: userClients };
97
- }));
98
- return preKeys.reduce((bundleMap, qualifiedPrekey) => {
99
- var _a, _b, _c;
100
- bundleMap[_a = qualifiedPrekey.user.domain] || (bundleMap[_a] = {});
101
- for (const client of qualifiedPrekey.clients) {
102
- (_b = bundleMap[qualifiedPrekey.user.domain])[_c = qualifiedPrekey.user.id] || (_b[_c] = {});
103
- bundleMap[qualifiedPrekey.user.domain][qualifiedPrekey.user.id][client.client] = client.prekey;
104
- }
105
- return bundleMap;
106
- }, {});
107
- }
108
- async getPreKeyBundleMap(conversationId, userIds) {
109
- let members = [];
110
- if (userIds) {
111
- if ((0, TypePredicateUtil_1.isStringArray)(userIds)) {
112
- members = userIds;
113
- }
114
- else if ((0, TypePredicateUtil_1.isUserClients)(userIds)) {
115
- members = Object.keys(userIds);
116
- }
117
- }
118
- if (!members.length) {
119
- const conversation = await this.apiClient.api.conversation.getConversation(conversationId);
120
- /*
121
- * If you are sending a message to a conversation, you have to include
122
- * yourself in the list of users if you want to sync a message also to your
123
- * other clients.
124
- */
125
- members = conversation.members.others.map(member => member.id).concat(conversation.members.self.id);
126
- }
127
- const preKeys = await Promise.all(members.map(member => this.apiClient.api.user.getUserPreKeys(member)));
128
- return preKeys.reduce((bundleMap, bundle) => {
129
- const userId = bundle.user;
130
- bundleMap[userId] || (bundleMap[userId] = {});
131
- for (const client of bundle.clients) {
132
- bundleMap[userId][client.client] = client.prekey;
133
- }
134
- return bundleMap;
135
- }, {});
136
- }
137
- async getSelfConversationId() {
138
- if (!this.selfConversationId) {
139
- const { userId } = this.apiClient.context;
140
- const { qualified_id, id } = await this.apiClient.api.conversation.getConversation(userId);
141
- const domain = this.config.useQualifiedIds ? qualified_id.domain : '';
142
- this.selfConversationId = { id, domain };
143
- }
144
- return this.selfConversationId;
145
- }
146
- async getQualifiedRecipientsForConversation(conversationId, userIds) {
147
- if ((0, TypePredicateUtil_1.isQualifiedUserClients)(userIds)) {
148
- return userIds;
149
- }
150
- const recipientIds = userIds || (await this.getConversationQualifiedMembers(conversationId));
151
- return this.getQualifiedPreKeyBundle(recipientIds);
152
- }
153
- async getRecipientsForConversation(conversationId, userIds) {
154
- if ((0, TypePredicateUtil_1.isUserClients)(userIds)) {
155
- return userIds;
156
- }
157
- return this.getPreKeyBundleMap(conversationId, userIds);
158
- }
159
- /**
160
- * Sends a message to a conversation
161
- *
162
- * @param sendingClientId The clientId from which the message is sent
163
- * @param conversationId The conversation in which to send the message
164
- * @param genericMessage The payload of the message to send
165
- * @return Resolves with the message sending status from backend
166
- */
167
- async sendGenericMessage(sendingClientId, conversationId, genericMessage, { conversationDomain, userIds, nativePush, sendAsProtobuf, onClientMismatch, targetMode = ConversationService_types_1.MessageTargetMode.NONE, } = {}) {
168
- const plainText = protocol_messaging_1.GenericMessage.encode(genericMessage).finish();
169
- if (targetMode !== ConversationService_types_1.MessageTargetMode.NONE && !userIds) {
170
- throw new Error('Cannot send targetted message when no userIds are given');
171
- }
172
- if (conversationDomain && this.config.useQualifiedIds) {
173
- if ((0, TypePredicateUtil_1.isStringArray)(userIds) || (0, TypePredicateUtil_1.isUserClients)(userIds)) {
174
- throw new Error('Invalid userIds option for sending to federated backend');
175
- }
176
- const recipients = await this.getQualifiedRecipientsForConversation({ id: conversationId, domain: conversationDomain }, userIds);
177
- let reportMissing;
178
- if (targetMode === ConversationService_types_1.MessageTargetMode.NONE) {
179
- reportMissing = (0, TypePredicateUtil_1.isQualifiedUserClients)(userIds); // we want to check mismatch in case the consumer gave an exact list of users/devices
180
- }
181
- else if (targetMode === ConversationService_types_1.MessageTargetMode.USERS) {
182
- reportMissing = this.extractQualifiedUserIds(userIds);
183
- }
184
- else {
185
- // in case the message is fully targetted at user/client pairs, we do not want to report the missing clients or users at all
186
- reportMissing = false;
187
- }
188
- return this.messageService.sendFederatedMessage(sendingClientId, recipients, plainText, {
189
- conversationId: { id: conversationId, domain: conversationDomain },
190
- nativePush,
191
- reportMissing,
192
- onClientMismatch: mismatch => onClientMismatch === null || onClientMismatch === void 0 ? void 0 : onClientMismatch(mismatch, false),
193
- });
194
- }
195
- if ((0, TypePredicateUtil_1.isQualifiedIdArray)(userIds) || (0, TypePredicateUtil_1.isQualifiedUserClients)(userIds)) {
196
- throw new Error('Invalid userIds option for sending');
197
- }
198
- const recipients = await this.getRecipientsForConversation(conversationId, userIds);
199
- let reportMissing;
200
- if (targetMode === ConversationService_types_1.MessageTargetMode.NONE) {
201
- reportMissing = (0, TypePredicateUtil_1.isUserClients)(userIds); // we want to check mismatch in case the consumer gave an exact list of users/devices
202
- }
203
- else if (targetMode === ConversationService_types_1.MessageTargetMode.USERS) {
204
- reportMissing = this.extractUserIds(userIds);
205
- }
206
- else {
207
- // in case the message is fully targetted at user/client pairs, we do not want to report the missing clients or users at all
208
- reportMissing = false;
209
- }
210
- return this.messageService.sendMessage(sendingClientId, recipients, plainText, {
211
- conversationId,
212
- sendAsProtobuf,
213
- nativePush,
214
- reportMissing,
215
- onClientMismatch: mistmatch => onClientMismatch === null || onClientMismatch === void 0 ? void 0 : onClientMismatch(mistmatch, false),
216
- });
217
- }
218
- extractUserIds(userIds) {
219
- if ((0, TypePredicateUtil_1.isUserClients)(userIds)) {
220
- return Object.keys(userIds);
221
- }
222
- return userIds;
223
- }
224
- extractQualifiedUserIds(userIds) {
225
- if ((0, TypePredicateUtil_1.isQualifiedUserClients)(userIds)) {
226
- return Object.entries(userIds).reduce((ids, [domain, userClients]) => {
227
- return ids.concat(Object.keys(userClients).map(userId => ({ domain, id: userId })));
228
- }, []);
229
- }
230
- return userIds;
231
- }
232
- generateButtonActionGenericMessage(payloadBundle) {
233
- return protocol_messaging_1.GenericMessage.create({
234
- [conversation_2.GenericMessageType.BUTTON_ACTION]: protocol_messaging_1.ButtonAction.create(payloadBundle.content),
235
- messageId: payloadBundle.id,
236
- });
237
- }
238
- generateButtonActionConfirmationGenericMessage(payloadBundle) {
239
- return protocol_messaging_1.GenericMessage.create({
240
- [conversation_2.GenericMessageType.BUTTON_ACTION_CONFIRMATION]: protocol_messaging_1.ButtonActionConfirmation.create(payloadBundle.content),
241
- messageId: payloadBundle.id,
242
- });
243
- }
244
- generateCompositeGenericMessage(payloadBundle) {
245
- return protocol_messaging_1.GenericMessage.create({
246
- [conversation_2.GenericMessageType.COMPOSITE]: protocol_messaging_1.Composite.create(payloadBundle.content),
247
- messageId: payloadBundle.id,
248
- });
249
- }
250
- generateConfirmationGenericMessage(payloadBundle) {
251
- const content = protocol_messaging_1.Confirmation.create(payloadBundle.content);
252
- return protocol_messaging_1.GenericMessage.create({
253
- [conversation_2.GenericMessageType.CONFIRMATION]: content,
254
- messageId: payloadBundle.id,
255
- });
256
- }
257
- generateEditedTextGenericMessage(payloadBundle) {
258
- const editedMessage = protocol_messaging_1.MessageEdit.create({
259
- replacingMessageId: payloadBundle.content.originalMessageId,
260
- text: MessageToProtoMapper_1.MessageToProtoMapper.mapText(payloadBundle),
261
- });
262
- return protocol_messaging_1.GenericMessage.create({
263
- [conversation_2.GenericMessageType.EDITED]: editedMessage,
264
- messageId: payloadBundle.id,
265
- });
266
- }
267
- generateFileDataGenericMessage(payloadBundle) {
268
- if (!payloadBundle.content) {
269
- throw new Error('No content for sendFileData provided.');
270
- }
271
- const { asset, expectsReadConfirmation, legalHoldStatus } = payloadBundle.content;
272
- const remoteData = protocol_messaging_1.Asset.RemoteData.create({
273
- assetId: asset.key,
274
- assetToken: asset.token,
275
- otrKey: asset.keyBytes,
276
- sha256: asset.sha256,
277
- assetDomain: asset.domain,
278
- });
279
- const assetMessage = protocol_messaging_1.Asset.create({
280
- expectsReadConfirmation,
281
- legalHoldStatus,
282
- uploaded: remoteData,
283
- });
284
- assetMessage.status = conversation_2.AssetTransferState.UPLOADED;
285
- const genericMessage = protocol_messaging_1.GenericMessage.create({
286
- [conversation_2.GenericMessageType.ASSET]: assetMessage,
287
- messageId: payloadBundle.id,
288
- });
289
- const expireAfterMillis = this.messageTimer.getMessageTimer(payloadBundle.conversation);
290
- return expireAfterMillis > 0 ? this.createEphemeral(genericMessage, expireAfterMillis) : genericMessage;
291
- }
292
- generateFileMetaDataGenericMessage(payloadBundle) {
293
- if (!payloadBundle.content) {
294
- throw new Error('No content for sendFileMetaData provided.');
295
- }
296
- const { expectsReadConfirmation, legalHoldStatus, metaData } = payloadBundle.content;
297
- const original = protocol_messaging_1.Asset.Original.create({
298
- audio: metaData.audio,
299
- mimeType: metaData.type,
300
- name: metaData.name,
301
- size: metaData.length,
302
- video: metaData.video,
303
- image: metaData.image,
304
- });
305
- const assetMessage = protocol_messaging_1.Asset.create({
306
- expectsReadConfirmation,
307
- legalHoldStatus,
308
- original,
309
- });
310
- const genericMessage = protocol_messaging_1.GenericMessage.create({
311
- [conversation_2.GenericMessageType.ASSET]: assetMessage,
312
- messageId: payloadBundle.id,
313
- });
314
- const expireAfterMillis = this.messageTimer.getMessageTimer(payloadBundle.conversation);
315
- return expireAfterMillis > 0 ? this.createEphemeral(genericMessage, expireAfterMillis) : genericMessage;
316
- }
317
- generateFileAbortGenericMessage(payloadBundle) {
318
- if (!payloadBundle.content) {
319
- throw new Error('No content for sendFileAbort provided.');
320
- }
321
- const { expectsReadConfirmation, legalHoldStatus, reason } = payloadBundle.content;
322
- const assetMessage = protocol_messaging_1.Asset.create({
323
- expectsReadConfirmation,
324
- legalHoldStatus,
325
- notUploaded: reason,
326
- });
327
- assetMessage.status = conversation_2.AssetTransferState.NOT_UPLOADED;
328
- const genericMessage = protocol_messaging_1.GenericMessage.create({
329
- [conversation_2.GenericMessageType.ASSET]: assetMessage,
330
- messageId: payloadBundle.id,
331
- });
332
- const expireAfterMillis = this.messageTimer.getMessageTimer(payloadBundle.conversation);
333
- return expireAfterMillis > 0 ? this.createEphemeral(genericMessage, expireAfterMillis) : genericMessage;
334
- }
335
- generateAsset(payloadBundle) {
336
- if (!payloadBundle.content) {
337
- throw new Error('No content for sendImage provided.');
338
- }
339
- const { asset, expectsReadConfirmation, image, legalHoldStatus } = payloadBundle.content;
340
- const imageMetadata = protocol_messaging_1.Asset.ImageMetaData.create({
341
- height: image.height,
342
- width: image.width,
343
- });
344
- const original = protocol_messaging_1.Asset.Original.create({
345
- [conversation_2.GenericMessageType.IMAGE]: imageMetadata,
346
- mimeType: image.type,
347
- name: null,
348
- size: image.data.length,
349
- });
350
- const remoteData = protocol_messaging_1.Asset.RemoteData.create({
351
- assetId: asset.key,
352
- assetToken: asset.token,
353
- assetDomain: asset.domain,
354
- otrKey: asset.keyBytes,
355
- sha256: asset.sha256,
356
- });
357
- const assetMessage = protocol_messaging_1.Asset.create({
358
- expectsReadConfirmation,
359
- legalHoldStatus,
360
- original,
361
- uploaded: remoteData,
362
- });
363
- assetMessage.status = conversation_2.AssetTransferState.UPLOADED;
364
- return assetMessage;
365
- }
366
- generateImageGenericMessage(payloadBundle) {
367
- const imageAsset = this.generateAsset(payloadBundle);
368
- let genericMessage = protocol_messaging_1.GenericMessage.create({
369
- [conversation_2.GenericMessageType.ASSET]: imageAsset,
370
- messageId: payloadBundle.id,
371
- });
372
- const expireAfterMillis = this.messageTimer.getMessageTimer(payloadBundle.conversation);
373
- if (expireAfterMillis) {
374
- genericMessage = this.createEphemeral(genericMessage, expireAfterMillis);
375
- }
376
- return { genericMessage, content: imageAsset };
377
- }
378
- generateLocationGenericMessage(payloadBundle) {
379
- const { expectsReadConfirmation, latitude, legalHoldStatus, longitude, name, zoom } = payloadBundle.content;
380
- const locationMessage = protocol_messaging_1.Location.create({
381
- expectsReadConfirmation,
382
- latitude,
383
- legalHoldStatus,
384
- longitude,
385
- name,
386
- zoom,
387
- });
388
- const genericMessage = protocol_messaging_1.GenericMessage.create({
389
- [conversation_2.GenericMessageType.LOCATION]: locationMessage,
390
- messageId: payloadBundle.id,
391
- });
392
- const expireAfterMillis = this.messageTimer.getMessageTimer(payloadBundle.conversation);
393
- return expireAfterMillis > 0 ? this.createEphemeral(genericMessage, expireAfterMillis) : genericMessage;
394
- }
395
- generatePingGenericMessage(payloadBundle) {
396
- const content = protocol_messaging_1.Knock.create(payloadBundle.content);
397
- const genericMessage = protocol_messaging_1.GenericMessage.create({
398
- [conversation_2.GenericMessageType.KNOCK]: content,
399
- messageId: payloadBundle.id,
400
- });
401
- const expireAfterMillis = this.messageTimer.getMessageTimer(payloadBundle.conversation);
402
- return expireAfterMillis > 0 ? this.createEphemeral(genericMessage, expireAfterMillis) : genericMessage;
403
- }
404
- generateReactionGenericMessage(payloadBundle) {
405
- const { legalHoldStatus, originalMessageId, type } = payloadBundle.content;
406
- const reaction = protocol_messaging_1.Reaction.create({
407
- emoji: type,
408
- legalHoldStatus,
409
- messageId: originalMessageId,
410
- });
411
- const genericMessage = protocol_messaging_1.GenericMessage.create({
412
- [conversation_2.GenericMessageType.REACTION]: reaction,
413
- messageId: payloadBundle.id,
414
- });
415
- return genericMessage;
416
- }
417
- generateSessionResetGenericMessage(payloadBundle) {
418
- return protocol_messaging_1.GenericMessage.create({
419
- [conversation_2.GenericMessageType.CLIENT_ACTION]: protocol_messaging_1.ClientAction.RESET_SESSION,
420
- messageId: payloadBundle.id,
421
- });
422
- }
423
- generateCallGenericMessage(payloadBundle) {
424
- const callMessage = protocol_messaging_1.Calling.create({
425
- content: payloadBundle.content,
426
- });
427
- return protocol_messaging_1.GenericMessage.create({
428
- [conversation_2.GenericMessageType.CALLING]: callMessage,
429
- messageId: payloadBundle.id,
430
- });
431
- }
432
- generateTextGenericMessage(payloadBundle) {
433
- const genericMessage = protocol_messaging_1.GenericMessage.create({
434
- messageId: payloadBundle.id,
435
- [conversation_2.GenericMessageType.TEXT]: MessageToProtoMapper_1.MessageToProtoMapper.mapText(payloadBundle),
436
- });
437
- const expireAfterMillis = this.messageTimer.getMessageTimer(payloadBundle.conversation);
438
- return expireAfterMillis > 0 ? this.createEphemeral(genericMessage, expireAfterMillis) : genericMessage;
439
- }
440
- async clearConversation(conversationId, timestamp = new Date(), messageId = MessageBuilder_1.MessageBuilder.createId(), sendAsProtobuf) {
441
- if (timestamp instanceof Date) {
442
- timestamp = timestamp.getTime();
443
- }
444
- const content = {
445
- clearedTimestamp: timestamp,
446
- conversationId,
447
- };
448
- const clearedMessage = protocol_messaging_1.Cleared.create(content);
449
- const genericMessage = protocol_messaging_1.GenericMessage.create({
450
- [conversation_2.GenericMessageType.CLEARED]: clearedMessage,
451
- messageId,
452
- });
453
- const { id: selfConversationId, domain } = await this.getSelfConversationId();
454
- await this.sendGenericMessage(this.apiClient.validatedClientId, selfConversationId, genericMessage, {
455
- conversationDomain: domain,
456
- sendAsProtobuf,
457
- });
458
- return {
459
- content,
460
- conversation: conversationId,
461
- from: this.apiClient.context.userId,
462
- id: messageId,
463
- messageTimer: 0,
464
- source: conversation_2.PayloadBundleSource.LOCAL,
465
- state: conversation_2.PayloadBundleState.OUTGOING_SENT,
466
- timestamp: Date.now(),
467
- type: conversation_2.PayloadBundleType.CONVERSATION_CLEAR,
468
- };
469
- }
470
- /**
471
- * Sends a LastRead message to the current user's self conversation.
472
- * This will allow all the user's devices to compute which messages are unread
473
- *
474
- * @param conversationId The conversation which has been read
475
- * @param lastReadTimestamp The timestamp at which the conversation was read
476
- * @param sendingOptions?
477
- * @return Resolves when the message has been sent
478
- */
479
- async sendLastRead(conversationId, lastReadTimestamp, sendingOptions) {
480
- const lastRead = new protocol_messaging_1.LastRead({
481
- conversationId,
482
- lastReadTimestamp,
483
- });
484
- const genericMessage = protocol_messaging_1.GenericMessage.create({
485
- [conversation_2.GenericMessageType.LAST_READ]: lastRead,
486
- messageId: MessageBuilder_1.MessageBuilder.createId(),
487
- });
488
- const { id: selfConversationId, domain: selfConversationDomain } = await this.getSelfConversationId();
489
- return this.sendGenericMessage(this.apiClient.validatedClientId, selfConversationId, genericMessage, Object.assign({ conversationDomain: selfConversationDomain }, sendingOptions));
490
- }
491
- /**
492
- * Syncs all self user's devices with the countly id
493
- *
494
- * @param countlyId The countly id of the current device
495
- * @param sendingOptions?
496
- * @return Resolves when the message has been sent
497
- */
498
- async sendCountlySync(countlyId, sendingOptions) {
499
- const { id: selfConversationId, domain: selfConversationDomain } = await this.getSelfConversationId();
500
- const dataTransfer = new protocol_messaging_1.DataTransfer({
501
- trackingIdentifier: {
502
- identifier: countlyId,
503
- },
504
- });
505
- const genericMessage = new protocol_messaging_1.GenericMessage({
506
- [conversation_2.GenericMessageType.DATA_TRANSFER]: dataTransfer,
507
- messageId: MessageBuilder_1.MessageBuilder.createId(),
508
- });
509
- return this.sendGenericMessage(this.apiClient.validatedClientId, selfConversationId, genericMessage, Object.assign({ conversationDomain: selfConversationDomain }, sendingOptions));
510
- }
511
- /**
512
- * Get a fresh list from backend of clients for all the participants of the conversation.
513
- * @param {string} conversationId
514
- * @param {string} conversationDomain? - If given will send the message to the new qualified endpoint
515
- */
516
- async getAllParticipantsClients(conversationId, conversationDomain) {
517
- const qualifiedMembers = await this.getConversationQualifiedMembers(conversationDomain ? { id: conversationId, domain: conversationDomain } : conversationId);
518
- const allClients = await this.apiClient.api.user.postListClients({ qualified_users: qualifiedMembers });
519
- const qualifiedUserClients = {};
520
- Object.entries(allClients.qualified_user_map).map(([domain, userClientMap]) => Object.entries(userClientMap).map(async ([userId, clients]) => {
521
- qualifiedUserClients[domain] || (qualifiedUserClients[domain] = {});
522
- qualifiedUserClients[domain][userId] = clients.map(client => client.id);
523
- }));
524
- return qualifiedUserClients;
525
- }
526
- async deleteMessageLocal(conversationId, messageIdToHide, sendAsProtobuf, conversationDomain) {
527
- const messageId = MessageBuilder_1.MessageBuilder.createId();
528
- const content = protocol_messaging_1.MessageHide.create({
529
- conversationId,
530
- messageId: messageIdToHide,
531
- });
532
- const genericMessage = protocol_messaging_1.GenericMessage.create({
533
- [conversation_2.GenericMessageType.HIDDEN]: content,
534
- messageId,
535
- });
536
- const { id: selfConversationId } = await this.getSelfConversationId();
537
- await this.sendGenericMessage(this.apiClient.validatedClientId, selfConversationId, genericMessage, {
538
- sendAsProtobuf,
539
- conversationDomain,
540
- });
541
- return {
542
- content,
543
- conversation: conversationId,
544
- from: this.apiClient.context.userId,
545
- id: messageId,
546
- messageTimer: this.messageTimer.getMessageTimer(conversationId),
547
- source: conversation_2.PayloadBundleSource.LOCAL,
548
- state: conversation_2.PayloadBundleState.OUTGOING_SENT,
549
- timestamp: Date.now(),
550
- type: conversation_2.PayloadBundleType.MESSAGE_HIDE,
551
- };
552
- }
553
- async deleteMessageEveryone(conversationId, messageIdToDelete, userIds, sendAsProtobuf, conversationDomain, callbacks) {
554
- var _a, _b;
555
- const messageId = MessageBuilder_1.MessageBuilder.createId();
556
- const content = protocol_messaging_1.MessageDelete.create({
557
- messageId: messageIdToDelete,
558
- });
559
- const genericMessage = protocol_messaging_1.GenericMessage.create({
560
- [conversation_2.GenericMessageType.DELETED]: content,
561
- messageId,
562
- });
563
- (_a = callbacks === null || callbacks === void 0 ? void 0 : callbacks.onStart) === null || _a === void 0 ? void 0 : _a.call(callbacks, genericMessage);
564
- const response = await this.sendGenericMessage(this.apiClient.validatedClientId, conversationId, genericMessage, {
565
- userIds,
566
- sendAsProtobuf,
567
- conversationDomain,
568
- });
569
- (_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);
570
- return {
571
- content,
572
- conversation: conversationId,
573
- from: this.apiClient.context.userId,
574
- id: messageId,
575
- messageTimer: this.messageTimer.getMessageTimer(conversationId),
576
- source: conversation_2.PayloadBundleSource.LOCAL,
577
- state: conversation_2.PayloadBundleState.OUTGOING_SENT,
578
- timestamp: Date.now(),
579
- type: conversation_2.PayloadBundleType.MESSAGE_DELETE,
580
- };
581
- }
582
- leaveConversation(conversationId) {
583
- if (!this.apiClient.context || !this.apiClient.context.userId || !this.apiClient.context.domain) {
584
- throw new Error('Cannot leave conversation without a userId and domain');
585
- }
586
- return this.apiClient.api.conversation.deleteMember(conversationId, {
587
- domain: this.apiClient.context.domain,
588
- id: this.apiClient.context.userId,
589
- });
590
- }
591
- leaveMLSConversation(conversationId) {
592
- return this.leaveConversation(conversationId);
593
- }
594
- /**
595
- * @depricated seems not to be used and is outdated. use leaveConversation instead
596
- */
597
- async leaveConversations(conversationIds) {
598
- if (!conversationIds) {
599
- const conversation = await this.getConversations();
600
- conversationIds = conversation
601
- .filter(conversation => conversation.type === conversation_1.CONVERSATION_TYPE.REGULAR)
602
- .map(conversation => conversation.id);
603
- }
604
- return Promise.all(conversationIds.map(conversationId => { var _a, _b; return this.leaveConversation({ id: conversationId, domain: (_b = (_a = this.apiClient.context) === null || _a === void 0 ? void 0 : _a.domain) !== null && _b !== void 0 ? _b : '' }); }));
605
- }
606
- createProteusConversation(conversationData, otherUserIds) {
607
- let payload;
608
- if (typeof conversationData === 'string') {
609
- const ids = typeof otherUserIds === 'string' ? [otherUserIds] : otherUserIds;
610
- payload = {
611
- name: conversationData,
612
- receipt_mode: null,
613
- users: ids !== null && ids !== void 0 ? ids : [],
614
- };
615
- }
616
- else {
617
- payload = conversationData;
618
- }
619
- return this.apiClient.api.conversation.postConversation(payload);
620
- }
621
- async getConversations(conversationIds) {
622
- if (!conversationIds || !conversationIds.length) {
623
- return this.apiClient.api.conversation.getAllConversations();
624
- }
625
- if (typeof conversationIds === 'string') {
626
- return this.apiClient.api.conversation.getConversation(conversationIds);
627
- }
628
- return this.apiClient.api.conversation.getConversationsByIds(conversationIds);
629
- }
630
- async getAsset({ assetId, assetToken, otrKey, sha256 }) {
631
- const request = this.apiClient.api.asset.getAssetV3(assetId, assetToken);
632
- const encryptedBuffer = (await request.response).buffer;
633
- return (0, AssetCryptography_1.decryptAsset)({
634
- cipherText: new Uint8Array(encryptedBuffer),
635
- keyBytes: otrKey,
636
- sha256: sha256,
637
- });
638
- }
639
- async getUnencryptedAsset(assetId, assetToken) {
640
- const request = await this.apiClient.api.asset.getAssetV3(assetId, assetToken);
641
- return (await request.response).buffer;
642
- }
643
- async addUsersToProteusConversation({ conversationId, qualifiedUserIds }) {
644
- return this.apiClient.api.conversation.postMembers(conversationId, qualifiedUserIds);
645
- }
646
- async removeUserFromProteusConversation(conversationId, userId) {
647
- return this.apiClient.api.conversation.deleteMember(conversationId, userId);
648
- }
649
- async sendProteusMessage(params, genericMessage, content) {
650
- var _a;
651
- const { userIds, sendAsProtobuf, conversationDomain, nativePush, targetMode, payload, onClientMismatch, onSuccess } = params;
652
- const response = await this.sendGenericMessage(this.apiClient.validatedClientId, payload.conversation, genericMessage, {
653
- userIds,
654
- sendAsProtobuf,
655
- conversationDomain,
656
- nativePush,
657
- targetMode,
658
- onClientMismatch,
659
- });
660
- if (!response.errored) {
661
- if (!this.isClearFromMismatch(response)) {
662
- // We warn the consumer that there is a mismatch that did not prevent message sending
663
- await (onClientMismatch === null || onClientMismatch === void 0 ? void 0 : onClientMismatch(response, true));
664
- }
665
- onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(genericMessage, response.time);
666
- }
667
- return Object.assign(Object.assign({}, payload), { content, messageTimer: ((_a = genericMessage.ephemeral) === null || _a === void 0 ? void 0 : _a.expireAfterMillis) || 0, state: response.errored ? conversation_2.PayloadBundleState.CANCELLED : conversation_2.PayloadBundleState.OUTGOING_SENT });
668
- }
669
- /**
670
- * Sends a message to a conversation
671
- * @return resolves with the sent message
672
- */
673
- async send(params) {
674
- function isMLS(params) {
675
- return params.protocol === conversation_1.ConversationProtocol.MLS;
676
- }
677
- const { payload, onStart } = params;
678
- const { genericMessage, content } = this.generateGenericMessage(payload);
679
- if ((await (onStart === null || onStart === void 0 ? void 0 : onStart(genericMessage))) === false) {
680
- // If the onStart call returns false, it means the consumer wants to cancel the message sending
681
- return Object.assign(Object.assign({}, payload), { state: conversation_2.PayloadBundleState.CANCELLED });
682
- }
683
- return isMLS(params)
684
- ? this.sendMLSMessage(params, genericMessage, content)
685
- : this.sendProteusMessage(params, genericMessage, content);
686
- }
687
- sendTypingStart(conversationId) {
688
- return this.apiClient.api.conversation.postTyping(conversationId, { status: data_1.CONVERSATION_TYPING.STARTED });
689
- }
690
- sendTypingStop(conversationId) {
691
- return this.apiClient.api.conversation.postTyping(conversationId, { status: data_1.CONVERSATION_TYPING.STOPPED });
692
- }
693
- setConversationMutedStatus(conversationId, status, muteTimestamp) {
694
- if (typeof muteTimestamp === 'number') {
695
- muteTimestamp = new Date(muteTimestamp);
696
- }
697
- const payload = {
698
- otr_muted_ref: muteTimestamp.toISOString(),
699
- otr_muted_status: status,
700
- };
701
- return this.apiClient.api.conversation.putMembershipProperties(conversationId, payload);
702
- }
703
- toggleArchiveConversation(conversationId, archived, archiveTimestamp = new Date()) {
704
- if (typeof archiveTimestamp === 'number') {
705
- archiveTimestamp = new Date(archiveTimestamp);
706
- }
707
- const payload = {
708
- otr_archived: archived,
709
- otr_archived_ref: archiveTimestamp.toISOString(),
710
- };
711
- return this.apiClient.api.conversation.putMembershipProperties(conversationId, payload);
712
- }
713
- setMemberConversationRole(conversationId, userId, conversationRole) {
714
- return this.apiClient.api.conversation.putOtherMember(userId, conversationId, {
715
- conversation_role: conversationRole,
716
- });
717
- }
718
- isClearFromMismatch(mismatch) {
719
- const hasMissing = Object.keys(mismatch.missing || {}).length > 0;
720
- const hasDeleted = Object.keys(mismatch.deleted || {}).length > 0;
721
- const hasRedundant = Object.keys(mismatch.redundant || {}).length > 0;
722
- const hasFailed = Object.keys(mismatch.failed_to_send || {}).length > 0;
723
- return !hasMissing && !hasDeleted && !hasRedundant && !hasFailed;
724
- }
725
- generateGenericMessage(payload) {
726
- let genericMessage;
727
- const content = payload.content;
728
- switch (payload.type) {
729
- case conversation_2.PayloadBundleType.ASSET:
730
- genericMessage = this.generateFileDataGenericMessage(payload);
731
- return { genericMessage, content };
732
- case conversation_2.PayloadBundleType.ASSET_ABORT:
733
- genericMessage = this.generateFileAbortGenericMessage(payload);
734
- return { genericMessage, content };
735
- case conversation_2.PayloadBundleType.ASSET_META:
736
- genericMessage = this.generateFileMetaDataGenericMessage(payload);
737
- return { genericMessage, content };
738
- case conversation_2.PayloadBundleType.ASSET_IMAGE:
739
- return this.generateImageGenericMessage(payload);
740
- case conversation_2.PayloadBundleType.BUTTON_ACTION:
741
- genericMessage = this.generateButtonActionGenericMessage(payload);
742
- return { genericMessage, content };
743
- case conversation_2.PayloadBundleType.BUTTON_ACTION_CONFIRMATION:
744
- genericMessage = this.generateButtonActionConfirmationGenericMessage(payload);
745
- return { genericMessage, content };
746
- case conversation_2.PayloadBundleType.CALL:
747
- genericMessage = this.generateCallGenericMessage(payload);
748
- return { genericMessage, content };
749
- case conversation_2.PayloadBundleType.CLIENT_ACTION: {
750
- if (payload.content.clientAction !== protocol_messaging_1.ClientAction.RESET_SESSION) {
751
- throw new Error(`No send method implemented for "${payload.type}" and ClientAction "${payload.content}".`);
752
- }
753
- genericMessage = this.generateSessionResetGenericMessage(payload);
754
- return { genericMessage, content };
755
- }
756
- case conversation_2.PayloadBundleType.COMPOSITE:
757
- genericMessage = this.generateCompositeGenericMessage(payload);
758
- return { genericMessage, content };
759
- case conversation_2.PayloadBundleType.CONFIRMATION:
760
- genericMessage = this.generateConfirmationGenericMessage(payload);
761
- return { genericMessage, content };
762
- case conversation_2.PayloadBundleType.LOCATION:
763
- genericMessage = this.generateLocationGenericMessage(payload);
764
- return { genericMessage, content };
765
- case conversation_2.PayloadBundleType.MESSAGE_EDIT:
766
- genericMessage = this.generateEditedTextGenericMessage(payload);
767
- return { genericMessage, content };
768
- case conversation_2.PayloadBundleType.PING:
769
- genericMessage = this.generatePingGenericMessage(payload);
770
- return { genericMessage, content };
771
- case conversation_2.PayloadBundleType.REACTION:
772
- genericMessage = this.generateReactionGenericMessage(payload);
773
- return { genericMessage, content };
774
- case conversation_2.PayloadBundleType.TEXT:
775
- genericMessage = this.generateTextGenericMessage(payload);
776
- return { genericMessage, content };
777
- /**
778
- * ToDo: Create Generic implementation for everything else
779
- */
780
- default:
781
- throw new Error(`No send method implemented for "${payload['type']}".`);
782
- }
783
- }
784
- /**
785
- * ###############################################
786
- * ################ MLS Functions ################
787
- * ###############################################
788
- */
789
- async createMLSConversation(conversationData) {
790
- /**
791
- * @note For creating MLS conversations the users & qualified_users
792
- * field must be empty as backend is not aware which users
793
- * are in a MLS conversation because of the MLS architecture.
794
- */
795
- const newConversation = await this.apiClient.api.conversation.postConversation(Object.assign(Object.assign({}, conversationData), { users: undefined, qualified_users: undefined }));
796
- const { group_id: groupId, qualified_id: qualifiedId } = newConversation;
797
- if (!groupId) {
798
- throw new Error('No group_id found in response which is required for creating MLS conversations.');
799
- }
800
- const groupIdDecodedFromBase64 = bazinga64_1.Decoder.fromBase64(groupId).asBytes;
801
- const { qualified_users: qualifiedUsers = [], selfUserId } = conversationData;
802
- if (!selfUserId) {
803
- throw new Error('You need to pass self user qualified id in order to create an MLS conversation');
804
- }
805
- await this.mlsService.createConversation(groupIdDecodedFromBase64);
806
- const coreCryptoKeyPackagesPayload = await this.mlsService.getKeyPackagesPayload([
807
- {
808
- id: selfUserId.id,
809
- domain: selfUserId.domain,
810
- /**
811
- * we should skip fetching key packages for current self client,
812
- * it's already added by the backend on the group creation time
813
- */
814
- skipOwn: conversationData.creator_client,
815
- },
816
- ...qualifiedUsers,
817
- ]);
818
- const response = await this.mlsService.addUsersToExistingConversation(groupIdDecodedFromBase64, coreCryptoKeyPackagesPayload);
819
- await this.notificationService.saveConversationGroupId(newConversation);
820
- //We store the info when conversation (along with key material) was created, so we will know when to renew it
821
- const groupCreationTimeStamp = new Date().getTime();
822
- await this.notificationService.storeLastKeyMaterialUpdateDate({
823
- previousUpdateDate: groupCreationTimeStamp,
824
- groupId,
825
- });
826
- // We fetch the fresh version of the conversation created on backend with the newly added users
827
- const conversation = await this.getConversations(qualifiedId.id);
828
- return {
829
- events: (response === null || response === void 0 ? void 0 : response.events) || [],
830
- conversation,
831
- };
832
- }
833
- async sendMLSMessage(params, genericMessage, content) {
834
- var _a, _b;
835
- const { groupId, onSuccess, payload } = params;
836
- const groupIdBytes = bazinga64_1.Decoder.fromBase64(groupId).asBytes;
837
- // immediately execute pending commits before sending the message
838
- await this.notificationService.commitPendingProposals({ groupId });
839
- const encrypted = await this.mlsService.encryptMessage(groupIdBytes, protocol_messaging_1.GenericMessage.encode(genericMessage).finish());
840
- try {
841
- const { time = '' } = await this.apiClient.api.conversation.postMlsMessage(encrypted);
842
- onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(genericMessage, (time === null || time === void 0 ? void 0 : time.length) > 0 ? time : new Date().toISOString());
843
- return Object.assign(Object.assign({}, payload), { content, messageTimer: ((_a = genericMessage.ephemeral) === null || _a === void 0 ? void 0 : _a.expireAfterMillis) || 0, state: conversation_2.PayloadBundleState.OUTGOING_SENT });
844
- }
845
- catch (_c) {
846
- return Object.assign(Object.assign({}, payload), { content, messageTimer: ((_b = genericMessage.ephemeral) === null || _b === void 0 ? void 0 : _b.expireAfterMillis) || 0, state: conversation_2.PayloadBundleState.CANCELLED });
847
- }
848
- }
849
- async addUsersToMLSConversation({ qualifiedUserIds, groupId, conversationId, }) {
850
- const groupIdDecodedFromBase64 = bazinga64_1.Decoder.fromBase64(groupId).asBytes;
851
- const coreCryptoKeyPackagesPayload = await this.mlsService.getKeyPackagesPayload([...qualifiedUserIds]);
852
- const response = await this.mlsService.addUsersToExistingConversation(groupIdDecodedFromBase64, coreCryptoKeyPackagesPayload);
853
- const conversation = await this.getConversations(conversationId.id);
854
- return {
855
- events: (response === null || response === void 0 ? void 0 : response.events) || [],
856
- conversation,
857
- };
858
- }
859
- async storeLastKeyMaterialUpdateDateWithCurrentTime(groupId) {
860
- const currentTime = new Date().getTime();
861
- await this.notificationService.storeLastKeyMaterialUpdateDate({ groupId, previousUpdateDate: currentTime });
862
- }
863
- async removeUsersFromMLSConversation({ groupId, conversationId, qualifiedUserIds, }) {
864
- const groupIdDecodedFromBase64 = bazinga64_1.Decoder.fromBase64(groupId).asBytes;
865
- const clientsToRemove = await this.apiClient.api.user.postListClients({ qualified_users: qualifiedUserIds });
866
- const fullyQualifiedClientIds = (0, mapQualifiedUserClientIdsToFullyQualifiedClientIds_1.mapQualifiedUserClientIdsToFullyQualifiedClientIds)(clientsToRemove.qualified_user_map);
867
- const messageResponse = await this.mlsService.removeClientsFromConversation(groupIdDecodedFromBase64, fullyQualifiedClientIds);
868
- //key material gets updated after removing a user from the group, so we can reset last key update time value in the store
869
- await this.storeLastKeyMaterialUpdateDateWithCurrentTime(groupId);
870
- const conversation = await this.getConversations(conversationId.id);
871
- return {
872
- events: (messageResponse === null || messageResponse === void 0 ? void 0 : messageResponse.events) || [],
873
- conversation,
874
- };
875
- }
876
- /**
877
- * Will send an external proposal for the current device to join a specific conversation.
878
- * In order for the external proposal to be sent correctly, the underlying mls conversation needs to be in a non-established state
879
- * @param conversationGroupId The conversation to join
880
- * @param epoch The current epoch of the local conversation
881
- */
882
- async sendExternalJoinProposal(conversationGroupId, epoch) {
883
- const groupIdDecodedFromBase64 = bazinga64_1.Decoder.fromBase64(conversationGroupId).asBytes;
884
- const externalProposal = await this.mlsService.newExternalProposal(0 /* Add */, {
885
- epoch,
886
- conversationId: groupIdDecodedFromBase64,
887
- });
888
- await this.apiClient.api.conversation.postMlsMessage(
889
- //@todo: it's temporary - we wait for core-crypto fix to return the actual Uint8Array instead of regular array
890
- (0, mls_1.optionalToUint8Array)(externalProposal));
891
- //We store the info when user was added (and key material was created), so we will know when to renew it
892
- await this.storeLastKeyMaterialUpdateDateWithCurrentTime(conversationGroupId);
893
- }
894
- async isMLSConversationEstablished(conversationGroupId) {
895
- const groupIdDecodedFromBase64 = bazinga64_1.Decoder.fromBase64(conversationGroupId).asBytes;
896
- return this.mlsService.conversationExists(groupIdDecodedFromBase64);
897
- }
898
- }
899
- exports.ConversationService = ConversationService;
900
- //# sourceMappingURL=ConversationService.js.map