Skip to content

Commit 0e6f6ae

Browse files
authored
fix: make startVoipFork reactive to permissions-changed (#7151)
1 parent 0157023 commit 0e6f6ae

2 files changed

Lines changed: 45 additions & 9 deletions

File tree

app/lib/services/voip/MediaSessionInstance.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { mediaSessionStore } from './MediaSessionStore';
1616
import { useCallStore } from './useCallStore';
1717
import { store } from '../../store/auxStore';
1818
import sdk from '../sdk';
19+
import { mediaCallsStateSignals } from '../restApi';
1920
import Navigation from '../../navigation/appNavigation';
2021
import { parseStringToIceServers } from './parseStringToIceServers';
2122
import type { IceServer } from '../../../definitions/Voip';
@@ -24,7 +25,6 @@ import type { ISubscription, TSubscriptionModel } from '../../../definitions';
2425
import { getDMSubscriptionByUsername } from '../../database/services/Subscription';
2526
import { getUidDirectMessage } from '../../methods/helpers/helpers';
2627
import { requestPhoneStatePermission } from '../../methods/voipPhoneStatePermission';
27-
import { mediaCallsStateSignals } from '../restApi';
2828

2929
class MediaSessionInstance {
3030
private iceServers: IceServer[] = [];
@@ -80,6 +80,7 @@ class MediaSessionInstance {
8080
iceGatheringTimeout: this.iceGatheringTimeout
8181
})
8282
);
83+
// TESTING: DDP signal transport — offer/answer/ICE stay on DDP
8384
mediaSessionStore.setSendSignalFn((signal: ClientMediaSignal) => {
8485
sdk.methodCall('stream-notify-user', `${userId}/media-calls`, JSON.stringify(signal));
8586
});
@@ -95,6 +96,7 @@ class MediaSessionInstance {
9596
this.instance = mediaSessionStore.getInstance(userId);
9697
});
9798

99+
// TESTING: DDP real-time signal subscription — stays for offer/answer/ICE/notifications
98100
this.mediaSignalListener = sdk.onStreamData('stream-notify-user', (ddpMessage: IDDPMessage) => {
99101
if (!this.instance) {
100102
return;

app/sagas/login.js

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ import { showActionSheetRef } from '../containers/ActionSheet';
4141
import { SupportedVersionsWarning } from '../containers/SupportedVersions';
4242
import { mediaSessionInstance } from '../lib/services/voip/MediaSessionInstance';
4343
import { hasPermission } from '../lib/methods/helpers/helpers';
44+
import { mediaSessionStore } from '../lib/services/voip/MediaSessionStore';
45+
import { store as reduxStore } from '../lib/store/auxStore';
4446

4547
const getServer = state => state.server.server;
4648
const loginWithPasswordCall = args => loginWithPassword(args);
@@ -242,23 +244,53 @@ const checkBackgroundAndSetAway = function* checkBackgroundAndSetAway() {
242244
}
243245
};
244246

245-
const startVoipFork = function* startVoipFork() {
247+
const checkVoipPermission = async () => {
246248
try {
247-
const allowInternalVoiceCallRoles = yield select(state => state.permissions['allow-internal-voice-calls']);
248-
const allowExternalVoiceCallRoles = yield select(state => state.permissions['allow-external-voice-calls']);
249+
const state = reduxStore.getState();
250+
const userId = state.login.user?.id;
251+
if (!userId) {
252+
return;
253+
}
249254

250-
const hasPermissions = yield hasPermission([allowInternalVoiceCallRoles, allowExternalVoiceCallRoles]);
251-
if (isVoipModuleAvailable() && (hasPermissions[0] || hasPermissions[1])) {
252-
const userId = yield select(state => state.login.user.id);
253-
mediaSessionInstance.init(userId);
254-
} else {
255+
const hasPermissions = await hasPermission([
256+
state.permissions['allow-internal-voice-calls'],
257+
state.permissions['allow-external-voice-calls']
258+
]);
259+
const canUseVoip = isVoipModuleAvailable() && (hasPermissions[0] || hasPermissions[1]);
260+
261+
if (!canUseVoip) {
255262
mediaSessionInstance.reset();
263+
return;
264+
}
265+
if (!mediaSessionStore.getCurrentInstance()) {
266+
mediaSessionInstance.init(userId);
256267
}
257268
} catch (e) {
258269
log(e);
259270
}
260271
};
261272

273+
let voipPermissionListener;
274+
275+
const stopVoipPermissionListener = () => {
276+
if (voipPermissionListener) {
277+
voipPermissionListener.stop();
278+
voipPermissionListener = null;
279+
}
280+
};
281+
282+
const startVoipFork = function* startVoipFork() {
283+
yield call(checkVoipPermission);
284+
285+
stopVoipPermissionListener();
286+
voipPermissionListener = sdk.current.onStreamData('stream-notify-logged', async ddpMessage => {
287+
const { eventName } = ddpMessage.fields || {};
288+
if (/permissions-changed/.test(eventName)) {
289+
await checkVoipPermission();
290+
}
291+
});
292+
};
293+
262294
const handleLoginSuccess = function* handleLoginSuccess({ user }) {
263295
try {
264296
yield put(setUser(user));
@@ -331,6 +363,7 @@ const handleLoginSuccess = function* handleLoginSuccess({ user }) {
331363
};
332364

333365
const handleLogout = function* handleLogout({ forcedByServer, message }) {
366+
stopVoipPermissionListener();
334367
yield put(encryptionStop());
335368
yield put(appStart({ root: RootEnum.ROOT_LOADING, text: I18n.t('Logging_out') }));
336369
const server = yield select(getServer);
@@ -407,6 +440,7 @@ const handleSetUser = function* handleSetUser({ user }) {
407440
};
408441

409442
const handleDeleteAccount = function* handleDeleteAccount() {
443+
stopVoipPermissionListener();
410444
yield put(encryptionStop());
411445
yield put(appStart({ root: RootEnum.ROOT_LOADING, text: I18n.t('Deleting_account') }));
412446
const server = yield select(getServer);

0 commit comments

Comments
 (0)