Skip to content

armgit / ente

Public Imported from GitHub
0

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

Sketch

Manav Rathid6a340ac7969
diff --git a/web/packages/new/photos/services/collection.ts b/web/packages/new/photos/services/collection.ts
index 2b91e367fb..a11239690c 100644
--- a/web/packages/new/photos/services/collection.ts
+++ b/web/packages/new/photos/services/collection.ts
@@ -9,6 +9,7 @@ import {
import { authenticatedRequestHeaders, ensureOk } from "ente-base/http";
import { apiURL } from "ente-base/origins";
import { ensureMasterKeyFromSession } from "ente-base/session";
+import type { UpdateMagicMetadataRequest } from "ente-gallery/services/file";
import {
CollectionSubType,
decryptRemoteCollection,
@@ -28,7 +29,6 @@ import { ItemVisibility } from "ente-media/file-metadata";
import {
createMagicMetadata,
encryptMagicMetadata,
- type RemoteMagicMetadata,
} from "ente-media/magic-metadata";
import { batch } from "ente-utils/array";
import { z } from "zod/v4";
@@ -541,7 +541,7 @@ export const updateCollectionSortOrder = async (
) => updateCollectionPublicMagicMetadata(collection, { asc });
/**
- * Update the private magic metadata contents of a collection on remote.
+ * Update the private magic metadata of a collection on remote.
*
* Remote only, does not modify local state.
*
@@ -557,7 +557,7 @@ export const updateCollectionSortOrder = async (
*
* See: [Note: Magic metadata data cannot have nullish values]
*/
-export const updateCollectionPrivateMagicMetadata = async (
+const updateCollectionPrivateMagicMetadata = async (
{ id, key, magicMetadata }: Collection,
updates: CollectionPrivateMagicMetadataData,
) =>
@@ -572,32 +572,11 @@ export const updateCollectionPrivateMagicMetadata = async (
),
});
-/**
- * The payload of the remote requests for updating the magic metadata of a
- * single collection.
- */
-interface UpdateCollectionMagicMetadataRequest {
- /**
- * Collection ID
- */
- id: number;
- /**
- * The updated magic metadata.
- *
- * Remote usually enforces the following constraints when we're trying to
- * update already existing data.
- *
- * - The version should be same as the existing version.
- * - The count should be greater than or equal to the existing count.
- */
- magicMetadata: RemoteMagicMetadata;
-}
-
/**
* Update the private magic metadata of a single collection on remote.
*/
const putCollectionsMagicMetadata = async (
- updateRequest: UpdateCollectionMagicMetadataRequest,
+ updateRequest: UpdateMagicMetadataRequest,
) =>
ensureOk(
await fetch(await apiURL("/collections/magic-metadata"), {
@@ -608,7 +587,7 @@ const putCollectionsMagicMetadata = async (
);
/**
- * Update the public magic metadata contents of a collection on remote.
+ * Update the public magic metadata of a collection on remote.
*
* Remote only, does not modify local state.
*
@@ -634,7 +613,7 @@ const updateCollectionPublicMagicMetadata = async (
* Update the public magic metadata of a single collection on remote.
*/
const putCollectionsPublicMagicMetadata = async (
- updateRequest: UpdateCollectionMagicMetadataRequest,
+ updateRequest: UpdateMagicMetadataRequest,
) =>
ensureOk(
await fetch(await apiURL("/collections/public-magic-metadata"), {
@@ -645,7 +624,7 @@ const putCollectionsPublicMagicMetadata = async (
);
/**
- * Update the per-sharee magic metadata contents of a collection on remote.
+ * Update the per-sharee magic metadata of a collection on remote.
*
* Remote only, does not modify local state.
*
@@ -671,7 +650,7 @@ const updateCollectionShareeMagicMetadata = async (
* Update the sharee magic metadata of a single shared collection on remote.
*/
const putCollectionsShareeMagicMetadata = async (
- updateRequest: UpdateCollectionMagicMetadataRequest,
+ updateRequest: UpdateMagicMetadataRequest,
) =>
ensureOk(
await fetch(await apiURL("/collections/sharee-magic-metadata"), {
diff --git a/web/packages/new/photos/services/file.ts b/web/packages/new/photos/services/file.ts
new file mode 100644
index 0000000000..8a2f4e270a
--- /dev/null
+++ b/web/packages/new/photos/services/file.ts
@@ -0,0 +1,105 @@
+import { authenticatedRequestHeaders, ensureOk } from "ente-base/http";
+import { apiURL } from "ente-base/origins";
+import type { EnteFile, EnteFile2 } from "ente-media/file";
+import type {
+ FilePrivateMagicMetadataData,
+ ItemVisibility,
+} from "ente-media/file-metadata";
+import {
+ createMagicMetadata,
+ encryptMagicMetadata,
+ type RemoteMagicMetadata,
+} from "ente-media/magic-metadata";
+
+/**
+ * Change the visibility (normal, archived, hidden) of a file on remote.
+ *
+ * Remote only, does not modify local state.
+ *
+ * @param visibility The new visibility (normal, archived, hidden).
+ */
+export const updateFileVisibility = async (
+ file: EnteFile,
+ visibility: ItemVisibility,
+) => updateFilePrivateMagicMetadata(file, { visibility });
+
+/**
+ * Update the private magic metadata of a file on remote.
+ *
+ * Remote only, does not modify local state.
+ *
+ * @param file The file whose magic metadata we want to update.
+ *
+ * The existing magic metadata of this collection is used both to obtain the
+ * current magic metadata version, and the existing contents on top of which the
+ * updates are applied, so it is imperative that both these values are up to
+ * sync with remote otherwise the update will fail.
+ *
+ * @param updates A non-empty subset of {@link FilePrivateMagicMetadataData} entries.
+ *
+ * See: [Note: Magic metadata data cannot have nullish values]
+ */
+const updateFilePrivateMagicMetadata = async (
+ { id, key, magicMetadata }: EnteFile2,
+ updates: FilePrivateMagicMetadataData,
+) =>
+ putFilesMagicMetadata({
+ metadataList: [
+ {
+ id,
+ magicMetadata: await encryptMagicMetadata(
+ createMagicMetadata(
+ { ...magicMetadata?.data, ...updates },
+ magicMetadata?.version,
+ ),
+ key,
+ ),
+ },
+ ],
+ });
+
+/**
+ * The payload of the remote requests for updating the magic metadata of a
+ * single item (file or collection).
+ */
+export interface UpdateMagicMetadataRequest {
+ /**
+ * File or collection ID
+ */
+ id: number;
+ /**
+ * The updated magic metadata.
+ *
+ * Remote usually enforces the following constraints when we're trying to
+ * update already existing data.
+ *
+ * - The version should be same as the existing version.
+ * - The count should be greater than or equal to the existing count.
+ */
+ magicMetadata: RemoteMagicMetadata;
+}
+
+/**
+ * The payload of the remote requests for updating the magic metadata of a
+ * multiple items.
+ *
+ * Currently this is only used by endpoints that update magic metadata for a
+ * list of files.
+ */
+export interface UpdateMultipleMagicMetadataRequest {
+ metadataList: UpdateMagicMetadataRequest[];
+}
+
+/**
+ * Update the private magic metadata of a list of files on remote.
+ */
+const putFilesMagicMetadata = async (
+ updateRequest: UpdateMultipleMagicMetadataRequest,
+) =>
+ ensureOk(
+ await fetch(await apiURL("/files/magic-metadata"), {
+ method: "PUT",
+ headers: await authenticatedRequestHeaders(),
+ body: JSON.stringify(updateRequest),
+ }),
+ );