Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Give web Contacts an explicit lifecycle

Manav Rathid39bca8dcbf1
diff --git a/web/apps/locker/src/components/locker/use-locker-data.ts b/web/apps/locker/src/components/locker/use-locker-data.ts
index 7fad7e4980..50300618b2 100644
--- a/web/apps/locker/src/components/locker/use-locker-data.ts
+++ b/web/apps/locker/src/components/locker/use-locker-data.ts
@@ -20,7 +20,7 @@ import { isNamedError } from "ente-base/error";
import { authenticatedRequestHeaders, isHTTP401Error } from "ente-base/http";
import log from "ente-base/log";
import { savedAuthToken } from "ente-base/token";
-import { ensureContactsReady } from "ente-contacts";
+import { initContacts, pullContacts } from "ente-contacts";
import { contactsGetDiff, contactsGetProfilePicture } from "ente-locker-wasm";
import { t } from "i18next";
import type { NextRouter } from "next/router";
@@ -94,12 +94,13 @@ export const useLockerData = ({
authToken,
masterKey,
);
- await ensureContactsReady(
+ await initContacts(
userID,
session,
contactsGetDiff,
contactsGetProfilePicture,
);
+ await pullContacts();
}, []);
const loadUserDetails = useCallback(async (): Promise<boolean> => {
diff --git a/web/apps/locker/src/services/logout.ts b/web/apps/locker/src/services/logout.ts
index d40da6387e..0fca2e437b 100644
--- a/web/apps/locker/src/services/logout.ts
+++ b/web/apps/locker/src/services/logout.ts
@@ -1,6 +1,10 @@
import { savedLocalUser } from "ente-accounts/services/accounts-db";
-import { accountLogout } from "ente-accounts/services/logout";
+import {
+ accountLogout,
+ logoutClearStateAgain,
+} from "ente-accounts/services/logout";
import log from "ente-base/log";
+import { logoutContacts } from "ente-contacts";
import { clearAuthenticatedSession } from "./authenticated-session";
import { clearLockerCache } from "./locker-cache";
import { clearLockerDB } from "./locker-db";
@@ -13,14 +17,20 @@ export const lockerLogout = async () => {
const userID = savedLocalUser()?.id;
+ // Session
+
try {
clearAuthenticatedSession();
} catch (error) {
ignoreError("Authenticated session", error);
}
+ // Remote logout and clear state
+
await accountLogout();
+ // Locker services
+
try {
if (userID !== undefined) {
await clearLockerDB(userID);
@@ -34,4 +44,14 @@ export const lockerLogout = async () => {
} catch (error) {
ignoreError("Locker in-memory cache", error);
}
+
+ try {
+ logoutContacts();
+ } catch (error) {
+ ignoreError("Contacts", error);
+ }
+
+ // Final sweep before the caller reloads
+
+ await logoutClearStateAgain();
};
diff --git a/web/apps/locker/tests/logout.test.ts b/web/apps/locker/tests/logout.test.ts
index ff43eedd59..a21060763a 100644
--- a/web/apps/locker/tests/logout.test.ts
+++ b/web/apps/locker/tests/logout.test.ts
@@ -1,3 +1,5 @@
+import { logoutClearStateAgain } from "ente-accounts/services/logout";
+import { logoutContacts } from "ente-contacts";
import { expect, test, vi } from "vitest";
import { openAuthenticatedSession } from "../src/services/authenticated-session";
import { lockerLogout } from "../src/services/logout";
@@ -18,6 +20,8 @@ const {
accountLogout: vi.fn<() => Promise<void>>(),
}));
+vi.mock("ente-contacts", () => ({ logoutContacts: vi.fn() }));
+
vi.mock("ente-base/app", () => ({
clientPackageName: "io.ente.locker.web",
desktopAppVersion: undefined,
@@ -44,7 +48,10 @@ vi.mock("ente-accounts/services/user", () => ({
vi.mock("ente-accounts/services/accounts-db", () => ({
savedLocalUser: () => ({ id: 1 }),
}));
-vi.mock("ente-accounts/services/logout", () => ({ accountLogout }));
+vi.mock("ente-accounts/services/logout", () => ({
+ accountLogout,
+ logoutClearStateAgain: vi.fn(),
+}));
vi.mock("ente-locker-wasm", () => ({
encryptBoxWithRecoveryKey,
generateKey,
@@ -72,4 +79,6 @@ test("logout rejects pending sessions while account cleanup is pending", async (
accountCleanup.resolve(undefined);
await loggingOut;
}
+ expect(logoutContacts).toHaveBeenCalledOnce();
+ expect(logoutClearStateAgain).toHaveBeenCalledOnce();
});
diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsx
index 46d1dfea08..17a1be86fa 100644
--- a/web/apps/photos/src/pages/gallery.tsx
+++ b/web/apps/photos/src/pages/gallery.tsx
@@ -89,7 +89,7 @@ import {
} from "ente-base/session-storage";
import { savedAuthToken } from "ente-base/token";
import type { Location } from "ente-base/types";
-import { ensureContactsReady } from "ente-contacts";
+import { initContacts, pullContacts } from "ente-contacts";
import { DownloadStatusNotifications } from "ente-gallery/components/DownloadStatusNotifications";
import { FullScreenDropZone } from "ente-gallery/components/FullScreenDropZone";
import type { UploadTypeSelectorIntent } from "ente-gallery/components/Upload";
@@ -522,17 +522,19 @@ const Page: React.FC = () => {
setIsFirstLoad(getAndClearIsFirstLogin());
const user = ensureLocalUser();
- void ensureContactsReady(
+ void initContacts(
user.id,
session,
contactsGetDiff,
contactsGetProfilePicture,
- ).catch((error: unknown) => {
- log.warn(
- "[gallery] Failed to warm contacts display cache",
- error,
- );
- });
+ )
+ .then(pullContacts)
+ .catch((error: unknown) => {
+ log.warn(
+ "[gallery] Failed to warm contacts display cache",
+ error,
+ );
+ });
const userDetails = await savedUserDetailsOrTriggerPull();
dispatch({
type: "mount",
diff --git a/web/apps/photos/src/services/logout.ts b/web/apps/photos/src/services/logout.ts
index 12ff34dd08..c630efe8df 100644
--- a/web/apps/photos/src/services/logout.ts
+++ b/web/apps/photos/src/services/logout.ts
@@ -4,6 +4,7 @@ import {
logoutClearStateAgain,
} from "ente-accounts/services/logout";
import log from "ente-base/log";
+import { logoutContacts } from "ente-contacts";
import { resetSaveGroups } from "ente-gallery/components/utils/save-groups";
import { logoutFileViewerDataSource } from "ente-gallery/components/viewer/data-source";
import { downloadManager } from "ente-gallery/services/download";
@@ -23,13 +24,15 @@ export const photosLogout = async () => {
const ignoreError = (label: string, e: unknown) =>
log.error(`Ignoring error during logout (${label})`, e);
+ // Session
+
try {
clearAuthenticatedSession();
} catch (e) {
ignoreError("Authenticated session", e);
}
- // Stop workers before clearing databases they may still access.
+ // Stop workers and schedulers before clearing persistent state.
try {
await terminateMLWorker();
} catch (e) {
@@ -45,8 +48,12 @@ export const photosLogout = async () => {
}
}
+ // Remote logout and clear state
+
await accountLogout();
+ // Photos services
+
log.info("logout (photos)");
try {
@@ -61,6 +68,12 @@ export const photosLogout = async () => {
ignoreError("Settings", e);
}
+ try {
+ logoutContacts();
+ } catch (e) {
+ ignoreError("Contacts", e);
+ }
+
try {
logoutUserDetails();
} catch (e) {
@@ -109,6 +122,8 @@ export const photosLogout = async () => {
ignoreError("File viewer", e);
}
+ // Desktop
+
if (electron) {
try {
await logoutAppLock();
@@ -129,6 +144,8 @@ export const photosLogout = async () => {
}
}
+ // Final sweep and reload
+
// Clear again after in-flight work has had a chance to finish.
await logoutClearStateAgain();
diff --git a/web/apps/photos/tests/logout.test.ts b/web/apps/photos/tests/logout.test.ts
index 677e06fe2f..c4d6bd5fa1 100644
--- a/web/apps/photos/tests/logout.test.ts
+++ b/web/apps/photos/tests/logout.test.ts
@@ -1,3 +1,4 @@
+import { logoutContacts } from "ente-contacts";
import { afterEach, expect, test, vi } from "vitest";
import { openAuthenticatedSession } from "../src/services/authenticated-session";
import { photosLogout } from "../src/services/logout";
@@ -18,6 +19,8 @@ const {
terminateMLWorker: vi.fn<() => Promise<void>>(),
}));
+vi.mock("ente-contacts", () => ({ logoutContacts: vi.fn() }));
+
vi.mock("ente-base/app", () => ({
clientPackageName: "io.ente.photos.web",
desktopAppVersion: undefined,
@@ -107,4 +110,5 @@ test("logout rejects pending sessions while worker shutdown is pending", async (
workerShutdown.resolve(undefined);
await loggingOut;
}
+ expect(logoutContacts).toHaveBeenCalledOnce();
});
diff --git a/web/packages/contacts/index.test.ts b/web/packages/contacts/index.test.ts
index fd96c624a7..f5bdd006db 100644
--- a/web/packages/contacts/index.test.ts
+++ b/web/packages/contacts/index.test.ts
@@ -27,7 +27,6 @@ const setupContactsModule = async (options: SetupOptions = {}) => {
return typeof value === "number" ? value : undefined;
});
- const savedAuthToken = vi.fn((): string | undefined => "auth-token-secret");
const apiOrigin = vi.fn(() => "https://api.example");
const info = vi.fn();
const warn = vi.fn();
@@ -78,7 +77,6 @@ const setupContactsModule = async (options: SetupOptions = {}) => {
);
});
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 },
@@ -86,27 +84,18 @@ const setupContactsModule = async (options: SetupOptions = {}) => {
}));
const contacts = await import("./index");
- return {
- contacts,
- setKV,
- savedAuthToken,
- getDiff,
- getProfilePicture,
- info,
- };
+ return { contacts, setKV, getDiff, getProfilePicture, info };
};
-describe("ensureContactsReady", () => {
- test("does not persist the auth token in contacts kv", async () => {
+describe("contacts synchronization", () => {
+ test("initializes locally before pulling and persisting contacts", async () => {
const { contacts, getDiff, getProfilePicture, setKV } =
await setupContactsModule();
- await contacts.ensureContactsReady(
- 101,
- session,
- getDiff,
- getProfilePicture,
- );
+ await contacts.initContacts(101, session, getDiff, getProfilePicture);
+ expect(getDiff).not.toHaveBeenCalled();
+ expect(contacts.contactsDisplaySnapshot().isHydrated).toBe(true);
+ await contacts.pullContacts();
const persisted = setKV.mock.calls
.map(([key, value]) => `${key}:${JSON.stringify(value)}`)
@@ -115,7 +104,6 @@ describe("ensureContactsReady", () => {
expect(persisted).toContain("contacts/");
expect(persisted).toContain("wrapped-root-key");
expect(persisted).toContain("Set");
- expect(persisted).not.toContain("auth-token-secret");
const resolved = contacts.resolveContactDisplay({ userID: 101 });
expect(resolved.profilePictureAttachmentID).toBe("ua_1");
@@ -125,12 +113,8 @@ describe("ensureContactsReady", () => {
const { contacts, getDiff, getProfilePicture, setKV } =
await setupContactsModule({ rootKeyResolved: false, diff: [] });
- await contacts.ensureContactsReady(
- 101,
- session,
- getDiff,
- getProfilePicture,
- );
+ await contacts.initContacts(101, session, getDiff, getProfilePicture);
+ await contacts.pullContacts();
const persisted = setKV.mock.calls
.map(([key, value]) => `${key}:${JSON.stringify(value)}`)
@@ -143,12 +127,8 @@ describe("ensureContactsReady", () => {
const { contacts, getDiff, getProfilePicture, setKV } =
await setupContactsModule({ rootKeyResolved: false });
- await contacts.ensureContactsReady(
- 101,
- session,
- getDiff,
- getProfilePicture,
- );
+ await contacts.initContacts(101, session, getDiff, getProfilePicture);
+ await contacts.pullContacts();
const persisted = setKV.mock.calls
.map(([key, value]) => `${key}:${JSON.stringify(value)}`)
@@ -165,12 +145,8 @@ describe("profile picture loading", () => {
getProfilePictureError: new Error("network failure"),
});
- await contacts.ensureContactsReady(
- 101,
- session,
- getDiff,
- getProfilePicture,
- );
+ await contacts.initContacts(101, session, getDiff, getProfilePicture);
+ await contacts.pullContacts();
await contacts.__testing.preloadResolvedContactAvatar({
userID: 101,
@@ -199,12 +175,8 @@ describe("profile picture loading", () => {
const { contacts, getDiff, getProfilePicture } =
await setupContactsModule({ getProfilePictureBytes: pngBytes });
- await contacts.ensureContactsReady(
- 101,
- session,
- getDiff,
- getProfilePicture,
- );
+ await contacts.initContacts(101, session, getDiff, getProfilePicture);
+ await contacts.pullContacts();
await contacts.__testing.preloadResolvedContactAvatar({ userID: 101 });
const blobArg = createObjectURL.mock.calls[0]?.[0] as Blob | undefined;
@@ -219,12 +191,13 @@ describe("profile picture loading", () => {
.mockReturnValue("blob:contact");
const { contacts, getDiff, getProfilePicture } =
await setupContactsModule();
- await contacts.ensureContactsReady(
+ await contacts.initContacts(
101,
session,
getDiff,
getProfilePicture,
);
+ await contacts.pullContacts();
const picture =
Promise.withResolvers<
@@ -249,12 +222,13 @@ describe("profile picture loading", () => {
],
});
}
- await contacts.ensureContactsReady(
+ await contacts.initContacts(
change === "account change" ? 202 : 101,
session,
getDiff,
getProfilePicture,
);
+ await contacts.pullContacts();
picture.resolve({
bytes: new Uint8Array([1, 2, 3]),
@@ -294,12 +268,8 @@ describe("retry after warm-up failure", () => {
})
.mockResolvedValueOnce({ records: [] });
- const ready = contacts.ensureContactsReady(
- 101,
- session,
- getDiff,
- getProfilePicture,
- );
+ await contacts.initContacts(101, session, getDiff, getProfilePicture);
+ const ready = contacts.pullContacts();
await vi.advanceTimersByTimeAsync(10_001);
await expect(ready).resolves.toBeUndefined();
@@ -313,14 +283,8 @@ describe("retry after warm-up failure", () => {
getDiff.mockReset();
getDiff.mockRejectedValue(new Error("down"));
- const ready = expect(
- contacts.ensureContactsReady(
- 101,
- session,
- getDiff,
- getProfilePicture,
- ),
- ).rejects.toThrow("down");
+ await contacts.initContacts(101, session, getDiff, getProfilePicture);
+ const ready = expect(contacts.pullContacts()).rejects.toThrow("down");
await vi.advanceTimersByTimeAsync(10_001);
await vi.advanceTimersByTimeAsync(30_001);
@@ -334,44 +298,54 @@ describe("retry after warm-up failure", () => {
test("a stale retry does not update a newer session", async () => {
vi.useFakeTimers();
- const { contacts, savedAuthToken, getDiff, getProfilePicture } =
+ const { contacts, getDiff, getProfilePicture } =
await setupContactsModule();
- savedAuthToken
- .mockReturnValueOnce("old-token")
- .mockReturnValueOnce(undefined)
- .mockReturnValue("new-token");
getDiff.mockReset();
getDiff
.mockRejectedValueOnce(new Error("transient"))
.mockResolvedValue({ records: [] });
- const staleReady = contacts.ensureContactsReady(
- 101,
- session,
- getDiff,
- getProfilePicture,
- );
+ await contacts.initContacts(101, session, getDiff, getProfilePicture);
+ const stalePull = contacts.pullContacts();
await vi.advanceTimersByTimeAsync(0);
expect(getDiff).toHaveBeenCalledTimes(1);
- await contacts.ensureContactsReady(
- 101,
- session,
- getDiff,
- getProfilePicture,
- );
+ contacts.logoutContacts();
+ await contacts.pullContacts();
expect(getDiff).toHaveBeenCalledTimes(1);
- await contacts.ensureContactsReady(
- 101,
- session,
- getDiff,
- getProfilePicture,
- );
+ await contacts.initContacts(101, session, getDiff, getProfilePicture);
+ await contacts.pullContacts();
expect(getDiff).toHaveBeenCalledTimes(2);
await vi.advanceTimersByTimeAsync(10_001);
- await expect(staleReady).resolves.toBeUndefined();
+ await expect(stalePull).resolves.toBeUndefined();
expect(getDiff).toHaveBeenCalledTimes(2);
});
});
+
+describe("logout", () => {
+ test("revokes cached avatars and notifies subscribers", async () => {
+ vi.spyOn(URL, "createObjectURL").mockReturnValue("blob:contact");
+ const revoke = vi
+ .spyOn(URL, "revokeObjectURL")
+ .mockImplementation(() => undefined);
+ const { contacts, getDiff, getProfilePicture } =
+ await setupContactsModule({
+ getProfilePictureBytes: new Uint8Array([1, 2, 3]),
+ });
+ await contacts.initContacts(101, session, getDiff, getProfilePicture);
+ await contacts.pullContacts();
+ await contacts.__testing.preloadResolvedContactAvatar({ userID: 101 });
+ const changed = vi.fn();
+ contacts.contactsDisplaySubscribe(changed);
+
+ contacts.logoutContacts();
+
+ expect(revoke).toHaveBeenCalledWith("blob:contact");
+ expect(changed).toHaveBeenCalledOnce();
+ expect(
+ contacts.contactsDisplaySnapshot().avatarURLsByContactID.size,
+ ).toBe(0);
+ });
+});
diff --git a/web/packages/contacts/index.ts b/web/packages/contacts/index.ts
index a12086fb45..d28d6fa2bf 100644
--- a/web/packages/contacts/index.ts
+++ b/web/packages/contacts/index.ts
@@ -1,7 +1,6 @@
import { retryAsyncOperation } from "ente-base/http";
import log from "ente-base/log";
import { apiOrigin } from "ente-base/origins";
-import { savedAuthToken } from "ente-base/token";
import { ensureArrayBufferBacked } from "ente-utils/bytes";
import { useCallback, useEffect, useMemo, useSyncExternalStore } from "react";
import {
@@ -58,6 +57,12 @@ interface ProfilePictureOutput {
wrappedRootContactKey?: WrappedRootContactKey;
}
+type GetDiff = (
+ wrappedRootContactKey: WrappedRootContactKey | undefined,
+ sinceTime: number,
+ limit: number,
+) => Promise<ContactsDiffOutput>;
+
type LoadProfilePicture = (
wrappedRootContactKey: WrappedRootContactKey | undefined,
contactID: string,
@@ -68,9 +73,10 @@ interface ContactsState {
listeners: Set<() => void>;
currentSessionKey: string | undefined;
sessionGeneration: number;
+ getDiff: GetDiff | undefined;
getProfilePicture: LoadProfilePicture | undefined;
wrappedRootContactKey: WrappedRootContactKey | undefined;
- readyPromise: Promise<void> | undefined;
+ pullPromise: Promise<void> | undefined;
contactsByID: Map<string, ContactDisplayRecord>;
contactIDByUserID: Map<number, string>;
contactIDByEmail: Map<string, string>;
@@ -92,9 +98,10 @@ const state: ContactsState = {
listeners: new Set(),
currentSessionKey: undefined,
sessionGeneration: 0,
+ getDiff: undefined,
getProfilePicture: undefined,
wrappedRootContactKey: undefined,
- readyPromise: undefined,
+ pullPromise: undefined,
contactsByID: new Map(),
contactIDByUserID: new Map(),
contactIDByEmail: new Map(),
@@ -138,9 +145,10 @@ const clearInMemoryState = () => {
for (const avatarURL of state.avatarURLByContactID.values()) {
URL.revokeObjectURL(avatarURL);
}
+ state.getDiff = undefined;
state.getProfilePicture = undefined;
state.wrappedRootContactKey = undefined;
- state.readyPromise = undefined;
+ state.pullPromise = undefined;
state.contactsByID = new Map();
state.contactIDByUserID = new Map();
state.contactIDByEmail = new Map();
@@ -304,11 +312,7 @@ const ensureSessionLoaded = async (sessionKey: string) =>
const syncContacts = async (
sessionKey: string,
generation: number,
- getDiff: (
- wrappedRootContactKey: WrappedRootContactKey | undefined,
- sinceTime: number,
- limit: number,
- ) => Promise<ContactsDiffOutput>,
+ getDiff: GetDiff,
) => {
if (!isCurrentSession(sessionKey, generation)) {
return;
@@ -371,7 +375,7 @@ const syncContacts = async (
}
};
-export const ensureContactsReady = async <Session>(
+export const initContacts = async <Session>(
userID: number,
session: Session,
getDiff: (
@@ -386,14 +390,6 @@ export const ensureContactsReady = async <Session>(
contactID: string,
) => Promise<ProfilePictureOutput>,
) => {
- if (!(await savedAuthToken())) {
- state.sessionGeneration += 1;
- state.currentSessionKey = undefined;
- clearInMemoryState();
- emitSnapshot(false);
- return;
- }
-
const baseURL = await apiOrigin();
const sessionKey = buildSessionKey(baseURL, userID);
@@ -402,27 +398,38 @@ export const ensureContactsReady = async <Session>(
return;
}
- if (state.readyPromise) {
- return state.readyPromise;
- }
-
+ state.getDiff = (key, sinceTime, limit) =>
+ getDiff(session, key, sinceTime, limit);
state.getProfilePicture = (key, contactID) =>
getProfilePicture(session, key, contactID);
- const readyPromise = retryAsyncOperation(
- () =>
- syncContacts(sessionKey, generation, (key, sinceTime, limit) =>
- getDiff(session, key, sinceTime, limit),
- ),
+};
+
+export const pullContacts = async () => {
+ const sessionKey = state.currentSessionKey;
+ const generation = state.sessionGeneration;
+ const getDiff = state.getDiff;
+ if (!sessionKey || !getDiff) return;
+ if (state.pullPromise) return state.pullPromise;
+
+ const pullPromise = retryAsyncOperation(
+ () => syncContacts(sessionKey, generation, getDiff),
{ retryProfile: "background" },
).finally(() => {
- if (state.readyPromise === readyPromise) {
- state.readyPromise = undefined;
+ if (state.pullPromise === pullPromise) {
+ state.pullPromise = undefined;
}
});
- state.readyPromise = readyPromise;
+ state.pullPromise = pullPromise;
- return readyPromise;
+ return pullPromise;
+};
+
+export const logoutContacts = () => {
+ state.sessionGeneration += 1;
+ state.currentSessionKey = undefined;
+ clearInMemoryState();
+ emitSnapshot(false);
};
const inferImageMimeType = (bytes: Uint8Array) => {