π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
wip checkpoint prep for making all faces clickable
Manav Rathie28eb325b308
diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsxindex 6a137abe10..2d663f8d81 100644--- a/web/apps/photos/src/pages/gallery.tsx+++ b/web/apps/photos/src/pages/gallery.tsx@@ -21,7 +21,6 @@ import {} from "@/new/photos/components/gallery";import type { GalleryBarMode } from "@/new/photos/components/gallery/BarImpl";import { GalleryPeopleState } from "@/new/photos/components/gallery/PeopleHeader";-import { usePeople } from "@/new/photos/components/utils/ml";import { shouldShowWhatsNew } from "@/new/photos/services/changelog";import type { CollectionSummaries } from "@/new/photos/services/collection/ui";import { areOnlySystemCollections } from "@/new/photos/services/collection/ui";@@ -31,6 +30,7 @@ import {getLocalTrashedFiles,sortFiles,} from "@/new/photos/services/files";+import { peopleSnapshot, peopleSubscribe } from "@/new/photos/services/ml";import type { Person } from "@/new/photos/services/ml/people";import {filterSearchableFiles,@@ -102,6 +102,7 @@ import {useMemo,useRef,useState,+ useSyncExternalStore,} from "react";import { useDropzone } from "react-dropzone";import {@@ -322,7 +323,10 @@ export default function Gallery() {// The ID of the currently selected person in the gallery bar (if any).const [activePersonID, setActivePersonID] = useState<string | undefined>();- const people = usePeople();+ const { people, visiblePeople } = useSyncExternalStore(+ peopleSubscribe,+ peopleSnapshot,+ );const [isClipSearchResult, setIsClipSearchResult] =useState<boolean>(false);@@ -567,6 +571,7 @@ export default function Gallery() {);} else if (barMode == "people") {let filteredPeople = people ?? [];+ let filteredVisiblePeople = visiblePeople ?? [];if (tempDeletedFileIds?.size ?? tempHiddenFileIds?.size) {// Prune the in-memory temp updates from the actual state to// obtain the UI state. Kept inside an preflight check to so@@ -581,12 +586,26 @@ export default function Gallery() {),})).filter((p) => p.fileIDs.length > 0);+ filteredVisiblePeople = filteredPeople.filter(+ (p) => p.isVisible,+ );+ }+ const findByID = (ps: Person[]) =>+ ps.find((p) => p.id == activePersonID);+ let activePerson = findByID(filteredVisiblePeople);+ if (!activePerson) {+ // This might be one of the normally hidden small clusters.+ activePerson = findByID(filteredPeople);+ if (activePerson) {+ // Temporarily add this person's entry to the list of people+ // surfaced in the people section.+ filteredVisiblePeople.push(activePerson);+ } else {+ // We don't have an "All" pseudo-album in people mode, so+ // default to the first person in the list.+ activePerson = filteredVisiblePeople[0];+ }}- const activePerson =- filteredPeople.find((p) => p.id == activePersonID) ??- // We don't have an "All" pseudo-album in people mode currently,- // so default to the first person in the list.- filteredPeople[0];const pfSet = new Set(activePerson?.fileIDs ?? []);filteredFiles = getUniqueFiles(files.filter(({ id }) => {diff --git a/web/packages/new/photos/components/SearchBar.tsx b/web/packages/new/photos/components/SearchBar.tsxindex d290feb903..cfd32b61b9 100644--- a/web/packages/new/photos/components/SearchBar.tsx+++ b/web/packages/new/photos/components/SearchBar.tsx@@ -38,7 +38,7 @@ import AsyncSelect from "react-select/async";import { SearchPeopleList } from "./PeopleList";import { UnstyledButton } from "./UnstyledButton";import type { ButtonishProps } from "./mui";-import { useMLStatus, usePeople } from "./utils/ml";+import { useMLStatus, useVisiblePeople } from "./utils/ml";export interface SearchBarProps {/**@@ -378,7 +378,7 @@ const EmptyState: React.FC<Pick<SearchBarProps, "onSelectPerson">> = ({onSelectPerson,}) => {const mlStatus = useMLStatus();- const people = usePeople();+ const people = useVisiblePeople();if (!mlStatus || mlStatus.phase == "disabled") {// The preflight check should've prevented us from coming here.diff --git a/web/packages/new/photos/components/utils/ml.ts b/web/packages/new/photos/components/utils/ml.tsindex ced31eac62..11054ea6b4 100644--- a/web/packages/new/photos/components/utils/ml.ts+++ b/web/packages/new/photos/components/utils/ml.ts@@ -14,8 +14,8 @@ export const useMLStatus = () =>useSyncExternalStore(mlStatusSubscribe, mlStatusSnapshot);/**- * A convenience hook that returns {@link peopleSnapshot}, subscribing to- * updates.+ * A convenience hook that returns visible people array from the+ * {@link peopleSnapshot}, and also subscribes to updates.*/-export const usePeople = () =>- useSyncExternalStore(peopleSubscribe, peopleSnapshot);+export const useVisiblePeople = () =>+ useSyncExternalStore(peopleSubscribe, peopleSnapshot)?.visiblePeople;diff --git a/web/packages/new/photos/services/ml/index.ts b/web/packages/new/photos/services/ml/index.tsindex 307aeaabce..fdfecb5109 100644--- a/web/packages/new/photos/services/ml/index.ts+++ b/web/packages/new/photos/services/ml/index.ts@@ -30,8 +30,9 @@ import { clearMLDB, getIndexableAndIndexedCounts, savedFaceIndex } from "./db";import {_applyPersonSuggestionUpdates,filterNamedPeople,- reconstructPeople,+ reconstructPeopleSnapshot,type CGroupPerson,+ type PeopleSnapshot,type Person,type PersonSuggestionUpdates,} from "./people";@@ -91,13 +92,13 @@ class MLState {peopleListeners: (() => void)[] = [];/**- * Snapshot of the {@link Person}s returned by the {@link peopleSnapshot}- * function.+ * Snapshot of the list of {@link Person}s and other pre-computed state. Use+ * the {@link peopleSnapshot} function to access this data.** It will be `undefined` only if ML is disabled. Otherwise, it will be an* empty array even if the snapshot is pending its first sync.*/- peopleSnapshot: Person[] | undefined;+ peopleSnapshot: PeopleSnapshot | undefined;/*** In flight face crop regeneration promises indexed by the IDs of the files@@ -561,7 +562,11 @@ export const peopleSubscribe = (onChange: () => void): (() => void) => {* Otherwise, if ML is disabled, set the people snapshot to `undefined`.*/const resetPeopleSnapshot = () =>- setPeopleSnapshot(_state.isMLEnabled ? [] : undefined);+ setPeopleSnapshot(+ _state.isMLEnabled+ ? { people: [], visiblePeople: [], personByFaceID: new Map() }+ : undefined,+ );/*** Return the last known, cached {@link people}.@@ -579,16 +584,16 @@ export const peopleSnapshot = () => _state.peopleSnapshot;// Update our, and the search subsystem's, snapshot of people by reconstructing// it from the latest local state.const updatePeople = async () => {- const people = await reconstructPeople();+ const snapshot = await reconstructPeopleSnapshot();// Notify the search subsystem of the update (search only uses named ones).- setSearchPeople(filterNamedPeople(people));+ setSearchPeople(filterNamedPeople(snapshot.visiblePeople));// Update our in-memory list of people.- setPeopleSnapshot(people);+ setPeopleSnapshot(snapshot);};-const setPeopleSnapshot = (snapshot: Person[] | undefined) => {+const setPeopleSnapshot = (snapshot: PeopleSnapshot | undefined) => {_state.peopleSnapshot = snapshot;_state.peopleListeners.forEach((l) => l());};@@ -609,26 +614,14 @@ export const clipMatches = (): Promise<CLIPMatches | undefined> =>worker().then((w) => w.clipMatches(searchPhrase));-/** A face ID annotated with the ID of the person to which it is associated. */-export interface AnnotatedFaceID {- faceID: string;- personID: string;-}-/**- * List of faces found in a file- *- * It is actually a pair of lists, one annotated by the person ids, and one with- * just the face ids.+ * A face ID annotated with the ID of the person or cluster with which it is+ * associated.*/-export interface AnnotatedFacesForFile {- /**- * A list of {@link AnnotatedFaceID}s for all faces in the file that are- * also associated with a {@link Person}.- */- annotatedFaceIDs: AnnotatedFaceID[];- /* A list of the remaining face (ids). */- otherFaceIDs: string[];+export interface AnnotatedFaceID {+ faceID: string;+ personID: string | undefined;+ clusterID: string | undefined;}/**@@ -636,28 +629,23 @@ export interface AnnotatedFacesForFile {*/export const getAnnotatedFacesForFile = async (file: EnteFile,-): Promise<AnnotatedFacesForFile> => {- const annotatedFaceIDs: AnnotatedFaceID[] = [];- const otherFaceIDs: string[] = [];-+): Promise<AnnotatedFaceID[]> => {const index = await savedFaceIndex(file.id);- if (!index) return { annotatedFaceIDs, otherFaceIDs };-- const people = _state.peopleSnapshot ?? [];+ if (!index) return [];const faceIDToPersonID = new Map<string, string>();+ const people = _state.peopleSnapshot ?? [];for (const person of people) {- let faceIDs: string[];- if (person.type == "cgroup") {- faceIDs = person.cgroup.data.assigned.map((c) => c.faces).flat();- } else {- faceIDs = person.cluster.faces;- }+ const faceIDs =+ person.type == "cgroup"+ ? person.cgroup.data.assigned.map((c) => c.faces).flat()+ : person.cluster.faces;for (const faceID of faceIDs) {faceIDToPersonID.set(faceID, person.id);}}+ const sortableFaces: { face: AnnotatedFaceID; clusterSize: number }[] = [];for (const { faceID } of index.faces) {const personID = faceIDToPersonID.get(faceID);if (personID) {diff --git a/web/packages/new/photos/services/ml/people.ts b/web/packages/new/photos/services/ml/people.tsindex cd3b6e2d49..253cf38d1a 100644--- a/web/packages/new/photos/services/ml/people.ts+++ b/web/packages/new/photos/services/ml/people.ts@@ -136,6 +136,15 @@ export type Person = (* The {@link EnteFile} which contains the display face.*/displayFaceFile: EnteFile;+ /**+ * `true` if this person should be normally shown in the UI.+ *+ * By default, people derived from small, local-only clusters are not+ * surfaced in the UI (For such people, this flag will be set to false). The+ * user can still see these people by clicking on one of the faces they+ * contain from a photo's info view.+ */+ isVisible: boolean;};/**@@ -168,18 +177,40 @@ export interface PreviewableFace {file: EnteFile;}+export interface PeopleSnapshot {+ /**+ * List of all people.+ *+ * The list is sorted such that named people are first, then the local only+ * unnamed clusters. Within each section, people are sorted by the number of+ * files that they reference.+ */+ people: Person[];+ /**+ * List of all people who should be normally shown in the UI.+ *+ * These are just people whose {@link isVisible} property is true. This list+ * is easily derivable from {@link people}, it is only maintained as a+ * separate list for convenience.+ */+ visiblePeople: Person[];+ /**+ * faceID => person+ *+ * {@link people}, but indexed by face (ids).+ */+ personByFaceID: Map<string, Person>;+}+/**- * Construct in-memory people using the data present locally, ignoring faces- * belonging to deleted and hidden files.+ * Construct an in-memory people snapshot using the data present locally,+ * ignoring faces belonging to deleted and hidden files.** This function is meant to run after files, cgroups and faces have been synced* with remote, and clustering has completed. It uses the current local state to* construct an in-memory list of {@link Person}s on which the UI will operate.- *- * @return A list of {@link Person}s, sorted by the number of files that they- * reference.*/-export const reconstructPeople = async (): Promise<Person[]> => {+export const reconstructPeopleSnapshot = async (): Promise<PeopleSnapshot> => {const files = await getLocalFiles("normal");const fileByID = new Map(files.map((f) => [f.id, f]));