Skip to content

armgit / ente

Public Imported from GitHub
0

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

Add manual tagging to desktop

laurenspriem5489c9dba1f0
diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsx
index 098c7049c2..e79f3efb23 100644
--- a/web/apps/photos/src/pages/gallery.tsx
+++ b/web/apps/photos/src/pages/gallery.tsx
@@ -114,6 +114,7 @@ import {
} from "ente-new/photos/services/collection-summary";
import exportService from "ente-new/photos/services/export";
import { updateFilesVisibility } from "ente-new/photos/services/file";
+import { addManualFileAssignmentsToPerson } from "ente-new/photos/services/ml";
import {
savedCollectionFiles,
savedCollections,
@@ -905,6 +906,18 @@ const Page: React.FC = () => {
})();
};
+ const handleAddPersonToSelectedFiles = useCallback(
+ async (personID: string) => {
+ const selectedFiles = getSelectedFiles(selected, filteredFiles);
+ await addManualFileAssignmentsToPerson(
+ personID,
+ selectedFiles.map((f) => f.id),
+ );
+ clearSelection();
+ },
+ [selected, filteredFiles, clearSelection],
+ );
+
const handleSelectSearchOption = (
searchOption: SearchOption | undefined,
options?: { shouldExitSearchMode?: boolean },
@@ -1269,6 +1282,8 @@ const Page: React.FC = () => {
createOnCreateForCollectionOp,
createOnSelectForCollectionOp,
createFileOpHandler,
+ onAddPersonToSelectedFiles:
+ handleAddPersonToSelectedFiles,
}}
/>
) : barMode == "hidden-albums" ? (
diff --git a/web/packages/base/locales/en-US/translation.json b/web/packages/base/locales/en-US/translation.json
index bb2db8a394..f62f240334 100644
--- a/web/packages/base/locales/en-US/translation.json
+++ b/web/packages/base/locales/en-US/translation.json
@@ -273,6 +273,7 @@
"people_empty_too_few": "People will be shown here when there are sufficient photos of a person",
"unnamed_person": "Unnamed person",
"add_a_name": "Add a name",
+ "add_a_person": "Add to person",
"new_person": "New person",
"add_name": "Add name",
"rename_person": "Rename person",
diff --git a/web/packages/gallery/components/FileInfo.tsx b/web/packages/gallery/components/FileInfo.tsx
index d9693080a0..487c356915 100644
--- a/web/packages/gallery/components/FileInfo.tsx
+++ b/web/packages/gallery/components/FileInfo.tsx
@@ -65,15 +65,20 @@ import {
type ParsedMetadataDate,
} from "ente-media/file-metadata";
import { FileType } from "ente-media/file-type";
+import { AssignPersonDialog } from "ente-new/photos/components/AssignPersonDialog";
import { FileDateTimePicker } from "ente-new/photos/components/FileDateTimePicker";
import { FilePeopleList } from "ente-new/photos/components/PeopleList";
-import { useSettingsSnapshot } from "ente-new/photos/components/utils/use-snapshot";
+import {
+ usePeopleStateSnapshot,
+ useSettingsSnapshot,
+} from "ente-new/photos/components/utils/use-snapshot";
import {
updateFileCaption,
updateFileFileName,
updateFilePublicMagicMetadata,
} from "ente-new/photos/services/file";
import {
+ addManualFileAssignmentsToPerson,
getAnnotatedFacesForFile,
isMLEnabled,
type AnnotatedFaceID,
@@ -194,11 +199,40 @@ export const FileInfo: React.FC<FileInfoProps> = ({
onSelectPerson,
}) => {
const { mapEnabled } = useSettingsSnapshot();
+ const peopleState = usePeopleStateSnapshot();
+ const { onGenericError } = useBaseContext();
const [annotatedFaces, setAnnotatedFaces] = useState<AnnotatedFaceID[]>([]);
const { show: showRawExif, props: rawExifVisibilityProps } =
useModalVisibility();
+ const { show: showAssignPerson, props: assignPersonVisibilityProps } =
+ useModalVisibility();
+
+ const assignablePeople = useMemo(
+ () =>
+ (peopleState?.visiblePeople ?? []).filter(
+ (p) => p.type == "cgroup" && !!p.name,
+ ),
+ [peopleState],
+ );
+
+ const manuallyAssignedPeople = useMemo(() => {
+ if (!isMLEnabled()) return [];
+
+ const detectedPersonIDs = new Set(
+ annotatedFaces.map((f) => f.personID),
+ );
+ return (peopleState?.people ?? []).filter(
+ (p) =>
+ p.type == "cgroup" &&
+ !!p.name &&
+ p.cgroup.data.manuallyAssigned.includes(file.id) &&
+ !detectedPersonIDs.has(p.id),
+ );
+ }, [peopleState, file.id, annotatedFaces]);
+
+ const canAddPerson = isMLEnabled() && assignablePeople.length > 0;
const location = useMemo(
// Prefer the location in the EnteFile, then fall back to Exif.
@@ -242,6 +276,15 @@ export const FileInfo: React.FC<FileInfoProps> = ({
onSelectPerson?.(personID);
};
+ const handleAddPerson = async (personID: string) => {
+ assignPersonVisibilityProps.onClose();
+ try {
+ await addManualFileAssignmentsToPerson(personID, [file.id]);
+ } catch (e) {
+ onGenericError(e);
+ }
+ };
+
const uploaderName = file.pubMagicMetadata?.data.uploaderName;
return (
@@ -336,15 +379,25 @@ export const FileInfo: React.FC<FileInfoProps> = ({
)
}
/>
- {annotatedFaces.length > 0 && (
- <InfoItem icon={<FaceRetouchingNaturalIcon />}>
- <FilePeopleList
- file={file}
- annotatedFaceIDs={annotatedFaces}
- onSelectFace={handleSelectFace}
- />
- </InfoItem>
- )}
+ {isMLEnabled() &&
+ (annotatedFaces.length > 0 ||
+ manuallyAssignedPeople.length > 0 ||
+ canAddPerson) && (
+ <InfoItem icon={<FaceRetouchingNaturalIcon />}>
+ <FilePeopleList
+ file={file}
+ annotatedFaceIDs={annotatedFaces}
+ onSelectFace={handleSelectFace}
+ manuallyAssignedPeople={manuallyAssignedPeople}
+ onSelectPerson={onSelectPerson}
+ onAddPerson={
+ canAddPerson ? showAssignPerson : undefined
+ }
+ addPersonTitle={t("add_a_person")}
+ addPersonLabel={t("add")}
+ />
+ </InfoItem>
+ )}
{showCollections &&
fileCollectionIDs &&
collectionNameByID &&
@@ -373,6 +426,15 @@ export const FileInfo: React.FC<FileInfoProps> = ({
tags={exif?.tags}
fileName={fileFileName(file)}
/>
+
+ {canAddPerson && (
+ <AssignPersonDialog
+ {...assignPersonVisibilityProps}
+ people={assignablePeople}
+ title={t("add_a_person")}
+ onSelectPerson={handleAddPerson}
+ />
+ )}
</FileInfoSidebar>
);
};
diff --git a/web/packages/new/photos/components/AssignPersonDialog.tsx b/web/packages/new/photos/components/AssignPersonDialog.tsx
new file mode 100644
index 0000000000..d6c39bf746
--- /dev/null
+++ b/web/packages/new/photos/components/AssignPersonDialog.tsx
@@ -0,0 +1,78 @@
+import {
+ Dialog,
+ DialogContent,
+ DialogTitle,
+ styled,
+ Typography,
+ useMediaQuery,
+} from "@mui/material";
+import { SpacedRow } from "ente-base/components/containers";
+import { DialogCloseIconButton } from "ente-base/components/mui/DialogCloseIconButton";
+import type { ModalVisibilityProps } from "ente-base/components/utils/modal";
+import type { Person } from "ente-new/photos/services/ml/people";
+import React from "react";
+import { ItemCard, LargeTileButton, LargeTileTextOverlay } from "./Tiles";
+
+export type AssignPersonDialogProps = ModalVisibilityProps & {
+ /**
+ * Existing named people that can be selected.
+ */
+ people: Person[];
+ /**
+ * Title to show on the dialog.
+ */
+ title: string;
+ /**
+ * Called when the user selects a person.
+ */
+ onSelectPerson: (personID: string) => void;
+};
+
+/**
+ * A dialog that allows selecting an existing person (cgroup) to associate
+ * something (e.g. file(s)) with.
+ */
+export const AssignPersonDialog: React.FC<AssignPersonDialogProps> = ({
+ open,
+ onClose,
+ people,
+ title,
+ onSelectPerson,
+}) => {
+ const isFullScreen = useMediaQuery("(max-width: 490px)");
+
+ return (
+ <Dialog
+ {...{ open, onClose }}
+ fullWidth
+ fullScreen={isFullScreen}
+ slotProps={{ paper: { sx: { maxWidth: "490px" } } }}
+ >
+ <SpacedRow sx={{ padding: "10px 8px 6px 0" }}>
+ <DialogTitle variant="h3">{title}</DialogTitle>
+ <DialogCloseIconButton {...{ onClose }} />
+ </SpacedRow>
+ <DialogContent_>
+ {people.map((person) => (
+ <ItemCard
+ key={person.id}
+ TileComponent={LargeTileButton}
+ coverFile={person.displayFaceFile}
+ coverFaceID={person.displayFaceID}
+ onClick={() => onSelectPerson(person.id)}
+ >
+ <LargeTileTextOverlay>
+ <Typography>{person.name ?? ""}</Typography>
+ </LargeTileTextOverlay>
+ </ItemCard>
+ ))}
+ </DialogContent_>
+ </Dialog>
+ );
+};
+
+const DialogContent_ = styled(DialogContent)`
+ display: flex;
+ flex-wrap: wrap;
+ gap: 4px;
+`;
diff --git a/web/packages/new/photos/components/PeopleList.tsx b/web/packages/new/photos/components/PeopleList.tsx
index 94f7c7ed6a..bdf51a1934 100644
--- a/web/packages/new/photos/components/PeopleList.tsx
+++ b/web/packages/new/photos/components/PeopleList.tsx
@@ -1,3 +1,4 @@
+import PersonAddIcon from "@mui/icons-material/PersonAdd";
import { Skeleton, styled, Typography } from "@mui/material";
import { useIsSmallWidth } from "ente-base/components/utils/hooks";
import type { EnteFile } from "ente-media/file";
@@ -85,6 +86,27 @@ export interface FilePeopleListProps {
* Called when the user selects a face in the list.
*/
onSelectFace: (annotatedFaceID: AnnotatedFaceID) => void;
+ /**
+ * People that were manually associated with the file, and should be shown
+ * in addition to detected faces.
+ */
+ manuallyAssignedPeople?: Person[];
+ /**
+ * Called when the user selects a person (used for manually assigned people).
+ */
+ onSelectPerson?: (personID: string) => void;
+ /**
+ * If set, show an option to manually associate the file with a person.
+ */
+ onAddPerson?: () => void;
+ /**
+ * Title/tooltip for the "add person" button (if shown).
+ */
+ addPersonTitle?: string;
+ /**
+ * Label shown under the "add person" button (if shown).
+ */
+ addPersonLabel?: string;
}
/**
@@ -95,6 +117,11 @@ export const FilePeopleList: React.FC<FilePeopleListProps> = ({
file,
annotatedFaceIDs,
onSelectFace,
+ manuallyAssignedPeople,
+ onSelectPerson,
+ onAddPerson,
+ addPersonTitle,
+ addPersonLabel,
}) => (
<FilePeopleList_>
{annotatedFaceIDs.map((annotatedFaceID) => (
@@ -112,6 +139,35 @@ export const FilePeopleList: React.FC<FilePeopleListProps> = ({
</Typography>
</AnnotatedFaceButton>
))}
+ {manuallyAssignedPeople?.map((person) => (
+ <ManuallyAssignedPersonButton
+ key={person.id}
+ onClick={() => onSelectPerson?.(person.id)}
+ >
+ <FaceCropImageView
+ faceID={person.displayFaceID}
+ file={person.displayFaceFile}
+ placeholderDimension={65}
+ />
+ <Typography variant="small" sx={{ color: "text.muted" }}>
+ {person.name}
+ </Typography>
+ </ManuallyAssignedPersonButton>
+ ))}
+ {onAddPerson && (
+ <AddPersonButton
+ onClick={onAddPerson}
+ title={addPersonTitle}
+ aria-label={addPersonTitle}
+ >
+ <AddPersonAvatar>
+ <PersonAddIcon />
+ </AddPersonAvatar>
+ <Typography variant="small" sx={{ color: "text.muted" }}>
+ {addPersonLabel}
+ </Typography>
+ </AddPersonButton>
+ )}
</FilePeopleList_>
);
@@ -141,6 +197,37 @@ const AnnotatedFaceButton = styled(UnstyledButton)(
`,
);
+const ManuallyAssignedPersonButton = AnnotatedFaceButton;
+
+const AddPersonButton = styled(UnstyledButton)(
+ ({ theme }) => `
+ width: 65px;
+ display: flex;
+ flex-direction: column;
+ gap: 2px;
+ & > div {
+ width: 100%;
+ aspect-ratio: 1;
+ border-radius: 50%;
+ overflow: hidden;
+ }
+ & > div:hover {
+ outline: 1px solid ${theme.vars.palette.stroke.faint};
+ outline-offset: 2px;
+ }
+`,
+);
+
+const AddPersonAvatar = styled("div")(
+ ({ theme }) => `
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ color: ${theme.vars.palette.text.muted};
+ border: 1px dashed ${theme.vars.palette.stroke.faint};
+`,
+);
+
export interface SuggestionFaceListProps {
/**
* Faces, each annotated with the corresponding {@link EnteFile}, to show in
diff --git a/web/packages/new/photos/components/SelectedFileOptions.tsx b/web/packages/new/photos/components/SelectedFileOptions.tsx
index 081235a9ba..fff9d3efaf 100644
--- a/web/packages/new/photos/components/SelectedFileOptions.tsx
+++ b/web/packages/new/photos/components/SelectedFileOptions.tsx
@@ -6,6 +6,7 @@ import CloseIcon from "@mui/icons-material/Close";
import DeleteIcon from "@mui/icons-material/Delete";
import DownloadIcon from "@mui/icons-material/Download";
import FavoriteBorderIcon from "@mui/icons-material/FavoriteBorderRounded";
+import PersonAddIcon from "@mui/icons-material/PersonAdd";
import RemoveIcon from "@mui/icons-material/RemoveCircleOutline";
import RestoreIcon from "@mui/icons-material/Restore";
import UnArchiveIcon from "@mui/icons-material/Unarchive";
@@ -14,15 +15,21 @@ import VisibilityOutlinedIcon from "@mui/icons-material/VisibilityOutlined";
import { IconButton, Tooltip, Typography } from "@mui/material";
import { SpacedRow } from "ente-base/components/containers";
import type { ButtonishProps } from "ente-base/components/mui";
+import { useModalVisibility } from "ente-base/components/utils/modal";
import { useBaseContext } from "ente-base/context";
import type { Collection } from "ente-media/collection";
+import { AssignPersonDialog } from "ente-new/photos/components/AssignPersonDialog";
import type { CollectionSelectorAttributes } from "ente-new/photos/components/CollectionSelector";
import type { GalleryBarMode } from "ente-new/photos/components/gallery/reducer";
+import { usePeopleStateSnapshot } from "ente-new/photos/components/utils/use-snapshot";
import {
PseudoCollectionID,
type CollectionSummary,
} from "ente-new/photos/services/collection-summary";
+import { isMLEnabled } from "ente-new/photos/services/ml";
import { t } from "i18next";
+import { useMemo } from "react";
+import { useWrapAsyncOperation } from "./utils/use-wrap-async";
/**
* Operations on selected files.
@@ -133,6 +140,11 @@ interface SelectedFileOptionsProps {
* @returns
*/
createFileOpHandler: (op: FileOp) => () => void;
+ /**
+ * If set, then an option to manually associate the selected file(s) with a
+ * person will be shown (when ML is enabled and there are named people).
+ */
+ onAddPersonToSelectedFiles?: (personID: string) => Promise<void>;
}
/**
@@ -152,9 +164,31 @@ export const SelectedFileOptions: React.FC<SelectedFileOptionsProps> = ({
createOnCreateForCollectionOp,
createOnSelectForCollectionOp,
createFileOpHandler,
+ onAddPersonToSelectedFiles,
}) => {
const { showMiniDialog } = useBaseContext();
+ const peopleState = usePeopleStateSnapshot();
+ const namedPeople = useMemo(
+ () =>
+ (peopleState?.visiblePeople ?? []).filter(
+ (p) => p.type == "cgroup" && !!p.name,
+ ),
+ [peopleState],
+ );
+ const shouldShowAddPerson =
+ !!onAddPersonToSelectedFiles && isMLEnabled() && namedPeople.length > 0;
+
+ const { show: showAssignPerson, props: assignPersonVisibilityProps } =
+ useModalVisibility();
+ const handleSelectPerson = useWrapAsyncOperation(
+ async (personID: string) => {
+ assignPersonVisibilityProps.onClose();
+ if (!onAddPersonToSelectedFiles) return;
+ await onAddPersonToSelectedFiles(personID);
+ },
+ );
+
const isUserFavorites =
!!collectionSummary?.attributes.has("userFavorites");
@@ -280,96 +314,133 @@ export const SelectedFileOptions: React.FC<SelectedFileOptionsProps> = ({
};
return (
- <SpacedRow sx={{ flex: 1, gap: 1, flexWrap: "wrap" }}>
- <IconButton onClick={onClearSelection}>
- <CloseIcon />
- </IconButton>
- <Typography sx={{ mr: "auto" }}>
- {selectedFileCount == selectedOwnFileCount
- ? t("selected_count", { selected: selectedFileCount })
- : t("selected_and_yours_count", {
- selected: selectedFileCount,
- yours: selectedOwnFileCount,
- })}
- </Typography>
-
- {isInSearchMode ? (
- <>
- <FavoriteButton onClick={handleFavorite} />
- <FixTimeButton onClick={handleFixTime} />
- <DownloadButton onClick={handleDownload} />
- <AddToCollectionButton onClick={handleAddToCollection} />
- <ArchiveButton onClick={handleArchive} />
- <HideButton onClick={handleHide} />
- <DeleteButton onClick={handleDelete} />
- </>
- ) : barMode == "people" ? (
- <>
- <FavoriteButton onClick={handleFavorite} />
- <DownloadButton onClick={handleDownload} />
- <AddToCollectionButton onClick={handleAddToCollection} />
- <ArchiveButton onClick={handleArchive} />
- <HideButton onClick={handleHide} />
- <DeleteButton onClick={handleDelete} />
- </>
- ) : collectionSummary?.id == PseudoCollectionID.trash ? (
- <>
- <RestoreButton onClick={handleRestore} />
- <DeletePermanentlyButton
- onClick={handleDeletePermanently}
- />
- </>
- ) : collectionSummary?.attributes.has("uncategorized") ? (
- <>
- <DownloadButton onClick={handleDownload} />
- <MoveToCollectionButton onClick={handleMoveToCollection} />
- <DeleteButton onClick={handleDelete} />
- </>
- ) : collectionSummary?.attributes.has("sharedIncoming") ? (
- <>
- <DownloadButton onClick={handleDownload} />
- <RemoveFromCollectionButton
- onClick={handleRemoveFromCollection}
- />
- </>
- ) : barMode == "hidden-albums" ? (
- <>
- <DownloadButton onClick={handleDownload} />
- <UnhideButton onClick={handleUnhide} />
- <DeleteButton onClick={handleDelete} />
- </>
- ) : (
- <>
- {!isUserFavorites &&
- collectionSummary?.id !=
- PseudoCollectionID.archiveItems && (
- <FavoriteButton onClick={handleFavorite} />
+ <>
+ <SpacedRow sx={{ flex: 1, gap: 1, flexWrap: "wrap" }}>
+ <IconButton onClick={onClearSelection}>
+ <CloseIcon />
+ </IconButton>
+ <Typography sx={{ mr: "auto" }}>
+ {selectedFileCount == selectedOwnFileCount
+ ? t("selected_count", { selected: selectedFileCount })
+ : t("selected_and_yours_count", {
+ selected: selectedFileCount,
+ yours: selectedOwnFileCount,
+ })}
+ </Typography>
+
+ {isInSearchMode ? (
+ <>
+ <FavoriteButton onClick={handleFavorite} />
+ <FixTimeButton onClick={handleFixTime} />
+ <DownloadButton onClick={handleDownload} />
+ <AddToCollectionButton
+ onClick={handleAddToCollection}
+ />
+ {shouldShowAddPerson && (
+ <AddPersonButton onClick={showAssignPerson} />
)}
- <FixTimeButton onClick={handleFixTime} />
- <DownloadButton onClick={handleDownload} />
- <AddToCollectionButton onClick={handleAddToCollection} />
- {collectionSummary?.id === PseudoCollectionID.all ? (
<ArchiveButton onClick={handleArchive} />
- ) : collectionSummary?.id ==
- PseudoCollectionID.archiveItems ? (
- <UnarchiveButton onClick={handleUnarchive} />
- ) : (
- !isUserFavorites && (
- <>
- <MoveToCollectionButton
- onClick={handleMoveToCollection}
- />
- <RemoveFromCollectionButton
- onClick={handleRemoveFromCollection}
- />
- </>
- )
- )}
- <HideButton onClick={handleHide} />
- <DeleteButton onClick={handleDelete} />
- </>
+ <HideButton onClick={handleHide} />
+ <DeleteButton onClick={handleDelete} />
+ </>
+ ) : barMode == "people" ? (
+ <>
+ <FavoriteButton onClick={handleFavorite} />
+ <DownloadButton onClick={handleDownload} />
+ <AddToCollectionButton
+ onClick={handleAddToCollection}
+ />
+ {shouldShowAddPerson && (
+ <AddPersonButton onClick={showAssignPerson} />
+ )}
+ <ArchiveButton onClick={handleArchive} />
+ <HideButton onClick={handleHide} />
+ <DeleteButton onClick={handleDelete} />
+ </>
+ ) : collectionSummary?.id == PseudoCollectionID.trash ? (
+ <>
+ <RestoreButton onClick={handleRestore} />
+ <DeletePermanentlyButton
+ onClick={handleDeletePermanently}
+ />
+ </>
+ ) : collectionSummary?.attributes.has("uncategorized") ? (
+ <>
+ <DownloadButton onClick={handleDownload} />
+ {shouldShowAddPerson && (
+ <AddPersonButton onClick={showAssignPerson} />
+ )}
+ <MoveToCollectionButton
+ onClick={handleMoveToCollection}
+ />
+ <DeleteButton onClick={handleDelete} />
+ </>
+ ) : collectionSummary?.attributes.has("sharedIncoming") ? (
+ <>
+ <DownloadButton onClick={handleDownload} />
+ {shouldShowAddPerson && (
+ <AddPersonButton onClick={showAssignPerson} />
+ )}
+ <RemoveFromCollectionButton
+ onClick={handleRemoveFromCollection}
+ />
+ </>
+ ) : barMode == "hidden-albums" ? (
+ <>
+ <DownloadButton onClick={handleDownload} />
+ {shouldShowAddPerson && (
+ <AddPersonButton onClick={showAssignPerson} />
+ )}
+ <UnhideButton onClick={handleUnhide} />
+ <DeleteButton onClick={handleDelete} />
+ </>
+ ) : (
+ <>
+ {!isUserFavorites &&
+ collectionSummary?.id !=
+ PseudoCollectionID.archiveItems && (
+ <FavoriteButton onClick={handleFavorite} />
+ )}
+ <FixTimeButton onClick={handleFixTime} />
+ <DownloadButton onClick={handleDownload} />
+ <AddToCollectionButton
+ onClick={handleAddToCollection}
+ />
+ {shouldShowAddPerson && (
+ <AddPersonButton onClick={showAssignPerson} />
+ )}
+ {collectionSummary?.id === PseudoCollectionID.all ? (
+ <ArchiveButton onClick={handleArchive} />
+ ) : collectionSummary?.id ==
+ PseudoCollectionID.archiveItems ? (
+ <UnarchiveButton onClick={handleUnarchive} />
+ ) : (
+ !isUserFavorites && (
+ <>
+ <MoveToCollectionButton
+ onClick={handleMoveToCollection}
+ />
+ <RemoveFromCollectionButton
+ onClick={handleRemoveFromCollection}
+ />
+ </>
+ )
+ )}
+ <HideButton onClick={handleHide} />
+ <DeleteButton onClick={handleDelete} />
+ </>
+ )}
+ </SpacedRow>
+
+ {shouldShowAddPerson && (
+ <AssignPersonDialog
+ {...assignPersonVisibilityProps}
+ people={namedPeople}
+ title={t("add_a_person")}
+ onSelectPerson={handleSelectPerson}
+ />
)}
- </SpacedRow>
+ </>
);
};
@@ -461,6 +532,14 @@ const AddToCollectionButton: React.FC<ButtonishProps> = ({ onClick }) => (
</Tooltip>
);
+const AddPersonButton: React.FC<ButtonishProps> = ({ onClick }) => (
+ <Tooltip title={t("add_a_person")}>
+ <IconButton {...{ onClick }}>
+ <PersonAddIcon />
+ </IconButton>
+ </Tooltip>
+);
+
const MoveToCollectionButton: React.FC<ButtonishProps> = ({ onClick }) => (
<Tooltip title={t("move")}>
<IconButton {...{ onClick }}>
diff --git a/web/packages/new/photos/services/ml/index.ts b/web/packages/new/photos/services/ml/index.ts
index d0f91cce78..7257929320 100644
--- a/web/packages/new/photos/services/ml/index.ts
+++ b/web/packages/new/photos/services/ml/index.ts
@@ -20,6 +20,7 @@ import { setSearchPeople } from "../search";
import {
addUserEntity,
pullUserEntities,
+ savedCGroups,
updateOrCreateUserEntities,
type CGroup,
} from "../user-entity";
@@ -32,6 +33,7 @@ import {
savedFaceIndex,
savedIndexCounts,
} from "./db";
+import { fileIDFromFaceID } from "./face";
import {
_applyPersonSuggestionUpdates,
filterNamedPeople,
@@ -865,6 +867,88 @@ export const pinCGroup = async (cgroup: CGroup) =>
export const unpinCGroup = async (cgroup: CGroup) =>
setCGroupPinned(cgroup, false);
+export interface ManualPersonAssignmentResult {
+ /**
+ * File IDs that were added to the person's manual assignment list.
+ */
+ addedFileIDs: number[];
+ /**
+ * File IDs that were already associated with the person (either via a
+ * detected face or via an existing manual assignment).
+ */
+ alreadyAssignedFileIDs: number[];
+}
+
+/**
+ * Manually associate the given {@link fileIDs} with a remote cgroup ("person").
+ *
+ * The manual assignment is stored in the cgroup user entity data as an
+ * unordered set of file IDs ({@link CGroupUserEntityData.manuallyAssigned}).
+ */
+export const addManualFileAssignmentsToPerson = async (
+ personID: string,
+ fileIDs: Iterable<number>,
+): Promise<ManualPersonAssignmentResult> => {
+ if (!isMLEnabled()) return { addedFileIDs: [], alreadyAssignedFileIDs: [] };
+
+ const uniqueFileIDs = [...new Set(fileIDs)].filter(
+ (id) => typeof id == "number" && !isNaN(id),
+ );
+ if (!uniqueFileIDs.length)
+ return { addedFileIDs: [], alreadyAssignedFileIDs: [] };
+
+ const cgroup = (await savedCGroups()).find((c) => c.id == personID);
+ if (!cgroup)
+ throw new Error(
+ `addManualFileAssignmentsToPerson: missing cgroup ${personID}`,
+ );
+
+ // File IDs already associated with the person via detected faces.
+ const existingClusterFileIDs = new Set<number>();
+ for (const cluster of cgroup.data.assigned) {
+ for (const faceID of cluster.faces) {
+ const fileID = fileIDFromFaceID(faceID);
+ if (fileID) existingClusterFileIDs.add(fileID);
+ }
+ }
+
+ const manualFileIDs = new Set(cgroup.data.manuallyAssigned);
+
+ const addedFileIDs: number[] = [];
+ const alreadyAssignedFileIDs: number[] = [];
+ for (const id of uniqueFileIDs) {
+ if (existingClusterFileIDs.has(id) || manualFileIDs.has(id)) {
+ alreadyAssignedFileIDs.push(id);
+ } else {
+ addedFileIDs.push(id);
+ }
+ }
+
+ if (!addedFileIDs.length) return { addedFileIDs, alreadyAssignedFileIDs };
+
+ const masterKey = await ensureMasterKeyFromSession();
+ await updateOrCreateUserEntities(
+ "cgroup",
+ [
+ {
+ ...cgroup,
+ data: {
+ ...cgroup.data,
+ manuallyAssigned: [...manualFileIDs, ...addedFileIDs],
+ },
+ },
+ ],
+ masterKey,
+ );
+
+ // Pull the updated entity to keep local updatedAt state in sync, and
+ // reconstruct people state to reflect the new manual associations.
+ await pullUserEntities("cgroup", masterKey);
+ await updatePeopleState();
+
+ return { addedFileIDs, alreadyAssignedFileIDs };
+};
+
/**
* Return suggestions for the given {@link person}.
*
diff --git a/web/packages/new/photos/services/ml/people.ts b/web/packages/new/photos/services/ml/people.ts
index 3020497057..213a747912 100644
--- a/web/packages/new/photos/services/ml/people.ts
+++ b/web/packages/new/photos/services/ml/people.ts
@@ -72,6 +72,14 @@ export interface CGroupUserEntityData {
* should conceptually be thought of as a set.
*/
rejectedFaceIDs: string[];
+ /**
+ * An unordered set of file IDs that the user has manually tagged as
+ * belonging to this group.
+ *
+ * For ease of transportation and persistence this is an array, but it
+ * should conceptually be thought of as a set.
+ */
+ manuallyAssigned: number[];
/**
* True if this cluster group should be hidden.
*
@@ -304,8 +312,14 @@ export const reconstructPeopleState = async (): Promise<PeopleState> => {
const mostRecentFace = faces[0];
if (!mostRecentFace) return undefined;
- // IDs of the files containing this face.
- const fileIDs = [...new Set(faces.map((f) => f.file.id))];
+ // IDs of the files containing this face, plus any files manually
+ // associated with this person.
+ const fileIDsSet = new Set(faces.map((f) => f.file.id));
+ for (const fileID of data.manuallyAssigned) {
+ // Only include normal (non-hidden, non-deleted) files.
+ if (fileByID.has(fileID)) fileIDsSet.add(fileID);
+ }
+ const fileIDs = [...fileIDsSet];
// Avatar face ID, or the highest scoring face.
let avatarFile: EnteFile | undefined;
diff --git a/web/packages/new/photos/services/user-entity/index.ts b/web/packages/new/photos/services/user-entity/index.ts
index ae765758ee..6a4dea2ce0 100644
--- a/web/packages/new/photos/services/user-entity/index.ts
+++ b/web/packages/new/photos/services/user-entity/index.ts
@@ -93,6 +93,7 @@ const RemoteCGroupData = z.looseObject({
),
),
rejectedFaceIDs: z.array(z.string()).nullish().transform(nullishToEmpty),
+ manuallyAssigned: z.array(z.number()).nullish().transform(nullishToEmpty),
isHidden: z.boolean(),
avatarFaceID: z.string().nullish().transform(nullToUndefined),
isPinned: z.boolean().nullish().transform(Boolean),