Source: member.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
'use strict';
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/*
 * Nexmo Client SDK
 *  Member Object Model
 *
 * Copyright (c) Nexmo Inc.
*/
const WildEmitter = require('wildemitter');
const nexmoClientError_1 = require("./nexmoClientError");
const nxmEvent_1 = __importDefault(require("./events/nxmEvent"));
const utils_1 = __importDefault(require("./utils"));
/**
 * An individual user (i.e. conversation member).
 * @class Member
 * @param {Conversation} conversation
 * @param {object} params
*/
class Member {
    constructor(conversation, params) {
        this.conversation = conversation;
        this.callStatus = null;
        this._normalise(params);
        WildEmitter.mixin(Member);
    }
    /**
     * Update object instance and align attribute names
     *
     * Handle params input to keep consistent the member object
     * @param {object} params member attributes
     * @private
    */
    _normalise(params) {
        if (params) {
            this.user = this.user || {};
            this.channel = params.channel || {
                type: 'app'
            };
            let key;
            for (key in params) {
                switch (key) {
                    case 'member_id':
                        this.id = params.member_id;
                        break;
                    case 'timestamp':
                        this.timestamp = params.timestamp;
                        break;
                    case 'state':
                        this.state = params.state;
                        break;
                    case 'from':
                        this.id = params.from; // special case for member events
                        break;
                    case 'user_id':
                        this.user.id = params.user_id;
                        break;
                    case 'name':
                        this.user.name = params.name;
                        break;
                    case 'user':
                        this.user = {
                            name: params.user.name,
                            id: params.user.user_id || params.user.id
                        };
                        this.display_name = this.display_name || params.user.display_name;
                        break;
                    case 'invited_by':
                        this.invited_by = params.invited_by;
                        break;
                    case 'display_name':
                        this.display_name = this.display_name || params.display_name;
                        break;
                    case '_embedded':
                        if (params._embedded.user) {
                            this.user = {
                                id: params._embedded.user.id || this.user.id,
                                name: params._embedded.user.name || this.user.name
                            };
                            this.display_name = this.display_name || params._embedded.user.display_name;
                        }
                    case 'conversation':
                        break;
                    default:
                        if (!params.type) {
                            this[key] = params[key];
                        }
                }
            }
            // join conversation returns our member with only id,
            // compare it for now and use the username we have in the application object
            if (this.conversation.application.me && params.user_id === this.conversation.application.me.id) {
                this.user.name = this.conversation.application.me.name;
            }
            // make sure we don't keep a member.user_id, name in any flow
            delete this.user_id;
            delete this.name;
            delete this.user.user_id;
        }
    }
    /**
     * Play the given stream only to this member within the conversation
     *
     * @param {string} [params]
     *
     * @returns {Promise<NXMEvent>}
     * @private
    */
    async playStream(params) {
        try {
            const response = await this.conversation.application.session.sendNetworkRequest({
                type: 'POST',
                path: `conversations/${this.id}/events`,
                data: {
                    type: 'audio:play',
                    to: this.id,
                    body: params
                }
            });
            return new nxmEvent_1.default(this.conversation, response);
        }
        catch (error) {
            throw new nexmoClientError_1.NexmoApiError(error);
        }
    }
    /**
     * Speak the given text only to this member within the Conversation.
     *
     * @param {string} [params]
     *
     * @returns {Promise<NXMEvent>}
     * @private
    */
    async sayText(params) {
        try {
            const response = await this.conversation.application.session.sendNetworkRequest({
                type: 'POST',
                path: `conversations/${this.id}/events`,
                data: {
                    type: 'audio:say',
                    cid: this.id,
                    from: this.conversation.me.id,
                    to: this.id,
                    body: {
                        text: params.text,
                        voice_name: params.voice_name || 'Amy',
                        level: params.level || 1,
                        queue: params.queue || true,
                        loop: params.loop || 1,
                        ssml: params.ssml || false
                    }
                }
            });
            return new nxmEvent_1.default(this.conversation, response);
        }
        catch (error) {
            throw new nexmoClientError_1.NexmoApiError(error);
        }
    }
    /**
     * Kick a Member from the Conversation.
     *
     * @param {object} [reason] the reason for kicking out a member
     * @param {string} [reason.reason_code] the code of the reason
     * @param {string} [reason.reason_text] the description of the reason
     * @example <caption>Remove a member from the Conversation.</caption>
     * // Remove a member
     * member.kick({reason_code: "Reason Code", reason_text: "Reason Text"})
     * .then(() => {
     *     console.log("Successfully removed member.");
     * }).catch((error) => {
     *     console.error("Error removing member: ", error);
     * });
     *
     * // Remove yourself
     * conversation.me.kick({reason_code: "Reason Code", reason_text: "Reason Text"})
     * .then(() => {
     *     console.log("Successfully removed yourself.");
     * }).catch((error) => {
     *     console.error("Error removing yourself: ", error);
     * });
     *
     * @returns {Promise}
    */
    async kick(reason) {
        var _a, _b;
        let path = `conversations/${this.conversation.id}/members/${this.id}`;
        let params = new URLSearchParams();
        // add member_id of from
        if ((_b = (_a = this === null || this === void 0 ? void 0 : this.conversation) === null || _a === void 0 ? void 0 : _a.me) === null || _b === void 0 ? void 0 : _b.id) {
            params.append('from', this.conversation.me.id);
        }
        if (reason) {
            Object.keys(reason).forEach((key) => {
                params.append(key, reason[key]);
            });
        }
        path += `?${params.toString()}`;
        try {
            return await this.conversation.application.session.sendNetworkRequest({
                type: 'DELETE',
                path
            });
        }
        catch (error) {
            throw new nexmoClientError_1.NexmoApiError(error);
        }
    }
    /**
     * Mute your stream.
     *
     * @param {boolean} [mute] true for mute, false for unmute
     * @param {number} [streamIndex] stream index of the stream
     * @example <caption>Mute audio stream of your Member.</caption>
     * // Mute yourself
     * conversation.me.mute(true);
     *
     * // Unmute yourself
     * conversation.me.mute(false);
     *
     * @returns {Promise}
    */
    mute(mute, streamIndex = null) {
        return this.conversation.media.mute(mute, streamIndex);
    }
    /**
     * Earmuff yourself in the Conversation.
     *
     * @param {boolean} earmuff true or false
     * @example <caption>Disables your Member from hearing other Members in the Conversation.</caption>
     * // Earmuff yourself
     * conversation.me.earmuff(true);
     *
     * // Unearmuff yourself
     * conversation.me.earmuff(false);
     *
     * @returns {Promise}
     *
    */
    earmuff(earmuff) {
        return this.conversation.media.earmuff(earmuff);
    }
    /**
     * Handle member object events
     *
     * Handle events that are modifying this member instance
     * @param {NXMEvent} event invited, joined, left, media events
     * @private
    */
    _handleEvent(event) {
        switch (event.type) {
            case 'member:invited':
                this._normalise(event.body); // take care of misaligned objects.
                this.state = 'INVITED';
                this.timestamp.invited = event.body.timestamp.invited;
                if (!event.body.invited_by && event.body.user.media && event.body.user.media.audio_settings
                    && event.body.user.media.audio_settings.enabled) {
                    this._setCallStatusAndEmit('started');
                }
                break;
            case 'member:joined':
                this._normalise(event.body); // take care of misaligned objects.
                this.state = 'JOINED';
                this.timestamp.joined = event.body.timestamp.joined;
                if (event.body.channel && event.body.channel.knocking_id) {
                    this._setCallStatusAndEmit('started');
                }
                break;
            case 'member:left':
                this._normalise(event.body); // take care of misaligned objects.
                this.state = 'LEFT';
                this.timestamp.left = event.body.timestamp.left;
                if (event.body.reason && event.body.reason.text) {
                    this._setCallStatusAndEmit(event.body.reason.text);
                }
                break;
            case 'member:media':
                this.media = event.body.media;
                break;
            case 'leg:status:update':
                this.channel.legs = utils_1.default.updateMemberLegs(this.channel.legs, event);
                this._setCallStatusAndEmit(event.body.status);
                break;
            case 'audio:ringing:start':
                if (!this.callStatus || this.callStatus === 'started') {
                    this._setCallStatusAndEmit('ringing');
                }
                break;
            default:
                break;
        }
    }
    /**
       * Set the member.callStatus and emit a member:call:status event
       *
       * @param {Member.callStatus} this.callStatus the call status to set
       * @private
      */
    _setCallStatusAndEmit(callStatus) {
        if (this.callStatus !== String(callStatus)) {
            this.callStatus = callStatus;
            this.conversation.emit('member:call:status', this);
        }
    }
}
exports.default = Member;
module.exports = Member;