@twilio/conversations 3.0.0-rc.9 → 3.0.1-rc.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 (121) hide show
  1. package/README.md +39 -29
  2. package/builds/browser.esm.js +10846 -0
  3. package/builds/browser.esm.js.map +1 -0
  4. package/builds/browser.js +2505 -479
  5. package/builds/browser.js.map +1 -1
  6. package/builds/lib.d.ts +805 -70
  7. package/builds/lib.esm.d.ts +3178 -0
  8. package/builds/lib.esm.js +10845 -0
  9. package/builds/lib.js +2505 -479
  10. package/builds/lib.js.map +1 -1
  11. package/builds/twilio-conversations.js +25853 -24412
  12. package/builds/twilio-conversations.js.map +1 -0
  13. package/builds/twilio-conversations.min.js +2 -1
  14. package/builds/twilio-conversations.min.js.map +1 -0
  15. package/dist/aggregated-delivery-receipt.js +10 -5
  16. package/dist/aggregated-delivery-receipt.js.map +1 -1
  17. package/dist/channel-metadata-client.js +187 -0
  18. package/dist/channel-metadata-client.js.map +1 -0
  19. package/dist/client.js +266 -192
  20. package/dist/client.js.map +1 -1
  21. package/dist/command-executor.js +53 -15
  22. package/dist/command-executor.js.map +1 -1
  23. package/dist/configuration.js +23 -12
  24. package/dist/configuration.js.map +1 -1
  25. package/dist/content-client.js +180 -0
  26. package/dist/content-client.js.map +1 -0
  27. package/dist/content-template.js +304 -0
  28. package/dist/content-template.js.map +1 -0
  29. package/dist/conversation.js +213 -134
  30. package/dist/conversation.js.map +1 -1
  31. package/dist/data/conversations.js +70 -50
  32. package/dist/data/conversations.js.map +1 -1
  33. package/dist/data/messages.js +40 -27
  34. package/dist/data/messages.js.map +1 -1
  35. package/dist/data/participants.js +38 -32
  36. package/dist/data/participants.js.map +1 -1
  37. package/dist/data/users.js +19 -14
  38. package/dist/data/users.js.map +1 -1
  39. package/dist/detailed-delivery-receipt.js +9 -4
  40. package/dist/detailed-delivery-receipt.js.map +1 -1
  41. package/dist/index.js +26 -41
  42. package/dist/index.js.map +1 -1
  43. package/dist/interfaces/notification-types.js +9 -4
  44. package/dist/interfaces/notification-types.js.map +1 -1
  45. package/dist/interfaces/rules.js +160 -0
  46. package/dist/interfaces/rules.js.map +1 -0
  47. package/dist/logger.js +17 -28
  48. package/dist/logger.js.map +1 -1
  49. package/dist/media.js +40 -24
  50. package/dist/media.js.map +1 -1
  51. package/dist/message-builder.js +79 -12
  52. package/dist/message-builder.js.map +1 -1
  53. package/dist/message-recipients-client.js +231 -0
  54. package/dist/message-recipients-client.js.map +1 -0
  55. package/dist/message.js +258 -92
  56. package/dist/message.js.map +1 -1
  57. package/dist/node_modules/quick-lru/index.js +261 -0
  58. package/dist/node_modules/quick-lru/index.js.map +1 -0
  59. package/dist/node_modules/tslib/tslib.es6.js +15 -5
  60. package/dist/node_modules/tslib/tslib.es6.js.map +1 -1
  61. package/dist/packages/conversations/package.json.js +12 -7
  62. package/dist/packages/conversations/package.json.js.map +1 -1
  63. package/dist/participant.js +42 -38
  64. package/dist/participant.js.map +1 -1
  65. package/dist/push-notification.js +9 -4
  66. package/dist/push-notification.js.map +1 -1
  67. package/dist/rest-paginator.js +9 -4
  68. package/dist/rest-paginator.js.map +1 -1
  69. package/dist/services/network.js +11 -6
  70. package/dist/services/network.js.map +1 -1
  71. package/dist/services/typing-indicator.js +17 -11
  72. package/dist/services/typing-indicator.js.map +1 -1
  73. package/dist/unsent-message.js +14 -9
  74. package/dist/unsent-message.js.map +1 -1
  75. package/dist/user.js +40 -32
  76. package/dist/user.js.map +1 -1
  77. package/dist/util/deferred.js +9 -4
  78. package/dist/util/deferred.js.map +1 -1
  79. package/dist/util/index.js +44 -8
  80. package/dist/util/index.js.map +1 -1
  81. package/dist/{interfaces/attributes.js → utils/uuid.js} +17 -19
  82. package/dist/utils/uuid.js.map +1 -0
  83. package/package.json +21 -20
  84. package/CHANGELOG.md +0 -509
  85. package/dist/interfaces/attributes.js.map +0 -1
  86. package/docs/assets/css/main.css +0 -2660
  87. package/docs/assets/images/icons.png +0 -0
  88. package/docs/assets/images/icons@2x.png +0 -0
  89. package/docs/assets/images/widgets.png +0 -0
  90. package/docs/assets/images/widgets@2x.png +0 -0
  91. package/docs/assets/js/main.js +0 -248
  92. package/docs/assets/js/search.js +0 -1
  93. package/docs/classes/AggregatedDeliveryReceipt.html +0 -3184
  94. package/docs/classes/CancellablePromise.html +0 -3213
  95. package/docs/classes/Client.html +0 -4239
  96. package/docs/classes/Conversation.html +0 -4354
  97. package/docs/classes/DetailedDeliveryReceipt.html +0 -3163
  98. package/docs/classes/Media.html +0 -3167
  99. package/docs/classes/Message.html +0 -3732
  100. package/docs/classes/MessageBuilder.html +0 -3277
  101. package/docs/classes/Participant.html +0 -3444
  102. package/docs/classes/PushNotification.html +0 -3130
  103. package/docs/classes/RestPaginator.html +0 -3160
  104. package/docs/classes/UnsentMessage.html +0 -3042
  105. package/docs/classes/User.html +0 -3349
  106. package/docs/index.html +0 -3509
  107. package/docs/interfaces/ClientOptions.html +0 -3034
  108. package/docs/interfaces/ConversationBindings.html +0 -3001
  109. package/docs/interfaces/ConversationEmailBinding.html +0 -3001
  110. package/docs/interfaces/ConversationLimits.html +0 -3098
  111. package/docs/interfaces/ConversationState.html +0 -3050
  112. package/docs/interfaces/ConversationUpdatedEventArgs.html +0 -3001
  113. package/docs/interfaces/CreateConversationOptions.html +0 -3066
  114. package/docs/interfaces/LastMessage.html +0 -3050
  115. package/docs/interfaces/Paginator.html +0 -3141
  116. package/docs/interfaces/ParticipantBindings.html +0 -3001
  117. package/docs/interfaces/ParticipantEmailBinding.html +0 -3001
  118. package/docs/interfaces/PushNotificationData.html +0 -3114
  119. package/docs/interfaces/SendEmailOptions.html +0 -3034
  120. package/docs/interfaces/SendMediaOptions.html +0 -3068
  121. package/docs/modules.html +0 -3510
package/dist/message.js CHANGED
@@ -126,68 +126,70 @@ This software includes platform.js under the following license.
126
126
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
127
127
 
128
128
  */
129
- 'use strict';
130
-
131
- Object.defineProperty(exports, '__esModule', { value: true });
132
-
133
- var tslib_es6 = require('./node_modules/tslib/tslib.es6.js');
134
- var index = require('./util/index.js');
135
- var logger = require('./logger.js');
136
- var mcsClient = require('@twilio/mcs-client');
137
- var media = require('./media.js');
138
- var aggregatedDeliveryReceipt = require('./aggregated-delivery-receipt.js');
139
- var declarativeTypeValidator = require('@twilio/declarative-type-validator');
140
- var attributes = require('./interfaces/attributes.js');
141
- var restPaginator = require('./rest-paginator.js');
142
- var detailedDeliveryReceipt = require('./detailed-delivery-receipt.js');
143
- var replayEventEmitter = require('@twilio/replay-event-emitter');
144
- var isEqual = require('lodash.isequal');
145
-
146
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
147
-
148
- var isEqual__default = /*#__PURE__*/_interopDefaultLegacy(isEqual);
149
-
150
- const log = logger.Logger.scope("Message");
129
+ var global =
130
+ typeof global !== "undefined"
131
+ ? global
132
+ : typeof self !== "undefined"
133
+ ? self
134
+ : typeof window !== "undefined"
135
+ ? window
136
+ : {};
137
+
138
+ import { __decorate, __metadata, __classPrivateFieldGet } from './node_modules/tslib/tslib.es6.js';
139
+ import { parseAttributes, isEqual, UriBuilder } from './util/index.js';
140
+ import { Logger } from './logger.js';
141
+ import { CancellablePromise } from '@twilio/mcs-client';
142
+ import { Media } from './media.js';
143
+ import { AggregatedDeliveryReceipt } from './aggregated-delivery-receipt.js';
144
+ import { validateTypes, nonEmptyString, validateTypesAsync, nonEmptyArray } from '@twilio/declarative-type-validator';
145
+ import { json } from './interfaces/rules.js';
146
+ import { RestPaginator } from './rest-paginator.js';
147
+ import { DetailedDeliveryReceipt } from './detailed-delivery-receipt.js';
148
+ import { ReplayEventEmitter } from '@twilio/replay-event-emitter';
149
+ import { deprecated } from '@twilio/deprecation-decorator';
150
+ import { parseVariant } from './content-template.js';
151
+
152
+ var _Message_instances, _Message_wrapMedia, _Message_assignMedias;
153
+ const log = Logger.scope("Message");
154
+ const XHR =
155
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
156
+ global["XMLHttpRequest"] || {};
151
157
  /**
152
158
  * A message in a conversation.
153
159
  */
154
- class Message extends replayEventEmitter.ReplayEventEmitter {
160
+ class Message extends ReplayEventEmitter {
155
161
  /**
156
162
  * @internal
157
163
  */
158
- constructor(index$1, data, conversation, links, configuration, services) {
159
- var _a, _b, _c, _d;
164
+ constructor(index, data, conversation, links, configuration, services) {
165
+ var _a, _b, _c, _d, _e, _f, _g;
160
166
  super();
167
+ _Message_instances.add(this);
161
168
  this.conversation = conversation;
162
169
  this.links = links;
163
170
  this.configuration = configuration;
164
171
  this.services = services;
165
172
  this.state = {
166
173
  sid: data.sid,
167
- index: index$1,
174
+ index: index,
168
175
  author: data.author,
169
176
  subject: data.subject,
177
+ contentSid: data.contentSid,
170
178
  body: (_a = data.text) !== null && _a !== void 0 ? _a : null,
171
179
  timestamp: data.timestamp ? new Date(data.timestamp) : null,
172
180
  dateUpdated: data.dateUpdated ? new Date(data.dateUpdated) : null,
173
181
  lastUpdatedBy: (_b = data.lastUpdatedBy) !== null && _b !== void 0 ? _b : null,
174
- attributes: index.parseAttributes(data.attributes, `Got malformed attributes for the message ${data.sid}`, log),
182
+ attributes: parseAttributes(data.attributes, `Got malformed attributes for the message ${data.sid}`, log),
175
183
  type: (_c = data.type) !== null && _c !== void 0 ? _c : "text",
176
- media: data.type === "media" && data.media
177
- ? new media.Media(data.media, this.services)
178
- : null,
179
- medias: data.type === "media" && data.medias
180
- ? data.medias.map((m) => new media.Media(m, this.services))
181
- : data.type === "media" && data.media && !data.medias
182
- ? [
183
- new media.Media(Object.assign(Object.assign({}, data.media), { category: "media" }), this.services),
184
- ]
185
- : null,
184
+ media: null,
185
+ medias: null,
186
186
  participantSid: (_d = data.memberSid) !== null && _d !== void 0 ? _d : null,
187
187
  aggregatedDeliveryReceipt: data.delivery
188
- ? new aggregatedDeliveryReceipt.AggregatedDeliveryReceipt(data.delivery)
188
+ ? new AggregatedDeliveryReceipt(data.delivery)
189
189
  : null,
190
+ hasChannelMetadata: (_e = data.channelMetadata) !== null && _e !== void 0 ? _e : false,
190
191
  };
192
+ __classPrivateFieldGet(this, _Message_instances, "m", _Message_assignMedias).call(this, (_f = data.medias) !== null && _f !== void 0 ? _f : null, (_g = data.media) !== null && _g !== void 0 ? _g : null);
191
193
  }
192
194
  /**
193
195
  * The server-assigned unique identifier for the message.
@@ -207,6 +209,12 @@ class Message extends replayEventEmitter.ReplayEventEmitter {
207
209
  get subject() {
208
210
  return this.state.subject;
209
211
  }
212
+ /**
213
+ * Unique identifier of {@link ContentTemplate} for this message.
214
+ */
215
+ get contentSid() {
216
+ return this.state.contentSid;
217
+ }
210
218
  /**
211
219
  * Body of the message.
212
220
  */
@@ -220,14 +228,15 @@ class Message extends replayEventEmitter.ReplayEventEmitter {
220
228
  return this.state.dateUpdated;
221
229
  }
222
230
  /**
223
- * Index of the message in the conversation's messages list.
224
- * By design of the Conversations system, the message indices may have arbitrary gaps between them,
231
+ * Index of this message in the conversation's list of messages.
232
+ *
233
+ * By design, the message indices may have arbitrary gaps between them,
225
234
  * that does not necessarily mean they were deleted or otherwise modified - just that
226
235
  * messages may have some non-contiguous indices even if they are being sent immediately one after another.
227
236
  *
228
237
  * Trying to use indices for some calculations is going to be unreliable.
229
238
  *
230
- * To calculate the number of unread messages it is better to use the read horizon API.
239
+ * To calculate the number of unread messages, it is better to use the Read Horizon API.
231
240
  * See {@link Conversation.getUnreadMessagesCount} for details.
232
241
  */
233
242
  get index() {
@@ -282,6 +291,16 @@ class Message extends replayEventEmitter.ReplayEventEmitter {
282
291
  get aggregatedDeliveryReceipt() {
283
292
  return this.state.aggregatedDeliveryReceipt;
284
293
  }
294
+ /**
295
+ * @deprecated
296
+ * Return a (possibly empty) array of media matching a specific set of categories.
297
+ * Allowed category is so far only 'media'.
298
+ * @param categories Array of categories to match.
299
+ * @returns Array of media descriptors matching given categories.
300
+ */
301
+ getMediaByCategory(categories) {
302
+ return this.getMediaByCategories(categories);
303
+ }
285
304
  /**
286
305
  * Return a (possibly empty) array of media matching a specific set of categories.
287
306
  * Allowed category is so far only 'media'.
@@ -311,6 +330,7 @@ class Message extends replayEventEmitter.ReplayEventEmitter {
311
330
  return ((_b = (_a = this.getMediaByCategories(["history"])) === null || _a === void 0 ? void 0 : _a.filter((m) => m.contentType == type).shift()) !== null && _b !== void 0 ? _b : null);
312
331
  }
313
332
  _update(data) {
333
+ var _a, _b, _c, _d, _e, _f;
314
334
  const updateReasons = [];
315
335
  if ((data.text || typeof data.text === "string") &&
316
336
  data.text !== this.state.body) {
@@ -341,8 +361,8 @@ class Message extends replayEventEmitter.ReplayEventEmitter {
341
361
  this.state.timestamp = new Date(data.timestamp);
342
362
  updateReasons.push("dateCreated");
343
363
  }
344
- const updatedAttributes = index.parseAttributes(data.attributes, `Got malformed attributes for the message ${this.sid}`, log);
345
- if (!isEqual__default["default"](this.state.attributes, updatedAttributes)) {
364
+ const updatedAttributes = parseAttributes(data.attributes, `Got malformed attributes for the message ${this.sid}`, log);
365
+ if (!isEqual(this.state.attributes, updatedAttributes)) {
346
366
  this.state.attributes = updatedAttributes;
347
367
  updateReasons.push("attributes");
348
368
  }
@@ -357,7 +377,7 @@ class Message extends replayEventEmitter.ReplayEventEmitter {
357
377
  !!updatedAggregatedDelivery.undelivered;
358
378
  if (isUpdatedAggregateDeliveryValid) {
359
379
  if (!currentAggregatedDelivery) {
360
- this.state.aggregatedDeliveryReceipt = new aggregatedDeliveryReceipt.AggregatedDeliveryReceipt(updatedAggregatedDelivery);
380
+ this.state.aggregatedDeliveryReceipt = new AggregatedDeliveryReceipt(updatedAggregatedDelivery);
361
381
  updateReasons.push("deliveryReceipt");
362
382
  }
363
383
  else if (!currentAggregatedDelivery._isEquals(updatedAggregatedDelivery)) {
@@ -365,6 +385,19 @@ class Message extends replayEventEmitter.ReplayEventEmitter {
365
385
  updateReasons.push("deliveryReceipt");
366
386
  }
367
387
  }
388
+ // @fixme: This is a fairly heavy check.
389
+ // Map data back and forth to perform a necessary field massage.
390
+ const input = ((_a = data.medias) !== null && _a !== void 0 ? _a : [])
391
+ .map((value) => { var _a; return (_a = __classPrivateFieldGet(this, _Message_instances, "m", _Message_wrapMedia).call(this, value)) === null || _a === void 0 ? void 0 : _a._state(); })
392
+ .filter((el) => !(el === null));
393
+ const current = ((_b = this.state.medias) !== null && _b !== void 0 ? _b : [])
394
+ .map((value) => value._state())
395
+ .filter((el) => !(el === null));
396
+ if (!isEqual(input, current) ||
397
+ !isEqual((_c = __classPrivateFieldGet(this, _Message_instances, "m", _Message_wrapMedia).call(this, data.media)) === null || _c === void 0 ? void 0 : _c._state(), (_d = this.state.media) === null || _d === void 0 ? void 0 : _d._state())) {
398
+ __classPrivateFieldGet(this, _Message_instances, "m", _Message_assignMedias).call(this, (_e = data.medias) !== null && _e !== void 0 ? _e : null, (_f = data.media) !== null && _f !== void 0 ? _f : null);
399
+ updateReasons.push("media");
400
+ }
368
401
  if (updateReasons.length > 0) {
369
402
  this.emit("updated", { message: this, updateReasons: updateReasons });
370
403
  }
@@ -393,37 +426,34 @@ class Message extends replayEventEmitter.ReplayEventEmitter {
393
426
  if (participant) {
394
427
  return participant;
395
428
  }
396
- let errorMesage = "Participant with ";
429
+ let errorMessage = "Participant with ";
397
430
  if (this.state.participantSid) {
398
- errorMesage += "SID '" + this.state.participantSid + "' ";
431
+ errorMessage += "SID '" + this.state.participantSid + "' ";
399
432
  }
400
433
  if (this.state.author) {
401
434
  if (this.state.participantSid) {
402
- errorMesage += "or ";
435
+ errorMessage += "or ";
403
436
  }
404
- errorMesage += "identity '" + this.state.author + "' ";
437
+ errorMessage += "identity '" + this.state.author + "' ";
405
438
  }
406
- if (errorMesage === "Participant with ") {
407
- errorMesage = "Participant ";
439
+ if (errorMessage === "Participant with ") {
440
+ errorMessage = "Participant ";
408
441
  }
409
- errorMesage += "was not found";
410
- throw new Error(errorMesage);
442
+ errorMessage += "was not found";
443
+ throw new Error(errorMessage);
411
444
  }
412
445
  /**
413
446
  * Get the delivery receipts of the message.
414
447
  */
415
448
  async getDetailedDeliveryReceipts() {
416
449
  let paginator = await this._getDetailedDeliveryReceiptsPaginator();
417
- let detailedDeliveryReceipts = [];
418
- while (true) {
450
+ let detailedDeliveryReceipts = paginator.items;
451
+ while (paginator.hasNextPage) {
452
+ paginator = await paginator.nextPage();
419
453
  detailedDeliveryReceipts = [
420
454
  ...detailedDeliveryReceipts,
421
455
  ...paginator.items,
422
456
  ];
423
- if (!paginator.hasNextPage) {
424
- break;
425
- }
426
- paginator = await paginator.nextPage();
427
457
  }
428
458
  return detailedDeliveryReceipts;
429
459
  }
@@ -456,6 +486,24 @@ class Message extends replayEventEmitter.ReplayEventEmitter {
456
486
  });
457
487
  return this;
458
488
  }
489
+ /**
490
+ * @deprecated
491
+ * Get content URLs for all media attachments in the given set using a single operation.
492
+ * @param contentSet Set of media attachments to query content URLs.
493
+ */
494
+ async attachTemporaryUrlsFor(contentSet) {
495
+ // We ignore existing mcsMedia members of each of the media entries.
496
+ // Instead we just collect their sids and pull new descriptors from a mediaSet GET endpoint.
497
+ const sids = contentSet === null || contentSet === void 0 ? void 0 : contentSet.map((m) => m.sid);
498
+ if (this.services.mcsClient && sids) {
499
+ return (await this.services.mcsClient.mediaSetGet(sids)).map((item) => {
500
+ return new Media(item, this.services);
501
+ });
502
+ }
503
+ else {
504
+ throw new Error("Media Content Service is unavailable");
505
+ }
506
+ }
459
507
  /**
460
508
  * Get content URLs for all media attachments in the given set using a single operation.
461
509
  * @param contentSet Set of media attachments to query content URLs.
@@ -471,7 +519,7 @@ class Message extends replayEventEmitter.ReplayEventEmitter {
471
519
  * @param mediaSids Set of media sids to query for the content URL.
472
520
  */
473
521
  getTemporaryContentUrlsForMediaSids(mediaSids) {
474
- return new mcsClient.CancellablePromise(async (resolve, reject, onCancel) => {
522
+ return new CancellablePromise(async (resolve, reject, onCancel) => {
475
523
  const mediaGetRequest = this.services.mcsClient.mediaSetGetContentUrls(mediaSids !== null && mediaSids !== void 0 ? mediaSids : []);
476
524
  if (!this.services.mcsClient || !mediaSids) {
477
525
  reject(new Error("Media Content Service is unavailable"));
@@ -502,14 +550,120 @@ class Message extends replayEventEmitter.ReplayEventEmitter {
502
550
  const messagesReceiptsUrl = this.configuration.links.messagesReceipts
503
551
  .replace("%s", this.conversation.sid)
504
552
  .replace("%s", this.sid);
505
- const url = new index.UriBuilder(messagesReceiptsUrl)
553
+ const url = new UriBuilder(messagesReceiptsUrl)
506
554
  .arg("PageToken", options === null || options === void 0 ? void 0 : options.pageToken)
507
555
  .arg("PageSize", options === null || options === void 0 ? void 0 : options.pageSize)
508
556
  .build();
509
557
  const response = await this.services.network.get(url);
510
- return new restPaginator.RestPaginator(response.body.delivery_receipts.map((x) => new detailedDeliveryReceipt.DetailedDeliveryReceipt(x)), (pageToken, pageSize) => this._getDetailedDeliveryReceiptsPaginator({ pageToken, pageSize }), response.body.meta.previous_token, response.body.meta.next_token);
558
+ return new RestPaginator(response.body.delivery_receipts.map((x) => new DetailedDeliveryReceipt(x)), (pageToken, pageSize) => this._getDetailedDeliveryReceiptsPaginator({ pageToken, pageSize }), response.body.meta.previous_token, response.body.meta.next_token);
559
+ }
560
+ /**
561
+ * Get the {@link ContentData} for this message. Resolves to `null` when
562
+ * {@link Message.contentSid} is null.
563
+ */
564
+ getContentData() {
565
+ return new CancellablePromise(async (resolve, reject, onCancel) => {
566
+ if (this.state.contentSid === null) {
567
+ resolve(null);
568
+ return;
569
+ }
570
+ const bodies = this.getMediaByCategories(["body"]);
571
+ if (bodies === null) {
572
+ resolve(null);
573
+ return;
574
+ }
575
+ const twilioPrefix = "application/x-vnd.com.twilio.rich.";
576
+ const filteredMedias = bodies.filter((media) => media.contentType.startsWith(twilioPrefix));
577
+ if (filteredMedias.length === 0) {
578
+ resolve(null);
579
+ return;
580
+ }
581
+ const contentMedia = filteredMedias[0];
582
+ const urlPromise = contentMedia.getContentTemporaryUrl();
583
+ onCancel(() => {
584
+ urlPromise.cancel();
585
+ });
586
+ let url;
587
+ try {
588
+ url = await urlPromise;
589
+ }
590
+ catch (e) {
591
+ reject(e);
592
+ return;
593
+ }
594
+ if (url === null) {
595
+ resolve(null);
596
+ return;
597
+ }
598
+ const jsonStringPromise = new Promise((resolve, reject) => {
599
+ let isCancelled = false;
600
+ const xhr = new XHR();
601
+ xhr.open("GET", url !== null && url !== void 0 ? url : "", true);
602
+ xhr.responseType = "text";
603
+ xhr.onreadystatechange = () => {
604
+ if (xhr.readyState !== 4 || isCancelled) {
605
+ return;
606
+ }
607
+ resolve(xhr.responseText);
608
+ };
609
+ xhr.onerror = () => {
610
+ reject(xhr.statusText);
611
+ };
612
+ onCancel(() => {
613
+ isCancelled = true;
614
+ xhr.abort();
615
+ reject(new Error("XHR has been aborted"));
616
+ });
617
+ xhr.send();
618
+ });
619
+ let json;
620
+ try {
621
+ const jsonString = await jsonStringPromise;
622
+ json = JSON.parse(jsonString);
623
+ }
624
+ catch (e) {
625
+ reject(e);
626
+ return;
627
+ }
628
+ const dataType = contentMedia.contentType
629
+ .replace(twilioPrefix, "")
630
+ .replace(".", "/");
631
+ resolve(parseVariant(dataType, json.data));
632
+ });
633
+ }
634
+ /**
635
+ * Get the {@link ChannelMetadata} for this message. Resolves to `null` if
636
+ * the message doesn't have any channel metadata.
637
+ */
638
+ async getChannelMetadata() {
639
+ if (!this.state.hasChannelMetadata) {
640
+ return null;
641
+ }
642
+ return await this.services.channelMetadataClient.getChannelMetadata(this.conversation.sid, this.sid);
643
+ }
644
+ /**
645
+ * Get recipients of the message.
646
+ */
647
+ async getMessageRecipients() {
648
+ return await this.services.messageRecipientsClient.getRecipientsFromMessage(this.conversation.sid, this.sid);
511
649
  }
512
650
  }
651
+ _Message_instances = new WeakSet(), _Message_wrapMedia = function _Message_wrapMedia(media, category = null) {
652
+ return media
653
+ ? category
654
+ ? new Media(Object.assign(Object.assign({}, media), { category }), this.services)
655
+ : new Media(media, this.services)
656
+ : null;
657
+ }, _Message_assignMedias = function _Message_assignMedias(medias, media) {
658
+ this.state.media = __classPrivateFieldGet(this, _Message_instances, "m", _Message_wrapMedia).call(this, media);
659
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
660
+ // @ts-ignore
661
+ this.state.medias = medias
662
+ ? medias.map((m) => __classPrivateFieldGet(this, _Message_instances, "m", _Message_wrapMedia).call(this, m)).filter((el) => el !== null)
663
+ : media && !medias
664
+ ? [__classPrivateFieldGet(this, _Message_instances, "m", _Message_wrapMedia).call(this, media, "media")].filter((el) => el !== null)
665
+ : null;
666
+ };
513
667
  /**
514
668
  * Fired when the properties or the body of the message has been updated.
515
669
  *
@@ -519,42 +673,54 @@ class Message extends replayEventEmitter.ReplayEventEmitter {
519
673
  * * {@link MessageUpdateReason}[] updateReasons - array of reasons for the update
520
674
  */
521
675
  Message.updated = "updated";
522
- tslib_es6.__decorate([
523
- declarativeTypeValidator.validateTypes([declarativeTypeValidator.nonEmptyString, "undefined"]),
524
- tslib_es6.__metadata("design:type", Function),
525
- tslib_es6.__metadata("design:paramtypes", [Object]),
526
- tslib_es6.__metadata("design:returntype", media.Media)
676
+ __decorate([
677
+ deprecated("getMediaByCategory", "getMediaByCategories"),
678
+ __metadata("design:type", Function),
679
+ __metadata("design:paramtypes", [Array]),
680
+ __metadata("design:returntype", Array)
681
+ ], Message.prototype, "getMediaByCategory", null);
682
+ __decorate([
683
+ validateTypes([nonEmptyString, "undefined"]),
684
+ __metadata("design:type", Function),
685
+ __metadata("design:paramtypes", [Object]),
686
+ __metadata("design:returntype", Media)
527
687
  ], Message.prototype, "getEmailBody", null);
528
- tslib_es6.__decorate([
529
- declarativeTypeValidator.validateTypes([declarativeTypeValidator.nonEmptyString, "undefined"]),
530
- tslib_es6.__metadata("design:type", Function),
531
- tslib_es6.__metadata("design:paramtypes", [Object]),
532
- tslib_es6.__metadata("design:returntype", media.Media)
688
+ __decorate([
689
+ validateTypes([nonEmptyString, "undefined"]),
690
+ __metadata("design:type", Function),
691
+ __metadata("design:paramtypes", [Object]),
692
+ __metadata("design:returntype", Media)
533
693
  ], Message.prototype, "getEmailHistory", null);
534
- tslib_es6.__decorate([
535
- declarativeTypeValidator.validateTypesAsync("string"),
536
- tslib_es6.__metadata("design:type", Function),
537
- tslib_es6.__metadata("design:paramtypes", [String]),
538
- tslib_es6.__metadata("design:returntype", Promise)
694
+ __decorate([
695
+ validateTypesAsync("string"),
696
+ __metadata("design:type", Function),
697
+ __metadata("design:paramtypes", [String]),
698
+ __metadata("design:returntype", Promise)
539
699
  ], Message.prototype, "updateBody", null);
540
- tslib_es6.__decorate([
541
- declarativeTypeValidator.validateTypesAsync(attributes.attributesValidator),
542
- tslib_es6.__metadata("design:type", Function),
543
- tslib_es6.__metadata("design:paramtypes", [Object]),
544
- tslib_es6.__metadata("design:returntype", Promise)
700
+ __decorate([
701
+ validateTypesAsync(json),
702
+ __metadata("design:type", Function),
703
+ __metadata("design:paramtypes", [Object]),
704
+ __metadata("design:returntype", Promise)
545
705
  ], Message.prototype, "updateAttributes", null);
546
- tslib_es6.__decorate([
547
- declarativeTypeValidator.validateTypesAsync(declarativeTypeValidator.nonEmptyArray("media", media.Media)),
548
- tslib_es6.__metadata("design:type", Function),
549
- tslib_es6.__metadata("design:paramtypes", [Array]),
550
- tslib_es6.__metadata("design:returntype", mcsClient.CancellablePromise)
706
+ __decorate([
707
+ deprecated("attachTemporaryUrlsFor", "getTemporaryContentUrlsForMedia"),
708
+ __metadata("design:type", Function),
709
+ __metadata("design:paramtypes", [Array]),
710
+ __metadata("design:returntype", Promise)
711
+ ], Message.prototype, "attachTemporaryUrlsFor", null);
712
+ __decorate([
713
+ validateTypesAsync(nonEmptyArray("media", Media)),
714
+ __metadata("design:type", Function),
715
+ __metadata("design:paramtypes", [Array]),
716
+ __metadata("design:returntype", CancellablePromise)
551
717
  ], Message.prototype, "getTemporaryContentUrlsForMedia", null);
552
- tslib_es6.__decorate([
553
- declarativeTypeValidator.validateTypesAsync(declarativeTypeValidator.nonEmptyArray("strings", "string")),
554
- tslib_es6.__metadata("design:type", Function),
555
- tslib_es6.__metadata("design:paramtypes", [Array]),
556
- tslib_es6.__metadata("design:returntype", mcsClient.CancellablePromise)
718
+ __decorate([
719
+ validateTypesAsync(nonEmptyArray("strings", "string")),
720
+ __metadata("design:type", Function),
721
+ __metadata("design:paramtypes", [Array]),
722
+ __metadata("design:returntype", CancellablePromise)
557
723
  ], Message.prototype, "getTemporaryContentUrlsForMediaSids", null);
558
724
 
559
- exports.Message = Message;
725
+ export { Message };
560
726
  //# sourceMappingURL=message.js.map