/
state-parsers.js
295 lines (267 loc) · 7.72 KB
/
state-parsers.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
/*!
* Copyright (c) 2015-2017 Cisco Systems, Inc. See LICENSE file.
*/
import {filter, get} from 'lodash';
/**
* Finds the Locus's active participants
* @param {Types~Locus} locus
* @private
* @returns {Array<Types~LocusParticipant>}
*/
export function activeParticipants(locus) {
return filter(locus.participants, {state: `JOINED`});
}
/**
* Indicates if the locus was activated form this device
* @param {Types~Locus} locus
* @private
* @returns {string} unknown|in|out
*/
export function direction(locus) {
if (!locus || !locus.self) {
return `unknown`;
}
return locus.self.isCreator ? `out` : `in`;
}
/**
* Indicates of the specified locus is active
* @param {Types~Locus} locus
* @private
* @returns {Boolean}
*/
export function isActive(locus) {
return locus.fullState.state === `ACTIVE`;
}
/**
* Indicates if the specified locus represents a call (in other words, has
* exactly two participants)
* @param {Types~Locus} locus
* @private
* @returns {Boolean}
*/
export function isCall(locus) {
return locus && locus.fullState && locus.fullState.type === `CALL`;
}
/**
* Indicates if the current user has joined the Locus
* @param {Types~Locus} locus
* @private
* @returns {Boolean}
*/
export function joined(locus) {
return Boolean(locus.self && participantIsJoined(locus.self));
}
/**
* Indicates if this device has joined the locus
* @param {ProxySpark} spark
* @param {Types~Locus} locus
* @private
* @returns {Boolean}
*/
export function joinedOnThisDevice(spark, locus) {
return joined(locus) && spark.internal.device.url === locus.self.deviceUrl;
}
/**
* Converts a list of participants to a list of memberships
* @param {SparkCore} spark
* @param {Types~Locus} locus
* @private
* @returns {Array<CallMembership>}
*/
export function participantsToCallMemberships(spark, locus) {
const users = new Map();
const devices = new Map();
for (const participant of locus.participants) {
if (participant.type === `USER`) {
users.set(participant.url, participant);
}
else if (participant.type === `RESOURCE_ROOM`) {
devices.set(participant.url, participant);
}
}
const memberships = [];
users.forEach((participant) => {
const membership = participantToCallMembership(spark, locus, participant);
if (participant.devices) {
for (const device of participant.devices) {
if (device.state === `JOINED` && get(device, `intent.type`) === `OBSERVE`) {
const deviceParticipant = devices.get(device.url);
membership.audioMuted = remoteAudioMuted(deviceParticipant);
membership.videoMuted = remoteVideoMuted(deviceParticipant);
}
}
}
memberships.push(membership);
});
return memberships;
}
/**
* Converts a single participant to a membership
* @param {SparkCore} spark
* @param {Types~Locus} locus
* @param {Object} participant
* @private
* @returns {CallMembership}
*/
export function participantToCallMembership(spark, locus, participant) {
return {
_self: locus.self.url === participant.url,
isInitiator: participant.isCreator || false,
personUuid: participant.person.id,
personId: spark.people.inferPersonIdFromUuid(participant.person.id),
state: participantStateToCallMembershipState(participant),
audioMuted: remoteAudioMuted(participant),
videoMuted: remoteVideoMuted(participant)
};
}
/**
*
* Maps participant details to membership state enum
* @param {LocusParticipant} participant
* @private
* @returns {string}
*/
export function participantStateToCallMembershipState(participant) {
const state = participant.state && participant.state.toLowerCase();
switch (state) {
case `idle`:
// TODO figure out how "waiting" is represented
return `notified`;
case `joined`:
return `connected`;
case `left`:
return `disconnected`;
default:
return state;
}
}
/**
* Creates a unique identifier for a call (but not necessarily the "callId" that
* we'll someday expose as a first-class property)
*
* @param {Object} locus
* @private
* @returns {string}
*/
export function makeInternalCallId(locus) {
return `${locus.url}_${locus.fullState.lastActive}`;
}
/**
* Indicates the direction of the specified media type for the specified
* participant
* @param {string} mediaType
* @param {Types~LocusParticipant} participant
* @private
* @returns {string} One of `sendonly`, `recvonly`, `sendrecv`, or `inactive`
*/
export function mediaDirection(mediaType, participant) {
if (!participant) {
return `inactive`;
}
if (!participant.status) {
return `inactive`;
}
return (participant.status[`${mediaType}Status`] || `inactive`).toLowerCase();
}
/**
* Indicates if the specified participant has joined the Locus
* @param {Types~LocusParticipant} participant
* @private
* @returns {Boolean}
*/
export function participantIsJoined(participant) {
return participant && participant.state === `JOINED`;
}
/**
* Finds the party in the call that is not the current user
* @param {Types~Locus} locus
* @private
* @returns {Types~LocusParticipant}
*/
export function remoteParticipant(locus) {
return remoteParticipants(locus)[0];
}
/**
* Finds all participants of the Locus that are not the current user
* @param {Types~Locus} locus
* @private
* @returns {Types~LocusParticipant}
*/
export function remoteParticipants(locus) {
return locus.participants.filter((participant) => participant.type === `USER` && participant.url !== locus.self.url);
}
/**
* Indicates if the remote party is sending audio
* @param {Types~LocusParticipant} participant
* @private
* @returns {Boolean}
*/
export function remoteAudioMuted(participant) {
return participantIsJoined(participant) && !participant.status.audioStatus.includes(`SEND`);
}
/**
* Indicates if the remote party is sending video
* @param {Types~LocusParticipant} participant
* @private
* @returns {Boolean}
*/
export function remoteVideoMuted(participant) {
return participantIsJoined(participant) && !participant.status.videoStatus.includes(`SEND`);
}
/**
* Indicates if the `call:incoming` event should be fired for the specified Locus
* @param {Types~MercuryEvent} event Event which delivered the Locus
* @param {ProxySpark} spark
* @private
* @returns {Boolean}
*/
export function shouldRing(event) {
return get(event, `data.locus.self.alertType.action`) !== `NONE`;
}
/**
* Determines the call state from a locus object
* @param {Types~Locus} locus
* @returns {string}
*/
export function getState(locus) {
return locus && locus.fullState && locus.fullState.state.toLowerCase();
}
// there's really no good way to split getStatus() up that won't make it less readable
/* eslint-disable complexity */
/**
* Determines the call state from a locus object. avoids the caching caused by
* amp state because that leads to out-of-order updates
* @param {ProxySpark} spark
* @param {Types~Locus} locus
* @param {Types~Locus} previousLocus
* @private
* @returns {string}
*/
export function getStatus(spark, locus, previousLocus) {
if (locus) {
const remote = remoteParticipant(locus);
if (remote) {
if (joinedOnThisDevice(spark, locus) && remote && participantIsJoined(remote)) {
return `connected`;
}
if (locus.replaces) {
for (const replaced of locus.replaces) {
if (replaced.locusUrl === previousLocus.url && replaced.lastActive === get(previousLocus, `fullState.lastActive`)) {
return `replaced`;
}
}
}
const local = locus.self;
if (remote.state === `LEFT` || local.state === `LEFT`) {
return `disconnected`;
}
if (remote.state === `DECLINED`) {
return `disconnected`;
}
if (remote.state === `NOTIFIED`) {
return `ringing`;
}
}
}
return `initiated`;
}