Исправить маршрутизацию call push по sessionId

This commit is contained in:
AidarKC
2026-06-19 19:18:16 +04:00
parent 47574100f9
commit cc074a941f
7 changed files with 56 additions and 5 deletions
+4
View File
@@ -100,6 +100,7 @@ self.addEventListener('push', (event) => {
const json = decodePushJson(rawText);
const callId = String(json.callId || '').trim();
const fromSessionId = String(json.fromSessionId || '').trim();
const targetSessionId = String(json.targetSessionId || '').trim();
const toLogin = String(json.toLogin || '').trim();
const reason = String(json.reason || '').trim();
const sentAtMs = Number(json.sentAtMs || 0);
@@ -139,6 +140,7 @@ self.addEventListener('push', (event) => {
callId,
fromLogin,
fromSessionId,
targetSessionId,
toLogin,
sentAtMs,
expiresAtMs,
@@ -165,6 +167,7 @@ self.addEventListener('push', (event) => {
body,
fromLogin,
fromSessionId,
targetSessionId,
toLogin,
callId,
sentAtMs,
@@ -186,6 +189,7 @@ self.addEventListener('notificationclick', (event) => {
callId: String(data.callId || '').trim(),
fromLogin: String(data.fromLogin || '').trim(),
fromSessionId: String(data.fromSessionId || '').trim(),
targetSessionId: String(data.targetSessionId || '').trim(),
toLogin: String(data.toLogin || '').trim(),
sentAtMs: Number(data.sentAtMs || 0),
expiresAtMs: Number(data.expiresAtMs || 0),
+10
View File
@@ -269,6 +269,13 @@ function savePendingCallPushAction(action, payload = {}) {
}
}
function isCallPushTargetForCurrentSession(payload = {}) {
const targetSessionId = String(payload?.targetSessionId || '').trim();
if (!targetSessionId) return true;
const currentSessionId = String(state?.session?.sessionId || '').trim();
return Boolean(currentSessionId) && currentSessionId === targetSessionId;
}
function loadPendingCallPushAction() {
try {
const raw = localStorage.getItem(CALL_PUSH_PENDING_ACTION_KEY);
@@ -322,6 +329,7 @@ async function processPendingCallPushActionIfPossible() {
if (!state.session.isAuthorized) return;
const pending = loadPendingCallPushAction();
if (!pending) return;
if (!isCallPushTargetForCurrentSession(pending.payload || {})) return;
clearPendingCallPushAction();
try {
await handleCallPushAction(pending.action, pending.payload || {});
@@ -827,6 +835,7 @@ async function init() {
const action = String(data.action || '').trim().toLowerCase();
const payload = data.payload || {};
if (action === 'accept' || action === 'decline') {
if (!isCallPushTargetForCurrentSession(payload)) return;
savePendingCallPushAction(action, payload);
void processPendingCallPushActionIfPossible();
}
@@ -835,6 +844,7 @@ async function init() {
if (data.type !== 'SHINE_WEB_PUSH_EVENT') return;
const payload = data.payload || {};
if (!isCallPushTargetForCurrentSession(payload)) return;
const kind = String(payload.kind || '').trim();
const now = Date.now();
try {
+22 -3
View File
@@ -831,6 +831,9 @@ async function finalizeCall(call, {
localReasonCode = 'error',
debugReason = '',
notifyRemoteHangup = false,
suppressRemoteSignal = false,
suppressReports = false,
suppressSummary = false,
} = {}) {
if (!call) return;
const diagnosticsBeforeClose = getCallDiagnosticsContext(call);
@@ -839,7 +842,8 @@ async function finalizeCall(call, {
stopTone();
const shouldNotifyRemoteFailure =
!notifyRemoteHangup
!suppressRemoteSignal
&& !notifyRemoteHangup
&& Boolean(call.remoteSessionId)
&& String(localReasonCode || '') !== 'completed'
&& String(debugReason || '') !== 'remote_hangup';
@@ -868,7 +872,7 @@ async function finalizeCall(call, {
}
const reasonText = debugReason || localReasonCode;
if (String(localReasonCode || '') !== 'completed') {
if (!suppressReports && String(localReasonCode || '') !== 'completed') {
const failureStage = call.phase || '';
const failureContext = {
failureStage,
@@ -890,7 +894,9 @@ async function finalizeCall(call, {
}
}
pushCallSummary(call, localReasonCode);
if (!suppressSummary) {
pushCallSummary(call, localReasonCode);
}
call.phase = 'ended';
call.statusText = 'Звонок завершён';
@@ -1128,6 +1134,13 @@ function isIncomingCallPushFresh(payload) {
return true;
}
function isCallPushForCurrentSession(payload = {}) {
const targetSessionId = String(payload?.targetSessionId || '').trim();
if (!targetSessionId) return true;
const currentSessionId = String(state?.session?.sessionId || '').trim();
return Boolean(currentSessionId) && currentSessionId === targetSessionId;
}
async function handleIncomingInvitePayload(payload, { source = 'ws' } = {}) {
const callId = String(payload?.callId || '').trim();
const fromLogin = String(payload?.fromLogin || '').trim();
@@ -1627,11 +1640,13 @@ export async function hangupActiveCall() {
}
export async function handleIncomingCallPush(payload = {}) {
if (!isCallPushForCurrentSession(payload)) return;
if (!isIncomingCallPushFresh(payload)) return;
await handleIncomingInvitePayload(payload, { source: 'push' });
}
export async function handleStopCallPush(payload = {}) {
if (!isCallPushForCurrentSession(payload)) return;
const callId = String(payload?.callId || '').trim();
if (!callId) return;
const call = getCall(callId);
@@ -1646,10 +1661,14 @@ export async function handleStopCallPush(payload = {}) {
await finalizeCall(call, {
localReasonCode: call.connectedAtMs ? 'completed' : 'no_answer',
debugReason: `stop_call_push:${reason}`,
suppressRemoteSignal: true,
suppressReports: true,
suppressSummary: true,
});
}
export async function handleCallPushAction(action, payload = {}) {
if (!isCallPushForCurrentSession(payload)) return;
const normalized = String(action || '').trim().toLowerCase();
if (normalized !== 'accept' && normalized !== 'decline') return;
if (!isIncomingCallPushFresh(payload)) return;