Skip to content

armgit / ente

Public Imported from GitHub
0

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

sync 2

Manav Rathi1314b8ccbb3d
diff --git a/web/apps/photos/src/services/searchService.ts b/web/apps/photos/src/services/searchService.ts
index f9507bd675..4ece8ab439 100644
--- a/web/apps/photos/src/services/searchService.ts
+++ b/web/apps/photos/src/services/searchService.ts
@@ -10,7 +10,7 @@ import {
wipClusterEnable,
} from "@/new/photos/services/ml";
import type { SearchPerson } from "@/new/photos/services/search";
-import { personDiff } from "@/new/photos/services/user-entity";
+import { syncPersons } from "@/new/photos/services/user-entity";
import { EnteFile } from "@/new/photos/types/file";
import * as chrono from "chrono-node";
import { t } from "i18next";
@@ -421,7 +421,7 @@ async function getAllPeople(limit: number = undefined) {
if (process.env.NEXT_PUBLIC_ENTE_WIP_CL_FETCH) {
const entityKey = await getEntityKey("person" as EntityType);
- const peopleR = await personDiff(entityKey.data);
+ const peopleR = await syncPersons(entityKey.data);
const r = peopleR.length;
log.debug(() => ["people", peopleR]);
diff --git a/web/packages/new/photos/services/ml/cluster-new.ts b/web/packages/new/photos/services/ml/cluster-new.ts
index 9bf342bab9..9953e474b8 100644
--- a/web/packages/new/photos/services/ml/cluster-new.ts
+++ b/web/packages/new/photos/services/ml/cluster-new.ts
@@ -60,7 +60,7 @@ export interface Person {
* The ID of the face that should be used as the display face, to represent
* this person in the UI.
*/
- avatarFaceID: string;
+ avatarFaceID: string | undefined;
}
/**
diff --git a/web/packages/new/photos/services/ml/db.ts b/web/packages/new/photos/services/ml/db.ts
index be35790591..4b8d621370 100644
--- a/web/packages/new/photos/services/ml/db.ts
+++ b/web/packages/new/photos/services/ml/db.ts
@@ -444,12 +444,55 @@ export const setFaceClusters = async (clusters: FaceCluster[]) => {
return tx.done;
};
+/**
+ * Update the person store to reflect the given changes, in order.
+ *
+ * @param changes A list of changes to apply. Each entry is either
+ *
+ * - A string, in which case the person with the given string as their ID
+ * should be deleted from the store, or
+ *
+ * - A person, in which case it should add or overwrite the entry for the
+ * corresponding person (as identified by their {@link id}).
+ */
+export const applyPersonDiff = async (changes: (string | Person)[]) => {
+ const db = await mlDB();
+ const tx = db.transaction("person", "readwrite");
+ // We want to do the changes in order, so we shouldn't use Promise.all.
+ for (const change of changes) {
+ await (typeof change == "string"
+ ? tx.store.delete(change)
+ : tx.store.put(change));
+ }
+ return tx.done;
+};
+
+/**
+ * Add or overwrite the entry for the given {@link person}, as identified by
+ * their {@link id}.
+ */
+export const savePerson = async (person: Person) => {
+ const db = await mlDB();
+ const tx = db.transaction("person", "readwrite");
+ await Promise.all([tx.store.put(person), tx.done]);
+};
+
+/**
+ * Delete the entry for the persons with the given {@link id}, if any.
+ */
+export const deletePerson = async (id: string) => {
+ const db = await mlDB();
+ const tx = db.transaction("person", "readwrite");
+ await Promise.all([tx.store.delete(id), tx.done]);
+};
+
/**
* Replace the persons stored locally with the given ones.
*
* This function deletes all entries from the person object store, and then
* inserts the given {@link persons} into it.
*/
+// TODO-Cluster: Remove me
export const setPersons = async (persons: Person[]) => {
const db = await mlDB();
const tx = db.transaction("person", "readwrite");
diff --git a/web/packages/new/photos/services/user-entity.ts b/web/packages/new/photos/services/user-entity.ts
index c695be5b93..c952c61f40 100644
--- a/web/packages/new/photos/services/user-entity.ts
+++ b/web/packages/new/photos/services/user-entity.ts
@@ -3,6 +3,8 @@ import { authenticatedRequestHeaders, ensureOk } from "@/base/http";
import { getKVN, setKV } from "@/base/kv";
import { apiURL } from "@/base/origins";
import { z } from "zod";
+import type { Person } from "./ml/cluster-new";
+import { applyPersonDiff } from "./ml/db";
/**
* User entities are predefined lists of otherwise arbitrary data that the user
@@ -135,36 +137,39 @@ export const userEntityDiff = async (
*
* This fetches all the user entities corresponding to the "person_v2" entity
* type from remote that have been created, updated or deleted since the last
- * time we checked. This diff is then applied to the data we have persisted
- * locally.
+ * time we checked.
+ *
+ * This diff is then applied to the data we have persisted locally.
*/
-export const personDiff = async (
- entityKeyB64: string,
-): Promise<RemotePerson[]> => {
- const sinceTime = 0;
-
- const parse = (data: Uint8Array) =>
- RemotePerson.parse(JSON.parse(new TextDecoder().decode(data)));
+export const syncPersons = async (entityKeyB64: string) => {
+ const type: EntityType = "person";
- const result: RemotePerson[] = [];
+ const parse = ({ id, data }: UserEntity): Person => {
+ const rp = RemotePerson.parse(
+ JSON.parse(new TextDecoder().decode(data)),
+ );
+ return {
+ id,
+ name: rp.name,
+ clusterIDs: rp.assigned.map(({ id }) => id),
+ avatarFaceID: undefined,
+ };
+ };
+
+ let sinceTime = (await latestUpdatedAt(type)) ?? 0;
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition, no-constant-condition
while (true) {
- const entities = await userEntityDiff("person", 0, entityKeyB64);
+ const entities = await userEntityDiff(type, sinceTime, entityKeyB64);
if (entities.length == 0) break;
- const latestUpdatedAt = entities.reduce(
+ await applyPersonDiff(entities.map((e) => (e.data ? parse(e) : e.id)));
+
+ sinceTime = entities.reduce(
(max, e) => Math.max(max, e.updatedAt),
sinceTime,
);
-
- const people = entities
- .map(({ data }) => (data ? parse(data) : undefined))
- .filter((p) => !!p);
- // TODO-Cluster
- console.log({ latestUpdatedAt, people });
- return people;
+ await setLatestUpdatedAt(type, sinceTime);
}
- return result;
};
/**