mattermost-mobile-upstream/app/actions/websocket/system.ts
Guillermo Vayá 62c694adeb feat(drafts): Phase 5 — WebSocket + lifecycle triggers make sync live
Wires the DraftSyncManager's reconcile/WS entry points (previously defined
but never called in production) into the app's WebSocket and lifecycle flow.
Design choice: WS draft events are treated as CHANGE SIGNALS that trigger an
authoritative GET reconcile of the current team, not applied directly — this
reuses Phase 4's matrix/scope/absence/observation-fence, and the server's
silent empty-upsert delete (no event) forces GET-reconciliation regardless.
The current-team GET also returns the user's DM/GM drafts, so it converges
them too; other teams' channel drafts converge on team switch.

Triggers:
- draft_created / draft_deleted WS events -> enqueue -> reconcile current team
  (constants + app/actions/websocket/draft.ts handler + event dispatch cases).
- Reconnect/first-connect: requestReconcile in doReconnect after entry batches.
- Team switch: requestReconcile in handleTeamChange (before the tablet early
  return, so both paths fire).
- Foreground: WebsocketManager forwards app-active to DraftSyncManager.onForeground
  (covers a socket that stayed open, so no reconnect fired). No second AppState
  listener is added.
- Capability change: handleCapabilityChange after config (AllowSyncedDrafts) and
  after sync_drafts preference create/update/delete batches.

Also adds a central empty-team guard in requestReconcile (the GET is per-team).

Deferred (noted for a follow-up): Global Drafts stale-on-open reconcile (touches
a screen, needs on-device verification); direct-apply of buffered WS events (the
buffer is retained, reserved for that future optimization).

tsc + eslint clean; new/updated unit tests across manager, ws handler, event
dispatch, reconnect, team switch, config/preference capability. All affected
suites green.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-15 16:50:19 +02:00

73 lines
3.3 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {updateDmGmDisplayName} from '@actions/local/channel';
import {reconcilePersistenceFlag} from '@actions/local/ephemeral_mode/wipe';
import {storeConfig} from '@actions/local/systems';
import {fetchCategories} from '@actions/remote/category';
import {applyPersistenceModeChange} from '@actions/remote/refresh';
import {SYSTEM_IDENTIFIERS} from '@constants/database';
import DatabaseManager from '@database/manager';
import DraftSyncManager from '@managers/draft_sync_manager';
import {getConfig, getCurrentTeamId, getLicense} from '@queries/servers/system';
import EphemeralStore from '@store/ephemeral_store';
import {getFullErrorMessage} from '@utils/errors';
import {logError} from '@utils/log';
export async function handleLicenseChangedEvent(serverUrl: string, msg: WebSocketMessage): Promise<void> {
try {
const {database, operator} = DatabaseManager.getServerDatabaseAndOperator(serverUrl);
const license = msg.data.license;
const systems: IdValue[] = [{id: SYSTEM_IDENTIFIERS.LICENSE, value: JSON.stringify(license)}];
const prevLicense = await getLicense(database);
await operator.handleSystem({systems, prepareRecordsOnly: false});
if (license?.LockTeammateNameDisplay && (prevLicense?.LockTeammateNameDisplay !== license.LockTeammateNameDisplay)) {
updateDmGmDisplayName(serverUrl);
}
} catch {
// do nothing
}
}
export async function handleConfigChangedEvent(serverUrl: string, msg: WebSocketMessage): Promise<void> {
try {
const {database} = DatabaseManager.getServerDatabaseAndOperator(serverUrl);
const config = msg.data.config;
const prevConfig = await getConfig(database);
await storeConfig(serverUrl, config);
if (config?.LockTeammateNameDisplay && (prevConfig?.LockTeammateNameDisplay !== config.LockTeammateNameDisplay)) {
updateDmGmDisplayName(serverUrl);
}
// Re-evaluate draft-sync capability: the AllowSyncedDrafts server config may have just changed
// (a persistence-mode change below re-initializes the manager, which re-reads capability too).
await DraftSyncManager.handleCapabilityChange(serverUrl);
const prevManagedSetting = prevConfig?.EnableManagedChannelCategories;
const newManagedSetting = config?.EnableManagedChannelCategories;
if (newManagedSetting !== prevManagedSetting) {
EphemeralStore.clearManagedCategoryPropertyIds(serverUrl);
const currentTeamId = await getCurrentTeamId(database);
if (currentTeamId) {
await fetchCategories(serverUrl, currentTeamId, true);
}
}
// Run last: a flag transition can wipe and recreate the server DB, invalidating
// the `database` reference captured above.
const needsModeChange = await reconcilePersistenceFlag(serverUrl, config);
if (needsModeChange) {
const {error: modeChangeError} = await applyPersistenceModeChange(serverUrl);
if (modeChangeError) {
logError('handleConfigChangedEvent', getFullErrorMessage(modeChangeError));
}
}
} catch {
// do nothing
}
}