π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
feat(photos):list-places-with-rust-location
Aswin Asok3544e2f662f9
diff --git a/.github/scripts/ci/paths.json b/.github/scripts/ci/paths.jsonindex 626a8ca041..b5a79ae0e1 100644--- a/.github/scripts/ci/paths.json+++ b/.github/scripts/ci/paths.json@@ -155,6 +155,7 @@"rust/crates/core/**","rust/crates/ensu-crypto/**","rust/crates/legacy/**",+ "rust/crates/location/**","rust/crates/locker/**","rust/crates/paste/**","rust/crates/space/**",diff --git a/rust/bindings/wasm/photos/Cargo.toml b/rust/bindings/wasm/photos/Cargo.tomlindex 605fceb362..4f18be74f5 100644--- a/rust/bindings/wasm/photos/Cargo.toml+++ b/rust/bindings/wasm/photos/Cargo.toml@@ -6,7 +6,7 @@ edition.workspace = truecrate-type = ["cdylib"][dependencies]-ente-wasm-lib = { workspace = true, features = ["cast", "collections", "contacts", "crypto"] }+ente-wasm-lib = { workspace = true, features = ["cast", "collections", "contacts", "crypto", "location"] }[lints]workspace = truediff --git a/web/apps/photos/public/images/places/your-map.png b/web/apps/photos/public/images/places/your-map.pngnew file mode 100644index 0000000000..f94b96aa76Binary files /dev/null and b/web/apps/photos/public/images/places/your-map.png differdiff --git a/web/apps/photos/src/components/collections/CollectionMap.tsx b/web/apps/photos/src/components/collections/CollectionMap.tsxindex 2d93439c29..08f4da0bcb 100644--- a/web/apps/photos/src/components/collections/CollectionMap.tsx+++ b/web/apps/photos/src/components/collections/CollectionMap.tsx@@ -1251,7 +1251,18 @@ function CollectionSidebar({const noOpSetSelected = useCallback(() => undefined, []);return (- <SidebarWrapper>+ <SidebarWrapper+ sx={+ collectionSummary.type === "all"+ ? {+ left: { md: 24 },+ top: { md: 24 },+ bottom: { md: 24 },+ width: { md: 420 },+ }+ : undefined+ }+ ><SidebarContainer><Stack sx={{ px: "20px", pt: "20px", pb: "20px", gap: "2px" }}><Typography@@ -1265,7 +1276,7 @@ function CollectionSidebar({}}>{collectionSummary.type === "all"- ? t("map")+ ? t("your_map"): collectionSummary.name}</Typography><Typographydiff --git a/web/apps/photos/src/components/gallery/BarImpl.tsx b/web/apps/photos/src/components/gallery/BarImpl.tsxindex 163f30657f..6721ad8474 100644--- a/web/apps/photos/src/components/gallery/BarImpl.tsx+++ b/web/apps/photos/src/components/gallery/BarImpl.tsx@@ -33,6 +33,7 @@ import {useTheme,} from "@mui/material";import type { ModalVisibilityProps } from "ente-base/components/utils/modal";+import type { EnteFile } from "ente-media/file";import { ItemCard } from "ente-new/photos/components/Tiles";import {PseudoCollectionID,@@ -58,6 +59,7 @@ import {areEqual,type ListChildComponentProps,} from "react-window";+import { PlacesList } from "./PlacesList";import type { GalleryBarMode } from "./reducer";export interface GalleryBarPresentationProps {@@ -67,6 +69,7 @@ export interface GalleryBarPresentationProps {export interface GalleryBarImplProps extends GalleryBarPresentationProps {mode: GalleryBarMode;+ placesFiles?: EnteFile[];albumFilter: AlbumFilter;albumSearchQuery: string;onAlbumSearchQueryChange: (query: string) => void;@@ -94,6 +97,7 @@ export interface GalleryBarImplProps extends GalleryBarPresentationProps {export const GalleryBarImpl: React.FC<GalleryBarImplProps> = ({mode,+ placesFiles,albumFilter,albumSearchQuery,onAlbumSearchQueryChange,@@ -223,16 +227,17 @@ export const GalleryBarImpl: React.FC<GalleryBarImplProps> = ({],);- const title =- mode == "people"- ? t("people")- : mode == "links"- ? t("links")- : mode == "hidden-albums"- ? t("hidden_albums")- : mode == "archive-albums"- ? t("section_archive")- : t("albums");+ const title = placesFiles+ ? t("search_places")+ : mode == "people"+ ? t("people")+ : mode == "links"+ ? t("links")+ : mode == "hidden-albums"+ ? t("hidden_albums")+ : mode == "archive-albums"+ ? t("section_archive")+ : t("albums");const albumShortcuts = [{@@ -501,7 +506,11 @@ export const GalleryBarImpl: React.FC<GalleryBarImplProps> = ({minHeight: 0,px: mobile ? "12px" : "16px",py: mobile ? "14px" : 0,- borderLeft: mobile ? 0 : "2px solid",+ borderLeft: mobile+ ? 0+ : placesFiles+ ? "1px solid"+ : "2px solid",borderColor: "rgba(224, 224, 224, 0.5)",gap: "16px",letterSpacing: 0,@@ -516,7 +525,7 @@ export const GalleryBarImpl: React.FC<GalleryBarImplProps> = ({sx={{alignItems: "center",flexShrink: 0,- height: 38,+ height: placesFiles ? 32 : 38,gap: 0.5,"& .MuiIconButton-root": {width: 38,@@ -541,7 +550,7 @@ export const GalleryBarImpl: React.FC<GalleryBarImplProps> = ({lineHeight: mobile ? "26px" : "32px",}}>- {mode == "people" ? (+ {mode == "people" && !placesFiles ? (<ButtonBaseonClick={() => {setPeopleSearchQuery("");@@ -566,33 +575,35 @@ export const GalleryBarImpl: React.FC<GalleryBarImplProps> = ({title)}</Typography>- {mode != "people" ? (- <CollectionsSortOptions- variant="v2"- activeSortBy={collectionsSortBy}- onChangeSortBy={onChangeCollectionsSortBy}- />- ) : (- <PeopleSortOptions- variant="v2"- activeSortBy={peopleSortBy}- onChangeSortBy={onChangePeopleSortBy}- />- )}- {(mode == "albums" || mode == "hidden-albums") && (- <Tooltip title={t("new_album")}>- <IconButton- aria-label={t("new_album")}- aria-haspopup="dialog"- onClick={() => {- mobileDrawerProps.onClose();- onCreateAlbum();- }}- >- <AddIcon sx={{ fontSize: 20 }} />- </IconButton>- </Tooltip>- )}+ {!placesFiles &&+ (mode != "people" ? (+ <CollectionsSortOptions+ variant="v2"+ activeSortBy={collectionsSortBy}+ onChangeSortBy={onChangeCollectionsSortBy}+ />+ ) : (+ <PeopleSortOptions+ variant="v2"+ activeSortBy={peopleSortBy}+ onChangeSortBy={onChangePeopleSortBy}+ />+ ))}+ {!placesFiles &&+ (mode == "albums" || mode == "hidden-albums") && (+ <Tooltip title={t("new_album")}>+ <IconButton+ aria-label={t("new_album")}+ aria-haspopup="dialog"+ onClick={() => {+ mobileDrawerProps.onClose();+ onCreateAlbum();+ }}+ >+ <AddIcon sx={{ fontSize: 20 }} />+ </IconButton>+ </Tooltip>+ )}{mobile && (<IconButtonaria-label={t("close")}@@ -607,6 +618,7 @@ export const GalleryBarImpl: React.FC<GalleryBarImplProps> = ({sx={{alignItems: "center",flexShrink: 0,+ display: placesFiles ? "none" : "flex",height: 52,gap: "10px",px: "16px",@@ -666,10 +678,12 @@ export const GalleryBarImpl: React.FC<GalleryBarImplProps> = ({</IconButton>)}</Stack>- {isSearching &&- (mode == "albums"- ? !visibleAlbumFilters.length && !albumShortcuts.length- : !getItemCount(itemData)) ? (+ {placesFiles ? (+ <PlacesList files={placesFiles} />+ ) : isSearching &&+ (mode == "albums"+ ? !visibleAlbumFilters.length && !albumShortcuts.length+ : !getItemCount(itemData)) ? (searchEmptyState) : mode == "albums" ? (<Boxdiff --git a/web/apps/photos/src/components/gallery/PhotosNavigationShell.tsx b/web/apps/photos/src/components/gallery/PhotosNavigationShell.tsxindex fc2968906f..a9c9afaca2 100644--- a/web/apps/photos/src/components/gallery/PhotosNavigationShell.tsx+++ b/web/apps/photos/src/components/gallery/PhotosNavigationShell.tsx@@ -123,7 +123,7 @@ export const PhotosNavigationShell: React.FC<};useEffect(() => {setNavigationCollapsed(false);- }, [props.mode, props.isInSearchMode]);+ }, [props.mode, props.isInSearchMode, props.isMapSelected]);useEffect(() => {if (!mobile) closeMobileNavigation();}, [mobile, closeMobileNavigation]);@@ -240,6 +240,11 @@ export const PhotosNavigationShell: React.FC<{alternateHeader == null && (<NormalNavigationRail{...props}+ onShowMap={() => {+ props.onShowMap();+ setNavigationCollapsed(false);+ if (mobile) showMobileNavigation();+ }}isHome={!props.isMapSelected &&props.mode == "albums" &&diff --git a/web/apps/photos/src/components/gallery/PlacesList.tsx b/web/apps/photos/src/components/gallery/PlacesList.tsxnew file mode 100644index 0000000000..dd143a759d--- /dev/null+++ b/web/apps/photos/src/components/gallery/PlacesList.tsx@@ -0,0 +1,126 @@+import { Box, List, ListItem, Stack, Typography, styled } from "@mui/material";+import { ActivityIndicator } from "ente-base/components/mui/ActivityIndicator";+import log from "ente-base/log";+import type { EnteFile } from "ente-media/file";+import { ItemCard, PreviewItemTile } from "ente-new/photos/components/Tiles";+import { t } from "i18next";+import { useEffect, useState } from "react";+import { listPlaces } from "../../services/places";++export const PlacesList = ({ files }: { files: EnteFile[] }) => {+ const [result, setResult] = useState<{+ files: EnteFile[];+ groups?: Awaited<ReturnType<typeof listPlaces>>;+ }>();++ useEffect(() => {+ let cancelled = false;+ void listPlaces(files)+ .then((groups) => {+ if (!cancelled) setResult({ files, groups });+ })+ .catch((error: unknown) => {+ log.error("Failed to list places", error);+ if (!cancelled) setResult({ files });+ });+ return () => {+ cancelled = true;+ };+ }, [files]);++ const currentResult = result?.files === files ? result : undefined;++ return (+ <Stack sx={{ flex: 1, minHeight: 0, gap: "8px" }}>+ <Stack+ direction="row"+ aria-current="page"+ sx={{+ ...placeRowSx,+ flexShrink: 0,+ bgcolor: "var(--gallery-navigation-surface)",+ }}+ >+ <Box+ component="img"+ src="/images/places/your-map.png"+ alt=""+ sx={{+ width: 36,+ height: 36,+ flexShrink: 0,+ borderRadius: "12px",+ }}+ />+ <Typography sx={placeNameSx}>{t("your_map")}</Typography>+ </Stack>+ {!currentResult ? (+ <Stack role="status" aria-label={t("loading")} sx={{ p: 2 }}>+ <ActivityIndicator />+ </Stack>+ ) : !currentResult.groups ? (+ <Typography role="alert">{t("generic_error_retry")}</Typography>+ ) : !currentResult.groups.length ? (+ <Typography role="status">{t("no_results")}</Typography>+ ) : (+ <List+ disablePadding+ aria-label={t("search_places")}+ sx={{+ flex: 1,+ minHeight: 0,+ overflow: "auto",+ display: "flex",+ flexDirection: "column",+ gap: "8px",+ }}+ >+ {currentResult.groups.map(({ city, coverFile }) => (+ <ListItem+ key={`${city.name}-${city.country}-${city.latitude}-${city.longitude}`}+ disableGutters+ title={`${city.name}, ${city.country}`}+ sx={placeRowSx}+ >+ <Box aria-hidden sx={{ flexShrink: 0 }}>+ <ItemCard+ coverFile={coverFile}+ TileComponent={PlaceCover}+ />+ </Box>+ <Typography noWrap sx={placeNameSx}>+ {city.name}+ </Typography>+ </ListItem>+ ))}+ </List>+ )}+ </Stack>+ );+};++const placeRowSx = {+ height: 48,+ minHeight: 48,+ px: "12px",+ py: 0,+ gap: "12px",+ alignItems: "center",+ borderRadius: "12px",+};++const placeNameSx = {+ flex: 1,+ minWidth: 0,+ fontFamily: '"Inter Variable", Inter, sans-serif',+ fontSize: 14,+ fontWeight: 500,+ lineHeight: "20px",+};++const PlaceCover = styled(PreviewItemTile)({+ width: 36,+ height: 36,+ borderRadius: "12px",+ cursor: "default",+});diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsxindex ae2da004a0..8d7db91967 100644--- a/web/apps/photos/src/pages/gallery.tsx+++ b/web/apps/photos/src/pages/gallery.tsx@@ -2251,11 +2251,15 @@ const Page: React.FC = () => {onSelectPeople={handleShowPeople}onSelectPerson={handleSelectPerson}galleryNavigation={- isCleanupViewOpen ||- (isMapView &&- activeCollectionID === PseudoCollectionID.all)+ isCleanupViewOpen? undefined: {+ placesFiles:+ isMapView &&+ activeCollectionID ===+ PseudoCollectionID.all+ ? filteredFiles+ : undefined,activeCollectionID:galleryBarActiveCollectionID,onSelectCollectionID: selectGalleryCollection,diff --git a/web/apps/photos/src/services/places.ts b/web/apps/photos/src/services/places.tsnew file mode 100644index 0000000000..1a3fe25ebb--- /dev/null+++ b/web/apps/photos/src/services/places.ts@@ -0,0 +1,42 @@+import { HTTPError } from "ente-base/http";+import type { EnteFile } from "ente-media/file";+import { fileLocation } from "ente-media/file-metadata";+import { createLocationIndex } from "ente-photos-wasm";++let locationIndex: ReturnType<typeof createLocationIndex> | undefined;++export const listPlaces = async (files: EnteFile[]) => {+ const locatedFiles = files.flatMap((file) => {+ const coordinate = fileLocation(file);+ return coordinate ? [{ file, coordinate }] : [];+ });+ if (!locatedFiles.length) return [];++ const index = await (locationIndex ??= Promise.all([+ fetchLocationData("cities.bin"),+ fetchLocationData("urban-centres.bin"),+ ])+ .then(([cities, urbanCenters]) =>+ createLocationIndex(cities, urbanCenters),+ )+ .catch((error: unknown) => {+ locationIndex = undefined;+ throw error;+ }));++ return index+ .groupCities({+ coordinates: locatedFiles.map(({ coordinate }) => coordinate),+ })+ .groups.map((group) => ({+ ...group,+ coverFile: locatedFiles[group.coordinateIndices[0] ?? -1]?.file,+ }))+ .sort((a, b) => a.city.name.localeCompare(b.city.name));+};++const fetchLocationData = async (name: string) => {+ const response = await fetch(`https://assets.ente.com/location/v2/${name}`);+ if (!response.ok) throw new HTTPError(response);+ return new Uint8Array(await response.arrayBuffer());+};diff --git a/web/apps/photos/tests/places.test.ts b/web/apps/photos/tests/places.test.tsnew file mode 100644index 0000000000..b620f12ab5--- /dev/null+++ b/web/apps/photos/tests/places.test.ts@@ -0,0 +1,80 @@+import type { EnteFile } from "ente-media/file";+import { createLocationIndex } from "ente-photos-wasm";+import { afterEach, expect, test, vi } from "vitest";++vi.mock("ente-photos-wasm", () => ({ createLocationIndex: vi.fn() }));++afterEach(() => {+ vi.unstubAllGlobals();+ vi.clearAllMocks();+ vi.resetModules();+});++test("loads places only for geotagged files and retries a failed dataset load", async () => {+ const fetch = vi.fn().mockRejectedValue(new Error("offline"));+ vi.stubGlobal("fetch", fetch);+ const { listPlaces } = await import("../src/services/places");+ const withoutLocation = { metadata: {} } as EnteFile;++ expect(await listPlaces([withoutLocation])).toEqual([]);+ expect(fetch).not.toHaveBeenCalled();++ const file = {+ metadata: { latitude: 1, longitude: 2 },+ pubMagicMetadata: { data: { lat: 9.93, long: 76.27 } },+ } as EnteFile;+ await expect(listPlaces([file])).rejects.toThrow("offline");++ fetch.mockImplementation(() =>+ Promise.resolve(new Response(new Uint8Array([1]))),+ );+ const secondFile = {+ metadata: { latitude: 12.97, longitude: 77.59 },+ } as EnteFile;+ const groups = [+ {+ city: {+ name: "Thiruvananthapuram",+ country: "India",+ latitude: 1,+ longitude: 2,+ },+ coordinateIndices: [1],+ },+ {+ city: {+ name: "Kochi",+ country: "India",+ latitude: 1,+ longitude: 2,+ },+ coordinateIndices: [0],+ },+ ];+ const groupCities = vi.fn().mockReturnValue({ groups });+ vi.mocked(createLocationIndex).mockResolvedValue({+ groupCities,+ free: vi.fn(),+ [Symbol.dispose]: vi.fn(),+ });++ const places = await listPlaces([withoutLocation, file, secondFile]);+ expect(places.map(({ city }) => city.name)).toEqual([+ "Kochi",+ "Thiruvananthapuram",+ ]);+ expect(groupCities).toHaveBeenCalledWith({+ coordinates: [+ { latitude: 9.93, longitude: 76.27 },+ { latitude: 12.97, longitude: 77.59 },+ ],+ });+ expect(places.map(({ coverFile }) => coverFile)).toEqual([+ file,+ secondFile,+ ]);++ await listPlaces([file]);+ expect(createLocationIndex).toHaveBeenCalledTimes(1);+ expect(fetch).toHaveBeenCalledTimes(4);+});diff --git a/web/apps/photos/tests/sidebar-album-search.test.tsx b/web/apps/photos/tests/sidebar-album-search.test.tsxindex 07b3e1fd93..7486c480c7 100644--- a/web/apps/photos/tests/sidebar-album-search.test.tsx+++ b/web/apps/photos/tests/sidebar-album-search.test.tsx@@ -57,7 +57,10 @@ vi.mock("i18next", () => {};return { t: (key: string) => translations[key] ?? key };});-vi.mock("ente-new/photos/components/Tiles", () => ({ ItemCard: () => null }));+vi.mock("ente-new/photos/components/Tiles", () => ({+ ItemCard: () => null,+ PreviewItemTile: "div",+}));vi.mock("react-virtualized-auto-sizer", () => ({default: ({children,diff --git a/web/packages/base/locales/en-US/translation.json b/web/packages/base/locales/en-US/translation.jsonindex cccc5f6bd7..6287cc5c09 100644--- a/web/packages/base/locales/en-US/translation.json+++ b/web/packages/base/locales/en-US/translation.json@@ -575,6 +575,7 @@"location": "Location","view_on_map": "View on OpenStreetMap","map": "Map",+ "your_map": "Your map","enable_map": "Enable map","disable_map": "Disable map","details": "Details",diff --git a/web/packages/wasm/photos/index.ts b/web/packages/wasm/photos/index.tsindex 9d36c83830..8ac5fb66c9 100644--- a/web/packages/wasm/photos/index.ts+++ b/web/packages/wasm/photos/index.ts@@ -1,14 +1,25 @@import type {EncryptedBox,+ LocationIndex,OpenSessionInput,Session,WrappedRootContactKey,} from "./pkg/ente_photos_wasm";-export type { OpenSessionInput, Session } from "./pkg/ente_photos_wasm";+export type {+ LocationCityGroup,+ OpenSessionInput,+ Session,+} from "./pkg/ente_photos_wasm";const wasm = () => import("./pkg/ente_photos_wasm");+export const createLocationIndex = async (+ cities: Uint8Array,+ urbanCenters: Uint8Array,+): Promise<LocationIndex> =>+ new (await wasm()).LocationIndex(cities, urbanCenters);+export const openSession = async (input: OpenSessionInput): Promise<Session> =>(await wasm()).openSession(input);