Skip to content

Commit a894d15

Browse files
author
evgeny-nadymov
committed
Confirmation to switch between p2p and voip chat
1 parent 82fd334 commit a894d15

File tree

2 files changed

+204
-58
lines changed

2 files changed

+204
-58
lines changed

src/Components/Calls/CallPanel.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class CallPanel extends React.Component {
9999
const { callId } = this.props;
100100
if (!callId) return;
101101

102-
await CallStore.p2pDiscardCall(callId, false, 0, false, 0);
102+
await CallStore.p2pHangUp(callId, true);
103103
};
104104

105105
handleOpenSettings = async event => {
@@ -121,7 +121,7 @@ class CallPanel extends React.Component {
121121
handleClose = () => {
122122
const { callId } = this.props;
123123

124-
CallStore.p2pDiscardCall(callId, false, 0, false, 0);
124+
CallStore.p2pHangUp(callId, true);
125125
};
126126

127127
handleShareScreen = () => {

src/Stores/CallStore.js

Lines changed: 202 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -455,9 +455,31 @@ class CallStore extends EventEmitter {
455455
}
456456

457457
async startGroupCall(chatId) {
458-
let { currentGroupCall } = this;
459-
LOG_CALL('startGroupCall', currentGroupCall);
460-
if (currentGroupCall) {
458+
let { currentCall, currentGroupCall } = this;
459+
LOG_CALL('startGroupCall', currentCall, currentGroupCall);
460+
if (currentCall) {
461+
const { callId } = currentCall;
462+
const call = this.p2pGet(callId);
463+
if (call) {
464+
showAlert({
465+
title: LStore.getString('VoipOngoingAlertTitle'),
466+
message: LStore.replaceTags(LStore.formatString('VoipOngoingAlert2', getUserFullName(call.user_id, null), getChatTitle(chatId))),
467+
ok: LStore.getString('OK'),
468+
cancel: LStore.getString('Cancel'),
469+
onResult: async result => {
470+
if (result) {
471+
currentCall = this.currentCall;
472+
if (currentCall) {
473+
await this.p2pHangUp(currentCall.callId, true);
474+
}
475+
await this.startGroupCallInternal(chatId);
476+
}
477+
}
478+
});
479+
}
480+
481+
return;
482+
} else if (currentGroupCall) {
461483
const { chatId: oldChatId } = currentGroupCall;
462484

463485
showAlert({
@@ -504,7 +526,7 @@ class CallStore extends EventEmitter {
504526
TdLibController.send({ '@type': 'getGroupCall', group_call_id: groupCallId });
505527
}
506528

507-
let { currentGroupCall } = this;
529+
let { currentGroupCall, currentCall } = this;
508530
let streamManager = null;
509531
try {
510532
if (rejoin) {
@@ -529,27 +551,53 @@ class CallStore extends EventEmitter {
529551
}
530552
if (!streamManager || !streamManager.inputStream) return;
531553

532-
LOG_CALL('joinGroupCall has another', groupCallId, currentGroupCall);
533-
if (currentGroupCall && !rejoin) {
534-
const { chatId: oldChatId } = currentGroupCall;
554+
LOG_CALL('joinGroupCall has another', groupCallId, currentCall, currentGroupCall);
555+
if (!rejoin) {
556+
if (currentCall) {
557+
const { callId } = currentCall;
558+
const call = this.p2pGet(callId);
559+
if (call) {
560+
const { user_id } = call;
561+
562+
showAlert({
563+
title: LStore.getString('VoipOngoingAlertTitle'),
564+
message: LStore.replaceTags(LStore.formatString('VoipOngoingAlert2', getUserFullName(user_id, null), getChatTitle(chatId))),
565+
ok: LStore.getString('OK'),
566+
cancel: LStore.getString('Cancel'),
567+
onResult: async result => {
568+
if (result) {
569+
currentCall = this.currentCall;
570+
if (currentCall) {
571+
this.p2pHangUp(currentCall.callId, true);
572+
}
573+
this.joinGroupCallInternal(chatId, groupCallId, streamManager, muted, rejoin);
574+
}
575+
}
576+
});
535577

536-
showAlert({
537-
title: LStore.getString('VoipOngoingChatAlertTitle'),
538-
message: LStore.replaceTags(LStore.formatString('VoipOngoingChatAlert', getChatTitle(oldChatId), getChatTitle(chatId))),
539-
ok: LStore.getString('OK'),
540-
cancel: LStore.getString('Cancel'),
541-
onResult: async result => {
542-
if (result) {
543-
currentGroupCall = this.currentGroupCall;
544-
if (currentGroupCall) {
545-
this.hangUp(currentGroupCall.groupCallId);
578+
return;
579+
}
580+
} else if (currentGroupCall) {
581+
const { chatId: oldChatId } = currentGroupCall;
582+
583+
showAlert({
584+
title: LStore.getString('VoipOngoingChatAlertTitle'),
585+
message: LStore.replaceTags(LStore.formatString('VoipOngoingChatAlert', getChatTitle(oldChatId), getChatTitle(chatId))),
586+
ok: LStore.getString('OK'),
587+
cancel: LStore.getString('Cancel'),
588+
onResult: async result => {
589+
if (result) {
590+
currentGroupCall = this.currentGroupCall;
591+
if (currentGroupCall) {
592+
this.hangUp(currentGroupCall.groupCallId);
593+
}
594+
this.joinGroupCallInternal(chatId, groupCallId, streamManager, muted, rejoin);
546595
}
547-
this.joinGroupCallInternal(chatId, groupCallId, streamManager, muted, rejoin);
548596
}
549-
}
550-
});
597+
});
551598

552-
return;
599+
return;
600+
}
553601
}
554602

555603
await this.joinGroupCallInternal(chatId, groupCallId, streamManager, muted, rejoin);
@@ -1439,23 +1487,65 @@ class CallStore extends EventEmitter {
14391487
};
14401488
}
14411489

1442-
p2pStartCall(userId, isVideo) {
1490+
async p2pStartCall(userId, isVideo) {
14431491
LOG_P2P_CALL('p2pStartCall', userId, isVideo);
14441492

1493+
let { currentCall, currentGroupCall } = this;
1494+
if (currentCall) {
1495+
const { callId } = currentCall;
1496+
const call = this.p2pGet(callId);
1497+
if (call) {
1498+
showAlert({
1499+
title: LStore.getString('VoipOngoingAlertTitle'),
1500+
message: LStore.replaceTags(LStore.formatString('VoipOngoingAlert', getUserFullName(call.user_id, null), getUserFullName(userId, null))),
1501+
ok: LStore.getString('OK'),
1502+
cancel: LStore.getString('Cancel'),
1503+
onResult: async result => {
1504+
if (result) {
1505+
currentCall = this.currentCall;
1506+
if (currentCall) {
1507+
await this.p2pHangUp(currentCall.callId, true);
1508+
}
1509+
await this.p2pStartCallInternal(userId, isVideo);
1510+
}
1511+
}
1512+
});
1513+
}
1514+
1515+
return;
1516+
} else if (currentGroupCall) {
1517+
const { chatId: oldChatId } = currentGroupCall;
1518+
1519+
showAlert({
1520+
title: LStore.getString('VoipOngoingChatAlertTitle'),
1521+
message: LStore.replaceTags(LStore.formatString('VoipOngoingChatAlert2', getChatTitle(oldChatId), getUserFullName(userId, null))),
1522+
ok: LStore.getString('OK'),
1523+
cancel: LStore.getString('Cancel'),
1524+
onResult: async result => {
1525+
if (result) {
1526+
currentGroupCall = this.currentGroupCall;
1527+
if (currentGroupCall) {
1528+
await this.hangUp(currentGroupCall.groupCallId);
1529+
}
1530+
await this.p2pStartCallInternal(userId, isVideo);
1531+
}
1532+
}
1533+
});
1534+
1535+
return;
1536+
}
1537+
1538+
await this.p2pStartCallInternal(userId, isVideo);
1539+
}
1540+
1541+
p2pStartCallInternal(userId, isVideo) {
1542+
LOG_P2P_CALL('p2pStartCallInternal', userId, isVideo);
1543+
14451544
const fullInfo = UserStore.getFullInfo(userId);
14461545
if (!fullInfo) return;
14471546

14481547
const { can_be_called, has_private_calls, supports_video_calls } = fullInfo;
1449-
LOG_P2P_CALL('p2pStartCall fullInfo', fullInfo, can_be_called, has_private_calls, supports_video_calls);
1450-
// if (!can_be_called || has_private_calls) {
1451-
// showAlert({
1452-
// title: LStore.getString('VoipFailed'),
1453-
// message: LStore.replaceTags(LStore.formatString('CallNotAvailable', getUserFullName(userId, null))),
1454-
// // LStore.getString('CallNotAvailable'), getUserFullName(userId, null)),
1455-
// ok: LStore.getString('OK')
1456-
// });
1457-
// return;
1458-
// }
1548+
LOG_P2P_CALL('p2pStartCallInternal fullInfo', fullInfo, can_be_called, has_private_calls, supports_video_calls);
14591549

14601550
const protocol = this.p2pGetProtocol();
14611551
LOG_P2P_CALL('[tdlib] createCall', userId, protocol, isVideo, supports_video_calls);
@@ -1470,6 +1560,60 @@ class CallStore extends EventEmitter {
14701560
p2pAcceptCall(callId) {
14711561
LOG_P2P_CALL('p2pAcceptCall', callId);
14721562

1563+
const call = this.p2pGet(callId);
1564+
if (!call) return;
1565+
1566+
let { currentCall, currentGroupCall } = this;
1567+
if (currentCall) {
1568+
const { callId: prevCallId } = currentCall;
1569+
const prevCall = this.p2pGet(prevCallId);
1570+
if (prevCall) {
1571+
showAlert({
1572+
title: LStore.getString('VoipOngoingAlertTitle'),
1573+
message: LStore.replaceTags(LStore.formatString('VoipOngoingAlert', getUserFullName(prevCall.user_id, null), getUserFullName(call.user_id, null))),
1574+
ok: LStore.getString('OK'),
1575+
cancel: LStore.getString('Cancel'),
1576+
onResult: async result => {
1577+
if (result) {
1578+
currentCall = this.currentCall;
1579+
if (currentCall) {
1580+
await this.p2pHangUp(currentCall.callId, true);
1581+
}
1582+
await this.p2pAcceptCallInternal(callId);
1583+
}
1584+
}
1585+
});
1586+
1587+
return;
1588+
}
1589+
} else if (currentGroupCall) {
1590+
const { chatId: oldChatId } = currentGroupCall;
1591+
1592+
showAlert({
1593+
title: LStore.getString('VoipOngoingChatAlertTitle'),
1594+
message: LStore.replaceTags(LStore.formatString('VoipOngoingChatAlert2', getChatTitle(oldChatId), getUserFullName(call.user_id, null))),
1595+
ok: LStore.getString('OK'),
1596+
cancel: LStore.getString('Cancel'),
1597+
onResult: async result => {
1598+
if (result) {
1599+
currentGroupCall = this.currentGroupCall;
1600+
if (currentGroupCall) {
1601+
await this.hangUp(currentGroupCall.groupCallId);
1602+
}
1603+
await this.p2pAcceptCallInternal(callId);
1604+
}
1605+
}
1606+
});
1607+
1608+
return;
1609+
}
1610+
1611+
this.p2pAcceptCallInternal(callId);
1612+
}
1613+
1614+
async p2pAcceptCallInternal(callId) {
1615+
LOG_P2P_CALL('p2pAcceptCallInternal', callId);
1616+
14731617
const protocol = this.p2pGetProtocol();
14741618
LOG_P2P_CALL('[tdlib] acceptCall', callId, protocol);
14751619
TdLibController.send({
@@ -1479,20 +1623,6 @@ class CallStore extends EventEmitter {
14791623
});
14801624
}
14811625

1482-
p2pDiscardCall(callId, isDisconnected, duration, isVideo, connectionId) {
1483-
LOG_P2P_CALL('p2pDiscardCall', callId);
1484-
1485-
LOG_P2P_CALL('[tdlib] discardCall', callId, isDisconnected, duration, isVideo, connectionId);
1486-
TdLibController.send({
1487-
'@type': 'discardCall',
1488-
call_id: callId,
1489-
is_disconnected: isDisconnected,
1490-
duration,
1491-
is_video: isVideo,
1492-
connection_id: connectionId
1493-
});
1494-
}
1495-
14961626
p2pSendCallSignalingData(callId, data) {
14971627
LOG_P2P_CALL('[tdlib] sendCallSignalingData', callId, data);
14981628
TdLibController.send({
@@ -1554,6 +1684,7 @@ class CallStore extends EventEmitter {
15541684

15551685
async p2pJoinCall(callId) {
15561686
LOG_P2P_CALL('p2pJoinCall', callId);
1687+
15571688
const call = this.p2pGet(callId);
15581689
if (!call) return;
15591690

@@ -1590,6 +1721,13 @@ class CallStore extends EventEmitter {
15901721
if (video) {
15911722
video.srcObject = outputStream;
15921723
}
1724+
1725+
track.onmute = () => {
1726+
LOG_P2P_CALL('[track] onmute', track);
1727+
};
1728+
track.onunmute = () => {
1729+
LOG_P2P_CALL('[track] onunmute', track);
1730+
};
15931731
};
15941732

15951733
this.currentCall = {
@@ -1758,19 +1896,27 @@ class CallStore extends EventEmitter {
17581896
// this.p2pSendCallSignalingData(callId, JSON.stringify({ type: 'answer', data: answer }));
17591897
}
17601898

1761-
p2pHangUp(callId) {
1762-
LOG_P2P_CALL('hangUp', callId);
1899+
p2pHangUp(callId, discard = false) {
17631900
const { currentCall } = this;
1764-
if (!currentCall) return;
1765-
if (currentCall.callId !== callId) return;
1766-
1767-
const call = this.get(callId);
1768-
if (call) return;
1901+
LOG_P2P_CALL('hangUp', callId, currentCall);
1902+
if (currentCall && currentCall.callId === callId) {
1903+
const { connection, inputStream, outputStream, screenStream } = currentCall;
1904+
this.p2pCloseConnectionAndStream(connection, inputStream, outputStream, screenStream);
17691905

1770-
const { connection, inputStream, outputStream, screenStream } = currentCall;
1771-
this.p2pCloseConnectionAndStream(connection, inputStream, outputStream, screenStream);
1906+
this.currentCall = null;
1907+
}
17721908

1773-
this.currentCall = null;
1909+
if (discard) {
1910+
LOG_P2P_CALL('[tdlib] discardCall', callId);
1911+
TdLibController.send({
1912+
'@type': 'discardCall',
1913+
call_id: callId,
1914+
is_disconnected: false,
1915+
duration: 0,
1916+
is_video: false,
1917+
connection_id: 0
1918+
});
1919+
}
17741920
}
17751921

17761922
async p2pStartScreenSharing() {

0 commit comments

Comments
 (0)