@twilio/conversations 1.3.0-rc7 → 2.0.0-rc.3
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/CHANGELOG.md +201 -0
- package/README.md +5 -5
- package/dist/browser.js +7866 -0
- package/dist/browser.js.map +1 -0
- package/dist/docs/assets/css/main.css +2660 -0
- package/dist/docs/assets/images/icons.png +0 -0
- package/dist/docs/assets/images/icons@2x.png +0 -0
- package/dist/docs/assets/images/widgets.png +0 -0
- package/dist/docs/assets/images/widgets@2x.png +0 -0
- package/dist/docs/assets/js/main.js +248 -0
- package/dist/docs/assets/js/search.js +1 -0
- package/dist/docs/classes/AggregatedDeliveryReceipt.html +3286 -0
- package/dist/docs/classes/Client.html +4181 -0
- package/dist/docs/classes/Conversation.html +4397 -0
- package/dist/docs/classes/DetailedDeliveryReceipt.html +3265 -0
- package/dist/docs/classes/Media.html +3295 -0
- package/dist/docs/classes/Message.html +3713 -0
- package/dist/docs/classes/MessageBuilder.html +3280 -0
- package/dist/docs/classes/Participant.html +3517 -0
- package/dist/docs/classes/PushNotification.html +3232 -0
- package/dist/docs/classes/RestPaginator.html +3262 -0
- package/dist/docs/classes/UnsentMessage.html +3144 -0
- package/dist/docs/classes/User.html +3451 -0
- package/dist/docs/index.html +3387 -212
- package/dist/docs/interfaces/ClientOptions.html +3136 -0
- package/dist/docs/interfaces/ConversationState.html +3152 -0
- package/dist/docs/interfaces/CreateConversationOptions.html +3168 -0
- package/dist/docs/interfaces/LastMessage.html +3152 -0
- package/dist/docs/interfaces/Paginator.html +3243 -0
- package/dist/docs/interfaces/PushNotificationData.html +3168 -0
- package/dist/docs/interfaces/SendEmailOptions.html +3136 -0
- package/dist/docs/interfaces/SendMediaOptions.html +3169 -0
- package/dist/docs/modules.html +3418 -0
- package/dist/lib.d.ts +1944 -0
- package/dist/lib.js +7866 -0
- package/dist/lib.js.map +1 -0
- package/dist/post-install.js +29 -0
- package/dist/react-native.js +4031 -0
- package/dist/react-native.js.map +1 -0
- package/dist/twilio-conversations.js +40905 -0
- package/dist/twilio-conversations.min.js +156 -0
- package/package.json +59 -48
- package/.circleci/config.yml +0 -650
- package/.deepsource.toml +0 -6
- package/.env.example +0 -13
- package/browser/aggregateddeliveryreceipt.js +0 -152
- package/browser/client.js +0 -991
- package/browser/configuration.js +0 -80
- package/browser/conversation.js +0 -1826
- package/browser/data/conversations.js +0 -665
- package/browser/data/messages.js +0 -522
- package/browser/data/participants.js +0 -411
- package/browser/data/users.js +0 -281
- package/browser/detaileddeliveryreceipt.js +0 -44
- package/browser/index.js +0 -4
- package/browser/interfaces/limits.js +0 -3
- package/browser/interfaces/notificationtypes.js +0 -21
- package/browser/interfaces/paginator.js +0 -53
- package/browser/interfaces/responsecodes.js +0 -20
- package/browser/interfaces/transport.js +0 -3
- package/browser/logger.js +0 -141
- package/browser/media.js +0 -198
- package/browser/message.js +0 -685
- package/browser/participant.js +0 -415
- package/browser/pushnotification.js +0 -49
- package/browser/restpaginator.js +0 -74
- package/browser/services/network.js +0 -223
- package/browser/services/readhorizon.js +0 -181
- package/browser/services/typingindicator.js +0 -142
- package/browser/session.js +0 -505
- package/browser/sessionerror.js +0 -93
- package/browser/sri.json +0 -1
- package/browser/synclist.js +0 -90
- package/browser/synclistdescriptor.js +0 -29
- package/browser/syncpaginator.js +0 -63
- package/browser/user.js +0 -431
- package/browser/util/deferred.js +0 -56
- package/browser/util/index.js +0 -108
- package/dist/@twilio/conversations.js +0 -35565
- package/dist/@twilio/conversations.min.js +0 -204
- package/dist/docs/AggregatedDeliveryReceipt.html +0 -948
- package/dist/docs/Client.html +0 -5075
- package/dist/docs/Conversation.html +0 -6018
- package/dist/docs/DetailedDeliveryReceipt.html +0 -641
- package/dist/docs/Media.html +0 -694
- package/dist/docs/Message.html +0 -1934
- package/dist/docs/Paginator.html +0 -605
- package/dist/docs/Participant.html +0 -1510
- package/dist/docs/PushNotification.html +0 -836
- package/dist/docs/SessionError.html +0 -379
- package/dist/docs/User.html +0 -1315
- package/dist/docs/classes.list.html +0 -4431
- package/dist/docs/fonts/glyphicons-halflings-regular.eot +0 -0
- package/dist/docs/fonts/glyphicons-halflings-regular.svg +0 -288
- package/dist/docs/fonts/glyphicons-halflings-regular.ttf +0 -0
- package/dist/docs/fonts/glyphicons-halflings-regular.woff +0 -0
- package/dist/docs/fonts/glyphicons-halflings-regular.woff2 +0 -0
- package/dist/docs/img/glyphicons-halflings-white.png +0 -0
- package/dist/docs/img/glyphicons-halflings.png +0 -0
- package/dist/docs/quicksearch.html +0 -31
- package/dist/docs/scripts/docstrap.lib.js +0 -11
- package/dist/docs/scripts/fulltext-search-ui.js +0 -89
- package/dist/docs/scripts/fulltext-search.js +0 -36
- package/dist/docs/scripts/lunr.min.js +0 -6
- package/dist/docs/scripts/prettify/Apache-License-2.0.txt +0 -202
- package/dist/docs/scripts/prettify/jquery.min.js +0 -6
- package/dist/docs/scripts/prettify/lang-css.js +0 -21
- package/dist/docs/scripts/prettify/prettify.js +0 -496
- package/dist/docs/scripts/sunlight.js +0 -1157
- package/dist/docs/scripts/toc.js +0 -203
- package/dist/docs/styles/darkstrap.css +0 -960
- package/dist/docs/styles/prettify-tomorrow.css +0 -132
- package/dist/docs/styles/site.cerulean.css +0 -7008
- package/dist/docs/styles/site.cosmo.css +0 -7061
- package/dist/docs/styles/site.cyborg.css +0 -7048
- package/dist/docs/styles/site.darkly.css +0 -7171
- package/dist/docs/styles/site.darkstrap.css +0 -5638
- package/dist/docs/styles/site.dibs-bootstrap.css +0 -5899
- package/dist/docs/styles/site.flatly.css +0 -7147
- package/dist/docs/styles/site.journal.css +0 -6973
- package/dist/docs/styles/site.lumen.css +0 -7298
- package/dist/docs/styles/site.paper.css +0 -7623
- package/dist/docs/styles/site.readable.css +0 -6997
- package/dist/docs/styles/site.sandstone.css +0 -7035
- package/dist/docs/styles/site.simplex.css +0 -7023
- package/dist/docs/styles/site.slate.css +0 -7343
- package/dist/docs/styles/site.spacelab.css +0 -7055
- package/dist/docs/styles/site.superhero.css +0 -7131
- package/dist/docs/styles/site.united.css +0 -6895
- package/dist/docs/styles/site.yeti.css +0 -7195
- package/dist/docs/styles/sunlight.dark.css +0 -345
- package/dist/docs/styles/sunlight.default.css +0 -344
- package/dist/sri.json +0 -1
- package/lib/aggregateddeliveryreceipt.d.ts +0 -98
- package/lib/aggregateddeliveryreceipt.js +0 -112
- package/lib/client.d.ts +0 -317
- package/lib/client.js +0 -633
- package/lib/configuration.d.ts +0 -21
- package/lib/configuration.js +0 -41
- package/lib/conversation.d.ts +0 -460
- package/lib/conversation.js +0 -940
- package/lib/data/conversations.d.ts +0 -57
- package/lib/data/conversations.js +0 -318
- package/lib/data/messages.d.ts +0 -75
- package/lib/data/messages.js +0 -208
- package/lib/data/participants.d.ts +0 -94
- package/lib/data/participants.js +0 -204
- package/lib/data/users.d.ts +0 -42
- package/lib/data/users.js +0 -113
- package/lib/detaileddeliveryreceipt.d.ts +0 -45
- package/lib/detaileddeliveryreceipt.js +0 -34
- package/lib/index.d.ts +0 -1
- package/lib/index.js +0 -3
- package/lib/interfaces/limits.d.ts +0 -8
- package/lib/interfaces/limits.js +0 -2
- package/lib/interfaces/notificationtypes.d.ts +0 -8
- package/lib/interfaces/notificationtypes.js +0 -11
- package/lib/interfaces/paginator.d.ts +0 -52
- package/lib/interfaces/paginator.js +0 -28
- package/lib/interfaces/responsecodes.d.ts +0 -8
- package/lib/interfaces/responsecodes.js +0 -10
- package/lib/interfaces/transport.d.ts +0 -13
- package/lib/interfaces/transport.js +0 -2
- package/lib/logger.d.ts +0 -18
- package/lib/logger.js +0 -35
- package/lib/media.d.ts +0 -52
- package/lib/media.js +0 -69
- package/lib/message.d.ts +0 -136
- package/lib/message.js +0 -305
- package/lib/participant.d.ts +0 -142
- package/lib/participant.js +0 -219
- package/lib/pushnotification.d.ts +0 -48
- package/lib/pushnotification.js +0 -39
- package/lib/restpaginator.d.ts +0 -19
- package/lib/restpaginator.js +0 -36
- package/lib/services/network.d.ts +0 -22
- package/lib/services/network.js +0 -87
- package/lib/services/readhorizon.d.ts +0 -28
- package/lib/services/readhorizon.js +0 -112
- package/lib/services/typingindicator.d.ts +0 -50
- package/lib/services/typingindicator.js +0 -102
- package/lib/session.d.ts +0 -63
- package/lib/session.js +0 -213
- package/lib/sessionerror.d.ts +0 -12
- package/lib/sessionerror.js +0 -24
- package/lib/sri.json +0 -1
- package/lib/synclist.d.ts +0 -24
- package/lib/synclist.js +0 -28
- package/lib/synclistdescriptor.d.ts +0 -27
- package/lib/synclistdescriptor.js +0 -19
- package/lib/syncpaginator.d.ts +0 -22
- package/lib/syncpaginator.js +0 -37
- package/lib/user.d.ts +0 -98
- package/lib/user.js +0 -219
- package/lib/util/deferred.d.ts +0 -12
- package/lib/util/deferred.js +0 -22
- package/lib/util/index.d.ts +0 -28
- package/lib/util/index.js +0 -82
- package/tools/rtd-sdk-cdn-pin/.gitrepo +0 -12
- package/tools/rtd-sdk-cdn-pin/README.md +0 -54
- package/tools/rtd-sdk-cdn-pin/bintray-settings.xml.template +0 -16
- package/tools/rtd-sdk-cdn-pin/cdn-common/cdn-prepare.sh +0 -67
- package/tools/rtd-sdk-cdn-pin/cdn-pin-latest.sh +0 -19
- package/tools/rtd-sdk-cdn-pin/cdn-pin.sh +0 -19
- package/tools/rtd-sdk-cdn-pin/cdn-upload.sh +0 -13
- package/tools/rtd-sdk-cdn-pin/fetch/common/fetch-bintray-android.sh +0 -34
- package/tools/rtd-sdk-cdn-pin/fetch/common/fetch-npm.sh +0 -18
- package/tools/rtd-sdk-cdn-pin/fetch/twilio-chat-android.sh +0 -5
- package/tools/rtd-sdk-cdn-pin/fetch/twilio-chat-js.sh +0 -5
- package/tools/rtd-sdk-cdn-pin/fetch/twilio-conversations-android.sh +0 -6
- package/tools/rtd-sdk-cdn-pin/fetch/twilio-conversations-js.sh +0 -5
- package/tools/rtd-sdk-cdn-pin/fetch/twilio-sync-android.sh +0 -5
- package/tools/rtd-sdk-cdn-pin/fetch/twilio-sync-js.sh +0 -5
- package/tools/rtd-sdk-cdn-pin/fetch-artifact.sh +0 -26
- package/tools/sdk-release-tool/.gitrepo +0 -12
- package/tools/sdk-release-tool/Makefile +0 -49
- package/tools/sdk-release-tool/README.md +0 -275
- package/tools/sdk-release-tool/delete +0 -3
- package/tools/sdk-release-tool/download +0 -3
- package/tools/sdk-release-tool/get-cors +0 -3
- package/tools/sdk-release-tool/list +0 -3
- package/tools/sdk-release-tool/pin +0 -3
- package/tools/sdk-release-tool/pin-latest +0 -3
- package/tools/sdk-release-tool/requirements.txt +0 -4
- package/tools/sdk-release-tool/sdk-release-tool +0 -3
- package/tools/sdk-release-tool/sdk_release_tools/__init__.py +0 -0
- package/tools/sdk-release-tool/sdk_release_tools/__main__.py +0 -110
- package/tools/sdk-release-tool/sdk_release_tools/aws.py +0 -70
- package/tools/sdk-release-tool/sdk_release_tools/cli.py +0 -181
- package/tools/sdk-release-tool/sdk_release_tools/log.py +0 -24
- package/tools/sdk-release-tool/sdk_release_tools/ops.py +0 -295
- package/tools/sdk-release-tool/sdk_release_tools/rpm.py +0 -28
- package/tools/sdk-release-tool/sdk_release_tools/util.py +0 -186
- package/tools/sdk-release-tool/sdk_release_tools/versions.py +0 -362
- package/tools/sdk-release-tool/signal-sdk-js.json +0 -28
- package/tools/sdk-release-tool/tests/test_versions.py +0 -94
- package/tools/sdk-release-tool/twilio-accessmanager-android.json +0 -19
- package/tools/sdk-release-tool/twilio-accessmanager-ios.json +0 -26
- package/tools/sdk-release-tool/twilio-auth-ios.json +0 -29
- package/tools/sdk-release-tool/twilio-authenticator-ios.json +0 -29
- package/tools/sdk-release-tool/twilio-chat-android-2.json +0 -19
- package/tools/sdk-release-tool/twilio-chat-android.json +0 -22
- package/tools/sdk-release-tool/twilio-chat-ios.json +0 -26
- package/tools/sdk-release-tool/twilio-chat-js.json +0 -27
- package/tools/sdk-release-tool/twilio-client-android-aar.json +0 -22
- package/tools/sdk-release-tool/twilio-client-android.json +0 -22
- package/tools/sdk-release-tool/twilio-client-ios.json +0 -20
- package/tools/sdk-release-tool/twilio-client-js.json +0 -17
- package/tools/sdk-release-tool/twilio-client-sounds-js.json +0 -14
- package/tools/sdk-release-tool/twilio-common-android-maven.json +0 -16
- package/tools/sdk-release-tool/twilio-common-android.json +0 -22
- package/tools/sdk-release-tool/twilio-common-ios-rtc.json +0 -25
- package/tools/sdk-release-tool/twilio-common-ios.json +0 -25
- package/tools/sdk-release-tool/twilio-common-js.json +0 -19
- package/tools/sdk-release-tool/twilio-conversations-android.json +0 -22
- package/tools/sdk-release-tool/twilio-conversations-ios.json +0 -26
- package/tools/sdk-release-tool/twilio-conversations-js.json +0 -27
- package/tools/sdk-release-tool/twilio-flex-js.json +0 -20
- package/tools/sdk-release-tool/twilio-flex-webchat-js.json +0 -16
- package/tools/sdk-release-tool/twilio-frame-chat-android.json +0 -22
- package/tools/sdk-release-tool/twilio-frame-chat-ios.json +0 -23
- package/tools/sdk-release-tool/twilio-frame-chat-js.json +0 -23
- package/tools/sdk-release-tool/twilio-ip-messaging-android-maven.json +0 -16
- package/tools/sdk-release-tool/twilio-ip-messaging-android.json +0 -22
- package/tools/sdk-release-tool/twilio-ip-messaging-ios.json +0 -25
- package/tools/sdk-release-tool/twilio-ip-messaging-js.json +0 -21
- package/tools/sdk-release-tool/twilio-sync-android.json +0 -22
- package/tools/sdk-release-tool/twilio-sync-ios.json +0 -26
- package/tools/sdk-release-tool/twilio-sync-js.json +0 -39
- package/tools/sdk-release-tool/twilio-taskrouter-js.json +0 -17
- package/tools/sdk-release-tool/twilio-video-android.json +0 -22
- package/tools/sdk-release-tool/twilio-video-cpp-linux-cross-rootfs.json +0 -13
- package/tools/sdk-release-tool/twilio-video-cpp.json +0 -33
- package/tools/sdk-release-tool/twilio-video-ios.json +0 -25
- package/tools/sdk-release-tool/twilio-video-js.json +0 -19
- package/tools/sdk-release-tool/twilio-video-jsdocs.json +0 -14
- package/tools/sdk-release-tool/twilio-voice-android.json +0 -22
- package/tools/sdk-release-tool/twilio-voice-ios.json +0 -29
- package/tools/sdk-release-tool/unpin +0 -3
- package/tools/sdk-release-tool/unpin-latest +0 -3
- package/tools/sdk-release-tool/upload +0 -3
- package/twilio-chat.js.iml +0 -9
@@ -1,57 +0,0 @@
|
|
1
|
-
/// <reference types="node" />
|
2
|
-
import { EventEmitter } from 'events';
|
3
|
-
import { Conversation } from '../conversation';
|
4
|
-
import { SyncClient } from 'twilio-sync';
|
5
|
-
import { Session } from '../session';
|
6
|
-
import { SyncList } from '../synclist';
|
7
|
-
import { Users } from './users';
|
8
|
-
import { Network } from '../services/network';
|
9
|
-
import { TypingIndicator } from '../services/typingindicator';
|
10
|
-
import { ReadHorizon } from '../services/readhorizon';
|
11
|
-
import { McsClient } from 'twilio-mcs-client';
|
12
|
-
import { Deferred } from '../util/deferred';
|
13
|
-
export interface ConversationsServices {
|
14
|
-
session: Session;
|
15
|
-
syncClient: SyncClient;
|
16
|
-
syncList: SyncList;
|
17
|
-
users: Users;
|
18
|
-
typingIndicator: TypingIndicator;
|
19
|
-
readHorizon: ReadHorizon;
|
20
|
-
network: Network;
|
21
|
-
mcsClient: McsClient;
|
22
|
-
}
|
23
|
-
/**
|
24
|
-
* Represents conversations collection
|
25
|
-
* {@see Conversation}
|
26
|
-
*/
|
27
|
-
declare class Conversations extends EventEmitter {
|
28
|
-
private services;
|
29
|
-
readonly conversations: Map<string, Conversation>;
|
30
|
-
private readonly tombstones;
|
31
|
-
private syncListFetched;
|
32
|
-
readonly syncListRead: Deferred<boolean>;
|
33
|
-
constructor(services: ConversationsServices);
|
34
|
-
private getMap;
|
35
|
-
/**
|
36
|
-
* Add conversation to server
|
37
|
-
* @private
|
38
|
-
* @returns {Promise<Conversation>} Conversation
|
39
|
-
*/
|
40
|
-
addConversation(options: any): Promise<Conversation>;
|
41
|
-
/**
|
42
|
-
* Fetch conversations list and instantiate all necessary objects
|
43
|
-
*/
|
44
|
-
fetchConversations(): void;
|
45
|
-
private _wrapPaginator;
|
46
|
-
getConversations(args: any): Promise<any>;
|
47
|
-
getConversation(sid: string): Promise<Conversation>;
|
48
|
-
getConversationByUniqueName(uniqueName: string): Promise<Conversation>;
|
49
|
-
getWhisperConversation(sid: string): Promise<Conversation>;
|
50
|
-
private upsertConversation;
|
51
|
-
private onConversationRemoved;
|
52
|
-
private registerForEvents;
|
53
|
-
}
|
54
|
-
declare namespace Conversations {
|
55
|
-
type DataSource = 'sync' | 'chat' | 'synclist';
|
56
|
-
}
|
57
|
-
export { Conversation, Conversations };
|
@@ -1,318 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
const events_1 = require("events");
|
4
|
-
const logger_1 = require("../logger");
|
5
|
-
const conversation_1 = require("../conversation");
|
6
|
-
exports.Conversation = conversation_1.Conversation;
|
7
|
-
const deferred_1 = require("../util/deferred");
|
8
|
-
const util_1 = require("../util");
|
9
|
-
const log = logger_1.Logger.scope('Conversations');
|
10
|
-
async function wrapService(services) {
|
11
|
-
return {
|
12
|
-
limits: await services.session.getMediaLimits(),
|
13
|
-
session: services.session,
|
14
|
-
syncClient: services.syncClient,
|
15
|
-
users: services.users,
|
16
|
-
typingIndicator: services.typingIndicator,
|
17
|
-
readHorizon: services.readHorizon,
|
18
|
-
network: services.network,
|
19
|
-
mcsClient: services.mcsClient,
|
20
|
-
};
|
21
|
-
}
|
22
|
-
/**
|
23
|
-
* Represents conversations collection
|
24
|
-
* {@see Conversation}
|
25
|
-
*/
|
26
|
-
class Conversations extends events_1.EventEmitter {
|
27
|
-
constructor(services) {
|
28
|
-
super();
|
29
|
-
this.services = services;
|
30
|
-
this.conversations = new Map();
|
31
|
-
this.tombstones = new Set();
|
32
|
-
this.syncListFetched = false;
|
33
|
-
this.syncListRead = new deferred_1.Deferred();
|
34
|
-
}
|
35
|
-
getMap() {
|
36
|
-
return this.services.session.getMyConversationsId()
|
37
|
-
.then(name => this.services.syncClient.map({ id: name, mode: 'open_existing' }));
|
38
|
-
}
|
39
|
-
/**
|
40
|
-
* Add conversation to server
|
41
|
-
* @private
|
42
|
-
* @returns {Promise<Conversation>} Conversation
|
43
|
-
*/
|
44
|
-
async addConversation(options) {
|
45
|
-
let attributes;
|
46
|
-
if (typeof options.attributes === 'undefined') {
|
47
|
-
attributes = {};
|
48
|
-
}
|
49
|
-
else {
|
50
|
-
attributes = options.attributes;
|
51
|
-
}
|
52
|
-
let response = await this.services.session.addCommand('createConversation', {
|
53
|
-
friendlyName: options.friendlyName,
|
54
|
-
uniqueName: options.uniqueName,
|
55
|
-
attributes: JSON.stringify(attributes)
|
56
|
-
});
|
57
|
-
let conversationSid = 'conversationSid' in response ? response['conversationSid'] : null;
|
58
|
-
let conversationDocument = 'conversation' in response ? response['conversation'] : null;
|
59
|
-
let existingConversation = this.conversations.get(conversationSid);
|
60
|
-
if (existingConversation) {
|
61
|
-
await existingConversation._subscribe();
|
62
|
-
return existingConversation;
|
63
|
-
}
|
64
|
-
let conversation = new conversation_1.Conversation(await wrapService(this.services), {
|
65
|
-
channel: conversationDocument,
|
66
|
-
entityName: null,
|
67
|
-
uniqueName: null,
|
68
|
-
attributes: null,
|
69
|
-
createdBy: null,
|
70
|
-
friendlyName: null,
|
71
|
-
lastConsumedMessageIndex: null,
|
72
|
-
dateCreated: null,
|
73
|
-
dateUpdated: null
|
74
|
-
}, conversationSid);
|
75
|
-
this.conversations.set(conversation.sid, conversation);
|
76
|
-
this.registerForEvents(conversation);
|
77
|
-
await conversation._subscribe();
|
78
|
-
this.emit('conversationAdded', conversation);
|
79
|
-
return conversation;
|
80
|
-
}
|
81
|
-
/**
|
82
|
-
* Fetch conversations list and instantiate all necessary objects
|
83
|
-
*/
|
84
|
-
fetchConversations() {
|
85
|
-
this.getMap()
|
86
|
-
.then(async (map) => {
|
87
|
-
map.on('itemAdded', args => {
|
88
|
-
log.debug('itemAdded: ' + args.item.key);
|
89
|
-
this.upsertConversation('sync', args.item.key, args.item.data);
|
90
|
-
});
|
91
|
-
map.on('itemRemoved', args => {
|
92
|
-
log.debug('itemRemoved: ' + args.key);
|
93
|
-
let sid = args.key;
|
94
|
-
if (!this.syncListFetched) {
|
95
|
-
this.tombstones.add(sid);
|
96
|
-
}
|
97
|
-
let conversation = this.conversations.get(sid);
|
98
|
-
if (conversation) {
|
99
|
-
if (conversation && conversation.status === 'joined' /*|| conversation.status === 'invited'*/) {
|
100
|
-
conversation._setStatus('notParticipating', 'sync');
|
101
|
-
this.emit('conversationLeft', conversation);
|
102
|
-
}
|
103
|
-
this.conversations.delete(sid);
|
104
|
-
this.emit('conversationRemoved', conversation);
|
105
|
-
conversation.emit('removed', conversation);
|
106
|
-
}
|
107
|
-
});
|
108
|
-
map.on('itemUpdated', args => {
|
109
|
-
log.debug('itemUpdated: ' + args.item.key);
|
110
|
-
this.upsertConversation('sync', args.item.key, args.item.data);
|
111
|
-
});
|
112
|
-
let upserts = [];
|
113
|
-
let paginator = await this.services.syncList.getPage();
|
114
|
-
let items = paginator.items;
|
115
|
-
items.forEach(item => {
|
116
|
-
upserts.push(this.upsertConversation('synclist', item.channel_sid, item));
|
117
|
-
});
|
118
|
-
while (paginator.hasNextPage) {
|
119
|
-
paginator = await paginator.nextPage();
|
120
|
-
paginator.items.forEach(item => {
|
121
|
-
upserts.push(this.upsertConversation('synclist', item.channel_sid, item));
|
122
|
-
});
|
123
|
-
}
|
124
|
-
this.syncListRead.set(true);
|
125
|
-
return Promise.all(upserts);
|
126
|
-
})
|
127
|
-
.then(() => {
|
128
|
-
this.syncListFetched = true;
|
129
|
-
this.tombstones.clear();
|
130
|
-
log.debug('Conversations list fetched');
|
131
|
-
})
|
132
|
-
.then(() => this)
|
133
|
-
.catch(e => {
|
134
|
-
if (this.services.syncClient.connectionState != 'disconnected') {
|
135
|
-
log.error('Failed to get conversations list', e);
|
136
|
-
}
|
137
|
-
log.debug('ERROR: Failed to get conversations list', e);
|
138
|
-
throw e;
|
139
|
-
});
|
140
|
-
}
|
141
|
-
_wrapPaginator(page, op) {
|
142
|
-
return op(page.items)
|
143
|
-
.then(items => ({
|
144
|
-
items: items,
|
145
|
-
hasNextPage: page.hasNextPage,
|
146
|
-
hasPrevPage: page.hasPrevPage,
|
147
|
-
nextPage: () => page.nextPage().then(x => this._wrapPaginator(x, op)),
|
148
|
-
prevPage: () => page.prevPage().then(x => this._wrapPaginator(x, op))
|
149
|
-
}));
|
150
|
-
}
|
151
|
-
getConversations(args) {
|
152
|
-
return this.getMap()
|
153
|
-
.then(conversationsMap => conversationsMap.getItems(args))
|
154
|
-
.then(page => this._wrapPaginator(page, items => Promise.all(items.map(item => this.upsertConversation('sync', item.key, item.data)))));
|
155
|
-
}
|
156
|
-
getConversation(sid) {
|
157
|
-
return this.getMap()
|
158
|
-
.then(conversationsMap => conversationsMap.getItems({ key: sid }))
|
159
|
-
.then(page => page.items.map(item => this.upsertConversation('sync', item.key, item.data)))
|
160
|
-
.then(items => items.length > 0 ? items[0] : null);
|
161
|
-
}
|
162
|
-
async getConversationByUniqueName(uniqueName) {
|
163
|
-
var _a, _b;
|
164
|
-
const links = await this.services.session.getSessionLinks();
|
165
|
-
const url = new util_1.UriBuilder(links.myChannelsUrl).path(uniqueName).build();
|
166
|
-
const response = await this.services.network.get(url);
|
167
|
-
const body = response.body;
|
168
|
-
const sid = body.channel_sid;
|
169
|
-
const data = {
|
170
|
-
entityName: null,
|
171
|
-
lastConsumedMessageIndex: body.last_consumed_message_index,
|
172
|
-
status: ((_a = body) === null || _a === void 0 ? void 0 : _a.status) || 'unknown',
|
173
|
-
friendlyName: body.friendly_name,
|
174
|
-
dateUpdated: body.date_updated,
|
175
|
-
dateCreated: body.date_created,
|
176
|
-
uniqueName: body.unique_name,
|
177
|
-
createdBy: body.created_by,
|
178
|
-
attributes: body.attributes,
|
179
|
-
channel: `${sid}.channel`,
|
180
|
-
notificationLevel: (_b = body) === null || _b === void 0 ? void 0 : _b.notification_level,
|
181
|
-
sid
|
182
|
-
};
|
183
|
-
return this.upsertConversation('sync', sid, data);
|
184
|
-
}
|
185
|
-
async getWhisperConversation(sid) {
|
186
|
-
var _a, _b, _c, _d;
|
187
|
-
const links = await this.services.session.getSessionLinks();
|
188
|
-
const url = new util_1.UriBuilder(links.publicChannelsUrl).path(sid).build();
|
189
|
-
const response = await this.services.network.get(url);
|
190
|
-
const body = response.body;
|
191
|
-
if (body.type !== 'private') {
|
192
|
-
return;
|
193
|
-
}
|
194
|
-
// todo: refactor this after the back-end change.
|
195
|
-
// Currently, a conversation that is created using a non-conversations-specific
|
196
|
-
// endpoint (i.e., a chat-specific endpoint) will not have a state property set.
|
197
|
-
// The back-end team will fix this, but only when they get some more time to work
|
198
|
-
// on this. For now, the SDK will assume that the default state is active when
|
199
|
-
// the property is absent from the REST response. The back-end team also mentioned
|
200
|
-
// that the state property will become a proper JSON object, as opposed to a JSON
|
201
|
-
// string, which is also covered in the following code.
|
202
|
-
let state;
|
203
|
-
// If the state property is a string, it's expected to be a string that represents
|
204
|
-
// a JSON object.
|
205
|
-
if (typeof body.state === 'string') {
|
206
|
-
state = JSON.parse(body.state);
|
207
|
-
}
|
208
|
-
// If the state property is already a non-nullable object, then no JSON parsing is
|
209
|
-
// required.
|
210
|
-
if (typeof body.state === 'object' && body.state !== null) {
|
211
|
-
state = body.state;
|
212
|
-
}
|
213
|
-
if (((_b = (_a = state) === null || _a === void 0 ? void 0 : _a['state.v1']) === null || _b === void 0 ? void 0 : _b.current) === 'closed') {
|
214
|
-
return;
|
215
|
-
}
|
216
|
-
const data = {
|
217
|
-
entityName: null,
|
218
|
-
lastConsumedMessageIndex: body.last_consumed_message_index,
|
219
|
-
status: ((_c = body) === null || _c === void 0 ? void 0 : _c.status) || 'unknown',
|
220
|
-
friendlyName: body.friendly_name,
|
221
|
-
dateUpdated: body.date_updated,
|
222
|
-
dateCreated: body.date_created,
|
223
|
-
uniqueName: body.unique_name,
|
224
|
-
createdBy: body.created_by,
|
225
|
-
attributes: body.attributes,
|
226
|
-
channel: `${sid}.channel`,
|
227
|
-
notificationLevel: (_d = body) === null || _d === void 0 ? void 0 : _d.notification_level,
|
228
|
-
sid
|
229
|
-
};
|
230
|
-
return this.upsertConversation('sync', sid, data);
|
231
|
-
}
|
232
|
-
async upsertConversation(source, sid, data) {
|
233
|
-
log.trace('upsertConversation(sid=' + sid + ', data=', data);
|
234
|
-
let conversation = this.conversations.get(sid);
|
235
|
-
// Update the Conversation's status if we know about it
|
236
|
-
if (conversation) {
|
237
|
-
log.trace('upsertConversation: conversation ' + sid + ' is known and it\'s' +
|
238
|
-
' status is known from source ' + conversation._statusSource() +
|
239
|
-
' and update came from source ' + source, conversation);
|
240
|
-
if (typeof conversation._statusSource() === 'undefined'
|
241
|
-
|| source === conversation._statusSource()
|
242
|
-
|| (source === 'synclist' && conversation._statusSource() !== 'sync')
|
243
|
-
|| source === 'sync') {
|
244
|
-
if (data.status === 'joined' && conversation.status !== 'joined') {
|
245
|
-
conversation._setStatus('joined', source);
|
246
|
-
let updateData = {};
|
247
|
-
if (typeof data.notificationLevel !== 'undefined') {
|
248
|
-
updateData.notificationLevel = data.notificationLevel;
|
249
|
-
}
|
250
|
-
if (typeof data.lastConsumedMessageIndex !== 'undefined') {
|
251
|
-
updateData.lastConsumedMessageIndex = data.lastConsumedMessageIndex;
|
252
|
-
}
|
253
|
-
if (!util_1.isDeepEqual(updateData, {})) {
|
254
|
-
conversation._update(updateData);
|
255
|
-
}
|
256
|
-
conversation._subscribe().then(() => { this.emit('conversationJoined', conversation); });
|
257
|
-
}
|
258
|
-
else if (data.status === 'notParticipating' && conversation.status === 'joined') {
|
259
|
-
conversation._setStatus('notParticipating', source);
|
260
|
-
conversation._update(data);
|
261
|
-
conversation._subscribe().then(() => { this.emit('conversationLeft', conversation); });
|
262
|
-
}
|
263
|
-
else if (data.status === 'notParticipating') {
|
264
|
-
conversation._subscribe();
|
265
|
-
}
|
266
|
-
else {
|
267
|
-
conversation._update(data);
|
268
|
-
}
|
269
|
-
}
|
270
|
-
else {
|
271
|
-
log.trace('upsertConversation: conversation is known from sync and came from chat, ignoring', {
|
272
|
-
sid: sid,
|
273
|
-
data: data.status,
|
274
|
-
conversation: conversation.status
|
275
|
-
});
|
276
|
-
}
|
277
|
-
return conversation._subscribe().then(() => conversation);
|
278
|
-
}
|
279
|
-
if ((source === 'chat' || source === 'synclist') && this.tombstones.has(sid)) {
|
280
|
-
// if conversation was deleted, we ignore it
|
281
|
-
log.trace('upsertConversation: conversation is deleted and came again from chat, ignoring', sid);
|
282
|
-
return;
|
283
|
-
}
|
284
|
-
// Fetch the Conversation if we don't know about it
|
285
|
-
log.trace('upsertConversation: creating local conversation object with sid ' + sid, data);
|
286
|
-
conversation = new conversation_1.Conversation(await wrapService(this.services), data, sid);
|
287
|
-
this.conversations.set(sid, conversation);
|
288
|
-
return conversation._subscribe().then(() => {
|
289
|
-
this.registerForEvents(conversation);
|
290
|
-
this.emit('conversationAdded', conversation);
|
291
|
-
if (data.status === 'joined') {
|
292
|
-
conversation._setStatus('joined', source);
|
293
|
-
this.emit('conversationJoined', conversation);
|
294
|
-
}
|
295
|
-
return conversation;
|
296
|
-
});
|
297
|
-
}
|
298
|
-
onConversationRemoved(sid) {
|
299
|
-
let conversation = this.conversations.get(sid);
|
300
|
-
if (conversation) {
|
301
|
-
this.conversations.delete(sid);
|
302
|
-
this.emit('conversationRemoved', conversation);
|
303
|
-
}
|
304
|
-
}
|
305
|
-
registerForEvents(conversation) {
|
306
|
-
conversation.on('removed', () => this.onConversationRemoved(conversation.sid));
|
307
|
-
conversation.on('updated', (args) => this.emit('conversationUpdated', args));
|
308
|
-
conversation.on('participantJoined', this.emit.bind(this, 'participantJoined'));
|
309
|
-
conversation.on('participantLeft', this.emit.bind(this, 'participantLeft'));
|
310
|
-
conversation.on('participantUpdated', (args) => this.emit('participantUpdated', args));
|
311
|
-
conversation.on('messageAdded', this.emit.bind(this, 'messageAdded'));
|
312
|
-
conversation.on('messageUpdated', (args) => this.emit('messageUpdated', args));
|
313
|
-
conversation.on('messageRemoved', this.emit.bind(this, 'messageRemoved'));
|
314
|
-
conversation.on('typingStarted', this.emit.bind(this, 'typingStarted'));
|
315
|
-
conversation.on('typingEnded', this.emit.bind(this, 'typingEnded'));
|
316
|
-
}
|
317
|
-
}
|
318
|
-
exports.Conversations = Conversations;
|
package/lib/data/messages.d.ts
DELETED
@@ -1,75 +0,0 @@
|
|
1
|
-
/// <reference types="node" />
|
2
|
-
import { EventEmitter } from 'events';
|
3
|
-
import { Message } from '../message';
|
4
|
-
import { Conversation, UnsentMessage } from '../conversation';
|
5
|
-
import { SyncList, SyncClient } from 'twilio-sync';
|
6
|
-
import { SyncPaginator } from '../syncpaginator';
|
7
|
-
import { Session } from '../session';
|
8
|
-
import { McsClient } from 'twilio-mcs-client';
|
9
|
-
import { Network } from '../services/network';
|
10
|
-
export interface MessagesServices {
|
11
|
-
session: Session;
|
12
|
-
mcsClient: McsClient;
|
13
|
-
network: Network;
|
14
|
-
syncClient: SyncClient;
|
15
|
-
}
|
16
|
-
/**
|
17
|
-
* Represents the collection of messages in a conversation
|
18
|
-
*/
|
19
|
-
declare class Messages extends EventEmitter {
|
20
|
-
private readonly services;
|
21
|
-
private readonly messagesByIndex;
|
22
|
-
private messagesListPromise;
|
23
|
-
readonly conversation: Conversation;
|
24
|
-
constructor(conversation: Conversation, services: MessagesServices);
|
25
|
-
/**
|
26
|
-
* Subscribe to the Messages Event Stream
|
27
|
-
* @param {String} name - The name of Sync object for the Messages resource.
|
28
|
-
* @returns {Promise}
|
29
|
-
*/
|
30
|
-
subscribe(name: string): Promise<SyncList>;
|
31
|
-
unsubscribe(): Promise<void>;
|
32
|
-
/**
|
33
|
-
* Send Message to the conversation, message could include both text and multiple media atttachments.
|
34
|
-
* @param {String} message - Message to post
|
35
|
-
* @param {any} attributes Message attributes
|
36
|
-
* @param {Conversation.SendEmailOptions} emailOptions Options that modify E-mail integration behaviors.
|
37
|
-
* @returns Returns promise which can fail
|
38
|
-
*/
|
39
|
-
sendV2(message: UnsentMessage): Promise<any>;
|
40
|
-
/**
|
41
|
-
* Send Message to the conversation
|
42
|
-
* @param {String} message - Message to post
|
43
|
-
* @param {any} attributes Message attributes
|
44
|
-
* @param {Conversation.SendEmailOptions} emailOptions Options that modify E-mail integration behaviors.
|
45
|
-
* @returns Returns promise which can fail
|
46
|
-
*/
|
47
|
-
send(message: string, attributes?: any, emailOptions?: Conversation.SendEmailOptions): Promise<any>;
|
48
|
-
/**
|
49
|
-
* Send Media Message to the conversation
|
50
|
-
* @param {FormData | Conversation#SendMediaOptions} mediaContent - Media content to post
|
51
|
-
* @param {any} attributes Message attributes
|
52
|
-
* @returns Returns promise which can fail
|
53
|
-
*/
|
54
|
-
sendMedia(mediaContent: FormData | Conversation.SendMediaOptions, attributes?: any, emailOptions?: Conversation.SendEmailOptions): Promise<any>;
|
55
|
-
/**
|
56
|
-
* Returns messages from conversation using paginator interface
|
57
|
-
* @param {Number} [pageSize] Number of messages to return in single chunk. By default it's 30.
|
58
|
-
* @param {String} [anchor] Most early message id which is already known, or 'end' by default
|
59
|
-
* @param {String} [direction] Pagination order 'backwards' or 'forward', 'forward' by default
|
60
|
-
* @returns {Promise<Paginator<Message>>} last page of messages by default
|
61
|
-
*/
|
62
|
-
getMessages(pageSize: any, anchor: any, direction: any): Promise<SyncPaginator<Message>>;
|
63
|
-
private wrapPaginator;
|
64
|
-
private _upsertMessage;
|
65
|
-
/**
|
66
|
-
* Returns last messages from conversation
|
67
|
-
* @param {Number} [pageSize] Number of messages to return in single chunk. By default it's 30.
|
68
|
-
* @param {String} [anchor] Most early message id which is already known, or 'end' by default
|
69
|
-
* @param {String} [direction] Pagination order 'backwards' or 'forward', or 'forward' by default
|
70
|
-
* @returns {Promise<SyncPaginator<Message>>} last page of messages by default
|
71
|
-
* @private
|
72
|
-
*/
|
73
|
-
private _getMessages;
|
74
|
-
}
|
75
|
-
export { Messages };
|
package/lib/data/messages.js
DELETED
@@ -1,208 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
const events_1 = require("events");
|
4
|
-
const logger_1 = require("../logger");
|
5
|
-
const message_1 = require("../message");
|
6
|
-
const log = logger_1.Logger.scope('Messages');
|
7
|
-
/**
|
8
|
-
* Represents the collection of messages in a conversation
|
9
|
-
*/
|
10
|
-
class Messages extends events_1.EventEmitter {
|
11
|
-
constructor(conversation, services) {
|
12
|
-
super();
|
13
|
-
this.conversation = conversation;
|
14
|
-
this.services = services;
|
15
|
-
this.messagesByIndex = new Map();
|
16
|
-
this.messagesListPromise = null;
|
17
|
-
}
|
18
|
-
/**
|
19
|
-
* Subscribe to the Messages Event Stream
|
20
|
-
* @param {String} name - The name of Sync object for the Messages resource.
|
21
|
-
* @returns {Promise}
|
22
|
-
*/
|
23
|
-
subscribe(name) {
|
24
|
-
return this.messagesListPromise =
|
25
|
-
this.messagesListPromise ||
|
26
|
-
this.services.syncClient.list({ id: name, mode: 'open_existing' })
|
27
|
-
.then(list => {
|
28
|
-
list.on('itemAdded', args => {
|
29
|
-
log.debug(this.conversation.sid + ' itemAdded: ' + args.item.index);
|
30
|
-
let message = new message_1.Message(this.conversation, this.services, args.item.index, args.item.data);
|
31
|
-
if (this.messagesByIndex.has(message.index)) {
|
32
|
-
log.debug('Message arrived, but already known and ignored', this.conversation.sid, message.index);
|
33
|
-
return;
|
34
|
-
}
|
35
|
-
this.messagesByIndex.set(message.index, message);
|
36
|
-
message.on('updated', (args) => this.emit('messageUpdated', args));
|
37
|
-
this.emit('messageAdded', message);
|
38
|
-
});
|
39
|
-
list.on('itemRemoved', args => {
|
40
|
-
log.debug(this.conversation.sid + ' itemRemoved: ' + args.index);
|
41
|
-
let index = args.index;
|
42
|
-
if (this.messagesByIndex.has(index)) {
|
43
|
-
let message = this.messagesByIndex.get(index);
|
44
|
-
this.messagesByIndex.delete(message.index);
|
45
|
-
message.removeAllListeners('updated');
|
46
|
-
this.emit('messageRemoved', message);
|
47
|
-
}
|
48
|
-
});
|
49
|
-
list.on('itemUpdated', args => {
|
50
|
-
log.debug(this.conversation.sid + ' itemUpdated: ' + args.item.index);
|
51
|
-
let message = this.messagesByIndex.get(args.item.index);
|
52
|
-
if (message) {
|
53
|
-
message._update(args.item.data);
|
54
|
-
}
|
55
|
-
});
|
56
|
-
return list;
|
57
|
-
})
|
58
|
-
.catch(err => {
|
59
|
-
this.messagesListPromise = null;
|
60
|
-
if (this.services.syncClient.connectionState != 'disconnected') {
|
61
|
-
log.error('Failed to get messages object for conversation', this.conversation.sid, err);
|
62
|
-
}
|
63
|
-
log.debug('ERROR: Failed to get messages object for conversation', this.conversation.sid, err);
|
64
|
-
throw err;
|
65
|
-
});
|
66
|
-
}
|
67
|
-
async unsubscribe() {
|
68
|
-
if (this.messagesListPromise) {
|
69
|
-
let entity = await this.messagesListPromise;
|
70
|
-
entity.close();
|
71
|
-
this.messagesListPromise = null;
|
72
|
-
}
|
73
|
-
}
|
74
|
-
/**
|
75
|
-
* Send Message to the conversation, message could include both text and multiple media atttachments.
|
76
|
-
* @param {String} message - Message to post
|
77
|
-
* @param {any} attributes Message attributes
|
78
|
-
* @param {Conversation.SendEmailOptions} emailOptions Options that modify E-mail integration behaviors.
|
79
|
-
* @returns Returns promise which can fail
|
80
|
-
*/
|
81
|
-
async sendV2(message) {
|
82
|
-
var _a;
|
83
|
-
log.debug('Sending message V2', message.mediaContent, message.attributes, message.emailOptions);
|
84
|
-
let media = new Array();
|
85
|
-
for (const [category, mediaContent] of message.mediaContent) {
|
86
|
-
if (typeof FormData !== 'undefined' && (mediaContent instanceof FormData)) {
|
87
|
-
log.debug('Adding media to a message as FormData', mediaContent);
|
88
|
-
media.push(await this.services.mcsClient.postFormData(mediaContent, category));
|
89
|
-
}
|
90
|
-
else {
|
91
|
-
log.debug('Adding media to a message as SendMediaOptions', mediaContent);
|
92
|
-
const mediaOptions = mediaContent;
|
93
|
-
if (!mediaOptions.contentType || !mediaOptions.media) {
|
94
|
-
throw new Error('Media content <Conversation#SendMediaOptions> must contain non-empty contentType and media');
|
95
|
-
}
|
96
|
-
media.push(await this.services.mcsClient.post(mediaOptions.contentType, mediaOptions.media, category));
|
97
|
-
}
|
98
|
-
}
|
99
|
-
return this.services.session.addCommand('sendMessageV2', {
|
100
|
-
conversationSid: this.conversation.sid,
|
101
|
-
text: message.text,
|
102
|
-
subject: (_a = message.emailOptions) === null || _a === void 0 ? void 0 : _a.subject,
|
103
|
-
mediaSids: [...media.map((m) => m.sid)],
|
104
|
-
attributes: JSON.stringify(message.attributes),
|
105
|
-
});
|
106
|
-
}
|
107
|
-
/**
|
108
|
-
* Send Message to the conversation
|
109
|
-
* @param {String} message - Message to post
|
110
|
-
* @param {any} attributes Message attributes
|
111
|
-
* @param {Conversation.SendEmailOptions} emailOptions Options that modify E-mail integration behaviors.
|
112
|
-
* @returns Returns promise which can fail
|
113
|
-
*/
|
114
|
-
async send(message, attributes = {}, emailOptions) {
|
115
|
-
var _a;
|
116
|
-
log.debug('Sending text message', message, attributes, emailOptions);
|
117
|
-
return this.services.session.addCommand('sendMessage', {
|
118
|
-
channelSid: this.conversation.sid,
|
119
|
-
text: message,
|
120
|
-
attributes: JSON.stringify(attributes),
|
121
|
-
subject: (_a = emailOptions) === null || _a === void 0 ? void 0 : _a.subject,
|
122
|
-
});
|
123
|
-
}
|
124
|
-
/**
|
125
|
-
* Send Media Message to the conversation
|
126
|
-
* @param {FormData | Conversation#SendMediaOptions} mediaContent - Media content to post
|
127
|
-
* @param {any} attributes Message attributes
|
128
|
-
* @returns Returns promise which can fail
|
129
|
-
*/
|
130
|
-
async sendMedia(mediaContent, attributes = {}, emailOptions) {
|
131
|
-
log.debug('Sending media message', mediaContent, attributes, emailOptions);
|
132
|
-
let media;
|
133
|
-
if (typeof FormData !== 'undefined' && (mediaContent instanceof FormData)) {
|
134
|
-
log.debug('Sending media message as FormData', mediaContent, attributes);
|
135
|
-
media = await this.services.mcsClient.postFormData(mediaContent);
|
136
|
-
}
|
137
|
-
else {
|
138
|
-
log.debug('Sending media message as SendMediaOptions', mediaContent, attributes);
|
139
|
-
let mediaOptions = mediaContent;
|
140
|
-
if (!mediaOptions.contentType || !mediaOptions.media) {
|
141
|
-
throw new Error('Media content <Conversation#SendMediaOptions> must contain non-empty contentType and media');
|
142
|
-
}
|
143
|
-
media = await this.services.mcsClient.post(mediaOptions.contentType, mediaOptions.media);
|
144
|
-
}
|
145
|
-
// emailOptions are currently ignored for media messages.
|
146
|
-
return this.services.session.addCommand('sendMediaMessage', {
|
147
|
-
channelSid: this.conversation.sid,
|
148
|
-
mediaSid: media.sid,
|
149
|
-
attributes: JSON.stringify(attributes)
|
150
|
-
});
|
151
|
-
}
|
152
|
-
/**
|
153
|
-
* Returns messages from conversation using paginator interface
|
154
|
-
* @param {Number} [pageSize] Number of messages to return in single chunk. By default it's 30.
|
155
|
-
* @param {String} [anchor] Most early message id which is already known, or 'end' by default
|
156
|
-
* @param {String} [direction] Pagination order 'backwards' or 'forward', 'forward' by default
|
157
|
-
* @returns {Promise<Paginator<Message>>} last page of messages by default
|
158
|
-
*/
|
159
|
-
getMessages(pageSize, anchor, direction) {
|
160
|
-
anchor = (typeof anchor !== 'undefined') ? anchor : 'end';
|
161
|
-
direction = direction || 'backwards';
|
162
|
-
return this._getMessages(pageSize, anchor, direction);
|
163
|
-
}
|
164
|
-
wrapPaginator(order, page, op) {
|
165
|
-
// We should swap next and prev page here, because of misfit of Sync and Chat paging conceptions
|
166
|
-
let shouldReverse = order === 'desc';
|
167
|
-
let np = () => page.nextPage().then(x => this.wrapPaginator(order, x, op));
|
168
|
-
let pp = () => page.prevPage().then(x => this.wrapPaginator(order, x, op));
|
169
|
-
return op(page.items).then(items => ({
|
170
|
-
items: items.sort((x, y) => { return x.index - y.index; }),
|
171
|
-
hasPrevPage: shouldReverse ? page.hasNextPage : page.hasPrevPage,
|
172
|
-
hasNextPage: shouldReverse ? page.hasPrevPage : page.hasNextPage,
|
173
|
-
prevPage: shouldReverse ? np : pp,
|
174
|
-
nextPage: shouldReverse ? pp : np
|
175
|
-
}));
|
176
|
-
}
|
177
|
-
_upsertMessage(index, value) {
|
178
|
-
let cachedMessage = this.messagesByIndex.get(index);
|
179
|
-
if (cachedMessage) {
|
180
|
-
return cachedMessage;
|
181
|
-
}
|
182
|
-
let message = new message_1.Message(this.conversation, this.services, index, value);
|
183
|
-
this.messagesByIndex.set(message.index, message);
|
184
|
-
message.on('updated', (args) => this.emit('messageUpdated', args));
|
185
|
-
return message;
|
186
|
-
}
|
187
|
-
/**
|
188
|
-
* Returns last messages from conversation
|
189
|
-
* @param {Number} [pageSize] Number of messages to return in single chunk. By default it's 30.
|
190
|
-
* @param {String} [anchor] Most early message id which is already known, or 'end' by default
|
191
|
-
* @param {String} [direction] Pagination order 'backwards' or 'forward', or 'forward' by default
|
192
|
-
* @returns {Promise<SyncPaginator<Message>>} last page of messages by default
|
193
|
-
* @private
|
194
|
-
*/
|
195
|
-
_getMessages(pageSize, anchor, direction) {
|
196
|
-
anchor = (typeof anchor !== 'undefined') ? anchor : 'end';
|
197
|
-
pageSize = pageSize || 30;
|
198
|
-
let order = direction === 'backwards' ? 'desc' : 'asc';
|
199
|
-
return this.messagesListPromise
|
200
|
-
.then(messagesList => messagesList.getItems({
|
201
|
-
from: anchor !== 'end' ? anchor : void (0),
|
202
|
-
pageSize,
|
203
|
-
order
|
204
|
-
}))
|
205
|
-
.then(page => this.wrapPaginator(order, page, items => Promise.all(items.map(item => this._upsertMessage(item.index, item.data)))));
|
206
|
-
}
|
207
|
-
}
|
208
|
-
exports.Messages = Messages;
|