From 75cc39e9e7f249b26730f1668a49ba1c9fb9317f Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Fri, 30 Apr 2021 13:19:46 +1000 Subject: [PATCH] move appStartup stuff todo on start --- ts/components/session/ActionsPanel.tsx | 85 ++++++++++++++------------ ts/opengroup/opengroupV2/ApiUtil.ts | 4 +- 2 files changed, 48 insertions(+), 41 deletions(-) diff --git a/ts/components/session/ActionsPanel.tsx b/ts/components/session/ActionsPanel.tsx index 1cd601f16..8cb3fda24 100644 --- a/ts/components/session/ActionsPanel.tsx +++ b/ts/components/session/ActionsPanel.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useState } from 'react'; +import React, { Dispatch, useEffect, useState } from 'react'; import { SessionIconButton, SessionIconSize, SessionIconType } from './icon'; import { Avatar, AvatarSize } from '../Avatar'; import { darkTheme, lightTheme } from '../../state/ducks/SessionTheme'; @@ -31,7 +31,7 @@ import { showLeftPaneSection } from '../../state/ducks/section'; import { cleanUpOldDecryptedMedias } from '../../session/crypto/DecryptedAttachmentsManager'; import { OpenGroupManagerV2 } from '../../opengroup/opengroupV2/OpenGroupManagerV2'; -import { loadDefaultRoomsIfNeeded } from '../../opengroup/opengroupV2/ApiUtil'; +import { loadDefaultRooms } from '../../opengroup/opengroupV2/ApiUtil'; // tslint:disable-next-line: no-import-side-effect no-submodule-imports export enum SectionType { @@ -130,6 +130,49 @@ const showResetSessionIDDialogIfNeeded = async () => { const cleanUpMediasInterval = MINUTES * 30; +/** + * This function is called only once: on app startup with a logged in user + */ +const doAppStartUp = (dispatch: Dispatch) => { + if (window.lokiFeatureFlags.useOnionRequests || window.lokiFeatureFlags.useFileOnionRequests) { + // Initialize paths for onion requests + void OnionPaths.getInstance().buildNewOnionPaths(); + } + + // init the messageQueue. In the constructor, we had all not send messages + // this call does nothing except calling the constructor, which will continue sending message in the pipeline + void getMessageQueue().processAllPending(); + + const theme = window.Events.getThemeSetting(); + window.setTheme(theme); + + const newThemeObject = theme === 'dark' ? darkTheme : lightTheme; + dispatch(applyTheme(newThemeObject)); + + void showResetSessionIDDialogIfNeeded(); + // remove existing prekeys, sign prekeys and sessions + void clearSessionsAndPreKeys(); + // we consider people had the time to upgrade, so remove this id from the db + // it was used to display a dialog when we added the light mode auto-enabled + void removeItemById('hasSeenLightModeDialog'); + + // Do this only if we created a new Session ID, or if we already received the initial configuration message + + const syncConfiguration = async () => { + const didWeHandleAConfigurationMessageAlready = + (await getItemById(hasSyncedInitialConfigurationItem))?.value || false; + if (didWeHandleAConfigurationMessageAlready) { + await syncConfigurationIfNeeded(); + } + }; + void generateAttachmentKeyIfEmpty(); + // trigger a sync message if needed for our other devices + void OpenGroupManagerV2.getInstance().startPolling(); + void syncConfiguration(); + + void loadDefaultRooms(); +}; + /** * ActionsPanel is the far left banner (not the left pane). * The panel with buttons to switch between the message/contact/settings/theme views @@ -143,43 +186,7 @@ export const ActionsPanel = () => { // this maxi useEffect is called only once: when the component is mounted. // For the action panel, it means this is called only one per app start/with a user loggedin useEffect(() => { - if (window.lokiFeatureFlags.useOnionRequests || window.lokiFeatureFlags.useFileOnionRequests) { - // Initialize paths for onion requests - void OnionPaths.getInstance().buildNewOnionPaths(); - } - - // init the messageQueue. In the constructor, we had all not send messages - // this call does nothing except calling the constructor, which will continue sending message in the pipeline - void getMessageQueue().processAllPending(); - - const theme = window.Events.getThemeSetting(); - window.setTheme(theme); - - const newThemeObject = theme === 'dark' ? darkTheme : lightTheme; - dispatch(applyTheme(newThemeObject)); - - void showResetSessionIDDialogIfNeeded(); - // remove existing prekeys, sign prekeys and sessions - void clearSessionsAndPreKeys(); - // we consider people had the time to upgrade, so remove this id from the db - // it was used to display a dialog when we added the light mode auto-enabled - void removeItemById('hasSeenLightModeDialog'); - - // Do this only if we created a new Session ID, or if we already received the initial configuration message - - const syncConfiguration = async () => { - const didWeHandleAConfigurationMessageAlready = - (await getItemById(hasSyncedInitialConfigurationItem))?.value || false; - if (didWeHandleAConfigurationMessageAlready) { - await syncConfigurationIfNeeded(); - } - }; - void generateAttachmentKeyIfEmpty(); - // trigger a sync message if needed for our other devices - void OpenGroupManagerV2.getInstance().startPolling(); - void syncConfiguration(); - - void loadDefaultRoomsIfNeeded(); + void doAppStartUp(dispatch); }, []); // wait for cleanUpMediasInterval and then start cleaning up medias diff --git a/ts/opengroup/opengroupV2/ApiUtil.ts b/ts/opengroup/opengroupV2/ApiUtil.ts index 21a341b03..2576885c9 100644 --- a/ts/opengroup/opengroupV2/ApiUtil.ts +++ b/ts/opengroup/opengroupV2/ApiUtil.ts @@ -133,7 +133,7 @@ const loadDefaultRoomsSingle = () => }; }); } catch (e) { - window.log.warn('loadDefaultRoomsIfNeeded failed', e); + window.log.warn('loadDefaultRoomloadDefaultRoomssIfNeeded failed', e); } return []; } @@ -145,7 +145,7 @@ const loadDefaultRoomsSingle = () => * Load to the cache all the details of the room of the default opengroupv2 server * This call will only run once at a time. */ -export const loadDefaultRoomsIfNeeded = async () => { +export const loadDefaultRooms = async () => { // FIXME audric do the UI and refresh this list from time to time const allRooms: Array = await loadDefaultRoomsSingle(); if (allRooms !== undefined) {