Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(mobile): 固定推送状态的环境命名空间

DavidShen7476e391cbe1
diff --git a/apps/mobile/src/__tests__/pushNotificationsRealm.test.ts b/apps/mobile/src/__tests__/pushNotificationsRealm.test.ts
index 395bf9d28c..98e4779f11 100644
--- a/apps/mobile/src/__tests__/pushNotificationsRealm.test.ts
+++ b/apps/mobile/src/__tests__/pushNotificationsRealm.test.ts
@@ -68,6 +68,8 @@ const REGISTERED_KEY = 'cindy.push.registered';
const PENDING_KEY = 'cindy.push.pendingUnregister';
const DEV_REGISTERED_KEY = `${REGISTERED_KEY}.dev`;
const RELEASE_REGISTERED_KEY = `${REGISTERED_KEY}.release`;
+const DEV_PENDING_KEY = `${PENDING_KEY}.dev`;
+const RELEASE_PENDING_KEY = `${PENDING_KEY}.release`;
function setStoredRealms(key: string, realms: Array<'cn' | 'global'>): void {
store.set(key, JSON.stringify({ version: 1, realms }));
@@ -162,6 +164,35 @@ describe('push notification realm routing', () => {
expect(apiFetch).toHaveBeenCalledTimes(2);
});
+ it('注册期间切换 CindyDev 环境仍把补偿状态留在原 namespace', async () => {
+ envState.buildRegion = 'dev';
+ let releaseRead: (() => void) | undefined;
+ const readGate = new Promise<void>((resolve) => {
+ releaseRead = resolve;
+ });
+ mocks.asyncGetItem.mockImplementationOnce(async (key: string) => {
+ expect(key).toBe(DEV_REGISTERED_KEY);
+ await readGate;
+ return null;
+ });
+ const apiFetch = vi.fn();
+
+ const registration = syncPushRegistration({ enabled: true, apiFetch });
+ await vi.waitFor(() => {
+ expect(mocks.asyncGetItem).toHaveBeenCalledWith(DEV_REGISTERED_KEY);
+ });
+ await unregisterPushTokenBestEffort(null, 'cn');
+ devEnvironmentState.active = 'release';
+ releaseRead?.();
+
+ await expect(registration).resolves.toBe('skipped');
+ expect(apiFetch).not.toHaveBeenCalled();
+ expect(readStoredRealms(DEV_REGISTERED_KEY)).toEqual(['cn']);
+ expect(readStoredRealms(DEV_PENDING_KEY)).toEqual(['cn']);
+ expect(store.has(RELEASE_REGISTERED_KEY)).toBe(false);
+ expect(store.has(RELEASE_PENDING_KEY)).toBe(false);
+ });
+
it('Release 环境不读取旧 CindyDev 的无后缀状态', async () => {
envState.buildRegion = 'dev';
devEnvironmentState.active = 'release';
diff --git a/apps/mobile/src/notifications/pushNotifications.ts b/apps/mobile/src/notifications/pushNotifications.ts
index 10374d78ff..3ec6df2164 100644
--- a/apps/mobile/src/notifications/pushNotifications.ts
+++ b/apps/mobile/src/notifications/pushNotifications.ts
@@ -9,7 +9,10 @@ import {
getMobileEndpointForRealm,
loadMobileEndpointsForRealm,
} from '@/config/env';
-import { getDevServerEnvironment } from '@/config/devServerEnvironment';
+import {
+ getDevServerEnvironment,
+ type DevServerEnvironment,
+} from '@/config/devServerEnvironment';
import Notifications from './nativeNotifications';
import { buildPushTokenRegistrationBody } from './pushRegistrationModel';
@@ -43,14 +46,28 @@ interface PushRealmState {
realms: ClientEndpointRegion[];
}
-function pushStateStorageKeys(baseKey: string): {
+interface PushStateScope {
+ environment: DevServerEnvironment;
+ legacyRealm: ClientEndpointRegion;
+}
+
+/** 一次 mutation 内冻结存储 namespace,不能在异步读写之间重新取当前环境。 */
+function capturePushStateScope(
+ legacyRealm: ClientEndpointRegion = BUILD_AUTH_REGION,
+): PushStateScope {
+ return { environment: getDevServerEnvironment(), legacyRealm };
+}
+
+function pushStateStorageKeys(
+ baseKey: string,
+ environment: DevServerEnvironment,
+): {
primary: string;
migrationFallback: string | null;
} {
if (AUTH_REGION !== 'dev') {
return { primary: baseKey, migrationFallback: null };
}
- const environment = getDevServerEnvironment();
return {
primary: `${baseKey}.${environment}`,
// 旧 CindyDev 只连接 Dev;无环境后缀的存量状态只能归到 Dev,绝不能
@@ -86,10 +103,10 @@ function isClientEndpointRegion(value: unknown): value is ClientEndpointRegion {
*/
async function readPushRealms(
key: string,
- legacyRealm: ClientEndpointRegion = BUILD_AUTH_REGION,
+ scope: PushStateScope,
): Promise<Set<ClientEndpointRegion>> {
try {
- const storageKeys = pushStateStorageKeys(key);
+ const storageKeys = pushStateStorageKeys(key, scope.environment);
const primaryRaw = await AsyncStorage.getItem(storageKeys.primary);
const raw =
primaryRaw ??
@@ -97,7 +114,7 @@ async function readPushRealms(
? await AsyncStorage.getItem(storageKeys.migrationFallback)
: null);
if (!raw || raw === '0') return new Set();
- if (raw === '1') return new Set([legacyRealm]);
+ if (raw === '1') return new Set([scope.legacyRealm]);
const parsed: unknown = JSON.parse(raw);
if (
typeof parsed !== 'object' ||
@@ -118,8 +135,9 @@ async function readPushRealms(
async function writePushRealms(
key: string,
realms: ReadonlySet<ClientEndpointRegion>,
+ scope: PushStateScope,
): Promise<void> {
- const storageKeys = pushStateStorageKeys(key);
+ const storageKeys = pushStateStorageKeys(key, scope.environment);
if (realms.size === 0) {
await AsyncStorage.removeItem(storageKeys.primary);
if (storageKeys.migrationFallback) {
@@ -144,21 +162,21 @@ async function writePushRealms(
async function addPushRealm(
key: string,
realm: ClientEndpointRegion,
- legacyRealm: ClientEndpointRegion = realm,
+ scope: PushStateScope,
): Promise<void> {
- const realms = await readPushRealms(key, legacyRealm);
+ const realms = await readPushRealms(key, scope);
realms.add(realm);
- await writePushRealms(key, realms);
+ await writePushRealms(key, realms, scope);
}
async function removePushRealm(
key: string,
realm: ClientEndpointRegion,
- legacyRealm: ClientEndpointRegion = realm,
+ scope: PushStateScope,
): Promise<void> {
- const realms = await readPushRealms(key, legacyRealm);
+ const realms = await readPushRealms(key, scope);
realms.delete(realm);
- await writePushRealms(key, realms);
+ await writePushRealms(key, realms, scope);
}
function normalizeDeviceToken(value: unknown): string | null {
@@ -283,10 +301,14 @@ async function syncPushRegistrationInternal(
if (!isPushSupported()) return 'unsupported';
if (lifecycleGeneration !== pushLifecycleGeneration) return 'skipped';
const realm = getActiveMobileSessionRealm();
+ const stateScope = capturePushStateScope(realm);
const baseUrl = deviceLinkBaseForRealm(realm);
if (!opts.enabled) {
- const registeredRealms = await readPushRealms(PUSH_REGISTERED_KEY, realm);
+ const registeredRealms = await readPushRealms(
+ PUSH_REGISTERED_KEY,
+ stateScope,
+ );
if (lifecycleGeneration !== pushLifecycleGeneration) return 'skipped';
if (!registeredRealms.has(realm)) return 'skipped';
const token = await getNativeDeviceTokenBestEffort();
@@ -298,13 +320,13 @@ async function syncPushRegistrationInternal(
...(token ? { body: pushDeleteBody(token) } : {}),
});
} catch (error) {
- await addPushRealm(PUSH_PENDING_UNREGISTER_KEY, realm).catch(
+ await addPushRealm(PUSH_PENDING_UNREGISTER_KEY, realm, stateScope).catch(
() => undefined,
);
throw error;
}
- await removePushRealm(PUSH_REGISTERED_KEY, realm);
- await removePushRealm(PUSH_PENDING_UNREGISTER_KEY, realm);
+ await removePushRealm(PUSH_REGISTERED_KEY, realm, stateScope);
+ await removePushRealm(PUSH_PENDING_UNREGISTER_KEY, realm, stateScope);
return 'unregistered';
}
@@ -327,9 +349,9 @@ async function syncPushRegistrationInternal(
// PUT 响应丢失时无法判断服务端是否提交,发出前即标记“可能已注册”;
// 后续只能在该区域持有有效会话时做幂等 DELETE。
- await addPushRealm(PUSH_REGISTERED_KEY, realm);
+ await addPushRealm(PUSH_REGISTERED_KEY, realm, stateScope);
if (lifecycleGeneration !== pushLifecycleGeneration) {
- await addPushRealm(PUSH_PENDING_UNREGISTER_KEY, realm);
+ await addPushRealm(PUSH_PENDING_UNREGISTER_KEY, realm, stateScope);
return 'skipped';
}
try {
@@ -340,17 +362,17 @@ async function syncPushRegistrationInternal(
});
} catch (error) {
if (lifecycleGeneration !== pushLifecycleGeneration) {
- await addPushRealm(PUSH_PENDING_UNREGISTER_KEY, realm).catch(
+ await addPushRealm(PUSH_PENDING_UNREGISTER_KEY, realm, stateScope).catch(
() => undefined,
);
}
throw error;
}
if (lifecycleGeneration !== pushLifecycleGeneration) {
- await addPushRealm(PUSH_PENDING_UNREGISTER_KEY, realm);
+ await addPushRealm(PUSH_PENDING_UNREGISTER_KEY, realm, stateScope);
return 'skipped';
}
- await removePushRealm(PUSH_PENDING_UNREGISTER_KEY, realm);
+ await removePushRealm(PUSH_PENDING_UNREGISTER_KEY, realm, stateScope);
return 'registered';
}
@@ -373,20 +395,24 @@ async function unregisterPushTokenBestEffortInternal(
accessToken: string | null,
): Promise<void> {
if (!isPushSupported()) return;
+ const stateScope = capturePushStateScope(realm);
try {
- const registeredRealms = await readPushRealms(PUSH_REGISTERED_KEY, realm);
+ const registeredRealms = await readPushRealms(
+ PUSH_REGISTERED_KEY,
+ stateScope,
+ );
if (!registeredRealms.has(realm)) return;
- await addPushRealm(PUSH_PENDING_UNREGISTER_KEY, realm);
+ await addPushRealm(PUSH_PENDING_UNREGISTER_KEY, realm, stateScope);
if (!accessToken) return;
await deletePushTokenWithAccessToken(realm, accessToken);
- await removePushRealm(PUSH_REGISTERED_KEY, realm);
- await removePushRealm(PUSH_PENDING_UNREGISTER_KEY, realm);
+ await removePushRealm(PUSH_REGISTERED_KEY, realm, stateScope);
+ await removePushRealm(PUSH_PENDING_UNREGISTER_KEY, realm, stateScope);
} catch {
// 离线/超时/旧 token 失效不能阻断退出或切换。标记只在以后登录同一区域时
// 重试,绝不引入服务端互信或客户端跨区 token 发送。
- await addPushRealm(PUSH_PENDING_UNREGISTER_KEY, realm).catch(
+ await addPushRealm(PUSH_PENDING_UNREGISTER_KEY, realm, stateScope).catch(
() => undefined,
);
}
@@ -411,9 +437,10 @@ async function retryPendingUnregisterInternal(
): Promise<void> {
if (!isPushSupported()) return;
const realm = getActiveMobileSessionRealm();
+ const stateScope = capturePushStateScope(realm);
const pendingRealms = await readPushRealms(
PUSH_PENDING_UNREGISTER_KEY,
- realm,
+ stateScope,
);
if (!pendingRealms.has(realm)) return;
@@ -424,8 +451,8 @@ async function retryPendingUnregisterInternal(
method: 'DELETE',
...(token ? { body: pushDeleteBody(token) } : {}),
});
- await removePushRealm(PUSH_PENDING_UNREGISTER_KEY, realm);
- await removePushRealm(PUSH_REGISTERED_KEY, realm);
+ await removePushRealm(PUSH_PENDING_UNREGISTER_KEY, realm, stateScope);
+ await removePushRealm(PUSH_REGISTERED_KEY, realm, stateScope);
} catch {
// 仍失败:保留当前区域标记,下次同区登录继续补偿。
}