Skip to content

armgit / ente

Public Imported from GitHub
0

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

Fin 2

Manav Rathi90e2dca36b0b
diff --git a/web/apps/photos/src/components/Search/SearchBar/searchInput/index.tsx b/web/apps/photos/src/components/Search/SearchBar/searchInput/index.tsx
index 0f85391ad0..9edc2c9adf 100644
--- a/web/apps/photos/src/components/Search/SearchBar/searchInput/index.tsx
+++ b/web/apps/photos/src/components/Search/SearchBar/searchInput/index.tsx
@@ -2,7 +2,6 @@ import { FileType } from "@/media/file-type";
import { isMLEnabled } from "@/new/photos/services/ml";
import type {
City,
- LocationTagData,
SearchDateComponents,
SearchPerson,
} from "@/new/photos/services/search/types";
@@ -13,6 +12,7 @@ import {
SuggestionType,
UpdateSearch,
} from "@/new/photos/services/search/types";
+import type { LocationTag } from "@/new/photos/services/user-entity";
import { EnteFile } from "@/new/photos/types/file";
import CloseIcon from "@mui/icons-material/Close";
import { IconButton } from "@mui/material";
@@ -126,7 +126,7 @@ export default function SearchInput(props: Iprops) {
break;
case SuggestionType.LOCATION:
search = {
- location: selectedOption.value as LocationTagData,
+ location: selectedOption.value as LocationTag,
};
props.setIsOpen(true);
break;
diff --git a/web/apps/photos/src/services/entityService.ts b/web/apps/photos/src/services/entityService.ts
deleted file mode 100644
index 7bd32e556a..0000000000
--- a/web/apps/photos/src/services/entityService.ts
+++ /dev/null
@@ -1,193 +0,0 @@
-import { decryptMetadataJSON, sharedCryptoWorker } from "@/base/crypto";
-import log from "@/base/log";
-import { apiURL } from "@/base/origins";
-import HTTPService from "@ente/shared/network/HTTPService";
-import localForage from "@ente/shared/storage/localForage";
-import { getToken } from "@ente/shared/storage/localStorage/helpers";
-import { getActualKey } from "@ente/shared/user";
-import {
- EncryptedEntity,
- EncryptedEntityKey,
- Entity,
- EntityKey,
- EntitySyncDiffResponse,
- EntityType,
-} from "types/entity";
-import { getLatestVersionEntities } from "utils/entity";
-
-const DIFF_LIMIT = 500;
-
-const ENTITY_TABLES: Record<EntityType, string> = {
- [EntityType.LOCATION_TAG]: "location_tags",
-};
-
-const ENTITY_KEY_TABLES: Record<EntityType, string> = {
- [EntityType.LOCATION_TAG]: "location_tags_key",
-};
-
-const ENTITY_SYNC_TIME_TABLES: Record<EntityType, string> = {
- [EntityType.LOCATION_TAG]: "location_tags_time",
-};
-
-const getLocalEntity = async <T>(type: EntityType) => {
- const entities: Array<Entity<T>> =
- (await localForage.getItem<Entity<T>[]>(ENTITY_TABLES[type])) || [];
- return entities;
-};
-
-const getEntityLastSyncTime = async (type: EntityType) => {
- return (
- (await localForage.getItem<number>(ENTITY_SYNC_TIME_TABLES[type])) ?? 0
- );
-};
-
-const getCachedEntityKey = async (type: EntityType) => {
- const entityKey: EntityKey =
- (await localForage.getItem<EntityKey>(ENTITY_KEY_TABLES[type])) || null;
- return entityKey;
-};
-
-// TODO: unexport
-export const getEntityKey = async (type: EntityType) => {
- try {
- const entityKey = await getCachedEntityKey(type);
- if (entityKey) {
- return entityKey;
- }
- const token = getToken();
- if (!token) {
- return;
- }
- const resp = await HTTPService.get(
- await apiURL("/user-entity/key"),
- {
- type,
- },
- {
- "X-Auth-Token": token,
- },
- );
- const encryptedEntityKey: EncryptedEntityKey = resp.data;
- const worker = await sharedCryptoWorker();
- const masterKey = await getActualKey();
- const { encryptedKey, header, ...rest } = encryptedEntityKey;
- const decryptedData = await worker.decryptB64(
- encryptedKey,
- header,
- masterKey,
- );
- const decryptedEntityKey: EntityKey = { data: decryptedData, ...rest };
- localForage.setItem(ENTITY_KEY_TABLES[type], decryptedEntityKey);
- return decryptedEntityKey;
- } catch (e) {
- log.error("Get entity key failed", e);
- throw e;
- }
-};
-
-export const getLatestEntities = async <T>(type: EntityType) => {
- try {
- await syncEntity<T>(type);
- return await getLocalEntity<T>(type);
- } catch (e) {
- log.error("Sync entities failed", e);
- throw e;
- }
-};
-
-export const syncEntities = async () => {
- try {
- await syncEntity(EntityType.LOCATION_TAG);
- } catch (e) {
- log.error("Sync entities failed", e);
- throw e;
- }
-};
-
-const syncEntity = async <T>(type: EntityType): Promise<Entity<T>> => {
- try {
- let entities = await getLocalEntity(type);
- log.info(
- `Syncing ${type} entities localEntitiesCount: ${entities.length}`,
- );
- let syncTime = await getEntityLastSyncTime(type);
- log.info(`Syncing ${type} entities syncTime: ${syncTime}`);
- let response: EntitySyncDiffResponse;
- do {
- response = await getEntityDiff(type, syncTime);
- if (!response.diff?.length) {
- return;
- }
-
- const entityKey = await getEntityKey(type);
- // @ts-expect-error TODO: Need to use zod here.
- const newDecryptedEntities: Array<Entity<T>> = await Promise.all(
- response.diff.map(async (entity: EncryptedEntity) => {
- if (entity.isDeleted) {
- // This entry is deleted, so we don't need to decrypt it, just return it as is
- // as unknown as EntityData is a hack to get around the type system
- return entity as unknown as Entity<T>;
- }
- const { encryptedData, header, ...rest } = entity;
- const decryptedData = await decryptMetadataJSON({
- encryptedDataB64: encryptedData,
- decryptionHeaderB64: header,
- keyB64: entityKey.data,
- });
- return {
- ...rest,
- data: decryptedData,
- };
- }),
- );
-
- entities = getLatestVersionEntities([
- ...entities,
- ...newDecryptedEntities,
- ]);
-
- const nonDeletedEntities = entities.filter(
- (entity) => !entity.isDeleted,
- );
-
- if (response.diff.length) {
- syncTime = response.diff.slice(-1)[0].updatedAt;
- }
- await localForage.setItem(ENTITY_TABLES[type], nonDeletedEntities);
- await localForage.setItem(ENTITY_SYNC_TIME_TABLES[type], syncTime);
- log.info(
- `Syncing ${type} entities syncedEntitiesCount: ${nonDeletedEntities.length}`,
- );
- } while (response.diff.length === DIFF_LIMIT);
- } catch (e) {
- log.error("Sync entity failed", e);
- }
-};
-
-const getEntityDiff = async (
- type: EntityType,
- time: number,
-): Promise<EntitySyncDiffResponse> => {
- try {
- const token = getToken();
- if (!token) {
- return;
- }
- const resp = await HTTPService.get(
- await apiURL("/user-entity/entity/diff"),
- {
- sinceTime: time,
- type,
- limit: DIFF_LIMIT,
- },
- {
- "X-Auth-Token": token,
- },
- );
-
- return resp.data;
- } catch (e) {
- log.error("Get entity diff failed", e);
- throw e;
- }
-};
diff --git a/web/apps/photos/src/services/searchService.ts b/web/apps/photos/src/services/searchService.ts
index 8cbadd4efd..14965889d3 100644
--- a/web/apps/photos/src/services/searchService.ts
+++ b/web/apps/photos/src/services/searchService.ts
@@ -16,18 +16,16 @@ import type {
import {
City,
ClipSearchScores,
- LocationTagData,
SearchOption,
SearchQuery,
Suggestion,
SuggestionType,
} from "@/new/photos/services/search/types";
+import type { LocationTag } from "@/new/photos/services/user-entity";
import { EnteFile } from "@/new/photos/types/file";
import { t } from "i18next";
import { Collection } from "types/collection";
-import { EntityType, LocationTag } from "types/entity";
import { getUniqueFiles } from "utils/file";
-import { getLatestEntities } from "./entityService";
// Suggestions shown in the search dropdown's empty state, i.e. when the user
// selects the search bar but does not provide any input.
@@ -56,7 +54,6 @@ export const getAutoCompleteSuggestions =
...getCollectionSuggestion(searchPhrase, collections),
getFileNameSuggestion(searchPhrase, files),
getFileCaptionSuggestion(searchPhrase, files),
- ...(await getLocationSuggestions(searchPhrase)),
].filter((suggestion) => !!suggestion);
return convertSuggestionsToOptions(suggestions);
@@ -242,21 +239,6 @@ function searchFilesByCaption(searchPhrase: string, files: EnteFile[]) {
);
}
-async function searchLocationTag(searchPhrase: string): Promise<LocationTag[]> {
- const locationTags = await getLatestEntities<LocationTagData>(
- EntityType.LOCATION_TAG,
- );
- const matchedLocationTags = locationTags.filter((locationTag) =>
- locationTag.data.name.toLowerCase().includes(searchPhrase),
- );
- if (matchedLocationTags.length > 0) {
- log.info(
- `Found ${matchedLocationTags.length} location tags for search phrase`,
- );
- }
- return matchedLocationTags;
-}
-
const searchClip = async (
searchPhrase: string,
): Promise<ClipSearchScores | undefined> => {
@@ -275,7 +257,7 @@ function convertSuggestionToSearchQuery(option: Suggestion): SearchQuery {
case SuggestionType.LOCATION:
return {
- location: option.value as LocationTagData,
+ location: option.value as LocationTag,
};
case SuggestionType.CITY:
diff --git a/web/apps/photos/src/services/sync.ts b/web/apps/photos/src/services/sync.ts
index 0e769fd6d5..ac16fa54bf 100644
--- a/web/apps/photos/src/services/sync.ts
+++ b/web/apps/photos/src/services/sync.ts
@@ -5,8 +5,6 @@ import {
triggerMLSync,
} from "@/new/photos/services/ml";
import { triggerSearchDataSync } from "@/new/photos/services/search";
-import { syncLocationTags } from "@/new/photos/services/user-entity";
-import { syncEntities } from "services/entityService";
import { syncMapEnabled } from "services/userService";
/**
@@ -37,11 +35,7 @@ export const triggerPreFileInfoSync = () => {
* before doing the file sync and thus should run immediately after login.
*/
export const sync = async () => {
- await Promise.allSettled([
- syncEntities(),
- syncLocationTags(),
- syncMapEnabled(),
- ]);
+ await Promise.allSettled([syncMapEnabled()]);
triggerSearchDataSync();
if (isMLSupported) triggerMLSync();
};
diff --git a/web/apps/photos/src/types/entity.ts b/web/apps/photos/src/types/entity.ts
deleted file mode 100644
index f22a7bac8a..0000000000
--- a/web/apps/photos/src/types/entity.ts
+++ /dev/null
@@ -1,39 +0,0 @@
-import type { LocationTagData } from "@/new/photos/services/search/types";
-
-export enum EntityType {
- LOCATION_TAG = "location",
-}
-
-export interface EncryptedEntityKey {
- userID: number;
- encryptedKey: string;
- type: EntityType;
- header: string;
- createdAt: number;
-}
-
-export interface EntityKey
- extends Omit<EncryptedEntityKey, "encryptedKey" | "header"> {
- data: string;
-}
-
-export interface EncryptedEntity {
- id: string;
- encryptedData: string;
- header: string;
- isDeleted: boolean;
- createdAt: number;
- updatedAt: number;
- userID: number;
-}
-
-export type LocationTag = Entity<LocationTagData>;
-
-export interface Entity<T>
- extends Omit<EncryptedEntity, "encryptedData" | "header"> {
- data: T;
-}
-
-export interface EntitySyncDiffResponse {
- diff: EncryptedEntity[];
-}
diff --git a/web/apps/photos/src/utils/entity.ts b/web/apps/photos/src/utils/entity.ts
deleted file mode 100644
index 7d8ad28b3c..0000000000
--- a/web/apps/photos/src/utils/entity.ts
+++ /dev/null
@@ -1,12 +0,0 @@
-import { Entity } from "types/entity";
-
-export const getLatestVersionEntities = <T>(entities: Entity<T>[]) => {
- const latestVersionEntities = new Map<string, Entity<T>>();
- entities.forEach((entity) => {
- const existingEntity = latestVersionEntities.get(entity.id);
- if (!existingEntity || existingEntity.updatedAt < entity.updatedAt) {
- latestVersionEntities.set(entity.id, entity);
- }
- });
- return Array.from(latestVersionEntities.values());
-};
diff --git a/web/packages/accounts/pages/verify.tsx b/web/packages/accounts/pages/verify.tsx
index 604c36b718..f47d612681 100644
--- a/web/packages/accounts/pages/verify.tsx
+++ b/web/packages/accounts/pages/verify.tsx
@@ -1,4 +1,5 @@
import type { UserVerificationResponse } from "@/accounts/types/user";
+import log from "@/base/log";
import { ensure } from "@/utils/ensure";
import { VerticallyCentered } from "@ente/shared/components/Container";
import EnteSpinner from "@ente/shared/components/EnteSpinner";
@@ -42,7 +43,6 @@ import {
import { configureSRP } from "../services/srp";
import type { PageProps } from "../types/page";
import type { SRPSetupAttributes } from "../types/srp";
-import log from "@/base/log";
const Page: React.FC<PageProps> = ({ appContext }) => {
const { logout, showNavBar, setDialogBoxAttributesV2 } = appContext;
diff --git a/web/packages/new/photos/services/search/types.ts b/web/packages/new/photos/services/search/types.ts
index b3bf4dad34..080803956a 100644
--- a/web/packages/new/photos/services/search/types.ts
+++ b/web/packages/new/photos/services/search/types.ts
@@ -58,8 +58,6 @@ export interface SearchPerson {
displayFaceFile: EnteFile;
}
-// TODO-cgroup: Audit below
-
/**
* A city as identified by a static dataset.
*
@@ -67,12 +65,12 @@ export interface SearchPerson {
* have information about the city's estimated radius.
*/
export type City = Location & {
- /**
- * Name of the city.
- */
+ /** Name of the city. */
name: string;
};
+// TODO-cgroup: Audit below
+
export enum SuggestionType {
DATE = "DATE",
LOCATION = "LOCATION",
diff --git a/web/packages/new/photos/services/search/worker.ts b/web/packages/new/photos/services/search/worker.ts
index b6b5f548a9..6815b7249d 100644
--- a/web/packages/new/photos/services/search/worker.ts
+++ b/web/packages/new/photos/services/search/worker.ts
@@ -327,6 +327,11 @@ const defaultCityRadius = 10;
const kmsPerDegree = 111.16;
const isInsideLocationTag = (location: Location, locationTag: LocationTag) =>
+ // This code is included in the photos app which currently doesn't have
+ // strict mode, and causes a spurious linter warning (but only when included
+ // in photos!), so we need to ts-ignore.
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment, @typescript-eslint/prefer-ts-expect-error
+ // @ts-ignore
isWithinRadius(location, locationTag.centerPoint, locationTag.radius);
const isInsideCity = (location: Location, city: City) =>