mirror of
https://github.com/mattermost/mattermost-mobile.git
synced 2026-11-05 18:38:54 +00:00
* implements auto cache cleanup when zero persistence mode is enabled (0 value)
* uses memory flag with sqlite instead of loki when running in zero persistence mode
* renames ephemeral mode manager and improves readability
* zero persistence refactors and restore values during transitions
* fixes ephemeral mode tests
* use initializeClient instead of just initialize to wait for doReconnect to finish
that way teams loading is awaited while WS reconnects during ZPM transitions
* adds inital sync as a mount hook to prevent undesired load error states
during app cold start in ZPM mode, we need a way to load team and channels
without showing loading error during the transition. The nre is initial sync
hook allows for it
* reset started sync subject on wipe
* removes extra log lines
* resets tracked server during ephemeral mode transitions
* improvements to apply persistence mode changes when something fails
* address coderabbit PR suggestions
* add batching time logging
* throws ephemeral mode change error as is
* make deleteServerDatabaseFiles private
* inline derive persistence flag function
* return current user id when refetching current user
* support zero persistence mode server for push notifications
* adds support for zero persistence mode when sharing to mattermost
* adds clarifying comment
* catch and log error deleting files
* reorder translations to correct position
* adds some new tests to improve coverage
* ios push notification native fix
* logout when fetchMe fails with 401 status code during reconnection
* properly surface errors
* return proper false value when checking for zero persistence flag
* change comments and properly await on some operations
* prevent not set persistence mode to report as an error
* include servers with no persistence flag set in share list
* disable button for accessibility
* improves when to store and remove signing key from global area
* review push notification reception running in zero persistence mode
* add server to ephemeral mode manager right after login
---------
(cherry picked from commit a5ff8a0d95)
Co-authored-by: Carlos Garcia <carlos.garcia@mattermost.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
70 lines
3.1 KiB
TypeScript
70 lines
3.1 KiB
TypeScript
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
import NetInfo from '@react-native-community/netinfo';
|
|
import {router} from 'expo-router';
|
|
import {DeviceEventEmitter} from 'react-native';
|
|
|
|
import {wipeServerDatabaseWithRetry} from '@actions/local/ephemeral_mode/wipe';
|
|
import {loginEntry} from '@actions/remote/entry';
|
|
import {fetchMe} from '@actions/remote/user';
|
|
import {Events, Launch} from '@constants';
|
|
import {HTTP_UNAUTHORIZED} from '@constants/network';
|
|
import DatabaseManager from '@database/manager';
|
|
import {getServerCredentials} from '@init/credentials';
|
|
import {determineRouteFromLaunchProps} from '@init/launch';
|
|
import {setCurrentUserId} from '@queries/servers/system';
|
|
import {getFullErrorMessage, isErrorWithStatusCode} from '@utils/errors';
|
|
import {logDebug, logError} from '@utils/log';
|
|
|
|
type Result = {error?: unknown};
|
|
|
|
export const restoreServerAfterDatabaseWipe = async (serverUrl: string): Promise<Result> => {
|
|
try {
|
|
const netInfo = await NetInfo.fetch();
|
|
if (!netInfo.isConnected) {
|
|
return {error: 'no_connection'};
|
|
}
|
|
|
|
const credentials = await getServerCredentials(serverUrl);
|
|
if (!credentials) {
|
|
return {error: 'no_credentials'};
|
|
}
|
|
|
|
// Restore the current user before opening the WS so doReconnect can resolve them.
|
|
const {user, error: fetchError} = await fetchMe(serverUrl);
|
|
if (!user) {
|
|
// the server database was just wiped, so forceLogoutIfNecessary has no currentUserId to key off of
|
|
if (isErrorWithStatusCode(fetchError) && fetchError.status_code === HTTP_UNAUTHORIZED) {
|
|
logDebug('restoreServerAfterDatabaseWipe: 401 on fetchMe', serverUrl, getFullErrorMessage(fetchError));
|
|
DeviceEventEmitter.emit(Events.SERVER_LOGOUT, {serverUrl, removeServer: false});
|
|
return {};
|
|
}
|
|
return {error: fetchError};
|
|
}
|
|
const {operator} = DatabaseManager.getServerDatabaseAndOperator(serverUrl);
|
|
const {error: setUserError} = await setCurrentUserId(operator, user.id);
|
|
if (setUserError) {
|
|
return {error: setUserError};
|
|
}
|
|
|
|
const {error: loginError} = await loginEntry({serverUrl});
|
|
if (loginError) {
|
|
// when there is a login error ensure to wipe all data to reinforce security
|
|
await wipeServerDatabaseWithRetry(serverUrl);
|
|
return {error: loginError};
|
|
}
|
|
|
|
// Update last_active_at so withServerDatabase picks up the new db instance after wipe.
|
|
await DatabaseManager.setActiveServerDatabase(serverUrl);
|
|
await DatabaseManager.updatePersistenceFlag(serverUrl, '');
|
|
|
|
const launchRoute = await determineRouteFromLaunchProps({launchType: Launch.Normal, serverUrl, coldStart: true});
|
|
router.replace({pathname: launchRoute.route, params: launchRoute.params});
|
|
return {};
|
|
} catch (error) {
|
|
logError('restoreServerAfterDatabaseWipe', serverUrl, getFullErrorMessage(error));
|
|
return {error};
|
|
}
|
|
};
|