mattermost-mobile-upstream/app/actions/remote/systems.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

117 lines
4.7 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {reconcilePersistenceFlag} from '@actions/local/ephemeral_mode/wipe';
import {storeConfigAndLicense, storeDataRetentionPolicies} from '@actions/local/systems';
import {applyPersistenceModeChange} from '@actions/remote/refresh';
import {forceLogoutIfNecessary} from '@actions/remote/session';
import DatabaseManager from '@database/manager';
import NetworkManager from '@managers/network_manager';
import {getCurrentUserId} from '@queries/servers/system';
import {getFullErrorMessage} from '@utils/errors';
import {logDebug} from '@utils/log';
export type ConfigAndLicenseRequest = {
config?: ClientConfig;
license?: ClientLicense;
error?: unknown;
}
export type DataRetentionPoliciesRequest = {
globalPolicy?: GlobalDataRetentionPolicy;
teamPolicies?: TeamDataRetentionPolicy[];
channelPolicies?: ChannelDataRetentionPolicy[];
error?: unknown;
}
export const fetchDataRetentionPolicy = async (serverUrl: string, fetchOnly = false, groupLabel?: RequestGroupLabel): Promise<DataRetentionPoliciesRequest> => {
const {data: globalPolicy, error: globalPolicyError} = await fetchGlobalDataRetentionPolicy(serverUrl, groupLabel);
const {data: teamPolicies, error: teamPoliciesError} = await fetchAllGranularDataRetentionPolicies(serverUrl, undefined, undefined, undefined, groupLabel);
const {data: channelPolicies, error: channelPoliciesError} = await fetchAllGranularDataRetentionPolicies(serverUrl, true);
const error = globalPolicyError || teamPoliciesError || channelPoliciesError;
if (error) {
return {error};
}
const data = {
globalPolicy,
teamPolicies: teamPolicies as TeamDataRetentionPolicy[],
channelPolicies: channelPolicies as ChannelDataRetentionPolicy[],
};
if (!fetchOnly) {
await storeDataRetentionPolicies(serverUrl, data);
}
return data;
};
export const fetchGlobalDataRetentionPolicy = async (serverUrl: string, groupLabel?: RequestGroupLabel): Promise<{data?: GlobalDataRetentionPolicy; error?: unknown}> => {
try {
const client = NetworkManager.getClient(serverUrl);
const data = await client.getGlobalDataRetentionPolicy(groupLabel);
return {data};
} catch (error) {
logDebug('error on fetchGlobalDataRetentionPolicy', getFullErrorMessage(error));
forceLogoutIfNecessary(serverUrl, error);
return {error};
}
};
export const fetchAllGranularDataRetentionPolicies = async (
serverUrl: string,
isChannel = false,
page = 0,
policies: Array<TeamDataRetentionPolicy | ChannelDataRetentionPolicy> = [],
groupLabel?: RequestGroupLabel,
): Promise<{data?: Array<TeamDataRetentionPolicy | ChannelDataRetentionPolicy>; error?: unknown}> => {
try {
const client = NetworkManager.getClient(serverUrl);
const {database} = DatabaseManager.getServerDatabaseAndOperator(serverUrl);
const currentUserId = await getCurrentUserId(database);
let data;
if (isChannel) {
data = await client.getChannelDataRetentionPolicies(currentUserId, page, undefined, groupLabel);
} else {
data = await client.getTeamDataRetentionPolicies(currentUserId, page, undefined, groupLabel);
}
policies.push(...data.policies);
if (policies.length < data.total_count) {
await fetchAllGranularDataRetentionPolicies(serverUrl, isChannel, page + 1, policies, groupLabel);
}
return {data: policies};
} catch (error) {
logDebug('error on fetchAllGranularDataRetentionPolicies', getFullErrorMessage(error));
return {error};
}
};
export const fetchConfigAndLicense = async (serverUrl: string, fetchOnly = false, groupLabel?: RequestGroupLabel): Promise<ConfigAndLicenseRequest> => {
try {
const client = NetworkManager.getClient(serverUrl);
const [config, license]: [ClientConfig, ClientLicense] = await Promise.all([
client.getClientConfigOld(groupLabel),
client.getClientLicenseOld(groupLabel),
]);
if (!fetchOnly) {
await storeConfigAndLicense(serverUrl, config, license);
const needsModeChange = await reconcilePersistenceFlag(serverUrl, config);
if (needsModeChange) {
const {error: modeChangeError} = await applyPersistenceModeChange(serverUrl);
if (modeChangeError) {
throw modeChangeError;
}
}
}
return {config, license};
} catch (error) {
logDebug('error on fetchConfigAndLicense', getFullErrorMessage(error));
forceLogoutIfNecessary(serverUrl, error);
return {error};
}
};