π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
contacts: normalize legacy info numeric fields
Neeraj Gupta5bef417736bb
diff --git a/web/packages/contacts/index.test.ts b/web/packages/contacts/index.test.tsindex b7ec0b0a98..01a67bd0fb 100644--- a/web/packages/contacts/index.test.ts+++ b/web/packages/contacts/index.test.ts@@ -72,6 +72,7 @@ beforeEach(() => {interface SetupOptions {diff?: object[];+ legacyInfo?: object;getProfilePictureError?: Error;getProfilePictureBytes?: Uint8Array;}@@ -122,11 +123,27 @@ const setupContactsModule = async (options: SetupOptions = {}) => {options.getProfilePictureError ?? new Error("boom"),);});+ const legacy_get_info = vi.fn(() =>+ Promise.resolve(+ options.legacyInfo ?? {+ contacts: [],+ recoverSessions: [],+ othersEmergencyContact: [],+ othersRecoverySession: [],+ },+ ),+ );vi.doMock("ente-base/kv", () => ({ getKV, getKVN, setKV }));vi.doMock("ente-base/token", () => ({ savedAuthToken }));vi.doMock("ente-base/origins", () => ({ apiOrigin }));vi.doMock("ente-base/log", () => ({ default: { info, warn, error } }));+ vi.doMock("ente-accounts-rs/services/session-storage", () => ({+ masterKeyFromSession: vi.fn(() => "MASTER_KEY"),+ }));+ vi.doMock("ente-accounts-rs/services/user", () => ({+ ensureLocalUser: vi.fn(() => ({ id: 101 })),+ }));vi.doMock("ente-base/app", () => ({appName: "photos",clientPackageName: "io.ente.photos.web",@@ -139,12 +156,20 @@ const setupContactsModule = async (options: SetupOptions = {}) => {current_wrapped_root_key,get_diff,get_profile_picture,+ legacy_get_info,})),}));const contacts = await import("./index");- return { contacts, setKV, get_diff, get_profile_picture, info };+ return {+ contacts,+ setKV,+ get_diff,+ get_profile_picture,+ legacy_get_info,+ info,+ };};describe("ensureContactsReady", () => {@@ -255,3 +280,51 @@ describe("retry after warm-up failure", () => {expect(get_diff).toHaveBeenCalledTimes(3);});});++describe("legacyGetInfo", () => {+ test("normalizes bigint legacy numeric fields at the API boundary", async () => {+ const { contacts } = await setupContactsModule({+ legacyInfo: {+ contacts: [+ {+ user: { id: 101n, email: "[email protected]" },+ emergencyContact: {+ id: 202n,+ email: "[email protected]",+ },+ state: "ACCEPTED",+ recoveryNoticeInDays: 14n,+ },+ ],+ recoverSessions: [+ {+ id: "session_1",+ user: { id: 101n, email: "[email protected]" },+ emergencyContact: {+ id: 202n,+ email: "[email protected]",+ },+ status: "WAITING",+ waitTill: 3_600_000_000n,+ createdAt: 1_700_000_000_000_000n,+ },+ ],+ othersEmergencyContact: [],+ othersRecoverySession: [],+ },+ });++ const info = await contacts.legacyGetInfo();++ expect(info.contacts[0]?.user.id).toBe(101);+ expect(info.contacts[0]?.emergencyContact.id).toBe(202);+ expect(info.contacts[0]?.recoveryNoticeInDays).toBe(14);+ expect(typeof info.contacts[0]?.user.id).toBe("number");+ expect(typeof info.contacts[0]?.emergencyContact.id).toBe("number");+ expect(typeof info.contacts[0]?.recoveryNoticeInDays).toBe("number");+ expect(info.recoverSessions[0]?.waitTill).toBe(3_600_000_000);+ expect(info.recoverSessions[0]?.createdAt).toBe(1_700_000_000_000_000);+ expect(typeof info.recoverSessions[0]?.waitTill).toBe("number");+ expect(typeof info.recoverSessions[0]?.createdAt).toBe("number");+ });+});diff --git a/web/packages/contacts/index.ts b/web/packages/contacts/index.tsindex 620e7dbfa9..903421e89a 100644--- a/web/packages/contacts/index.ts+++ b/web/packages/contacts/index.ts@@ -29,6 +29,7 @@ import type {LegacyContactState,LegacyInfo,LegacyRecoveryBundle,+ LegacyRecoveryStatus,ResolvedContactAvatar,ResolvedContactDisplay,WrappedRootContactKey,@@ -65,6 +66,34 @@ interface RemoteContactRecord {updatedAt: number | bigint;}+interface RemoteLegacyUser {+ id: number | bigint;+ email: string;+}++interface RemoteLegacyContactRecord {+ user: RemoteLegacyUser;+ emergencyContact: RemoteLegacyUser;+ state: LegacyContactState;+ recoveryNoticeInDays: number | bigint;+}++interface RemoteLegacyRecoverySession {+ id: string;+ user: RemoteLegacyUser;+ emergencyContact: RemoteLegacyUser;+ status: LegacyRecoveryStatus;+ waitTill: number | bigint;+ createdAt: number | bigint;+}++interface RemoteLegacyInfo {+ contacts: RemoteLegacyContactRecord[];+ recoverSessions: RemoteLegacyRecoverySession[];+ othersEmergencyContact: RemoteLegacyContactRecord[];+ othersRecoverySession: RemoteLegacyRecoverySession[];+}+interface ContactsReadyInput {userID: number;masterKeyB64: string;@@ -521,9 +550,45 @@ const ensureCurrentLegacyKeyAttributes = () => {return keyAttributes as unknown as Record<string, unknown>;};+const normalizeLegacyUser = (user: RemoteLegacyUser) => ({+ id: Number(user.id),+ email: user.email,+});++const normalizeLegacyContactRecord = (record: RemoteLegacyContactRecord) => ({+ user: normalizeLegacyUser(record.user),+ emergencyContact: normalizeLegacyUser(record.emergencyContact),+ state: record.state,+ recoveryNoticeInDays: Number(record.recoveryNoticeInDays),+});++const normalizeLegacyRecoverySession = (+ session: RemoteLegacyRecoverySession,+) => ({+ id: session.id,+ user: normalizeLegacyUser(session.user),+ emergencyContact: normalizeLegacyUser(session.emergencyContact),+ status: session.status,+ waitTill: Number(session.waitTill),+ createdAt: Number(session.createdAt),+});++const normalizeLegacyInfo = (info: RemoteLegacyInfo): LegacyInfo => ({+ contacts: info.contacts.map(normalizeLegacyContactRecord),+ recoverSessions: info.recoverSessions.map(normalizeLegacyRecoverySession),+ othersEmergencyContact: info.othersEmergencyContact.map(+ normalizeLegacyContactRecord,+ ),+ othersRecoverySession: info.othersRecoverySession.map(+ normalizeLegacyRecoverySession,+ ),+});+export const legacyGetInfo = async (): Promise<LegacyInfo> => {const ctx = await ensureCurrentLegacyCtx();- return (await ctx.legacy_get_info()) as LegacyInfo;+ return normalizeLegacyInfo(+ (await ctx.legacy_get_info()) as RemoteLegacyInfo,+ );};export const legacyPublicKey = async (email: string) => {