-
Notifications
You must be signed in to change notification settings - Fork 292
/
Copy pathCallingViewModel.ts
487 lines (440 loc) · 18.2 KB
/
CallingViewModel.ts
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/*
* Wire
* Copyright (C) 2022 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*
*/
import {QualifiedId} from '@wireapp/api-client/lib/user';
import {amplify} from 'amplify';
import ko from 'knockout';
import {container} from 'tsyringe';
import {CALL_TYPE, REASON as CALL_REASON, STATE as CALL_STATE} from '@wireapp/avs';
import {Availability} from '@wireapp/protocol-messaging';
import {WebAppEvents} from '@wireapp/webapp-events';
import {ButtonGroupTab} from 'Components/calling/ButtonGroup';
import 'Components/calling/ChooseScreen';
import {replaceLink, t} from 'Util/LocalizerUtil';
import {matchQualifiedIds} from 'Util/QualifiedId';
import {safeWindowOpen} from 'Util/SanitizationUtil';
import type {AudioRepository} from '../audio/AudioRepository';
import {AudioType} from '../audio/AudioType';
import type {Call} from '../calling/Call';
import {CallingRepository} from '../calling/CallingRepository';
import {CallState, DesktopScreenShareMenu} from '../calling/CallState';
import {LEAVE_CALL_REASON} from '../calling/enum/LeaveCallReason';
import {PrimaryModal} from '../components/Modals/PrimaryModal';
import {Config} from '../Config';
import {ConversationState} from '../conversation/ConversationState';
import {ConversationVerificationState} from '../conversation/ConversationVerificationState';
import type {Conversation} from '../entity/Conversation';
import type {User} from '../entity/User';
import type {ElectronDesktopCapturerSource, MediaDevicesHandler} from '../media/MediaDevicesHandler';
import type {MediaStreamHandler} from '../media/MediaStreamHandler';
import type {PermissionRepository} from '../permission/PermissionRepository';
import {PermissionStatusState} from '../permission/PermissionStatusState';
import {PropertiesRepository} from '../properties/PropertiesRepository';
import {PROPERTIES_TYPE} from '../properties/PropertiesType';
import type {TeamRepository} from '../team/TeamRepository';
import {TeamState} from '../team/TeamState';
import {ROLE} from '../user/UserPermission';
export interface CallActions {
answer: (call: Call) => Promise<void>;
changePage: (newPage: number, call: Call) => void;
leave: (call: Call) => void;
reject: (call: Call) => void;
startAudio: (conversationEntity: Conversation) => Promise<void>;
startVideo: (conversationEntity: Conversation) => Promise<void>;
switchCameraInput: (deviceId: string) => void;
switchScreenInput: (deviceId: string) => void;
toggleCamera: (call: Call) => void;
toggleMute: (call: Call, muteState: boolean) => void;
toggleScreenshare: (call: Call, desktopScreenShareMenu: DesktopScreenShareMenu) => void;
}
export enum CallViewTab {
ALL = 'all',
SPEAKERS = 'speakers',
}
export const CallViewTabs: ButtonGroupTab[] = [
{getText: () => t('videoSpeakersTabSpeakers').toUpperCase(), value: CallViewTab.SPEAKERS},
{getText: substitute => t('videoSpeakersTabAll', substitute as unknown as {count: number}), value: CallViewTab.ALL},
];
declare global {
interface HTMLAudioElement {
setSinkId?: (sinkId: string) => Promise<void>;
}
}
const MAX_USERS_TO_CALL_WITHOUT_CONFIRM = Config.getConfig().FEATURE.MAX_USERS_TO_PING_WITHOUT_ALERT;
export class CallingViewModel {
readonly activeCalls: ko.PureComputed<Call[]>;
readonly callActions: CallActions;
readonly isSelfVerified: ko.Computed<boolean>;
constructor(
readonly callingRepository: CallingRepository,
readonly audioRepository: AudioRepository,
readonly mediaDevicesHandler: MediaDevicesHandler,
readonly mediaStreamHandler: MediaStreamHandler,
readonly permissionRepository: PermissionRepository,
readonly teamRepository: TeamRepository,
readonly propertiesRepository: PropertiesRepository,
private readonly selfUser: ko.Observable<User>,
private readonly conversationState = container.resolve(ConversationState),
readonly callState = container.resolve(CallState),
private readonly teamState = container.resolve(TeamState),
) {
this.isSelfVerified = ko.pureComputed(() => selfUser().is_verified());
this.activeCalls = ko.pureComputed(() =>
this.callState.calls().filter(call => {
const {conversation} = call;
if (!conversation || conversation.isSelfUserRemoved()) {
return false;
}
return call.reason() !== CALL_REASON.ANSWERED_ELSEWHERE;
}),
);
const toggleState = async (withVideo: boolean): Promise<void> => {
const conversation = this.conversationState.activeConversation();
if (conversation) {
const isActiveCall = this.callingRepository.findCall(conversation.qualifiedId);
const callType = withVideo ? CALL_TYPE.VIDEO : CALL_TYPE.NORMAL;
if (isActiveCall) {
this.callingRepository.leaveCall(conversation.qualifiedId, LEAVE_CALL_REASON.ELECTRON_TRAY_MENU_MESSAGE);
return;
}
await handleCallAction(conversation, callType);
}
};
amplify.subscribe(WebAppEvents.CALL.STATE.TOGGLE, toggleState); // This event needs to be kept, it is sent by the wrapper
const ring = (call: Call): void => {
const sounds: Partial<Record<CALL_STATE, AudioType>> = {
[CALL_STATE.INCOMING]: AudioType.INCOMING_CALL,
[CALL_STATE.OUTGOING]: AudioType.OUTGOING_CALL,
};
const initialCallState = call.state();
const soundId = sounds[initialCallState];
if (!soundId || typeof call.reason() !== 'undefined') {
return;
}
audioRepository.loop(soundId).then(() => {
const stateSubscription = ko.computed(() => {
if (call.state() !== initialCallState || typeof call.reason() !== 'undefined') {
window.setTimeout(() => {
audioRepository.stop(soundId);
stateSubscription.dispose();
});
}
});
});
};
const startCall = async (conversation: Conversation, callType: CALL_TYPE): Promise<void> => {
const canStart = await this.canInitiateCall(conversation.qualifiedId, {
action: t('modalCallSecondOutgoingAction'),
message: t('modalCallSecondOutgoingMessage'),
title: t('modalCallSecondOutgoingHeadline'),
});
if (!canStart) {
return;
}
const call = await this.callingRepository.startCall(conversation, callType);
if (!call) {
return;
}
ring(call);
};
const answerCall = async (call: Call) => {
const canAnswer = await this.canInitiateCall(call.conversation.qualifiedId, {
action: t('modalCallSecondIncomingAction'),
message: t('modalCallSecondIncomingMessage'),
title: t('modalCallSecondIncomingHeadline'),
});
if (!canAnswer) {
return;
}
await this.callingRepository.answerCall(call);
};
const hasSoundlessCallsEnabled = (): boolean => {
return this.propertiesRepository.getPreference(PROPERTIES_TYPE.CALL.ENABLE_SOUNDLESS_INCOMING_CALLS);
};
const hasJoinedCall = (): boolean => {
return !!this.callState.joinedCall();
};
this.callingRepository.onIncomingCall(async (call: Call) => {
const shouldRing = this.selfUser().availability() !== Availability.Type.AWAY;
if (shouldRing && (!hasSoundlessCallsEnabled() || !hasJoinedCall())) {
ring(call);
}
});
const showE2EICallModal = (conversationEntity: Conversation, callType: CALL_TYPE) => {
const memberCount = conversationEntity.participating_user_ets().length;
PrimaryModal.show(PrimaryModal.type.CONFIRM, {
primaryAction: {
action: async () => {
conversationEntity.mlsVerificationState(ConversationVerificationState.UNVERIFIED);
if (memberCount > MAX_USERS_TO_CALL_WITHOUT_CONFIRM) {
showMaxUsersToCallModalWithoutConfirm(conversationEntity, callType);
} else {
await startCall(conversationEntity, callType);
}
},
text: t('conversation.E2EICallAnyway'),
},
secondaryAction: {
action: () => {},
text: t('conversation.E2EICancel'),
},
text: {
message: t('conversation.E2EIDegradedInitiateCall'),
title: t('conversation.E2EIConversationNoLongerVerified'),
},
});
};
const showMaxUsersToCallModalWithoutConfirm = (conversationEntity: Conversation, callType: CALL_TYPE) => {
const memberCount = conversationEntity.participating_user_ets().length;
PrimaryModal.show(PrimaryModal.type.WITHOUT_TITLE, {
preventClose: true,
primaryAction: {
action: async () => await startCall(conversationEntity, callType),
text: t('groupCallModalPrimaryBtnName'),
},
secondaryAction: {
text: t('modalConfirmSecondary'),
},
text: {
htmlMessage: `<div class="modal-description">
${t('groupCallConfirmationModalTitle', {memberCount})}
</div>`,
closeBtnLabel: t('groupCallModalCloseBtnLabel'),
},
});
};
const handleCallAction = async (conversationEntity: Conversation, callType: CALL_TYPE): Promise<void> => {
const memberCount = conversationEntity.participating_user_ets().length;
const isE2EIDegraded = conversationEntity.mlsVerificationState() === ConversationVerificationState.DEGRADED;
if (isE2EIDegraded) {
showE2EICallModal(conversationEntity, callType);
} else if (memberCount > MAX_USERS_TO_CALL_WITHOUT_CONFIRM) {
showMaxUsersToCallModalWithoutConfirm(conversationEntity, callType);
} else {
await startCall(conversationEntity, callType);
}
};
this.callActions = {
answer: async (call: Call) => {
if (call.isConference && !this.callingRepository.supportsConferenceCalling) {
PrimaryModal.show(PrimaryModal.type.ACKNOWLEDGE, {
primaryAction: {
action: () => {
this.callingRepository.rejectCall(call.conversation.qualifiedId);
},
},
text: {
message: `${t('modalConferenceCallNotSupportedMessage')} ${t(
'modalConferenceCallNotSupportedJoinMessage',
)}`,
title: t('modalConferenceCallNotSupportedHeadline'),
},
});
} else {
return answerCall(call);
}
},
changePage: (newPage, call) => {
this.callingRepository.changeCallPage(call, newPage);
},
leave: ({conversation}: Call) => {
this.callingRepository.leaveCall(conversation.qualifiedId, LEAVE_CALL_REASON.MANUAL_LEAVE_BY_UI_CLICK);
callState.activeCallViewTab(CallViewTab.ALL);
},
reject: ({conversation}: Call) => {
this.callingRepository.rejectCall(conversation.qualifiedId);
},
startAudio: async (conversationEntity: Conversation) => {
if (conversationEntity.isGroup() && !this.teamState.isConferenceCallingEnabled()) {
this.showRestrictedConferenceCallingModal();
} else {
await handleCallAction(conversationEntity, CALL_TYPE.NORMAL);
}
},
startVideo: async (conversationEntity: Conversation) => {
if (conversationEntity.isGroup() && !this.teamState.isConferenceCallingEnabled()) {
this.showRestrictedConferenceCallingModal();
} else {
await handleCallAction(conversationEntity, CALL_TYPE.VIDEO);
}
},
switchCameraInput: (deviceId: string) => {
this.mediaDevicesHandler.currentDeviceId.videoinput(deviceId);
this.callingRepository.refreshVideoInput();
},
switchScreenInput: (deviceId: string) => {
this.mediaDevicesHandler.currentDeviceId.screeninput(deviceId);
},
toggleCamera: (call: Call) => {
this.callingRepository.toggleCamera(call);
},
toggleMute: (call: Call, muteState: boolean) => {
this.callingRepository.muteCall(call, muteState);
},
toggleScreenshare: async (call, desktopScreenShareMenu): Promise<void> => {
if (call.getSelfParticipant().sharesScreen()) {
return this.callingRepository.toggleScreenshare(call);
}
const showScreenSelection = (): Promise<void> => {
this.callState.desktopScreenShareMenu(desktopScreenShareMenu);
return new Promise(resolve => {
this.callingRepository.onChooseScreen = (deviceId: string): void => {
this.mediaDevicesHandler.currentDeviceId.screeninput(deviceId);
this.callState.selectableScreens([]);
this.callState.selectableWindows([]);
resolve();
};
this.mediaDevicesHandler.getScreenSources().then((sources: ElectronDesktopCapturerSource[]) => {
if (sources.length === 1) {
return this.callingRepository.onChooseScreen(sources[0].id);
}
this.callState.selectableScreens(sources.filter(source => source.id.startsWith('screen')));
this.callState.selectableWindows(sources.filter(source => source.id.startsWith('window')));
});
});
};
this.mediaStreamHandler
.selectScreenToShare(showScreenSelection)
.then(() => this.callingRepository.toggleScreenshare(call));
},
};
}
/**
* Will reject or leave the call depending on the state of the call.
* @param activeCall - the call to gracefully tear down
*/
private async gracefullyTeardownCall(activeCall: Call): Promise<void> {
const {conversation} = activeCall;
if (activeCall.state() === CALL_STATE.INCOMING) {
this.callingRepository.rejectCall(conversation.qualifiedId);
} else {
this.callingRepository.leaveCall(conversation.qualifiedId, LEAVE_CALL_REASON.MANUAL_LEAVE_TO_JOIN_ANOTHER_CALL);
}
// We want to wait a bit to be sure the call have been tear down properly
await new Promise(resolve => setTimeout(resolve, 1000));
}
/**
* Will make sure everything is ready for a call to start/be joined in the given conversation.
* If there is another ongoing call, the user will be asked to first leave that other call before starting a new call.
*
* @param conversationId - the conversation in which the call should be started/joined
* @param warningStrings - the strings to display in case there is already an active call
* @returns true if the call can be started, false otherwise
*/
private canInitiateCall(
conversationId: QualifiedId,
warningStrings: {action: string; message: string; title: string},
): Promise<boolean> {
const idleCallStates = [CALL_STATE.INCOMING, CALL_STATE.NONE, CALL_STATE.UNKNOWN];
const otherActiveCall = this.callState
.calls()
.find(
call =>
!matchQualifiedIds(call.conversation.qualifiedId, conversationId) && !idleCallStates.includes(call.state()),
);
if (!otherActiveCall) {
return Promise.resolve(true);
}
return new Promise(resolve => {
PrimaryModal.show(PrimaryModal.type.CONFIRM, {
primaryAction: {
action: async () => {
await this.gracefullyTeardownCall(otherActiveCall);
resolve(true);
},
text: warningStrings.action,
},
secondaryAction: {
action: () => resolve(false),
},
text: {
message: warningStrings.message,
title: warningStrings.title,
},
});
});
}
private showRestrictedConferenceCallingModal() {
if (this.teamState.isInTeam(this.selfUser())) {
if (this.selfUser().teamRole() === ROLE.OWNER) {
const replaceEnterprise = replaceLink(
Config.getConfig().URL.PRICING,
'modal__text__read-more',
'read-more-pricing',
);
PrimaryModal.show(PrimaryModal.type.CONFIRM, {
primaryAction: {
action: () => {
safeWindowOpen(Config.getConfig().URL.TEAMS_BILLING);
},
text: t('callingRestrictedConferenceCallOwnerModalUpgradeButton'),
},
text: {
htmlMessage: t(
'callingRestrictedConferenceCallOwnerModalDescription',
{brandName: Config.getConfig().BRAND_NAME},
replaceEnterprise,
),
title: t('callingRestrictedConferenceCallOwnerModalTitle'),
},
});
} else {
PrimaryModal.show(PrimaryModal.type.ACKNOWLEDGE, {
text: {
message: t('callingRestrictedConferenceCallTeamMemberModalDescription'),
title: t('callingRestrictedConferenceCallTeamMemberModalTitle'),
},
});
}
} else {
PrimaryModal.show(PrimaryModal.type.ACKNOWLEDGE, {
text: {
htmlMessage: t('callingRestrictedConferenceCallPersonalModalDescription', {
brandName: Config.getConfig().BRAND_NAME,
}),
title: t('callingRestrictedConferenceCallPersonalModalTitle'),
},
});
}
}
isIdle(call: Call): boolean {
return call.state() === CALL_STATE.NONE;
}
isOutgoing(call: Call): boolean {
return call.state() === CALL_STATE.OUTGOING;
}
isConnecting(call: Call): boolean {
return call.state() === CALL_STATE.ANSWERED;
}
isIncoming(call: Call): boolean {
return call.state() === CALL_STATE.INCOMING;
}
isOngoing(call: Call): boolean {
return call.state() === CALL_STATE.MEDIA_ESTAB;
}
getConversationById(conversationId: QualifiedId): Conversation | undefined {
return this.conversationState.findConversation(conversationId);
}
hasAccessToCamera(): boolean {
return this.permissionRepository.permissionState.camera() === PermissionStatusState.GRANTED;
}
readonly onCancelScreenSelection = () => {
this.callState.selectableScreens([]);
this.callState.selectableWindows([]);
};
}