mattermost-mobile-upstream/app/actions/remote/refresh.ts
Mattermost Build bf24c1812b
Mm 68767 zero persistence (#9803) (#9957)
* 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>
2026-07-20 13:31:53 +03:00

92 lines
4.1 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {wipeServerDatabaseWithRetry, wipeServerFiles} from '@actions/local/ephemeral_mode/wipe';
import {cancelSessionNotification, terminateSession} from '@actions/local/session';
import DatabaseManager from '@database/manager';
import {getServerCredentials} from '@init/credentials';
import EphemeralModeManager from '@managers/ephemeral_mode_manager';
import WebsocketManager from '@managers/websocket_manager';
import {getCurrentChannelId, getCurrentTeamId, getPushVerificationStatus, prepareCommonSystemValues} from '@queries/servers/system';
import {getFullErrorMessage} from '@utils/errors';
import {logError, logWarning} from '@utils/log';
import {refetchCurrentUser} from './user';
export const applyPersistenceModeChange = async (serverUrl: string): Promise<{error?: unknown}> => {
let resumeServerTracking = true;
try {
// untrack server during the transition
EphemeralModeManager.removeServer(serverUrl);
const credentials = await getServerCredentials(serverUrl);
const {database} = DatabaseManager.getServerDatabaseAndOperator(serverUrl);
// Snapshot SYSTEM values that the WS reconnect won't restore on its own so the post-wipe
// UI doesn't show a regressed state (push-proxy red alert, team-select screen flash, etc).
const previousPushVerificationStatus = await getPushVerificationStatus(database);
const previousCurrentTeamId = await getCurrentTeamId(database);
const previousCurrentChannelId = await getCurrentChannelId(database);
try {
await cancelSessionNotification(serverUrl);
} catch {
logWarning('applyPersistenceModeChange: cancelSessionNotification failed (non-critical)');
}
await WebsocketManager.invalidateClient(serverUrl);
const {success} = await wipeServerDatabaseWithRetry(serverUrl);
if (!success) {
throw new Error('Failed to wipe server database after changing persistence mode');
}
wipeServerFiles(serverUrl);
const {error: refetchError} = await refetchCurrentUser(serverUrl, undefined);
if (refetchError) {
throw new Error(`applyPersistenceModeChange: failed to seed current user: ${getFullErrorMessage(refetchError)}`);
}
const {operator} = DatabaseManager.getServerDatabaseAndOperator(serverUrl);
const systemModels = await prepareCommonSystemValues(operator, {
currentPushVerificationStatus: previousPushVerificationStatus,
currentTeamId: previousCurrentTeamId,
currentChannelId: previousCurrentChannelId,
});
if (systemModels?.length) {
await operator.batchRecords(systemModels, 'applyPersistenceModeChange');
}
if (credentials?.token) {
await WebsocketManager.createClient(serverUrl, credentials.token, credentials.preauthSecret);
await WebsocketManager.initializeClient(serverUrl);
}
// Touch lastActiveAt so withServerDatabase re-reads the newly-created database instance.
// Only needed (and safe) for the active server
const activeServerUrl = await DatabaseManager.getActiveServerUrl();
if (activeServerUrl === serverUrl) {
await DatabaseManager.setActiveServerDatabase(serverUrl);
}
return {};
} catch (error) {
logError('applyPersistenceModeChange', serverUrl, getFullErrorMessage(error));
const {error: logoutError} = await terminateSession(serverUrl, false);
if (logoutError) {
logError('applyPersistenceModeChange: terminateSession failed', serverUrl, getFullErrorMessage(logoutError));
} else {
// do not resume server tracking in case of session termination
resumeServerTracking = false;
}
return {error};
} finally {
if (resumeServerTracking) {
// restart tracking; file cache was already wiped during the transition
await EphemeralModeManager.addServer(serverUrl, {cleanFileCache: false});
}
}
};