@@ -41,6 +41,8 @@ import { showActionSheetRef } from '../containers/ActionSheet';
4141import { SupportedVersionsWarning } from '../containers/SupportedVersions' ;
4242import { mediaSessionInstance } from '../lib/services/voip/MediaSessionInstance' ;
4343import { hasPermission } from '../lib/methods/helpers/helpers' ;
44+ import { mediaSessionStore } from '../lib/services/voip/MediaSessionStore' ;
45+ import { store as reduxStore } from '../lib/store/auxStore' ;
4446
4547const getServer = state => state . server . server ;
4648const 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 ( / p e r m i s s i o n s - c h a n g e d / . test ( eventName ) ) {
289+ await checkVoipPermission ( ) ;
290+ }
291+ } ) ;
292+ } ;
293+
262294const handleLoginSuccess = function * handleLoginSuccess ( { user } ) {
263295 try {
264296 yield put ( setUser ( user ) ) ;
@@ -331,6 +363,7 @@ const handleLoginSuccess = function* handleLoginSuccess({ user }) {
331363} ;
332364
333365const 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
409442const 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