This commit is contained in:
Manav Rathi
2025-06-25 18:40:12 +05:30
parent 58f3144ea0
commit 2b9d80d23f
3 changed files with 40 additions and 19 deletions

View File

@@ -1,22 +1,11 @@
import { type Collection } from "ente-media/collection";
import localForage from "ente-shared/storage/localForage";
import { getCollections } from "./collection";
import { savedCollections } from "./photos-fdb";
import { removeCollectionIDLastSyncTime, savedCollections } from "./photos-fdb";
const COLLECTION_TABLE = "collections";
const COLLECTION_UPDATION_TIME = "collection-updation-time";
export const getCollectionLastSyncTime = async (collection: Collection) =>
(await localForage.getItem<number>(`${collection.id}-time`)) ?? 0;
export const setCollectionLastSyncTime = async (
collection: Collection,
time: number,
) => await localForage.setItem<number>(`${collection.id}-time`, time);
export const removeCollectionIDLastSyncTime = async (collectionID: number) =>
await localForage.removeItem(`${collectionID}-time`);
export const getCollectionUpdationTime = async (): Promise<number> =>
(await localForage.getItem<number>(COLLECTION_UPDATION_TIME)) ?? 0;

View File

@@ -11,10 +11,11 @@ import { metadataHash } from "ente-media/file-metadata";
import HTTPService from "ente-shared/network/HTTPService";
import { getToken } from "ente-shared/storage/localStorage/helpers";
import {
getCollectionLastSyncTime,
setCollectionLastSyncTime,
} from "./collections";
import { saveCollectionFiles, savedCollectionFiles } from "./photos-fdb";
saveCollectionFiles,
saveCollectionLastSyncTime,
savedCollectionFiles,
savedCollectionLastSyncTime,
} from "./photos-fdb";
/**
* Fetch all files of the given {@link type}, belonging to the given
@@ -48,7 +49,8 @@ export const pullCollectionFiles = async (
if (!getToken()) {
continue;
}
const lastSyncTime = await getCollectionLastSyncTime(collection);
const lastSyncTime =
(await savedCollectionLastSyncTime(collection)) ?? 0;
if (collection.updationTime === lastSyncTime) {
continue;
}
@@ -62,7 +64,7 @@ export const pullCollectionFiles = async (
files = getLatestVersionFiles([...files, ...newFiles]);
await saveCollectionFiles(files);
didUpdateFiles = true;
await setCollectionLastSyncTime(collection, collection.updationTime);
await saveCollectionLastSyncTime(collection, collection.updationTime);
}
return didUpdateFiles;
};

View File

@@ -120,7 +120,7 @@ export const saveTrashItemCollectionKeys = async (
* for the same file, one for each collection that the file belongs to. For more
* details, See: [Note: Collection File].
*
* Use {@link saveFiles} to update the database.
* Use {@link saveCollectionFiles} to update the database.
*/
export const savedCollectionFiles = async (): Promise<EnteFile[]> => {
// [Note: Avoiding Zod parsing for large DB arrays]
@@ -167,6 +167,36 @@ export const saveCollectionFiles = async (files: EnteFile[]) => {
await localForage.setItem("files", transformFilesIfNeeded(files));
};
/**
* Return the locally persisted {@link updationTime} of the latest file from the
* given {@link collection} that we have pulled from remote.
*
* Use {@link saveCollectionLastSyncTime} to update the value saved in the
* database, and {@link removeCollectionIDLastSyncTime} to remove the saved
* value from the database.
*/
export const savedCollectionLastSyncTime = async (collection: Collection) =>
LocalTimestamp.parse(await localForage.getItem(`${collection.id}-time`));
/**
* Update the locally persisted timestamp that will be returned by subsequent
* calls to {@link savedCollectionLastSyncTime}.
*/
export const saveCollectionLastSyncTime = async (
collection: Collection,
time: number,
) => {
await localForage.setItem(`${collection.id}-time`, time);
};
/**
* Remove the locally persisted timestamp, if any, previously saved for a
* collection with the given ID using {@link saveCollectionLastSyncTime}.
*/
export const removeCollectionIDLastSyncTime = async (collectionID: number) => {
await localForage.removeItem(`${collectionID}-time`);
};
/**
* Zod schema for a trash entry saved in our local database.
*/