Skip to content

armgit / ente

Public Imported from GitHub
0

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

Pass lft

Manav Rathi8e7a3a9347d2
diff --git a/web/apps/photos/src/services/logout.ts b/web/apps/photos/src/services/logout.ts
index 8ebf30aec9..fa36e4f8fa 100644
--- a/web/apps/photos/src/services/logout.ts
+++ b/web/apps/photos/src/services/logout.ts
@@ -3,6 +3,7 @@ import log from "@/base/log";
import DownloadManager from "@/new/photos/services/download";
import { clearFeatureFlagSessionState } from "@/new/photos/services/feature-flags";
import { logoutML, terminateMLWorker } from "@/new/photos/services/ml";
+import { logoutSearch } from "@/new/photos/services/search";
import exportService from "./export";
/**
@@ -18,13 +19,13 @@ export const photosLogout = async () => {
// - Workers
- // Terminate any workers before clearing persistent state.
- // See: [Note: Caching IDB instances in separate execution contexts].
+ // Terminate any workers that might access the DB before clearing persistent
+ // state. See: [Note: Caching IDB instances in separate execution contexts].
try {
await terminateMLWorker();
} catch (e) {
- ignoreError("face", e);
+ ignoreError("ml/worker", e);
}
// - Remote logout and clear state
@@ -47,6 +48,12 @@ export const photosLogout = async () => {
ignoreError("download", e);
}
+ try {
+ logoutSearch();
+ } catch (e) {
+ ignoreError("search", e);
+ }
+
// - Desktop
const electron = globalThis.electron;
diff --git a/web/packages/new/photos/services/search/index.ts b/web/packages/new/photos/services/search/index.ts
index abd920c9e1..7a8dd9e749 100644
--- a/web/packages/new/photos/services/search/index.ts
+++ b/web/packages/new/photos/services/search/index.ts
@@ -1,12 +1,15 @@
import { isDesktop } from "@/base/app";
import { masterKeyFromSession } from "@/base/session-store";
import { ComlinkWorker } from "@/base/worker/comlink-worker";
+import { FileType } from "@/media/file-type";
import i18n, { t } from "i18next";
import type { EnteFile } from "../../types/file";
import { clipMatches, isMLEnabled } from "../ml";
import {
SuggestionType,
type DateSearchResult,
+ type LabelledFileType,
+ type LocalizedSearchData,
type SearchQuery,
} from "./types";
import type { SearchWorker } from "./worker";
@@ -31,6 +34,17 @@ const createComlinkWorker = () =>
new Worker(new URL("worker.ts", import.meta.url)),
);
+/**
+ * Perform any logout specific cleanup for the search subsystem.
+ */
+export const logoutSearch = () => {
+ if (_comlinkWorker) {
+ _comlinkWorker.terminate();
+ _comlinkWorker = undefined;
+ }
+ _localizedSearchData = undefined;
+};
+
/**
* Fetch any data that would be needed if the user were to search.
*/
@@ -59,7 +73,7 @@ export const createSearchQuery = async (searchString: string) => {
// the search worker, then combine the two.
const results = await Promise.all([
clipSuggestions(s, searchString).then((s) => s ?? []),
- worker().then((w) => w.createSearchQuery(s, i18n.language, holidays())),
+ worker().then((w) => w.createSearchQuery(s, localizedSearchData())),
]);
return results.flat();
};
@@ -85,12 +99,34 @@ export const search = async (search: SearchQuery) =>
worker().then((w) => w.search(search));
/**
- * A list of holidays - their yearly dates and localized names.
+ * Cached value of {@link localizedSearchData}.
+ */
+let _localizedSearchData: LocalizedSearchData | undefined;
+
+/*
+ * For searching, the web worker needs a bunch of otherwise static data that has
+ * names and labels formed by localized strings.
+ *
+ * Since it would be tricky to get the t() function to work in a web worker, we
+ * instead pass this from the main thread (lazily initialized and cached).
+ *
+ * Note that these need to be evaluated at runtime, and cannot be static
+ * constants since t() depends on the user's locale.
*
- * We need to keep this on the main thread since it uses the t() function for
- * localization (although I haven't tried that in a web worker, it might work
- * there too). Also, it cannot be a const since it needs to be evaluated lazily
- * for the t() to work.
+ * We currently clear the cached data on logout, but this is not necessary. The
+ * only point we necessarily need to clear this data is if the user changes their
+ * preferred locale, but currently we reload the page in such cases so any in
+ * memory state would be reset that way.
+ */
+const localizedSearchData = () =>
+ (_localizedSearchData ??= {
+ locale: i18n.language,
+ holidays: holidays(),
+ labelledFileTypes: labelledFileTypes(),
+ });
+
+/**
+ * A list of holidays - their yearly dates and localized names.
*/
const holidays = (): DateSearchResult[] => [
{ components: { month: 12, day: 25 }, label: t("CHRISTMAS") },
@@ -98,3 +134,12 @@ const holidays = (): DateSearchResult[] => [
{ components: { month: 1, day: 1 }, label: t("NEW_YEAR") },
{ components: { month: 12, day: 31 }, label: t("NEW_YEAR_EVE") },
];
+
+/**
+ * A list of file types with their localized names.
+ */
+const labelledFileTypes = (): LabelledFileType[] => [
+ { fileType: FileType.image, label: t("IMAGE") },
+ { fileType: FileType.video, label: t("VIDEO") },
+ { fileType: FileType.livePhoto, label: t("LIVE_PHOTO") },
+];
diff --git a/web/packages/new/photos/services/search/types.ts b/web/packages/new/photos/services/search/types.ts
index 080803956a..9066f12791 100644
--- a/web/packages/new/photos/services/search/types.ts
+++ b/web/packages/new/photos/services/search/types.ts
@@ -14,6 +14,21 @@ export interface DateSearchResult {
label: string;
}
+export interface LabelledFileType {
+ fileType: FileType;
+ label: string;
+}
+
+/**
+ * Various bits of static but locale specific data that the search worker needs
+ * during searching.
+ */
+export interface LocalizedSearchData {
+ locale: string;
+ holidays: DateSearchResult[];
+ labelledFileTypes: LabelledFileType[];
+}
+
/**
* A parsed version of a potential natural language date time string.
*
diff --git a/web/packages/new/photos/services/search/worker.ts b/web/packages/new/photos/services/search/worker.ts
index 0492863db0..62e21af4bf 100644
--- a/web/packages/new/photos/services/search/worker.ts
+++ b/web/packages/new/photos/services/search/worker.ts
@@ -16,6 +16,7 @@ import {
import type {
City,
DateSearchResult,
+ LocalizedSearchData,
SearchDateComponents,
SearchQuery,
Suggestion,
@@ -81,11 +82,10 @@ export class SearchWorker {
/**
* Convert a search string into a reusable query.
*/
- createSearchQuery(s: string, locale: string, holidays: DateSearchResult[]) {
+ createSearchQuery(s: string, localizedSearchData: LocalizedSearchData) {
return createSearchQuery(
s,
- locale,
- holidays,
+ localizedSearchData,
this.locationTags,
this.cities,
);
@@ -103,8 +103,7 @@ expose(SearchWorker);
const createSearchQuery = (
s: string,
- locale: string,
- holidays: DateSearchResult[],
+ { locale, holidays }: LocalizedSearchData,
locationTags: SearchableLocationTag[],
cities: SearchableCity[],
): Suggestion[] =>