@twilio/conversations 2.1.0 → 3.0.0-canary.13
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.
- package/README.md +8 -4
- package/builds/browser.js +2726 -1984
- package/builds/browser.js.map +1 -1
- package/builds/lib.d.ts +514 -308
- package/builds/lib.js +2726 -1984
- package/builds/lib.js.map +1 -1
- package/builds/twilio-conversations.js +5414 -5035
- package/builds/twilio-conversations.min.js +1 -16
- package/dist/aggregated-delivery-receipt.js.map +1 -1
- package/dist/client.js +429 -284
- package/dist/client.js.map +1 -1
- package/dist/command-executor.js.map +1 -1
- package/dist/configuration.js +2 -2
- package/dist/configuration.js.map +1 -1
- package/dist/conversation.js +403 -370
- package/dist/conversation.js.map +1 -1
- package/dist/data/conversations.js +10 -11
- package/dist/data/conversations.js.map +1 -1
- package/dist/data/messages.js +46 -26
- package/dist/data/messages.js.map +1 -1
- package/dist/data/participants.js +19 -10
- package/dist/data/participants.js.map +1 -1
- package/dist/data/users.js +2 -2
- package/dist/data/users.js.map +1 -1
- package/dist/detailed-delivery-receipt.js.map +1 -1
- package/dist/index.js +6 -4
- package/dist/index.js.map +1 -1
- package/dist/interfaces/attributes.js.map +1 -1
- package/dist/interfaces/notification-types.js.map +1 -1
- package/dist/logger.js +2 -4
- package/dist/logger.js.map +1 -1
- package/dist/media.js +36 -25
- package/dist/media.js.map +1 -1
- package/dist/message-builder.js +24 -18
- package/dist/message-builder.js.map +1 -1
- package/dist/message.js +60 -29
- package/dist/message.js.map +1 -1
- package/dist/node_modules/tslib/tslib.es6.js +1 -1
- package/dist/node_modules/tslib/tslib.es6.js.map +1 -1
- package/dist/packages/conversations/package.json.js +1 -1
- package/dist/participant.js +2 -2
- package/dist/participant.js.map +1 -1
- package/dist/push-notification.js.map +1 -1
- package/dist/rest-paginator.js.map +1 -1
- package/dist/services/network.js.map +1 -1
- package/dist/services/typing-indicator.js +1 -1
- package/dist/services/typing-indicator.js.map +1 -1
- package/dist/unsent-message.js +13 -3
- package/dist/unsent-message.js.map +1 -1
- package/dist/user.js +1 -1
- package/dist/user.js.map +1 -1
- package/dist/util/deferred.js.map +1 -1
- package/dist/util/index.js.map +1 -1
- package/docs/assets/js/search.js +1 -1
- package/docs/classes/Client.html +205 -39
- package/docs/classes/Conversation.html +80 -51
- package/docs/classes/Media.html +2 -28
- package/docs/classes/Message.html +94 -39
- package/docs/classes/MessageBuilder.html +31 -8
- package/docs/classes/PushNotification.html +1 -1
- package/docs/classes/UnsentMessage.html +2 -2
- package/docs/index.html +20 -27
- package/docs/interfaces/ConversationLimits.html +12 -12
- package/docs/interfaces/ConversationState.html +2 -2
- package/docs/interfaces/ConversationUpdatedEventArgs.html +3001 -0
- package/docs/interfaces/PushNotificationData.html +48 -0
- package/docs/modules.html +19 -26
- package/package.json +24 -20
- package/CHANGELOG.md +0 -420
package/dist/conversation.js
CHANGED
@@ -131,6 +131,7 @@ This software includes platform.js under the following license.
|
|
131
131
|
Object.defineProperty(exports, '__esModule', { value: true });
|
132
132
|
|
133
133
|
var tslib_es6 = require('./node_modules/tslib/tslib.es6.js');
|
134
|
+
require('isomorphic-form-data');
|
134
135
|
var logger = require('./logger.js');
|
135
136
|
var participants = require('./data/participants.js');
|
136
137
|
var participant = require('./participant.js');
|
@@ -146,7 +147,9 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
|
|
146
147
|
|
147
148
|
var isEqual__default = /*#__PURE__*/_interopDefaultLegacy(isEqual);
|
148
149
|
|
149
|
-
|
150
|
+
/**
|
151
|
+
* Map of the fields that will be processed with update messages.
|
152
|
+
*/
|
150
153
|
const fieldMappings = {
|
151
154
|
lastMessage: "lastMessage",
|
152
155
|
attributes: "attributes",
|
@@ -162,255 +165,150 @@ const fieldMappings = {
|
|
162
165
|
state: "state",
|
163
166
|
bindings: "bindings",
|
164
167
|
};
|
165
|
-
function parseTime(timeString) {
|
166
|
-
try {
|
167
|
-
return new Date(timeString);
|
168
|
-
}
|
169
|
-
catch (e) {
|
170
|
-
return null;
|
171
|
-
}
|
172
|
-
}
|
173
168
|
/**
|
174
|
-
* A conversation represents communication between multiple Conversations
|
169
|
+
* A conversation represents communication between multiple Conversations
|
170
|
+
* clients.
|
175
171
|
*/
|
176
172
|
class Conversation extends replayEventEmitter.ReplayEventEmitter {
|
177
173
|
/**
|
174
|
+
* @param descriptor Conversation descriptor.
|
175
|
+
* @param sid Conversation SID.
|
176
|
+
* @param links Conversation links for REST requests.
|
177
|
+
* @param configuration Client configuration.
|
178
|
+
* @param services Conversation services.
|
178
179
|
* @internal
|
179
180
|
*/
|
180
181
|
constructor(descriptor, sid, links, configuration, services) {
|
181
|
-
var _a;
|
182
|
+
var _a, _b;
|
182
183
|
super();
|
183
184
|
this.sid = sid;
|
184
|
-
this.
|
185
|
-
this.
|
186
|
-
this.
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
const dateUpdated = parseTime(descriptor.dateUpdated);
|
191
|
-
const friendlyName = descriptor.friendlyName || null;
|
192
|
-
const lastReadMessageIndex = Number.isInteger(descriptor.lastConsumedMessageIndex)
|
193
|
-
? descriptor.lastConsumedMessageIndex
|
194
|
-
: null;
|
195
|
-
const uniqueName = descriptor.uniqueName || null;
|
196
|
-
try {
|
197
|
-
JSON.stringify(attributes);
|
198
|
-
}
|
199
|
-
catch (e) {
|
200
|
-
throw new Error("Attributes must be a valid JSON object.");
|
201
|
-
}
|
202
|
-
this.entityName = descriptor.channel;
|
203
|
-
this.channelState = {
|
204
|
-
uniqueName,
|
185
|
+
this._links = links;
|
186
|
+
this._configuration = configuration;
|
187
|
+
this._services = services;
|
188
|
+
this._entityName = descriptor.channel;
|
189
|
+
this._internalState = {
|
190
|
+
uniqueName: descriptor.uniqueName || null,
|
205
191
|
status: "notParticipating",
|
206
|
-
attributes,
|
207
|
-
createdBy,
|
208
|
-
dateCreated,
|
209
|
-
dateUpdated,
|
210
|
-
friendlyName,
|
211
|
-
lastReadMessageIndex
|
212
|
-
|
192
|
+
attributes: (_a = descriptor.attributes) !== null && _a !== void 0 ? _a : {},
|
193
|
+
createdBy: descriptor.createdBy,
|
194
|
+
dateCreated: index.parseTime(descriptor.dateCreated),
|
195
|
+
dateUpdated: index.parseTime(descriptor.dateUpdated),
|
196
|
+
friendlyName: descriptor.friendlyName || null,
|
197
|
+
lastReadMessageIndex: Number.isInteger(descriptor.lastConsumedMessageIndex)
|
198
|
+
? descriptor.lastConsumedMessageIndex
|
199
|
+
: null,
|
200
|
+
bindings: (_b = descriptor.bindings) !== null && _b !== void 0 ? _b : {},
|
213
201
|
};
|
214
202
|
if (descriptor.notificationLevel) {
|
215
|
-
this.
|
203
|
+
this._internalState.notificationLevel = descriptor.notificationLevel;
|
216
204
|
}
|
217
205
|
const participantsLinks = {
|
218
|
-
participants: this.
|
206
|
+
participants: this._links.participants,
|
219
207
|
};
|
220
|
-
this.
|
221
|
-
this.
|
222
|
-
this.
|
223
|
-
this.
|
224
|
-
this.
|
225
|
-
this.
|
226
|
-
this.
|
227
|
-
this.
|
228
|
-
this.
|
208
|
+
this._participants = new Map();
|
209
|
+
this._participantsEntity = new participants.Participants(this, this._participants, participantsLinks, this._configuration, this._services);
|
210
|
+
this._participantsEntity.on("participantJoined", (participant) => this.emit("participantJoined", participant));
|
211
|
+
this._participantsEntity.on("participantLeft", (participant) => this.emit("participantLeft", participant));
|
212
|
+
this._participantsEntity.on("participantUpdated", (args) => this.emit("participantUpdated", args));
|
213
|
+
this._messagesEntity = new messages.Messages(this, configuration, services);
|
214
|
+
this._messagesEntity.on("messageAdded", (message) => this._onMessageAdded(message));
|
215
|
+
this._messagesEntity.on("messageUpdated", (args) => this.emit("messageUpdated", args));
|
216
|
+
this._messagesEntity.on("messageRemoved", (message) => this.emit("messageRemoved", message));
|
229
217
|
}
|
230
218
|
/**
|
231
219
|
* Unique name of the conversation.
|
232
220
|
*/
|
233
221
|
get uniqueName() {
|
234
|
-
return this.
|
222
|
+
return this._internalState.uniqueName;
|
235
223
|
}
|
236
224
|
/**
|
237
225
|
* Status of the conversation.
|
238
226
|
*/
|
239
227
|
get status() {
|
240
|
-
return this.
|
228
|
+
return this._internalState.status;
|
241
229
|
}
|
242
230
|
/**
|
243
231
|
* Name of the conversation.
|
244
232
|
*/
|
245
233
|
get friendlyName() {
|
246
|
-
return this.
|
234
|
+
return this._internalState.friendlyName;
|
247
235
|
}
|
248
236
|
/**
|
249
237
|
* Date this conversation was last updated on.
|
250
238
|
*/
|
251
239
|
get dateUpdated() {
|
252
|
-
return this.
|
240
|
+
return this._internalState.dateUpdated;
|
253
241
|
}
|
254
242
|
/**
|
255
243
|
* Date this conversation was created on.
|
256
244
|
*/
|
257
245
|
get dateCreated() {
|
258
|
-
return this.
|
246
|
+
return this._internalState.dateCreated;
|
259
247
|
}
|
260
248
|
/**
|
261
249
|
* Identity of the user that created this conversation.
|
262
250
|
*/
|
263
251
|
get createdBy() {
|
264
252
|
var _a;
|
265
|
-
return (_a = this.
|
253
|
+
return (_a = this._internalState.createdBy) !== null && _a !== void 0 ? _a : "";
|
266
254
|
}
|
267
255
|
/**
|
268
256
|
* Custom attributes of the conversation.
|
269
257
|
*/
|
270
258
|
get attributes() {
|
271
|
-
return this.
|
259
|
+
return this._internalState.attributes;
|
272
260
|
}
|
273
261
|
/**
|
274
262
|
* Index of the last message the user has read in this conversation.
|
275
263
|
*/
|
276
264
|
get lastReadMessageIndex() {
|
277
|
-
return this.
|
265
|
+
return this._internalState.lastReadMessageIndex;
|
278
266
|
}
|
279
267
|
/**
|
280
268
|
* Last message sent to this conversation.
|
281
269
|
*/
|
282
270
|
get lastMessage() {
|
283
271
|
var _a;
|
284
|
-
return (_a = this.
|
272
|
+
return (_a = this._internalState.lastMessage) !== null && _a !== void 0 ? _a : undefined;
|
285
273
|
}
|
286
274
|
/**
|
287
275
|
* User notification level for this conversation.
|
288
276
|
*/
|
289
277
|
get notificationLevel() {
|
290
278
|
var _a;
|
291
|
-
return (_a = this.
|
279
|
+
return (_a = this._internalState.notificationLevel) !== null && _a !== void 0 ? _a : "default";
|
292
280
|
}
|
281
|
+
/**
|
282
|
+
* Conversation bindings. Undocumented feature (for now).
|
283
|
+
* @internal
|
284
|
+
*/
|
293
285
|
get bindings() {
|
294
|
-
return this.
|
286
|
+
return this._internalState.bindings;
|
295
287
|
}
|
296
288
|
/**
|
297
289
|
* Current conversation limits.
|
298
290
|
*/
|
299
291
|
get limits() {
|
300
|
-
return this.
|
292
|
+
return this._configuration.limits;
|
301
293
|
}
|
302
294
|
/**
|
303
295
|
* State of the conversation.
|
304
296
|
*/
|
305
297
|
get state() {
|
306
|
-
return this.
|
307
|
-
}
|
308
|
-
/**
|
309
|
-
* Load and subscribe to this conversation and do not subscribe to its participants and messages.
|
310
|
-
* This or _subscribeStreams will need to be called before any events on conversation will fire.
|
311
|
-
* @internal
|
312
|
-
*/
|
313
|
-
_subscribe() {
|
314
|
-
var _a;
|
315
|
-
return (this.entityPromise =
|
316
|
-
(_a = this.entityPromise) !== null && _a !== void 0 ? _a : this.services.syncClient
|
317
|
-
.document({ id: this.entityName, mode: "open_existing" })
|
318
|
-
.then((entity) => {
|
319
|
-
this.entity = entity;
|
320
|
-
this.entity.on("updated", (args) => {
|
321
|
-
this._update(args.data);
|
322
|
-
});
|
323
|
-
this.entity.on("removed", () => this.emit("removed", this));
|
324
|
-
this._update(this.entity.data);
|
325
|
-
return entity;
|
326
|
-
})
|
327
|
-
.catch((err) => {
|
328
|
-
this.entity = null;
|
329
|
-
this.entityPromise = null;
|
330
|
-
if (this.services.syncClient.connectionState != "disconnected") {
|
331
|
-
log.error("Failed to get conversation object", err);
|
332
|
-
}
|
333
|
-
log.debug("ERROR: Failed to get conversation object", err);
|
334
|
-
throw err;
|
335
|
-
}));
|
336
|
-
}
|
337
|
-
/**
|
338
|
-
* Load the attributes of this conversation and instantiate its participants and messages.
|
339
|
-
* This or _subscribe will need to be called before any events on the conversation will fire.
|
340
|
-
* This will need to be called before any events on participants or messages will fire
|
341
|
-
* @internal
|
342
|
-
*/
|
343
|
-
async _subscribeStreams() {
|
344
|
-
var _a, _b, _c;
|
345
|
-
try {
|
346
|
-
await this._subscribe();
|
347
|
-
log.trace("_subscribeStreams, this.entity.data=", (_a = this.entity) === null || _a === void 0 ? void 0 : _a.data);
|
348
|
-
const messagesObjectName = ((_b = this.entity) === null || _b === void 0 ? void 0 : _b.data)
|
349
|
-
.messages;
|
350
|
-
const rosterObjectName = ((_c = this.entity) === null || _c === void 0 ? void 0 : _c.data)
|
351
|
-
.roster;
|
352
|
-
await Promise.all([
|
353
|
-
this.messagesEntity.subscribe(messagesObjectName),
|
354
|
-
this.participantsEntity.subscribe(rosterObjectName),
|
355
|
-
]);
|
356
|
-
}
|
357
|
-
catch (err) {
|
358
|
-
if (this.services.syncClient.connectionState !== "disconnected") {
|
359
|
-
log.error("Failed to subscribe on conversation objects", this.sid, err);
|
360
|
-
}
|
361
|
-
log.debug("ERROR: Failed to subscribe on conversation objects", this.sid, err);
|
362
|
-
throw err;
|
363
|
-
}
|
364
|
-
}
|
365
|
-
/**
|
366
|
-
* Stop listening for and firing events on this conversation.
|
367
|
-
* @internal
|
368
|
-
*/
|
369
|
-
async _unsubscribe() {
|
370
|
-
if (this.entity) {
|
371
|
-
await this.entity.close();
|
372
|
-
this.entity = null;
|
373
|
-
this.entityPromise = null;
|
374
|
-
}
|
375
|
-
return Promise.all([
|
376
|
-
this.participantsEntity.unsubscribe(),
|
377
|
-
this.messagesEntity.unsubscribe(),
|
378
|
-
]);
|
298
|
+
return this._internalState.state;
|
379
299
|
}
|
380
300
|
/**
|
381
|
-
*
|
301
|
+
* Source of the conversation update.
|
382
302
|
* @internal
|
383
303
|
*/
|
384
|
-
|
385
|
-
this.
|
386
|
-
if (this.channelState.status === status) {
|
387
|
-
return;
|
388
|
-
}
|
389
|
-
this.channelState.status = status;
|
390
|
-
if (status === "joined") {
|
391
|
-
this._subscribeStreams().catch((err) => {
|
392
|
-
log.debug("ERROR while setting conversation status " + status, err);
|
393
|
-
if (this.services.syncClient.connectionState !== "disconnected") {
|
394
|
-
throw err;
|
395
|
-
}
|
396
|
-
});
|
397
|
-
}
|
398
|
-
else if (this.entityPromise) {
|
399
|
-
this._unsubscribe().catch((err) => {
|
400
|
-
log.debug("ERROR while setting conversation status " + status, err);
|
401
|
-
if (this.services.syncClient.connectionState !== "disconnected") {
|
402
|
-
throw err;
|
403
|
-
}
|
404
|
-
});
|
405
|
-
}
|
304
|
+
get _statusSource() {
|
305
|
+
return this._dataSource;
|
406
306
|
}
|
407
307
|
/**
|
408
|
-
*
|
409
|
-
* @
|
308
|
+
* Preprocess the update object.
|
309
|
+
* @param update The update object received from Sync.
|
310
|
+
* @param conversationSid The SID of the conversation in question.
|
410
311
|
*/
|
411
|
-
_statusSource() {
|
412
|
-
return this.statusSource;
|
413
|
-
}
|
414
312
|
static preprocessUpdate(update, conversationSid) {
|
415
313
|
try {
|
416
314
|
if (typeof update.attributes === "string") {
|
@@ -421,7 +319,7 @@ class Conversation extends replayEventEmitter.ReplayEventEmitter {
|
|
421
319
|
}
|
422
320
|
}
|
423
321
|
catch (e) {
|
424
|
-
|
322
|
+
Conversation._logger.warn("Retrieved malformed attributes from the server for conversation: " +
|
425
323
|
conversationSid);
|
426
324
|
update.attributes = {};
|
427
325
|
}
|
@@ -431,7 +329,7 @@ class Conversation extends replayEventEmitter.ReplayEventEmitter {
|
|
431
329
|
}
|
432
330
|
}
|
433
331
|
catch (e) {
|
434
|
-
|
332
|
+
Conversation._logger.warn("Retrieved malformed dateCreated from the server for conversation: " +
|
435
333
|
conversationSid);
|
436
334
|
delete update.dateCreated;
|
437
335
|
}
|
@@ -441,7 +339,7 @@ class Conversation extends replayEventEmitter.ReplayEventEmitter {
|
|
441
339
|
}
|
442
340
|
}
|
443
341
|
catch (e) {
|
444
|
-
|
342
|
+
Conversation._logger.warn("Retrieved malformed dateUpdated from the server for conversation: " +
|
445
343
|
conversationSid);
|
446
344
|
delete update.dateUpdated;
|
447
345
|
}
|
@@ -451,158 +349,44 @@ class Conversation extends replayEventEmitter.ReplayEventEmitter {
|
|
451
349
|
}
|
452
350
|
}
|
453
351
|
catch (e) {
|
454
|
-
|
352
|
+
Conversation._logger.warn("Retrieved malformed lastMessage.timestamp from the server for conversation: " +
|
455
353
|
conversationSid);
|
456
354
|
delete update.lastMessage.timestamp;
|
457
355
|
}
|
458
356
|
}
|
459
|
-
/**
|
460
|
-
* Update the local conversation object with new values.
|
461
|
-
* @internal
|
462
|
-
*/
|
463
|
-
_update(update) {
|
464
|
-
var _a, _b, _c, _d, _e;
|
465
|
-
log.trace("_update", update);
|
466
|
-
Conversation.preprocessUpdate(update, this.sid);
|
467
|
-
const updateReasons = new Set();
|
468
|
-
for (const key of Object.keys(update)) {
|
469
|
-
const localKey = fieldMappings[key];
|
470
|
-
if (!localKey) {
|
471
|
-
continue;
|
472
|
-
}
|
473
|
-
switch (localKey) {
|
474
|
-
case fieldMappings.status:
|
475
|
-
if (!update.status ||
|
476
|
-
update.status === "unknown" ||
|
477
|
-
this.channelState.status === update.status) {
|
478
|
-
break;
|
479
|
-
}
|
480
|
-
this.channelState.status = update.status;
|
481
|
-
updateReasons.add(localKey);
|
482
|
-
break;
|
483
|
-
case fieldMappings.attributes:
|
484
|
-
if (isEqual__default['default'](this.channelState.attributes, update.attributes)) {
|
485
|
-
break;
|
486
|
-
}
|
487
|
-
this.channelState.attributes = update.attributes;
|
488
|
-
updateReasons.add(localKey);
|
489
|
-
break;
|
490
|
-
case fieldMappings.lastConsumedMessageIndex:
|
491
|
-
if (update.lastConsumedMessageIndex === undefined ||
|
492
|
-
update.lastConsumedMessageIndex ===
|
493
|
-
this.channelState.lastReadMessageIndex) {
|
494
|
-
break;
|
495
|
-
}
|
496
|
-
this.channelState.lastReadMessageIndex =
|
497
|
-
update.lastConsumedMessageIndex;
|
498
|
-
updateReasons.add("lastReadMessageIndex");
|
499
|
-
break;
|
500
|
-
case fieldMappings.lastMessage:
|
501
|
-
if (this.channelState.lastMessage && !update.lastMessage) {
|
502
|
-
delete this.channelState.lastMessage;
|
503
|
-
updateReasons.add(localKey);
|
504
|
-
break;
|
505
|
-
}
|
506
|
-
this.channelState.lastMessage = this.channelState.lastMessage || {};
|
507
|
-
if (((_a = update.lastMessage) === null || _a === void 0 ? void 0 : _a.index) !== undefined &&
|
508
|
-
update.lastMessage.index !== this.channelState.lastMessage.index) {
|
509
|
-
this.channelState.lastMessage.index = update.lastMessage.index;
|
510
|
-
updateReasons.add(localKey);
|
511
|
-
}
|
512
|
-
if (((_b = update.lastMessage) === null || _b === void 0 ? void 0 : _b.timestamp) !== undefined &&
|
513
|
-
((_d = (_c = this.channelState.lastMessage) === null || _c === void 0 ? void 0 : _c.dateCreated) === null || _d === void 0 ? void 0 : _d.getTime()) !==
|
514
|
-
update.lastMessage.timestamp.getTime()) {
|
515
|
-
this.channelState.lastMessage.dateCreated =
|
516
|
-
update.lastMessage.timestamp;
|
517
|
-
updateReasons.add(localKey);
|
518
|
-
}
|
519
|
-
if (isEqual__default['default'](this.channelState.lastMessage, {})) {
|
520
|
-
delete this.channelState.lastMessage;
|
521
|
-
}
|
522
|
-
break;
|
523
|
-
case fieldMappings.state:
|
524
|
-
const state = update.state || undefined;
|
525
|
-
if (state !== undefined) {
|
526
|
-
state.dateUpdated = new Date(state.dateUpdated);
|
527
|
-
}
|
528
|
-
if (isEqual__default['default'](this.channelState.state, state)) {
|
529
|
-
break;
|
530
|
-
}
|
531
|
-
this.channelState.state = state;
|
532
|
-
updateReasons.add(localKey);
|
533
|
-
break;
|
534
|
-
case fieldMappings.bindings:
|
535
|
-
if (isEqual__default['default'](this.channelState.bindings, update.bindings)) {
|
536
|
-
break;
|
537
|
-
}
|
538
|
-
this.channelState.bindings = update.bindings;
|
539
|
-
updateReasons.add(localKey);
|
540
|
-
break;
|
541
|
-
default:
|
542
|
-
const isDate = update[key] instanceof Date;
|
543
|
-
const keysMatchAsDates = isDate &&
|
544
|
-
((_e = this.channelState[localKey]) === null || _e === void 0 ? void 0 : _e.getTime()) === update[key].getTime();
|
545
|
-
const keysMatchAsNonDates = !isDate && this[localKey] === update[key];
|
546
|
-
if (keysMatchAsDates || keysMatchAsNonDates) {
|
547
|
-
break;
|
548
|
-
}
|
549
|
-
this.channelState[localKey] = update[key];
|
550
|
-
updateReasons.add(localKey);
|
551
|
-
}
|
552
|
-
}
|
553
|
-
if (updateReasons.size > 0) {
|
554
|
-
this.emit("updated", {
|
555
|
-
conversation: this,
|
556
|
-
updateReasons: [...updateReasons],
|
557
|
-
});
|
558
|
-
}
|
559
|
-
}
|
560
|
-
/**
|
561
|
-
* @internal
|
562
|
-
*/
|
563
|
-
_onMessageAdded(message) {
|
564
|
-
for (const participant of this.participants.values()) {
|
565
|
-
if (participant.identity === message.author) {
|
566
|
-
participant._endTyping();
|
567
|
-
break;
|
568
|
-
}
|
569
|
-
}
|
570
|
-
this.emit("messageAdded", message);
|
571
|
-
}
|
572
|
-
async _setLastReadMessageIndex(index) {
|
573
|
-
const result = await this.services.commandExecutor.mutateResource("post", `${this.configuration.links.myConversations}/${this.sid}`, {
|
574
|
-
last_read_message_index: index,
|
575
|
-
});
|
576
|
-
return result.unread_messages_count;
|
577
|
-
}
|
578
357
|
/**
|
579
358
|
* Add a participant to the conversation by its identity.
|
580
359
|
* @param identity Identity of the Client to add.
|
581
360
|
* @param attributes Attributes to be attached to the participant.
|
361
|
+
* @returns The added participant.
|
582
362
|
*/
|
583
363
|
async add(identity, attributes) {
|
584
|
-
return this.
|
364
|
+
return this._participantsEntity.add(identity, attributes !== null && attributes !== void 0 ? attributes : {});
|
585
365
|
}
|
586
366
|
/**
|
587
367
|
* Add a non-chat participant to the conversation.
|
588
368
|
* @param proxyAddress Proxy (Twilio) address of the participant.
|
589
369
|
* @param address User address of the participant.
|
590
370
|
* @param attributes Attributes to be attached to the participant.
|
591
|
-
* @param bindingOptions Options for adding email participants - name and
|
371
|
+
* @param bindingOptions Options for adding email participants - name and
|
372
|
+
* CC/To level.
|
373
|
+
* @returns The added participant.
|
592
374
|
*/
|
593
375
|
async addNonChatParticipant(proxyAddress, address, attributes = {}, bindingOptions = {}) {
|
594
|
-
return this.
|
376
|
+
return this._participantsEntity.addNonChatParticipant(proxyAddress, address, attributes !== null && attributes !== void 0 ? attributes : {}, bindingOptions !== null && bindingOptions !== void 0 ? bindingOptions : {});
|
595
377
|
}
|
596
378
|
/**
|
597
|
-
* Advance the conversation's last read message index to the current read
|
598
|
-
* Rejects if the user is not a participant of the conversation.
|
599
|
-
*
|
379
|
+
* Advance the conversation's last read message index to the current read
|
380
|
+
* horizon. Rejects if the user is not a participant of the conversation. Last
|
381
|
+
* read message index is updated only if the new index value is higher than
|
382
|
+
* the previous.
|
600
383
|
* @param index Message index to advance to.
|
601
384
|
* @return Resulting unread messages count in the conversation.
|
602
385
|
*/
|
603
386
|
async advanceLastReadMessageIndex(index) {
|
387
|
+
var _a;
|
604
388
|
await this._subscribeStreams();
|
605
|
-
if (index < (this.lastReadMessageIndex
|
389
|
+
if (index < ((_a = this.lastReadMessageIndex) !== null && _a !== void 0 ? _a : 0)) {
|
606
390
|
return await this._setLastReadMessageIndex(this.lastReadMessageIndex);
|
607
391
|
}
|
608
392
|
return await this._setLastReadMessageIndex(index);
|
@@ -611,7 +395,7 @@ class Conversation extends replayEventEmitter.ReplayEventEmitter {
|
|
611
395
|
* Delete the conversation and unsubscribe from its events.
|
612
396
|
*/
|
613
397
|
async delete() {
|
614
|
-
await this.
|
398
|
+
await this._services.commandExecutor.mutateResource("delete", this._links.self);
|
615
399
|
return this;
|
616
400
|
}
|
617
401
|
/**
|
@@ -623,39 +407,43 @@ class Conversation extends replayEventEmitter.ReplayEventEmitter {
|
|
623
407
|
}
|
624
408
|
/**
|
625
409
|
* Returns messages from the conversation using the paginator interface.
|
626
|
-
* @param pageSize Number of messages to return in a single chunk. Default is
|
627
|
-
*
|
628
|
-
* @param
|
629
|
-
*
|
410
|
+
* @param pageSize Number of messages to return in a single chunk. Default is
|
411
|
+
* 30.
|
412
|
+
* @param anchor Index of the newest message to fetch. Default is from the
|
413
|
+
* end.
|
414
|
+
* @param direction Query direction. By default, it queries backwards
|
415
|
+
* from newer to older. The `"forward"` value will query in the opposite
|
416
|
+
* direction.
|
630
417
|
* @return A page of messages.
|
631
418
|
*/
|
632
419
|
async getMessages(pageSize, anchor, direction) {
|
633
420
|
await this._subscribeStreams();
|
634
|
-
return this.
|
421
|
+
return this._messagesEntity.getMessages(pageSize, anchor, direction);
|
635
422
|
}
|
636
423
|
/**
|
637
424
|
* Get a list of all the participants who are joined to this conversation.
|
638
425
|
*/
|
639
426
|
async getParticipants() {
|
640
427
|
await this._subscribeStreams();
|
641
|
-
return this.
|
428
|
+
return this._participantsEntity.getParticipants();
|
642
429
|
}
|
643
430
|
/**
|
644
431
|
* Get conversation participants count.
|
645
432
|
*
|
646
|
-
* This method is semi-realtime. This means that this data will be eventually
|
647
|
-
* but will also be possibly incorrect for a few seconds. The
|
648
|
-
* provide real time events for counter values
|
433
|
+
* This method is semi-realtime. This means that this data will be eventually
|
434
|
+
* correct, but will also be possibly incorrect for a few seconds. The
|
435
|
+
* Conversations system does not provide real time events for counter values
|
436
|
+
* changes.
|
649
437
|
*
|
650
|
-
* This is useful for any UI badges, but it is not recommended to build any
|
651
|
-
* logic based on these counters being accurate in real time.
|
438
|
+
* This is useful for any UI badges, but it is not recommended to build any
|
439
|
+
* core application logic based on these counters being accurate in real time.
|
652
440
|
*/
|
653
441
|
async getParticipantsCount() {
|
654
442
|
var _a;
|
655
|
-
const url = new index.UriBuilder(this.
|
443
|
+
const url = new index.UriBuilder(this._configuration.links.conversations)
|
656
444
|
.path(this.sid)
|
657
445
|
.build();
|
658
|
-
const response = await this.
|
446
|
+
const response = await this._services.network.get(url);
|
659
447
|
return (_a = response.body.participants_count) !== null && _a !== void 0 ? _a : 0;
|
660
448
|
}
|
661
449
|
/**
|
@@ -663,53 +451,55 @@ class Conversation extends replayEventEmitter.ReplayEventEmitter {
|
|
663
451
|
* @param participantSid Participant SID.
|
664
452
|
*/
|
665
453
|
async getParticipantBySid(participantSid) {
|
666
|
-
return this.
|
454
|
+
return this._participantsEntity.getParticipantBySid(participantSid);
|
667
455
|
}
|
668
456
|
/**
|
669
457
|
* Get a participant by its identity.
|
670
458
|
* @param identity Participant identity.
|
671
459
|
*/
|
672
460
|
async getParticipantByIdentity(identity = "") {
|
673
|
-
return this.
|
461
|
+
return this._participantsEntity.getParticipantByIdentity(identity !== null && identity !== void 0 ? identity : "");
|
674
462
|
}
|
675
463
|
/**
|
676
464
|
* Get the total message count in the conversation.
|
677
465
|
*
|
678
|
-
* This method is semi-realtime. This means that this data will be eventually
|
679
|
-
* but will also be possibly incorrect for a few seconds. The
|
680
|
-
* provide real time events for counter values
|
466
|
+
* This method is semi-realtime. This means that this data will be eventually
|
467
|
+
* correct, but will also be possibly incorrect for a few seconds. The
|
468
|
+
* Conversations system does not provide real time events for counter values
|
469
|
+
* changes.
|
681
470
|
*
|
682
|
-
* This is useful for any UI badges, but it is not recommended to build any
|
683
|
-
* logic based on these counters being accurate in real time.
|
471
|
+
* This is useful for any UI badges, but it is not recommended to build any
|
472
|
+
* core application logic based on these counters being accurate in real time.
|
684
473
|
*/
|
685
474
|
async getMessagesCount() {
|
686
475
|
var _a;
|
687
|
-
const url = new index.UriBuilder(this.
|
476
|
+
const url = new index.UriBuilder(this._configuration.links.conversations)
|
688
477
|
.path(this.sid)
|
689
478
|
.build();
|
690
|
-
const response = await this.
|
479
|
+
const response = await this._services.network.get(url);
|
691
480
|
return (_a = response.body.messages_count) !== null && _a !== void 0 ? _a : 0;
|
692
481
|
}
|
693
482
|
/**
|
694
|
-
* Get unread messages count for the user if they are a participant of this
|
695
|
-
* Rejects if the user is not a participant of the conversation.
|
483
|
+
* Get unread messages count for the user if they are a participant of this
|
484
|
+
* conversation. Rejects if the user is not a participant of the conversation.
|
696
485
|
*
|
697
486
|
* Use this method to obtain the number of unread messages together with
|
698
487
|
* {@link Conversation.updateLastReadMessageIndex} instead of relying on the
|
699
488
|
* message indices which may have gaps. See {@link Message.index} for details.
|
700
489
|
*
|
701
|
-
* This method is semi-realtime. This means that this data will be eventually
|
702
|
-
* but will also be possibly incorrect for a few seconds. The
|
703
|
-
* provide real time events for counter values
|
490
|
+
* This method is semi-realtime. This means that this data will be eventually
|
491
|
+
* correct, but will also be possibly incorrect for a few seconds. The
|
492
|
+
* Conversations system does not provide real time events for counter values
|
493
|
+
* changes.
|
704
494
|
*
|
705
|
-
* This is useful for any UI badges, but it is not recommended to build any
|
706
|
-
* logic based on these counters being accurate in real time.
|
495
|
+
* This is useful for any UI badges, but it is not recommended to build any
|
496
|
+
* core application logic based on these counters being accurate in real time.
|
707
497
|
*/
|
708
498
|
async getUnreadMessagesCount() {
|
709
|
-
const url = new index.UriBuilder(this.
|
499
|
+
const url = new index.UriBuilder(this._configuration.links.myConversations)
|
710
500
|
.path(this.sid)
|
711
501
|
.build();
|
712
|
-
const response = await this.
|
502
|
+
const response = await this._services.network.get(url);
|
713
503
|
if (response.body.conversation_sid !== this.sid) {
|
714
504
|
throw new Error("Conversation was not found in the user conversations list");
|
715
505
|
}
|
@@ -723,8 +513,8 @@ class Conversation extends replayEventEmitter.ReplayEventEmitter {
|
|
723
513
|
* Join the conversation and subscribe to its events.
|
724
514
|
*/
|
725
515
|
async join() {
|
726
|
-
await this.
|
727
|
-
identity: this.
|
516
|
+
await this._services.commandExecutor.mutateResource("post", this._links.participants, {
|
517
|
+
identity: this._configuration.userIdentity,
|
728
518
|
});
|
729
519
|
return this;
|
730
520
|
}
|
@@ -732,8 +522,8 @@ class Conversation extends replayEventEmitter.ReplayEventEmitter {
|
|
732
522
|
* Leave the conversation.
|
733
523
|
*/
|
734
524
|
async leave() {
|
735
|
-
if (this.
|
736
|
-
await this.
|
525
|
+
if (this._internalState.status === "joined") {
|
526
|
+
await this._services.commandExecutor.mutateResource("delete", `${this._links.participants}/${this._configuration.userIdentity}`);
|
737
527
|
}
|
738
528
|
return this;
|
739
529
|
}
|
@@ -742,15 +532,14 @@ class Conversation extends replayEventEmitter.ReplayEventEmitter {
|
|
742
532
|
* argument, it will assume that the string is an identity or SID.
|
743
533
|
* @param participant Identity, SID or the participant object to remove.
|
744
534
|
*/
|
745
|
-
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
746
|
-
// @ts-ignore TODO: fix validateTypesAsync typing
|
747
535
|
async removeParticipant(participant) {
|
748
|
-
await this.
|
536
|
+
await this._participantsEntity.remove(typeof participant === "string" ? participant : participant.sid);
|
749
537
|
}
|
750
538
|
/**
|
751
539
|
* Send a message to the conversation.
|
752
540
|
* @param message Message body for the text message,
|
753
|
-
* `FormData` or {@link SendMediaOptions} for media content. Sending FormData
|
541
|
+
* `FormData` or {@link SendMediaOptions} for media content. Sending FormData
|
542
|
+
* is supported only with the browser engine.
|
754
543
|
* @param messageAttributes Attributes for the message.
|
755
544
|
* @param emailOptions Email options for the message.
|
756
545
|
* @return Index of the new message.
|
@@ -758,22 +547,23 @@ class Conversation extends replayEventEmitter.ReplayEventEmitter {
|
|
758
547
|
async sendMessage(message, messageAttributes, emailOptions) {
|
759
548
|
var _a, _b;
|
760
549
|
if (typeof message === "string" || message === null) {
|
761
|
-
const response = await this.
|
550
|
+
const response = await this._messagesEntity.send(message, messageAttributes, emailOptions);
|
762
551
|
return (_a = index.parseToNumber(response.index)) !== null && _a !== void 0 ? _a : 0;
|
763
552
|
}
|
764
|
-
const response = await this.
|
553
|
+
const response = await this._messagesEntity.sendMedia(message, messageAttributes, emailOptions);
|
765
554
|
return (_b = index.parseToNumber(response.index)) !== null && _b !== void 0 ? _b : 0;
|
766
555
|
}
|
767
556
|
/**
|
768
557
|
* New interface to prepare for sending a message.
|
769
|
-
* Use instead of
|
558
|
+
* Use this instead of {@link Message.sendMessage}.
|
770
559
|
* @return A MessageBuilder to help set all message sending options.
|
771
560
|
*/
|
772
561
|
prepareMessage() {
|
773
|
-
return new messageBuilder.MessageBuilder(this.limits, this.
|
562
|
+
return new messageBuilder.MessageBuilder(this.limits, this._messagesEntity);
|
774
563
|
}
|
775
564
|
/**
|
776
|
-
* Set last read message index of the conversation to the index of the last
|
565
|
+
* Set last read message index of the conversation to the index of the last
|
566
|
+
* known message.
|
777
567
|
* @return Resulting unread messages count in the conversation.
|
778
568
|
*/
|
779
569
|
async setAllMessagesRead() {
|
@@ -782,7 +572,7 @@ class Conversation extends replayEventEmitter.ReplayEventEmitter {
|
|
782
572
|
if (messagesPage.items.length > 0) {
|
783
573
|
return this.advanceLastReadMessageIndex(messagesPage.items[0].index);
|
784
574
|
}
|
785
|
-
return
|
575
|
+
return 0;
|
786
576
|
}
|
787
577
|
/**
|
788
578
|
* Set all messages in the conversation unread.
|
@@ -797,23 +587,25 @@ class Conversation extends replayEventEmitter.ReplayEventEmitter {
|
|
797
587
|
* @param notificationLevel New user notification level.
|
798
588
|
*/
|
799
589
|
async setUserNotificationLevel(notificationLevel) {
|
800
|
-
await this.
|
590
|
+
await this._services.commandExecutor.mutateResource("post", `${this._configuration.links.myConversations}/${this.sid}`, {
|
801
591
|
notification_level: notificationLevel,
|
802
592
|
});
|
803
593
|
}
|
804
594
|
/**
|
805
|
-
* Send a notification to the server indicating that this client is currently
|
806
|
-
* Typing ended notification is sent after a
|
595
|
+
* Send a notification to the server indicating that this client is currently
|
596
|
+
* typing in this conversation. Typing ended notification is sent after a
|
597
|
+
* while automatically, but by calling this method again you ensure that
|
598
|
+
* typing ended is not received.
|
807
599
|
*/
|
808
600
|
typing() {
|
809
|
-
return this.
|
601
|
+
return this._services.typingIndicator.send(this.sid);
|
810
602
|
}
|
811
603
|
/**
|
812
604
|
* Update the attributes of the conversation.
|
813
605
|
* @param attributes New attributes.
|
814
606
|
*/
|
815
607
|
async updateAttributes(attributes) {
|
816
|
-
await this.
|
608
|
+
await this._services.commandExecutor.mutateResource("post", this._links.self, {
|
817
609
|
attributes: attributes !== undefined ? JSON.stringify(attributes) : undefined,
|
818
610
|
});
|
819
611
|
return this;
|
@@ -823,15 +615,15 @@ class Conversation extends replayEventEmitter.ReplayEventEmitter {
|
|
823
615
|
* @param friendlyName New friendly name.
|
824
616
|
*/
|
825
617
|
async updateFriendlyName(friendlyName) {
|
826
|
-
if (this.
|
827
|
-
await this.
|
618
|
+
if (this._internalState.friendlyName !== friendlyName) {
|
619
|
+
await this._services.commandExecutor.mutateResource("post", this._links.self, { friendly_name: friendlyName });
|
828
620
|
}
|
829
621
|
return this;
|
830
622
|
}
|
831
623
|
/**
|
832
624
|
* Set the last read message index to the current read horizon.
|
833
|
-
* @param index Message index to set as last read.
|
834
|
-
*
|
625
|
+
* @param index Message index to set as last read. If null is provided, then
|
626
|
+
* the behavior is identical to {@link Conversation.setAllMessagesUnread}.
|
835
627
|
* @returns Resulting unread messages count in the conversation.
|
836
628
|
*/
|
837
629
|
async updateLastReadMessageIndex(index) {
|
@@ -840,25 +632,251 @@ class Conversation extends replayEventEmitter.ReplayEventEmitter {
|
|
840
632
|
}
|
841
633
|
/**
|
842
634
|
* Update the unique name of the conversation.
|
843
|
-
* @param uniqueName New unique name for the conversation. Setting unique name
|
635
|
+
* @param uniqueName New unique name for the conversation. Setting unique name
|
636
|
+
* to null removes it.
|
844
637
|
*/
|
845
638
|
async updateUniqueName(uniqueName) {
|
846
|
-
if (this.
|
847
|
-
|
848
|
-
|
849
|
-
}
|
850
|
-
await this.services.commandExecutor.mutateResource("post", this.links.self, {
|
639
|
+
if (this._internalState.uniqueName !== uniqueName) {
|
640
|
+
uniqueName || (uniqueName = "");
|
641
|
+
await this._services.commandExecutor.mutateResource("post", this._links.self, {
|
851
642
|
unique_name: uniqueName,
|
852
643
|
});
|
853
644
|
}
|
854
645
|
return this;
|
855
646
|
}
|
647
|
+
/**
|
648
|
+
* Load and subscribe to this conversation and do not subscribe to its
|
649
|
+
* participants and messages. This or _subscribeStreams will need to be called
|
650
|
+
* before any events on conversation will fire.
|
651
|
+
* @internal
|
652
|
+
*/
|
653
|
+
async _subscribe() {
|
654
|
+
if (this._entityPromise) {
|
655
|
+
return this._entityPromise;
|
656
|
+
}
|
657
|
+
this._entityPromise = this._services.syncClient.document({
|
658
|
+
id: this._entityName,
|
659
|
+
mode: "open_existing",
|
660
|
+
});
|
661
|
+
try {
|
662
|
+
this._entity = await this._entityPromise;
|
663
|
+
this._entity.on("updated", (args) => this._update(args.data));
|
664
|
+
this._entity.on("removed", () => this.emit("removed", this));
|
665
|
+
this._update(this._entity.data);
|
666
|
+
return this._entity;
|
667
|
+
}
|
668
|
+
catch (err) {
|
669
|
+
this._entity = null;
|
670
|
+
this._entityPromise = null;
|
671
|
+
if (this._services.syncClient.connectionState != "disconnected") {
|
672
|
+
Conversation._logger.error("Failed to get conversation object", err);
|
673
|
+
}
|
674
|
+
Conversation._logger.debug("ERROR: Failed to get conversation object", err);
|
675
|
+
throw err;
|
676
|
+
}
|
677
|
+
}
|
678
|
+
/**
|
679
|
+
* Load the attributes of this conversation and instantiate its participants
|
680
|
+
* and messages. This or _subscribe will need to be called before any events
|
681
|
+
* on the conversation will fire. This will need to be called before any
|
682
|
+
* events on participants or messages will fire
|
683
|
+
* @internal
|
684
|
+
*/
|
685
|
+
async _subscribeStreams() {
|
686
|
+
var _a, _b;
|
687
|
+
try {
|
688
|
+
await this._subscribe();
|
689
|
+
Conversation._logger.trace("_subscribeStreams, this.entity.data=", (_a = this._entity) === null || _a === void 0 ? void 0 : _a.data);
|
690
|
+
const data = (_b = this._entity) === null || _b === void 0 ? void 0 : _b.data;
|
691
|
+
const messagesObjectName = data.messages;
|
692
|
+
const rosterObjectName = data.roster;
|
693
|
+
await Promise.all([
|
694
|
+
this._messagesEntity.subscribe(messagesObjectName),
|
695
|
+
this._participantsEntity.subscribe(rosterObjectName),
|
696
|
+
]);
|
697
|
+
}
|
698
|
+
catch (err) {
|
699
|
+
if (this._services.syncClient.connectionState !== "disconnected") {
|
700
|
+
Conversation._logger.error("Failed to subscribe on conversation objects", this.sid, err);
|
701
|
+
}
|
702
|
+
Conversation._logger.debug("ERROR: Failed to subscribe on conversation objects", this.sid, err);
|
703
|
+
throw err;
|
704
|
+
}
|
705
|
+
}
|
706
|
+
/**
|
707
|
+
* Stop listening for and firing events on this conversation.
|
708
|
+
* @internal
|
709
|
+
*/
|
710
|
+
async _unsubscribe() {
|
711
|
+
if (this._entity) {
|
712
|
+
await this._entity.close();
|
713
|
+
this._entity = null;
|
714
|
+
this._entityPromise = null;
|
715
|
+
}
|
716
|
+
return Promise.all([
|
717
|
+
this._participantsEntity.unsubscribe(),
|
718
|
+
this._messagesEntity.unsubscribe(),
|
719
|
+
]);
|
720
|
+
}
|
721
|
+
/**
|
722
|
+
* Set conversation status.
|
723
|
+
* @internal
|
724
|
+
*/
|
725
|
+
_setStatus(status, source) {
|
726
|
+
this._dataSource = source;
|
727
|
+
if (this._internalState.status === status) {
|
728
|
+
return;
|
729
|
+
}
|
730
|
+
this._internalState.status = status;
|
731
|
+
if (status === "joined") {
|
732
|
+
this._subscribeStreams().catch((err) => {
|
733
|
+
Conversation._logger.debug("ERROR while setting conversation status " + status, err);
|
734
|
+
if (this._services.syncClient.connectionState !== "disconnected") {
|
735
|
+
throw err;
|
736
|
+
}
|
737
|
+
});
|
738
|
+
return;
|
739
|
+
}
|
740
|
+
if (this._entityPromise) {
|
741
|
+
this._unsubscribe().catch((err) => {
|
742
|
+
Conversation._logger.debug("ERROR while setting conversation status " + status, err);
|
743
|
+
if (this._services.syncClient.connectionState !== "disconnected") {
|
744
|
+
throw err;
|
745
|
+
}
|
746
|
+
});
|
747
|
+
}
|
748
|
+
}
|
749
|
+
/**
|
750
|
+
* Update the local conversation object with new values.
|
751
|
+
* @internal
|
752
|
+
*/
|
753
|
+
_update(update) {
|
754
|
+
var _a, _b, _c, _d, _e;
|
755
|
+
Conversation._logger.trace("_update", update);
|
756
|
+
Conversation.preprocessUpdate(update, this.sid);
|
757
|
+
const updateReasons = new Set();
|
758
|
+
for (const key of Object.keys(update)) {
|
759
|
+
const localKey = fieldMappings[key];
|
760
|
+
if (!localKey) {
|
761
|
+
continue;
|
762
|
+
}
|
763
|
+
switch (localKey) {
|
764
|
+
case fieldMappings.status:
|
765
|
+
if (!update.status ||
|
766
|
+
update.status === "unknown" ||
|
767
|
+
this._internalState.status === update.status) {
|
768
|
+
break;
|
769
|
+
}
|
770
|
+
this._internalState.status = update.status;
|
771
|
+
updateReasons.add(localKey);
|
772
|
+
break;
|
773
|
+
case fieldMappings.attributes:
|
774
|
+
if (isEqual__default["default"](this._internalState.attributes, update.attributes)) {
|
775
|
+
break;
|
776
|
+
}
|
777
|
+
this._internalState.attributes = update.attributes;
|
778
|
+
updateReasons.add(localKey);
|
779
|
+
break;
|
780
|
+
case fieldMappings.lastConsumedMessageIndex:
|
781
|
+
if (update.lastConsumedMessageIndex === undefined ||
|
782
|
+
update.lastConsumedMessageIndex ===
|
783
|
+
this._internalState.lastReadMessageIndex) {
|
784
|
+
break;
|
785
|
+
}
|
786
|
+
this._internalState.lastReadMessageIndex =
|
787
|
+
update.lastConsumedMessageIndex;
|
788
|
+
updateReasons.add("lastReadMessageIndex");
|
789
|
+
break;
|
790
|
+
case fieldMappings.lastMessage:
|
791
|
+
if (this._internalState.lastMessage && !update.lastMessage) {
|
792
|
+
delete this._internalState.lastMessage;
|
793
|
+
updateReasons.add(localKey);
|
794
|
+
break;
|
795
|
+
}
|
796
|
+
this._internalState.lastMessage =
|
797
|
+
this._internalState.lastMessage || {};
|
798
|
+
if (((_a = update.lastMessage) === null || _a === void 0 ? void 0 : _a.index) !== undefined &&
|
799
|
+
update.lastMessage.index !== this._internalState.lastMessage.index) {
|
800
|
+
this._internalState.lastMessage.index = update.lastMessage.index;
|
801
|
+
updateReasons.add(localKey);
|
802
|
+
}
|
803
|
+
if (((_b = update.lastMessage) === null || _b === void 0 ? void 0 : _b.timestamp) !== undefined &&
|
804
|
+
((_d = (_c = this._internalState.lastMessage) === null || _c === void 0 ? void 0 : _c.dateCreated) === null || _d === void 0 ? void 0 : _d.getTime()) !==
|
805
|
+
update.lastMessage.timestamp.getTime()) {
|
806
|
+
this._internalState.lastMessage.dateCreated =
|
807
|
+
update.lastMessage.timestamp;
|
808
|
+
updateReasons.add(localKey);
|
809
|
+
}
|
810
|
+
if (isEqual__default["default"](this._internalState.lastMessage, {})) {
|
811
|
+
delete this._internalState.lastMessage;
|
812
|
+
}
|
813
|
+
break;
|
814
|
+
case fieldMappings.state:
|
815
|
+
const state = update.state || undefined;
|
816
|
+
if (state !== undefined) {
|
817
|
+
state.dateUpdated = new Date(state.dateUpdated);
|
818
|
+
}
|
819
|
+
if (isEqual__default["default"](this._internalState.state, state)) {
|
820
|
+
break;
|
821
|
+
}
|
822
|
+
this._internalState.state = state;
|
823
|
+
updateReasons.add(localKey);
|
824
|
+
break;
|
825
|
+
case fieldMappings.bindings:
|
826
|
+
if (isEqual__default["default"](this._internalState.bindings, update.bindings)) {
|
827
|
+
break;
|
828
|
+
}
|
829
|
+
this._internalState.bindings = update.bindings;
|
830
|
+
updateReasons.add(localKey);
|
831
|
+
break;
|
832
|
+
default:
|
833
|
+
const isDate = update[key] instanceof Date;
|
834
|
+
const keysMatchAsDates = isDate &&
|
835
|
+
((_e = this._internalState[localKey]) === null || _e === void 0 ? void 0 : _e.getTime()) === update[key].getTime();
|
836
|
+
const keysMatchAsNonDates = !isDate && this[localKey] === update[key];
|
837
|
+
if (keysMatchAsDates || keysMatchAsNonDates) {
|
838
|
+
break;
|
839
|
+
}
|
840
|
+
this._internalState[localKey] = update[key];
|
841
|
+
updateReasons.add(localKey);
|
842
|
+
}
|
843
|
+
}
|
844
|
+
if (updateReasons.size > 0) {
|
845
|
+
this.emit("updated", {
|
846
|
+
conversation: this,
|
847
|
+
updateReasons: [...updateReasons],
|
848
|
+
});
|
849
|
+
}
|
850
|
+
}
|
851
|
+
/**
|
852
|
+
* Handle onMessageAdded event.
|
853
|
+
*/
|
854
|
+
_onMessageAdded(message) {
|
855
|
+
for (const participant of this._participants.values()) {
|
856
|
+
if (participant.identity === message.author) {
|
857
|
+
participant._endTyping();
|
858
|
+
break;
|
859
|
+
}
|
860
|
+
}
|
861
|
+
this.emit("messageAdded", message);
|
862
|
+
}
|
863
|
+
/**
|
864
|
+
* Set last read message index.
|
865
|
+
* @param index New index to set.
|
866
|
+
*/
|
867
|
+
async _setLastReadMessageIndex(index) {
|
868
|
+
const result = await this._services.commandExecutor.mutateResource("post", `${this._configuration.links.myConversations}/${this.sid}`, {
|
869
|
+
last_read_message_index: index,
|
870
|
+
});
|
871
|
+
return result.unread_messages_count;
|
872
|
+
}
|
856
873
|
}
|
857
874
|
/**
|
858
875
|
* Fired when a participant has joined the conversation.
|
859
876
|
*
|
860
877
|
* Parameters:
|
861
|
-
* 1. {@link Participant} `participant` - participant that joined the
|
878
|
+
* 1. {@link Participant} `participant` - participant that joined the
|
879
|
+
* conversation
|
862
880
|
* @event
|
863
881
|
*/
|
864
882
|
Conversation.participantJoined = "participantJoined";
|
@@ -866,7 +884,8 @@ Conversation.participantJoined = "participantJoined";
|
|
866
884
|
* Fired when a participant has left the conversation.
|
867
885
|
*
|
868
886
|
* Parameters:
|
869
|
-
* 1. {@link Participant} `participant` - participant that left the
|
887
|
+
* 1. {@link Participant} `participant` - participant that left the
|
888
|
+
* conversation
|
870
889
|
* @event
|
871
890
|
*/
|
872
891
|
Conversation.participantLeft = "participantLeft";
|
@@ -874,9 +893,12 @@ Conversation.participantLeft = "participantLeft";
|
|
874
893
|
* Fired when data of a participant has been updated.
|
875
894
|
*
|
876
895
|
* Parameters:
|
877
|
-
* 1. object `data` - info object provided with the event. It has the
|
878
|
-
*
|
879
|
-
* * {@link
|
896
|
+
* 1. object `data` - info object provided with the event. It has the
|
897
|
+
* following properties:
|
898
|
+
* * {@link Participant} `participant` - participant that has received the
|
899
|
+
* update
|
900
|
+
* * {@link ParticipantUpdateReason}[] `updateReasons` - array of reasons
|
901
|
+
* for the update
|
880
902
|
* @event
|
881
903
|
*/
|
882
904
|
Conversation.participantUpdated = "participantUpdated";
|
@@ -900,9 +922,11 @@ Conversation.messageRemoved = "messageRemoved";
|
|
900
922
|
* Fired when data of a message has been updated.
|
901
923
|
*
|
902
924
|
* Parameters:
|
903
|
-
* 1. object `data` - info object provided with the event. It has the
|
925
|
+
* 1. object `data` - info object provided with the event. It has the
|
926
|
+
* following properties:
|
904
927
|
* * {@link Message} `message` - message that has received the update
|
905
|
-
* * {@link MessageUpdateReason}[] `updateReasons` - array of reasons for
|
928
|
+
* * {@link MessageUpdateReason}[] `updateReasons` - array of reasons for
|
929
|
+
* the update
|
906
930
|
* @event
|
907
931
|
*/
|
908
932
|
Conversation.messageUpdated = "messageUpdated";
|
@@ -910,7 +934,8 @@ Conversation.messageUpdated = "messageUpdated";
|
|
910
934
|
* Fired when a participant has stopped typing.
|
911
935
|
*
|
912
936
|
* Parameters:
|
913
|
-
* 1. {@link Participant} `participant` - the participant that has stopped
|
937
|
+
* 1. {@link Participant} `participant` - the participant that has stopped
|
938
|
+
* typing
|
914
939
|
* @event
|
915
940
|
*/
|
916
941
|
Conversation.typingEnded = "typingEnded";
|
@@ -918,7 +943,8 @@ Conversation.typingEnded = "typingEnded";
|
|
918
943
|
* Fired when a participant has started typing.
|
919
944
|
*
|
920
945
|
* Parameters:
|
921
|
-
* 1. {@link Participant} `participant` - the participant that has started
|
946
|
+
* 1. {@link Participant} `participant` - the participant that has started
|
947
|
+
* typing
|
922
948
|
* @event
|
923
949
|
*/
|
924
950
|
Conversation.typingStarted = "typingStarted";
|
@@ -926,20 +952,28 @@ Conversation.typingStarted = "typingStarted";
|
|
926
952
|
* Fired when the data of the conversation has been updated.
|
927
953
|
*
|
928
954
|
* Parameters:
|
929
|
-
* 1. object `data` - info object provided with the event. It has the
|
930
|
-
*
|
931
|
-
* * {@link
|
955
|
+
* 1. object `data` - info object provided with the event. It has the
|
956
|
+
* following properties:
|
957
|
+
* * {@link Conversation} `conversation` - conversation that has received
|
958
|
+
* the update
|
959
|
+
* * {@link ConversationUpdateReason}[] `updateReasons` - array of reasons
|
960
|
+
* for the update
|
932
961
|
* @event
|
933
962
|
*/
|
934
963
|
Conversation.updated = "updated";
|
935
964
|
/**
|
936
|
-
* Fired when the conversation was destroyed or the currently-logged-in user
|
965
|
+
* Fired when the conversation was destroyed or the currently-logged-in user
|
966
|
+
* has left private conversation.
|
937
967
|
*
|
938
968
|
* Parameters:
|
939
969
|
* 1. {@link Conversation} `conversation` - conversation that has been removed
|
940
970
|
* @event
|
941
971
|
*/
|
942
972
|
Conversation.removed = "removed";
|
973
|
+
/**
|
974
|
+
* Logger instance.
|
975
|
+
*/
|
976
|
+
Conversation._logger = logger.Logger.scope("Conversation");
|
943
977
|
tslib_es6.__decorate([
|
944
978
|
declarativeTypeValidator.validateTypesAsync(declarativeTypeValidator.nonEmptyString, attributes.optionalAttributesValidator),
|
945
979
|
tslib_es6.__metadata("design:type", Function),
|
@@ -985,9 +1019,8 @@ tslib_es6.__decorate([
|
|
985
1019
|
tslib_es6.__decorate([
|
986
1020
|
declarativeTypeValidator.validateTypesAsync([
|
987
1021
|
"string",
|
1022
|
+
FormData,
|
988
1023
|
declarativeTypeValidator.literal(null),
|
989
|
-
// Wrapping it into a custom rule is necessary because the FormData class is not available on initialization.
|
990
|
-
declarativeTypeValidator.custom((value) => [value instanceof FormData, "an instance of FormData"]),
|
991
1024
|
declarativeTypeValidator.objectSchema("media options", {
|
992
1025
|
contentType: declarativeTypeValidator.nonEmptyString,
|
993
1026
|
media: declarativeTypeValidator.custom((value) => {
|
@@ -1027,7 +1060,7 @@ tslib_es6.__decorate([
|
|
1027
1060
|
tslib_es6.__metadata("design:returntype", Promise)
|
1028
1061
|
], Conversation.prototype, "updateAttributes", null);
|
1029
1062
|
tslib_es6.__decorate([
|
1030
|
-
declarativeTypeValidator.validateTypesAsync(
|
1063
|
+
declarativeTypeValidator.validateTypesAsync("string"),
|
1031
1064
|
tslib_es6.__metadata("design:type", Function),
|
1032
1065
|
tslib_es6.__metadata("design:paramtypes", [String]),
|
1033
1066
|
tslib_es6.__metadata("design:returntype", Promise)
|