Skip to content

armgit / ente

Public Imported from GitHub
0

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

Split

Manav Rathic01a439b81c9
diff --git a/web/packages/new/photos/services/user-entity/db.ts b/web/packages/new/photos/services/user-entity/db.ts
new file mode 100644
index 0000000000..336277ce67
--- /dev/null
+++ b/web/packages/new/photos/services/user-entity/db.ts
@@ -0,0 +1,55 @@
+import { getKV, getKVN, setKV } from "@/base/kv";
+import { LocalLocationTag, type EntityType, type LocationTag } from ".";
+import { RemoteUserEntityKey } from "./remote";
+
+export const saveLocationTags = (tags: LocationTag[]) =>
+ setKV("locationTags", JSON.stringify(tags));
+
+/**
+ * Return all the location tags that are present locally.
+ *
+ * Use {@link pullLocationTags} to synchronize this list with remote.
+ */
+export const savedLocationTags = async () =>
+ LocalLocationTag.array().parse(
+ JSON.parse((await getKV("locationTags")) ?? "[]"),
+ );
+
+const entityKeyKey = (type: EntityType) => `entityKey/${type}`;
+
+/**
+ * Return the locally persisted {@link RemoteUserEntityKey}, if any,
+ * corresponding the given {@link type}.
+ */
+export const savedRemoteUserEntityKey = (
+ type: EntityType,
+): Promise<RemoteUserEntityKey | undefined> =>
+ getKV(entityKeyKey(type)).then((s) =>
+ s ? RemoteUserEntityKey.parse(JSON.parse(s)) : undefined,
+ );
+
+/**
+ * Setter for {@link entityKey}.
+ */
+export const saveRemoteUserEntityKey = (
+ type: EntityType,
+ entityKey: RemoteUserEntityKey,
+) => setKV(entityKeyKey(type), JSON.stringify(entityKey));
+
+const latestUpdatedAtKey = (type: EntityType) => `latestUpdatedAt/${type}`;
+
+/**
+ * Return the locally persisted value for the latest `updatedAt` time for the
+ * given entity {@link type}.
+ *
+ * This is used to checkpoint diffs, so that we can resume fetching from the
+ * last time we did a fetch.
+ */
+export const savedLatestUpdatedAt = (type: EntityType) =>
+ getKVN(latestUpdatedAtKey(type));
+
+/**
+ * Setter for {@link savedLatestUpdatedAt}.
+ */
+export const saveLatestUpdatedAt = (type: EntityType, value: number) =>
+ setKV(latestUpdatedAtKey(type), value);
diff --git a/web/packages/new/photos/services/user-entity/index.ts b/web/packages/new/photos/services/user-entity/index.ts
index a3b0250c65..f2286ff65f 100644
--- a/web/packages/new/photos/services/user-entity/index.ts
+++ b/web/packages/new/photos/services/user-entity/index.ts
@@ -1,18 +1,28 @@
import {
- decryptBlob,
decryptBoxB64,
encryptBoxB64,
generateNewBlobOrStreamKey,
} from "@/base/crypto";
-import { authenticatedRequestHeaders, ensureOk, HTTPError } from "@/base/http";
-import { getKV, getKVN, setKV } from "@/base/kv";
-import { apiURL } from "@/base/origins";
-import { ensure } from "@/utils/ensure";
import { nullToUndefined } from "@/utils/transform";
import { z } from "zod";
import { gunzip } from "../gzip";
import { applyCGroupDiff } from "../ml/db";
import type { CGroup } from "../ml/people";
+import {
+ savedLatestUpdatedAt,
+ savedLocationTags,
+ savedRemoteUserEntityKey,
+ saveLatestUpdatedAt,
+ saveLocationTags,
+ saveRemoteUserEntityKey,
+} from "./db";
+import {
+ getUserEntityKey,
+ postUserEntityKey,
+ RemoteUserEntityKey,
+ type UserEntityChange,
+ userEntityDiff,
+} from "./remote";
/**
* User entities are predefined lists of otherwise arbitrary data that the user
@@ -76,26 +86,15 @@ const RemoteLocationTag = z.object({
}),
});
+export type RemoteLocationTag = z.infer<typeof RemoteLocationTag>;
+
/** Zod schema for the tag that we persist locally. */
-const LocalLocationTag = RemoteLocationTag.extend({
+export const LocalLocationTag = RemoteLocationTag.extend({
id: z.string(),
});
export type LocationTag = z.infer<typeof LocalLocationTag>;
-const saveLocationTags = (tags: LocationTag[]) =>
- setKV("locationTags", JSON.stringify(tags));
-
-/**
- * Return all the location tags that are present locally.
- *
- * Use {@link pullLocationTags} to synchronize this list with remote.
- */
-export const savedLocationTags = async () =>
- LocalLocationTag.array().parse(
- JSON.parse((await getKV("locationTags")) ?? "[]"),
- );
-
/**
* Update our local cgroups with changes from remote.
*
@@ -146,57 +145,7 @@ const RemoteCGroup = z.object({
avatarFaceID: z.string().nullish().transform(nullToUndefined),
});
-/**
- * The maximum number of items to fetch in a single diff
- *
- * [Note: Limit of returned items in /diff requests]
- *
- * The various GET /diff API methods, which tell the client what all has changed
- * since a timestamp (provided by the client) take a limit parameter.
- *
- * These diff API calls return all items whose updated at is greater
- * (non-inclusive) than the timestamp we provide. So there is no mechanism for
- * pagination of items which have the exact same updated at.
- *
- * Conceptually, it may happen that there are more items than the limit we've
- * provided, but there are practical safeguards.
- *
- * For file diff, the limit is advisory, and remote may return less, equal or
- * more items than the provided limit. The scenario where it returns more is
- * when more files than the limit have the same updated at. Theoretically it
- * would make the diff response unbounded, however in practice file
- * modifications themselves are all batched. Even if the user were to select all
- * the files in their library and updates them all in one go in the UI, their
- * client app is required to use batched API calls to make those updates, and
- * each of those batches would get distinct updated at.
- */
-const defaultDiffLimit = 500;
-
-/**
- * An entry in the user entity diff.
- *
- * Each change either contains the latest data associated with a particular user
- * entity that has been created or updated, or indicates that the corresponding
- * entity has been deleted.
- */
-interface UserEntityChange {
- /**
- * A UUID or nanoid of the entity.
- */
- id: string;
- /**
- * Arbitrary (decrypted) data associated with the entity. The format of this
- * data is specific to each entity type.
- *
- * This will not be present for entities that have been deleted on remote.
- */
- data: Uint8Array | undefined;
- /**
- * Epoch microseconds denoting when this entity was last changed (created or
- * updated or deleted).
- */
- updatedAt: number;
-}
+export type RemoteCGroup = z.infer<typeof RemoteCGroup>;
/**
* Sync of the given {@link type} entities that we have locally with remote.
@@ -233,92 +182,6 @@ const pullUserEntities = async (
}
};
-/**
- * Zod schema for a item in the user entity diff.
- */
-const RemoteUserEntityChange = z.object({
- id: z.string(),
- /**
- * Base64 string containing the encrypted contents of the entity.
- *
- * Will be `null` when isDeleted is true.
- */
- encryptedData: z.string().nullable(),
- /**
- * Base64 string containing the decryption header.
- *
- * Will be `null` when isDeleted is true.
- */
- header: z.string().nullable(),
- isDeleted: z.boolean(),
- updatedAt: z.number(),
-});
-
-/**
- * Fetch the next set of changes (upsert or deletion) to user entities of the
- * given type since the given time.
- *
- * @param type The type of the entities to fetch.
- *
- * @param sinceTime Epoch milliseconds. This is used to ask remote to provide us
- * only entities whose {@link updatedAt} is more than the given value. Set this
- * to zero to start from the beginning.
- *
- * @param entityKeyB64 The base64 encoded key to use for decrypting the
- * encrypted contents of the user entity.
- *
- * [Note: Diff response will have at most one entry for an id]
- *
- * Unlike git diffs which track all changes, the diffs we get from remote are
- * guaranteed to contain only one entry (upsert or delete) for a particular Ente
- * object. This holds true irrespective of the diff limit.
- *
- * For example, in a user entity diff, it is guaranteed that there will only be
- * at max one entry for a particular entity id. The entry will have no data to
- * indicate that the corresponding entity was deleted. Otherwise, when the data
- * is present, it is taken as the creation of a new entity or the updation of an
- * existing one.
- *
- * This behaviour comes from how remote stores the underlying, say, entities. A
- * diff returns just entities whose updation times greater than the provided
- * since time (limited to the given diff limit). So there will be at most one
- * row for a particular entity id. And if that entity has been deleted, then the
- * row will be a tombstone, so data be absent.
- */
-const userEntityDiff = async (
- type: EntityType,
- sinceTime: number,
- entityKeyB64: string,
-): Promise<UserEntityChange[]> => {
- const decrypt = (encryptedData: string, decryptionHeader: string) =>
- decryptBlob({ encryptedData, decryptionHeader }, entityKeyB64);
-
- const params = new URLSearchParams({
- type,
- sinceTime: sinceTime.toString(),
- limit: defaultDiffLimit.toString(),
- });
- const url = await apiURL(`/user-entity/entity/diff`);
- const res = await fetch(`${url}?${params.toString()}`, {
- headers: await authenticatedRequestHeaders(),
- });
- ensureOk(res);
- const diff = z
- .object({ diff: z.array(RemoteUserEntityChange) })
- .parse(await res.json()).diff;
- return Promise.all(
- diff.map(
- async ({ id, encryptedData, header, isDeleted, updatedAt }) => ({
- id,
- data: !isDeleted
- ? await decrypt(ensure(encryptedData), ensure(header))
- : undefined,
- updatedAt,
- }),
- ),
- );
-};
-
/**
* Return the entity key that can be used to decrypt the encrypted contents of
* user entities of the given {@link type}.
@@ -366,31 +229,6 @@ const getOrCreateEntityKeyB64 = async (
return result;
};
-const entityKeyKey = (type: EntityType) => `entityKey/${type}`;
-
-/**
- * Return the locally persisted {@link RemoteUserEntityKey}, if any,
- * corresponding the given {@link type}.
- */
-const savedRemoteUserEntityKey = (
- type: EntityType,
-): Promise<RemoteUserEntityKey | undefined> =>
- getKV(entityKeyKey(type)).then((s) =>
- s ? RemoteUserEntityKey.parse(JSON.parse(s)) : undefined,
- );
-
-/**
- * Setter for {@link entityKey}.
- */
-const saveRemoteUserEntityKey = (
- type: EntityType,
- entityKey: RemoteUserEntityKey,
-) => setKV(entityKeyKey(type), JSON.stringify(entityKey));
-
-/**
- * Generate a new entity key and return it in the shape of an
- * {@link RemoteUserEntityKey} after encrypting it using the user's master key.
- */
const generateNewEncryptedEntityKey = async (masterKey: Uint8Array) => {
const { encryptedData, nonce } = await encryptBoxB64(
await generateNewBlobOrStreamKey(),
@@ -415,75 +253,3 @@ const decryptEntityKey = async (
},
masterKey,
);
-
-/**
- * Fetch the encryption key for the given user entity {@link type} from remote.
- *
- * [Note: User entity keys]
- *
- * There is one encryption key (itself encrypted with the user's master key) for
- * each user entity type. If the key doesn't exist on remote, then the client is
- * expected to create one on the user's behalf. Remote will disallow attempts to
- * multiple keys for the same user entity type.
- */
-const getUserEntityKey = async (
- type: EntityType,
-): Promise<RemoteUserEntityKey | undefined> => {
- const params = new URLSearchParams({ type });
- const url = await apiURL("/user-entity/key");
- const res = await fetch(`${url}?${params.toString()}`, {
- headers: await authenticatedRequestHeaders(),
- });
- if (!res.ok) {
- // Remote says HTTP 404 Not Found if there is no key yet for the user.
- if (res.status == 404) return undefined;
- throw new HTTPError(res);
- } else {
- return RemoteUserEntityKey.parse(await res.json());
- }
-};
-
-const RemoteUserEntityKey = z.object({
- /** Base64 encoded entity key, encrypted with the user's master key. */
- encryptedKey: z.string(),
- /** Base64 encoded nonce used during encryption of this entity key. */
- header: z.string(),
-});
-
-type RemoteUserEntityKey = z.infer<typeof RemoteUserEntityKey>;
-
-/**
- * Create a new encryption key for the given user entity {@link type} on remote.
- *
- * See: [Note: User entity keys]
- */
-const postUserEntityKey = async (
- type: EntityType,
- entityKey: RemoteUserEntityKey,
-) => {
- const url = await apiURL("/user-entity/key");
- const res = await fetch(url, {
- method: "POST",
- headers: await authenticatedRequestHeaders(),
- body: JSON.stringify({ type, ...entityKey }),
- });
- ensureOk(res);
-};
-
-const latestUpdatedAtKey = (type: EntityType) => `latestUpdatedAt/${type}`;
-
-/**
- * Return the locally persisted value for the latest `updatedAt` time for the
- * given entity {@link type}.
- *
- * This is used to checkpoint diffs, so that we can resume fetching from the
- * last time we did a fetch.
- */
-const savedLatestUpdatedAt = (type: EntityType) =>
- getKVN(latestUpdatedAtKey(type));
-
-/**
- * Setter for {@link savedLatestUpdatedAt}.
- */
-const saveLatestUpdatedAt = (type: EntityType, value: number) =>
- setKV(latestUpdatedAtKey(type), value);
diff --git a/web/packages/new/photos/services/user-entity/remote.ts b/web/packages/new/photos/services/user-entity/remote.ts
new file mode 100644
index 0000000000..17c2123741
--- /dev/null
+++ b/web/packages/new/photos/services/user-entity/remote.ts
@@ -0,0 +1,202 @@
+import { decryptBlob } from "@/base/crypto";
+import { authenticatedRequestHeaders, ensureOk, HTTPError } from "@/base/http";
+import { apiURL } from "@/base/origins";
+import { ensure } from "@/utils/ensure";
+import { z } from "zod";
+import type { EntityType } from ".";
+
+/**
+ * The maximum number of items to fetch in a single diff
+ *
+ * [Note: Limit of returned items in /diff requests]
+ *
+ * The various GET /diff API methods, which tell the client what all has changed
+ * since a timestamp (provided by the client) take a limit parameter.
+ *
+ * These diff API calls return all items whose updated at is greater
+ * (non-inclusive) than the timestamp we provide. So there is no mechanism for
+ * pagination of items which have the exact same updated at.
+ *
+ * Conceptually, it may happen that there are more items than the limit we've
+ * provided, but there are practical safeguards.
+ *
+ * For file diff, the limit is advisory, and remote may return less, equal or
+ * more items than the provided limit. The scenario where it returns more is
+ * when more files than the limit have the same updated at. Theoretically it
+ * would make the diff response unbounded, however in practice file
+ * modifications themselves are all batched. Even if the user were to select all
+ * the files in their library and updates them all in one go in the UI, their
+ * client app is required (with server side enforcement) to use batched API
+ * calls to make those updates. Thus, each of those batches would get distinct
+ * updated at values.
+ *
+ * For entity diff, there are no bulk operations yet that can result in the
+ * assignment of the same microsecond to hundreds of items.
+ */
+const defaultDiffLimit = 500;
+
+/**
+ * An entry in the user entity diff.
+ *
+ * Each change either contains the latest data associated with a particular user
+ * entity that has been created or updated, or indicates that the corresponding
+ * entity has been deleted.
+ */
+export interface UserEntityChange {
+ /**
+ * A UUID or nanoid of the entity.
+ */
+ id: string;
+ /**
+ * Arbitrary (decrypted) data associated with the entity. The format of this
+ * data is specific to each entity type.
+ *
+ * This will not be present for entities that have been deleted on remote.
+ */
+ data: Uint8Array | undefined;
+ /**
+ * Epoch microseconds denoting when this entity was last changed (created or
+ * updated or deleted).
+ */
+ updatedAt: number;
+}
+
+/**
+ * Zod schema for a item in the user entity diff.
+ */
+const RemoteUserEntityChange = z.object({
+ id: z.string(),
+ /**
+ * Base64 string containing the encrypted contents of the entity.
+ *
+ * Will be `null` when isDeleted is true.
+ */
+ encryptedData: z.string().nullable(),
+ /**
+ * Base64 string containing the decryption header.
+ *
+ * Will be `null` when isDeleted is true.
+ */
+ header: z.string().nullable(),
+ isDeleted: z.boolean(),
+ updatedAt: z.number(),
+});
+
+/**
+ * Fetch the next set of changes (upserts or deletions) to user entities of the
+ * given type since the given time.
+ *
+ * @param type The type of the entities to fetch.
+ *
+ * @param sinceTime Epoch milliseconds. This is used to ask remote to provide us
+ * only entities whose {@link updatedAt} is more than the given value. Set this
+ * to zero to start from the beginning.
+ *
+ * @param entityKeyB64 The base64 encoded key to use for decrypting the
+ * encrypted contents of the user entity.
+ *
+ * [Note: Diff response will have at most one entry for an id]
+ *
+ * Unlike git diffs which track all changes, the diffs we get from remote are
+ * guaranteed to contain only one entry (upsert or delete) for a particular Ente
+ * object. This holds true irrespective of the diff limit.
+ *
+ * For example, in a user entity diff, it is guaranteed that there will only be
+ * at max one entry for a particular entity id. The entry will have no data to
+ * indicate that the corresponding entity was deleted. Otherwise, when the data
+ * is present, it is taken as the creation of a new entity or the updation of an
+ * existing one.
+ *
+ * This behaviour comes from how remote stores the underlying, say, entities. A
+ * diff returns just entities whose updation times greater than the provided
+ * since time (limited to the given diff limit). So there will be at most one
+ * row for a particular entity id. And if that entity has been deleted, then the
+ * row will be a tombstone, so data be absent.
+ */
+export const userEntityDiff = async (
+ type: EntityType,
+ sinceTime: number,
+ entityKeyB64: string,
+): Promise<UserEntityChange[]> => {
+ const decrypt = (encryptedData: string, decryptionHeader: string) =>
+ decryptBlob({ encryptedData, decryptionHeader }, entityKeyB64);
+
+ const params = new URLSearchParams({
+ type,
+ sinceTime: sinceTime.toString(),
+ limit: defaultDiffLimit.toString(),
+ });
+ const url = await apiURL(`/user-entity/entity/diff`);
+ const res = await fetch(`${url}?${params.toString()}`, {
+ headers: await authenticatedRequestHeaders(),
+ });
+ ensureOk(res);
+ const diff = z
+ .object({ diff: z.array(RemoteUserEntityChange) })
+ .parse(await res.json()).diff;
+ return Promise.all(
+ diff.map(
+ async ({ id, encryptedData, header, isDeleted, updatedAt }) => ({
+ id,
+ data: !isDeleted
+ ? await decrypt(ensure(encryptedData), ensure(header))
+ : undefined,
+ updatedAt,
+ }),
+ ),
+ );
+};
+
+/**
+ * Fetch the encryption key for the given user entity {@link type} from remote.
+ *
+ * [Note: User entity keys]
+ *
+ * There is one encryption key (itself encrypted with the user's master key) for
+ * each user entity type. If the key doesn't exist on remote, then the client is
+ * expected to create one on the user's behalf. Remote will disallow attempts to
+ * multiple keys for the same user entity type.
+ */
+export const getUserEntityKey = async (
+ type: EntityType,
+): Promise<RemoteUserEntityKey | undefined> => {
+ const params = new URLSearchParams({ type });
+ const url = await apiURL("/user-entity/key");
+ const res = await fetch(`${url}?${params.toString()}`, {
+ headers: await authenticatedRequestHeaders(),
+ });
+ if (!res.ok) {
+ // Remote says HTTP 404 Not Found if there is no key yet for the user.
+ if (res.status == 404) return undefined;
+ throw new HTTPError(res);
+ } else {
+ return RemoteUserEntityKey.parse(await res.json());
+ }
+};
+
+export const RemoteUserEntityKey = z.object({
+ /** Base64 encoded entity key, encrypted with the user's master key. */
+ encryptedKey: z.string(),
+ /** Base64 encoded nonce used during encryption of this entity key. */
+ header: z.string(),
+});
+
+export type RemoteUserEntityKey = z.infer<typeof RemoteUserEntityKey>;
+
+/**
+ * Create a new encryption key for the given user entity {@link type} on remote.
+ *
+ * See: [Note: User entity keys]
+ */
+export const postUserEntityKey = async (
+ type: EntityType,
+ entityKey: RemoteUserEntityKey,
+) => {
+ const url = await apiURL("/user-entity/key");
+ const res = await fetch(url, {
+ method: "POST",
+ headers: await authenticatedRequestHeaders(),
+ body: JSON.stringify({ type, ...entityKey }),
+ });
+ ensureOk(res);
+};