From d71b19e1e5ee935d25b33e33406bc8d4bdc8ec1c Mon Sep 17 00:00:00 2001 From: siepra Date: Wed, 17 Apr 2024 14:20:15 +0200 Subject: [PATCH 01/12] chore: start own metro --- .github/workflows/e2e-android-self.yml | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/.github/workflows/e2e-android-self.yml b/.github/workflows/e2e-android-self.yml index 113d6e1d6f..2e96f4f94d 100644 --- a/.github/workflows/e2e-android-self.yml +++ b/.github/workflows/e2e-android-self.yml @@ -32,6 +32,14 @@ jobs: ndk.path=/Users/quiet/Library/Android/sdk/ndk/25.1.8937393 EOF + - name: Install pm2 + run: npm install pm2@latest -g + + - name: Start metro + run: | + cd packages/mobile + pm2 --name METRO start npm -- start + - name: Build Detox run: | export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home @@ -41,4 +49,7 @@ jobs: - name: Run basic tests run: | cd packages/mobile - detox test starter -c android.emu.debug.ci \ No newline at end of file + detox test starter -c android.emu.debug.ci + + - name: Stop metro + run: pm2 stop METRO From 13dbb74869fd52f19eb76a6d5f0c086dd1f63beb Mon Sep 17 00:00:00 2001 From: siepra Date: Fri, 19 Apr 2024 12:58:08 +0200 Subject: [PATCH 02/12] chore: comment out restore connection saga usage --- packages/mobile/src/store/root.saga.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/mobile/src/store/root.saga.ts b/packages/mobile/src/store/root.saga.ts index b77b14dfb4..041aedf481 100644 --- a/packages/mobile/src/store/root.saga.ts +++ b/packages/mobile/src/store/root.saga.ts @@ -14,7 +14,7 @@ export function* rootSaga(): Generator { takeEvery(initActions.setStoreReady.type, initMasterSaga), takeEvery(initActions.setStoreReady.type, navigationMasterSaga), takeEvery(initActions.setStoreReady.type, nativeServicesMasterSaga), - fork(restoreConnectionSaga), + // fork(restoreConnectionSaga), // Below line is reponsible for displaying notifications about messages from channels other than currently viewing one takeEvery(publicChannels.actions.markUnreadChannel.type, showNotificationSaga), ]) From 6778d4e172ba9980c6d84745bc078688016eec99 Mon Sep 17 00:00:00 2001 From: siepra Date: Fri, 19 Apr 2024 12:58:34 +0200 Subject: [PATCH 03/12] chore: repeat websocket connection tries on android --- .../main/java/com/quietmobile/Backend/BackendWorker.kt | 8 ++++++-- .../app/src/main/java/com/quietmobile/Utils/Const.kt | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/mobile/android/app/src/main/java/com/quietmobile/Backend/BackendWorker.kt b/packages/mobile/android/app/src/main/java/com/quietmobile/Backend/BackendWorker.kt index b25ff8ef7f..303ca560c0 100644 --- a/packages/mobile/android/app/src/main/java/com/quietmobile/Backend/BackendWorker.kt +++ b/packages/mobile/android/app/src/main/java/com/quietmobile/Backend/BackendWorker.kt @@ -121,8 +121,12 @@ class BackendWorker(private val context: Context, workerParams: WorkerParameters * * In any case, websocket won't connect until data server starts listening */ - delay(WEBSOCKET_CONNECTION_DELAY) - startWebsocketConnection(dataPort, socketIOSecret) + val intervals: Array = arrayOf(WEBSOCKET_CONNECTION_DELAY, 15000, 30000, 60000, 90000) + + for (interval in intervals) { + delay(interval) + startWebsocketConnection(dataPort, socketIOSecret) + } } val dataPath = Utils.createDirectory(context) diff --git a/packages/mobile/android/app/src/main/java/com/quietmobile/Utils/Const.kt b/packages/mobile/android/app/src/main/java/com/quietmobile/Utils/Const.kt index ae769aa39d..2a73ae6a99 100644 --- a/packages/mobile/android/app/src/main/java/com/quietmobile/Utils/Const.kt +++ b/packages/mobile/android/app/src/main/java/com/quietmobile/Utils/Const.kt @@ -24,5 +24,5 @@ object Const { const val NODEJS_TRASH_DIR = "nodejs-project-trash" // Websocket - const val WEBSOCKET_CONNECTION_DELAY: Long = 7000 + const val WEBSOCKET_CONNECTION_DELAY: Long = 5000 } From 9d3dafd79aa4743cb968862bc7c4ca09c29edb74 Mon Sep 17 00:00:00 2001 From: siepra Date: Fri, 19 Apr 2024 12:58:41 +0200 Subject: [PATCH 04/12] chore: update pods --- packages/mobile/ios/Quiet.xcodeproj/project.pbxproj | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/mobile/ios/Quiet.xcodeproj/project.pbxproj b/packages/mobile/ios/Quiet.xcodeproj/project.pbxproj index c101495008..3a85467e84 100644 --- a/packages/mobile/ios/Quiet.xcodeproj/project.pbxproj +++ b/packages/mobile/ios/Quiet.xcodeproj/project.pbxproj @@ -5482,7 +5482,8 @@ OTHER_CPLUSPLUSFLAGS = "$(inherited)"; OTHER_LDFLAGS = ( "$(inherited)", - " ", + "-Wl", + "-ld_classic", ); REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native"; SDKROOT = iphoneos; @@ -5547,7 +5548,8 @@ OTHER_CPLUSPLUSFLAGS = "$(inherited)"; OTHER_LDFLAGS = ( "$(inherited)", - " ", + "-Wl", + "-ld_classic", ); REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native"; SDKROOT = iphoneos; From 852c5c7c557d5770c4fdc23c235c06c5a00edb9a Mon Sep 17 00:00:00 2001 From: siepra Date: Fri, 19 Apr 2024 13:10:46 +0200 Subject: [PATCH 05/12] chore: increase action timeout --- .github/workflows/e2e-android-self.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/e2e-android-self.yml b/.github/workflows/e2e-android-self.yml index 2e96f4f94d..11baac2ebc 100644 --- a/.github/workflows/e2e-android-self.yml +++ b/.github/workflows/e2e-android-self.yml @@ -10,7 +10,7 @@ on: jobs: detox-android-self-hosted: - timeout-minutes: 10 + timeout-minutes: 25 runs-on: [self-hosted, macOS, ARM64, android] steps: From 338ad1053b498c27b798ed637c5558c56c369b87 Mon Sep 17 00:00:00 2001 From: siepra Date: Mon, 22 Apr 2024 13:38:55 +0200 Subject: [PATCH 06/12] fix: always stop metro --- .github/workflows/e2e-android-self.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/e2e-android-self.yml b/.github/workflows/e2e-android-self.yml index 11baac2ebc..1cf7ab9493 100644 --- a/.github/workflows/e2e-android-self.yml +++ b/.github/workflows/e2e-android-self.yml @@ -52,4 +52,5 @@ jobs: detox test starter -c android.emu.debug.ci - name: Stop metro + if: always() run: pm2 stop METRO From 120e25053968e92767f36348c4e285652a7a837a Mon Sep 17 00:00:00 2001 From: siepra Date: Mon, 22 Apr 2024 13:52:07 +0200 Subject: [PATCH 07/12] chore: limit intervals --- .../app/src/main/java/com/quietmobile/Backend/BackendWorker.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/mobile/android/app/src/main/java/com/quietmobile/Backend/BackendWorker.kt b/packages/mobile/android/app/src/main/java/com/quietmobile/Backend/BackendWorker.kt index 303ca560c0..7d776ad6dc 100644 --- a/packages/mobile/android/app/src/main/java/com/quietmobile/Backend/BackendWorker.kt +++ b/packages/mobile/android/app/src/main/java/com/quietmobile/Backend/BackendWorker.kt @@ -121,7 +121,7 @@ class BackendWorker(private val context: Context, workerParams: WorkerParameters * * In any case, websocket won't connect until data server starts listening */ - val intervals: Array = arrayOf(WEBSOCKET_CONNECTION_DELAY, 15000, 30000, 60000, 90000) + val intervals: Array = arrayOf(15000) for (interval in intervals) { delay(interval) From 8f759e02c4c862f888b74776d5ec4cf365553b71 Mon Sep 17 00:00:00 2001 From: siepra Date: Mon, 22 Apr 2024 14:07:56 +0200 Subject: [PATCH 08/12] chore: enlarge intervals --- .../app/src/main/java/com/quietmobile/Backend/BackendWorker.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/mobile/android/app/src/main/java/com/quietmobile/Backend/BackendWorker.kt b/packages/mobile/android/app/src/main/java/com/quietmobile/Backend/BackendWorker.kt index 7d776ad6dc..acc28c1c89 100644 --- a/packages/mobile/android/app/src/main/java/com/quietmobile/Backend/BackendWorker.kt +++ b/packages/mobile/android/app/src/main/java/com/quietmobile/Backend/BackendWorker.kt @@ -121,7 +121,7 @@ class BackendWorker(private val context: Context, workerParams: WorkerParameters * * In any case, websocket won't connect until data server starts listening */ - val intervals: Array = arrayOf(15000) + val intervals: Array = arrayOf(30000) for (interval in intervals) { delay(interval) From 12b24d4474eb42713dbee86ac76296db676c3f64 Mon Sep 17 00:00:00 2001 From: siepra Date: Tue, 23 Apr 2024 11:04:50 +0200 Subject: [PATCH 09/12] fix: use callback on react app init --- .../com/quietmobile/Backend/BackendWorker.kt | 44 +++---------------- .../Communication/CommunicationModule.java | 20 ++++++++- .../java/com/quietmobile/MainApplication.kt | 19 ++++++++ .../main/java/com/quietmobile/Utils/Const.kt | 3 -- .../main/java/com/quietmobile/Utils/Utils.kt | 2 + packages/mobile/src/App.tsx | 6 ++- packages/state-manager/src/constants.ts | 1 + 7 files changed, 51 insertions(+), 44 deletions(-) diff --git a/packages/mobile/android/app/src/main/java/com/quietmobile/Backend/BackendWorker.kt b/packages/mobile/android/app/src/main/java/com/quietmobile/Backend/BackendWorker.kt index acc28c1c89..57ea48a0ee 100644 --- a/packages/mobile/android/app/src/main/java/com/quietmobile/Backend/BackendWorker.kt +++ b/packages/mobile/android/app/src/main/java/com/quietmobile/Backend/BackendWorker.kt @@ -9,14 +9,12 @@ import androidx.core.app.NotificationCompat import androidx.work.CoroutineWorker import androidx.work.ForegroundInfo import androidx.work.WorkerParameters -import com.google.gson.Gson import com.quietmobile.BuildConfig import com.quietmobile.Communication.CommunicationModule +import com.quietmobile.MainApplication import com.quietmobile.Notification.NotificationHandler import com.quietmobile.R -import com.quietmobile.Scheme.WebsocketConnectionPayload import com.quietmobile.Utils.Const -import com.quietmobile.Utils.Const.WEBSOCKET_CONNECTION_DELAY import com.quietmobile.Utils.Utils import com.quietmobile.Utils.isAppOnForeground import io.socket.client.IO @@ -98,9 +96,12 @@ class BackendWorker(private val context: Context, workerParams: WorkerParameters withContext(Dispatchers.IO) { // Get and store data port for usage in methods across the app - val dataPort = Utils.getOpenPort(11000) + val socketPort = Utils.getOpenPort(11000) val socketIOSecret = Utils.generateRandomString(20) + (applicationContext as MainApplication).setSocketPort(socketPort) + (applicationContext as MainApplication).setSocketIOSecret(socketIOSecret) + // Init nodejs project launch { nodeProject.init() @@ -108,25 +109,7 @@ class BackendWorker(private val context: Context, workerParams: WorkerParameters launch { notificationHandler = NotificationHandler(context) - subscribePushNotifications(dataPort, socketIOSecret) - } - - launch { - /* - * Wait for CommunicationModule to be initialized with reactContext - * (there's no callback we can use for that purpose). - * - * Code featured below suspends nothing but the websocket connection - * and it doesn't affect anything besides that. - * - * In any case, websocket won't connect until data server starts listening - */ - val intervals: Array = arrayOf(30000) - - for (interval in intervals) { - delay(interval) - startWebsocketConnection(dataPort, socketIOSecret) - } + subscribePushNotifications(socketPort, socketIOSecret) } val dataPath = Utils.createDirectory(context) @@ -143,7 +126,7 @@ class BackendWorker(private val context: Context, workerParams: WorkerParameters * https://github.com/TryQuiet/quiet/issues/2214 */ delay(500) - startNodeProjectWithArguments("bundle.cjs --torBinary $torBinary --dataPath $dataPath --dataPort $dataPort --platform $platform --socketIOSecret $socketIOSecret") + startNodeProjectWithArguments("bundle.cjs --torBinary $torBinary --dataPath $dataPath --dataPort $socketPort --platform $platform --socketIOSecret $socketIOSecret") } } @@ -159,8 +142,6 @@ class BackendWorker(private val context: Context, workerParams: WorkerParameters return Result.success() } - private external fun sendMessageToNodeChannel(channelName: String, message: String): Void - private external fun startNodeWithArguments( arguments: Array?, modulesPath: String? @@ -218,17 +199,6 @@ class BackendWorker(private val context: Context, workerParams: WorkerParameters notificationHandler.notify(message, username) } - private fun startWebsocketConnection(port: Int, socketIOSecret: String) { - Log.d("WEBSOCKET CONNECTION", "Starting on $port") - // Proceed only if data port is defined - val websocketConnectionPayload = WebsocketConnectionPayload(port, socketIOSecret) - CommunicationModule.handleIncomingEvents( - CommunicationModule.WEBSOCKET_CONNECTION_CHANNEL, - Gson().toJson(websocketConnectionPayload), - "" // Empty extras - ) - } - fun handleNodeMessages(channelName: String, msg: String?) { print("handle node message - channel name $channelName") print("handle node message - msg $msg") diff --git a/packages/mobile/android/app/src/main/java/com/quietmobile/Communication/CommunicationModule.java b/packages/mobile/android/app/src/main/java/com/quietmobile/Communication/CommunicationModule.java index e51ee1f125..ff186756b6 100644 --- a/packages/mobile/android/app/src/main/java/com/quietmobile/Communication/CommunicationModule.java +++ b/packages/mobile/android/app/src/main/java/com/quietmobile/Communication/CommunicationModule.java @@ -10,7 +10,10 @@ import com.facebook.react.bridge.ReactMethod; import com.facebook.react.bridge.WritableMap; import com.facebook.react.modules.core.RCTNativeAppEventEmitter; +import com.google.gson.Gson; +import com.quietmobile.MainApplication; import com.quietmobile.Notification.NotificationHandler; +import com.quietmobile.Scheme.WebsocketConnectionPayload; import androidx.annotation.NonNull; @@ -24,6 +27,8 @@ public class CommunicationModule extends ReactContextBaseJavaModule { + public static final String APP_READY_CHANNEL = "_APP_READY_"; + public static final String PUSH_NOTIFICATION_CHANNEL = "_PUSH_NOTIFICATION_"; public static final String WEBSOCKET_CONNECTION_CHANNEL = "_WEBSOCKET_CONNECTION_"; public static final String INIT_CHECK_CHANNEL = "_INIT_CHECK_"; @@ -48,14 +53,16 @@ public CommunicationModule(ReactApplicationContext reactContext) { } @ReactMethod - public static void handleIncomingEvents(String event, String payload, String extra) { + public static void handleIncomingEvents(String event, @Nullable String payload, @Nullable String extra) { switch (event) { + case APP_READY_CHANNEL: + startWebsocketConnection(); + break; case PUSH_NOTIFICATION_CHANNEL: String message = payload; String username = extra; notificationHandler.notify(message, username); break; - case WEBSOCKET_CONNECTION_CHANNEL: case INIT_CHECK_CHANNEL: case BACKEND_CLOSED_CHANNEL: passDataToReact(event, payload); @@ -88,6 +95,15 @@ private static void sendEvent(@Nullable WritableMap params) { } } + private static void startWebsocketConnection() { + Context context = reactContext.getApplicationContext(); + int port = ((MainApplication) context).getSocketPort(); + String socketIOSecret = ((MainApplication) context).getSocketIOSecret(); + + WebsocketConnectionPayload websocketConnectionPayload = new WebsocketConnectionPayload(port, socketIOSecret); + passDataToReact(WEBSOCKET_CONNECTION_CHANNEL, new Gson().toJson(websocketConnectionPayload)); + } + @ReactMethod private static void deleteBackendData() { Context context = reactContext.getApplicationContext(); diff --git a/packages/mobile/android/app/src/main/java/com/quietmobile/MainApplication.kt b/packages/mobile/android/app/src/main/java/com/quietmobile/MainApplication.kt index 901a31de38..6b29a6d01a 100644 --- a/packages/mobile/android/app/src/main/java/com/quietmobile/MainApplication.kt +++ b/packages/mobile/android/app/src/main/java/com/quietmobile/MainApplication.kt @@ -52,6 +52,25 @@ class MainApplication : Application(), ReactApplication { createNotificationChannel() } + private var socketPort: Int = 0 + private var socketIOSecret: String = "" + + fun getSocketPort(): Int { + return socketPort + } + + fun setSocketPort(value: Int) { + this.socketPort = value + } + + fun getSocketIOSecret(): String { + return socketIOSecret + } + + fun setSocketIOSecret(value: String) { + this.socketIOSecret = value + } + private fun createForegroundServiceNotificationChannel() { // Create the NotificationChannel, but only on API 26+ because // the NotificationChannel class is new and not in the support library diff --git a/packages/mobile/android/app/src/main/java/com/quietmobile/Utils/Const.kt b/packages/mobile/android/app/src/main/java/com/quietmobile/Utils/Const.kt index 2a73ae6a99..7254e6a0dc 100644 --- a/packages/mobile/android/app/src/main/java/com/quietmobile/Utils/Const.kt +++ b/packages/mobile/android/app/src/main/java/com/quietmobile/Utils/Const.kt @@ -22,7 +22,4 @@ object Const { const val NODEJS_PROJECT_DIR = "nodejs-project" const val NODEJS_BUILTIN_NATIVE_ASSETS_PREFIX = "nodejs-native-assets-" const val NODEJS_TRASH_DIR = "nodejs-project-trash" - - // Websocket - const val WEBSOCKET_CONNECTION_DELAY: Long = 5000 } diff --git a/packages/mobile/android/app/src/main/java/com/quietmobile/Utils/Utils.kt b/packages/mobile/android/app/src/main/java/com/quietmobile/Utils/Utils.kt index 29491191a0..7d38d92c2a 100644 --- a/packages/mobile/android/app/src/main/java/com/quietmobile/Utils/Utils.kt +++ b/packages/mobile/android/app/src/main/java/com/quietmobile/Utils/Utils.kt @@ -30,6 +30,7 @@ object Utils { return dataDirectory.absolutePath } + @JvmStatic fun generateRandomString(length: Int): String { val CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" val secureRandom = SecureRandom() @@ -43,6 +44,7 @@ object Utils { return randomString.toString() } + @JvmStatic suspend fun getOpenPort(starting: Int) = suspendCoroutine { continuation -> val port = checkPort(starting) continuation.resume(port) diff --git a/packages/mobile/src/App.tsx b/packages/mobile/src/App.tsx index f5128ddc39..9944888ea9 100644 --- a/packages/mobile/src/App.tsx +++ b/packages/mobile/src/App.tsx @@ -1,7 +1,9 @@ import React, { useEffect } from 'react' import { useDispatch } from 'react-redux' -import { LogBox, StatusBar } from 'react-native' +import { LogBox, NativeModules, StatusBar } from 'react-native' + +import { APP_READY_CHANNEL } from '@quiet/state-manager' import WebviewCrypto from 'react-native-webview-crypto' @@ -28,7 +30,6 @@ import { NavigationContainer } from '@react-navigation/native' import { createNativeStackNavigator } from '@react-navigation/native-stack' import { navigationRef } from './RootNavigation' -import { initActions } from './store/init/init.slice' import { navigationActions } from './store/navigation/navigation.slice' import { rootSaga } from './store/root.saga' @@ -76,6 +77,7 @@ function App(): JSX.Element { useEffect(() => { console.log('LAUNCHED APPLICATION: ', (Math.random() + 1).toString(36).substring(7)) + NativeModules.CommunicationModule.handleIncomingEvents(APP_READY_CHANNEL, null, null) }, []) return ( diff --git a/packages/state-manager/src/constants.ts b/packages/state-manager/src/constants.ts index a8f9ac9130..848b3409ca 100644 --- a/packages/state-manager/src/constants.ts +++ b/packages/state-manager/src/constants.ts @@ -1,5 +1,6 @@ export const AUTODOWNLOAD_SIZE_LIMIT = 20971520 // 20 MB +export const APP_READY_CHANNEL = '_APP_READY_' export const PUSH_NOTIFICATION_CHANNEL = '_PUSH_NOTIFICATION_' export const WEBSOCKET_CONNECTION_CHANNEL = '_WEBSOCKET_CONNECTION_' export const INIT_CHECK_CHANNEL = '_INIT_CHECK_' From de6eb32cc506f9a89bf370641439138e1c1f8fce Mon Sep 17 00:00:00 2001 From: siepra Date: Tue, 23 Apr 2024 11:14:53 +0200 Subject: [PATCH 10/12] chore: remove redundand sagas --- .../blindConnection/blindConnection.saga.ts | 15 ------- .../mobile/src/store/init/init.master.saga.ts | 4 +- packages/mobile/src/store/init/init.slice.ts | 1 - .../restoreConnection.saga.test.ts | 41 ------------------- .../restoreConnection.saga.ts | 20 --------- packages/mobile/src/store/root.saga.ts | 4 +- 6 files changed, 2 insertions(+), 83 deletions(-) delete mode 100644 packages/mobile/src/store/init/blindConnection/blindConnection.saga.ts delete mode 100644 packages/mobile/src/store/init/startConnection/restoreConnection/restoreConnection.saga.test.ts delete mode 100644 packages/mobile/src/store/init/startConnection/restoreConnection/restoreConnection.saga.ts diff --git a/packages/mobile/src/store/init/blindConnection/blindConnection.saga.ts b/packages/mobile/src/store/init/blindConnection/blindConnection.saga.ts deleted file mode 100644 index 44571067c0..0000000000 --- a/packages/mobile/src/store/init/blindConnection/blindConnection.saga.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { select, put } from 'typed-redux-saga' -import { initSelectors } from '../init.selectors' -import { initActions } from '../init.slice' - -export function* blindConnectionSaga(): Generator { - const isWebsocketConnected = yield* select(initSelectors.isWebsocketConnected) - const lastKnownSocketIOData = yield* select(initSelectors.lastKnownSocketIOData) - - console.log('WEBSOCKET', 'Entered blind connection saga', isWebsocketConnected, lastKnownSocketIOData) - - if (!isWebsocketConnected && lastKnownSocketIOData.dataPort !== 0) { - console.log('WEBSOCKET', 'Hooking up blindly at last known data port: ', lastKnownSocketIOData.dataPort) - yield* put(initActions.startWebsocketConnection(lastKnownSocketIOData)) - } -} diff --git a/packages/mobile/src/store/init/init.master.saga.ts b/packages/mobile/src/store/init/init.master.saga.ts index af5ddec265..82fc31efd1 100644 --- a/packages/mobile/src/store/init/init.master.saga.ts +++ b/packages/mobile/src/store/init/init.master.saga.ts @@ -1,12 +1,10 @@ -import { all, takeEvery, takeLatest, takeLeading } from 'typed-redux-saga' +import { all, takeLatest, takeLeading } from 'typed-redux-saga' import { initActions } from './init.slice' -import { blindConnectionSaga } from './blindConnection/blindConnection.saga' import { startConnectionSaga } from './startConnection/startConnection.saga' import { deepLinkSaga } from './deepLink/deepLink.saga' export function* initMasterSaga(): Generator { yield all([ - takeEvery(initActions.blindWebsocketConnection.type, blindConnectionSaga), takeLatest(initActions.startWebsocketConnection.type, startConnectionSaga), takeLeading(initActions.deepLink.type, deepLinkSaga), ]) diff --git a/packages/mobile/src/store/init/init.slice.ts b/packages/mobile/src/store/init/init.slice.ts index f256964c22..f9106199d6 100644 --- a/packages/mobile/src/store/init/init.slice.ts +++ b/packages/mobile/src/store/init/init.slice.ts @@ -60,7 +60,6 @@ export const initSlice = createSlice({ id: event, }) }, - blindWebsocketConnection: state => state, startWebsocketConnection: (state, _action: PayloadAction) => state, suspendWebsocketConnection: state => { state.isWebsocketConnected = false diff --git a/packages/mobile/src/store/init/startConnection/restoreConnection/restoreConnection.saga.test.ts b/packages/mobile/src/store/init/startConnection/restoreConnection/restoreConnection.saga.test.ts deleted file mode 100644 index c9fe7cc4dd..0000000000 --- a/packages/mobile/src/store/init/startConnection/restoreConnection/restoreConnection.saga.test.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { socket } from '@quiet/state-manager' -import { combineReducers } from '@reduxjs/toolkit' -import { expectSaga } from 'redux-saga-test-plan' -import { StoreKeys } from '../../../store.keys' -import { initActions, initReducer, InitState } from '../../init.slice' -import { restoreConnectionSaga } from './restoreConnection.saga' - -describe('restoreConnectionSaga', () => { - test('do nothing if connection is already started', async () => { - const socketIOData = { - dataPort: 9477, - socketIOSecret: 'secret', - } - await expectSaga(restoreConnectionSaga) - .withReducer(combineReducers({ [StoreKeys.Init]: initReducer }), { - [StoreKeys.Init]: { - ...new InitState(), - isWebsocketConnected: true, - lastKnownSocketIOData: socketIOData, - }, - }) - .not.put(initActions.startWebsocketConnection(socketIOData)) - .run() - }) - test('do nothing if last known data port is not set', async () => { - const socketIOData = { - dataPort: 0, - socketIOSecret: 'secret', - } - await expectSaga(restoreConnectionSaga) - .withReducer(combineReducers({ [StoreKeys.Init]: initReducer }), { - [StoreKeys.Init]: { - ...new InitState(), - isWebsocketConnected: false, - lastKnownSocketIOData: socketIOData, - }, - }) - .not.put(initActions.startWebsocketConnection(socketIOData)) - .run() - }) -}) diff --git a/packages/mobile/src/store/init/startConnection/restoreConnection/restoreConnection.saga.ts b/packages/mobile/src/store/init/startConnection/restoreConnection/restoreConnection.saga.ts deleted file mode 100644 index 3a7c001360..0000000000 --- a/packages/mobile/src/store/init/startConnection/restoreConnection/restoreConnection.saga.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { delay, put, select } from 'typed-redux-saga' -import { initSelectors } from '../../init.selectors' -import { initActions } from '../../init.slice' - -const WEBSOCKET_CONNECTION_DELAY = 15000 - -export function* restoreConnectionSaga(): Generator { - // Give the worker time to init websocket connection - yield* delay(WEBSOCKET_CONNECTION_DELAY + 1000) - - const isWebsocketConnected = yield* select(initSelectors.isWebsocketConnected) - const socketIOData = yield* select(initSelectors.lastKnownSocketIOData) - - console.log('WEBSOCKET', 'Entered restore connection saga', isWebsocketConnected, socketIOData) - - if (!isWebsocketConnected && socketIOData.dataPort !== 0) { - console.log('WEBSOCKET', 'Restoring connection with data port: ', socketIOData.dataPort) - yield* put(initActions.startWebsocketConnection(socketIOData)) - } -} diff --git a/packages/mobile/src/store/root.saga.ts b/packages/mobile/src/store/root.saga.ts index 041aedf481..9b2fffff52 100644 --- a/packages/mobile/src/store/root.saga.ts +++ b/packages/mobile/src/store/root.saga.ts @@ -1,4 +1,4 @@ -import { all, takeEvery, fork } from 'typed-redux-saga' +import { all, takeEvery } from 'typed-redux-saga' import { nativeServicesMasterSaga } from './nativeServices/nativeServices.master.saga' import { navigationMasterSaga } from './navigation/navigation.master.saga' import { initMasterSaga } from './init/init.master.saga' @@ -6,7 +6,6 @@ import { initActions } from './init/init.slice' import { setupCryptoSaga } from './init/setupCrypto/setupCrypto.saga' import { publicChannels } from '@quiet/state-manager' import { showNotificationSaga } from './nativeServices/showNotification/showNotification.saga' -import { restoreConnectionSaga } from './init/startConnection/restoreConnection/restoreConnection.saga' export function* rootSaga(): Generator { yield all([ @@ -14,7 +13,6 @@ export function* rootSaga(): Generator { takeEvery(initActions.setStoreReady.type, initMasterSaga), takeEvery(initActions.setStoreReady.type, navigationMasterSaga), takeEvery(initActions.setStoreReady.type, nativeServicesMasterSaga), - // fork(restoreConnectionSaga), // Below line is reponsible for displaying notifications about messages from channels other than currently viewing one takeEvery(publicChannels.actions.markUnreadChannel.type, showNotificationSaga), ]) From de8fb2531d86d9477a7b9f3819a6552799ab21c9 Mon Sep 17 00:00:00 2001 From: siepra Date: Tue, 23 Apr 2024 15:38:43 +0200 Subject: [PATCH 11/12] fix: move app_ready event --- packages/mobile/src/App.tsx | 1 - .../init/startConnection/startConnection.saga.ts | 12 +++++++++++- .../store/navigation/redirection/redirection.saga.ts | 8 ++++++-- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/packages/mobile/src/App.tsx b/packages/mobile/src/App.tsx index 9944888ea9..4a2666ddfd 100644 --- a/packages/mobile/src/App.tsx +++ b/packages/mobile/src/App.tsx @@ -77,7 +77,6 @@ function App(): JSX.Element { useEffect(() => { console.log('LAUNCHED APPLICATION: ', (Math.random() + 1).toString(36).substring(7)) - NativeModules.CommunicationModule.handleIncomingEvents(APP_READY_CHANNEL, null, null) }, []) return ( diff --git a/packages/mobile/src/store/init/startConnection/startConnection.saga.ts b/packages/mobile/src/store/init/startConnection/startConnection.saga.ts index 6cf1f49ef3..8b19e75084 100644 --- a/packages/mobile/src/store/init/startConnection/startConnection.saga.ts +++ b/packages/mobile/src/store/init/startConnection/startConnection.saga.ts @@ -1,5 +1,5 @@ import { io } from 'socket.io-client' -import { select, put, call, cancel, fork, takeEvery, FixedTask } from 'typed-redux-saga' +import { select, put, call, cancel, fork, takeEvery, delay, FixedTask } from 'typed-redux-saga' import { PayloadAction } from '@reduxjs/toolkit' import { socket as stateManager, Socket } from '@quiet/state-manager' import { encodeSecret } from '@quiet/common' @@ -13,6 +13,16 @@ export function* startConnectionSaga( const isAlreadyConnected = yield* select(initSelectors.isWebsocketConnected) if (isAlreadyConnected) return + while (true) { + const isCryptoEngineInitialized = yield* select(initSelectors.isCryptoEngineInitialized) + console.log('WEBSOCKET', 'Waiting for crypto engine to initialize') + if (!isCryptoEngineInitialized) { + yield* delay(500) + } else { + break + } + } + const { dataPort, socketIOSecret } = action.payload console.log('WEBSOCKET', 'Entered start connection saga', dataPort) diff --git a/packages/mobile/src/store/navigation/redirection/redirection.saga.ts b/packages/mobile/src/store/navigation/redirection/redirection.saga.ts index 798ab18390..7c903adfc5 100644 --- a/packages/mobile/src/store/navigation/redirection/redirection.saga.ts +++ b/packages/mobile/src/store/navigation/redirection/redirection.saga.ts @@ -1,12 +1,16 @@ -import { put, take, select } from 'typed-redux-saga' +import { NativeModules } from 'react-native' +import { call, put, take, select } from 'typed-redux-saga' import { initSelectors } from '../../init/init.selectors' import { navigationSelectors } from '../navigation.selectors' import { navigationActions } from '../navigation.slice' import { ScreenNames } from '../../../const/ScreenNames.enum' -import { identity } from '@quiet/state-manager' +import { APP_READY_CHANNEL, identity } from '@quiet/state-manager' import { initActions } from '../../init/init.slice' export function* redirectionSaga(): Generator { + // Let the native modules know to init web socket connection + yield* call(NativeModules.CommunicationModule.handleIncomingEvents, APP_READY_CHANNEL, null, null) + // Do not redirect if user opened the app from url (quiet://) const deepLinking = yield* select(initSelectors.deepLinking) if (deepLinking) { From a70030d2df0ff740290195c9f1e579d2253f5a73 Mon Sep 17 00:00:00 2001 From: siepra Date: Tue, 23 Apr 2024 15:47:08 +0200 Subject: [PATCH 12/12] test: mock native modules --- .../store/navigation/redirection/redirection.saga.test.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/mobile/src/store/navigation/redirection/redirection.saga.test.ts b/packages/mobile/src/store/navigation/redirection/redirection.saga.test.ts index 6d8dab20d0..66ea6b5496 100644 --- a/packages/mobile/src/store/navigation/redirection/redirection.saga.test.ts +++ b/packages/mobile/src/store/navigation/redirection/redirection.saga.test.ts @@ -12,11 +12,18 @@ import { reducers } from '../../root.reducer' import { redirectionSaga } from './redirection.saga' import { initActions } from '../../init/init.slice' +import { NativeModules } from 'react-native' describe('redirectionSaga', () => { let store: Store let factory: FactoryGirl + beforeAll(() => { + NativeModules.CommunicationModule = { + handleIncomingEvents: jest.fn(), + } + }) + beforeEach(async () => { setupCrypto() store = (await prepareStore()).store