Skip to content

armgit / ente

Public Imported from GitHub
0

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

Simplify

Manav Rathic60288b0df9f
diff --git a/web/apps/photos/src/pages/shared-albums.tsx b/web/apps/photos/src/pages/shared-albums.tsx
index 4d700952cf..d85900880d 100644
--- a/web/apps/photos/src/pages/shared-albums.tsx
+++ b/web/apps/photos/src/pages/shared-albums.tsx
@@ -59,7 +59,8 @@ import {
savePublicCollectionAccessTokenJWT,
} from "ente-new/albums/services/public-albums-fdb";
import {
- fetchAndSavePublicCollection,
+ pullCollection,
+ pullPublicCollectionFiles,
verifyPublicAlbumPassword,
} from "ente-new/albums/services/public-collection";
import {
@@ -78,7 +79,6 @@ import {
getPublicCollectionUID,
removePublicCollectionWithFiles,
removePublicFiles,
- syncPublicFiles,
} from "services/publicCollectionService";
import { uploadManager } from "services/upload-manager";
import {
@@ -307,7 +307,7 @@ export default function PublicCollectionGallery() {
showLoadingBar();
setLoading(true);
const { collection, referralCode: userReferralCode } =
- await fetchAndSavePublicCollection(
+ await pullCollection(
credentials.current.accessToken,
collectionKey.current,
);
@@ -327,14 +327,12 @@ export default function PublicCollectionGallery() {
removePublicCollectionAccessTokenJWT(collectionUID);
}
- if (
- !isPasswordProtected ||
- (isPasswordProtected && credentials.current.accessTokenJWT)
- ) {
+ if (isPasswordProtected && !credentials.current.accessTokenJWT) {
+ await removePublicFiles(collectionUID);
+ } else {
try {
- await syncPublicFiles(
- credentials.current.accessToken,
- credentials.current.accessTokenJWT,
+ await pullPublicCollectionFiles(
+ credentials.current,
collection,
(files) =>
setPublicFiles(
@@ -342,21 +340,26 @@ export default function PublicCollectionGallery() {
),
);
} catch (e) {
- const parsedError = parseSharingErrorCodes(e);
- if (parsedError.message === CustomError.TOKEN_EXPIRED) {
- // passwordToken has expired, sharer has changed the password,
- // so,clearing local cache token value to prompt user to re-enter password
+ // If we reached the try block and attempted to pull files,
+ // it means the accessToken itself is very likely valid
+ // (since the `pullCollection` succeeded just a moment ago).
+ //
+ // So a 401 Unauthorized now indicates that the
+ // accessTokenJWT is no longer valid since the sharer has
+ // changed the password.
+ //
+ // Clear the locally cached accessTokenJWT and ask the user
+ // to reenter the password.
+ if (isHTTP401Error(e)) {
credentials.current.accessTokenJWT = undefined;
downloadManager.setPublicAlbumsCredentials(
credentials.current,
);
+ } else {
+ throw e;
}
}
}
-
- if (isPasswordProtected && !credentials.current.accessTokenJWT) {
- await removePublicFiles(collectionUID);
- }
} catch (e) {
const parsedError = parseSharingErrorCodes(e);
if (
diff --git a/web/apps/photos/src/services/publicCollectionService.ts b/web/apps/photos/src/services/publicCollectionService.ts
index 360ad750dc..6ee913d032 100644
--- a/web/apps/photos/src/services/publicCollectionService.ts
+++ b/web/apps/photos/src/services/publicCollectionService.ts
@@ -1,166 +1,14 @@
-import log from "ente-base/log";
-import { apiURL } from "ente-base/origins";
-import { sortFiles } from "ente-gallery/utils/file";
-import type { Collection } from "ente-media/collection";
-import type { EnteFile, RemoteEnteFile } from "ente-media/file";
-import { decryptRemoteFile } from "ente-media/file";
import {
removePublicCollectionAccessTokenJWT,
removePublicCollectionByKey,
removePublicCollectionFiles,
removePublicCollectionLastSyncTime,
- savedPublicCollectionFiles,
- savedPublicCollectionLastSyncTime,
- savePublicCollectionFiles,
- savePublicCollectionLastSyncTime,
} from "ente-new/albums/services/public-albums-fdb";
-import { CustomError, parseSharingErrorCodes } from "ente-shared/error";
-import HTTPService from "ente-shared/network/HTTPService";
// Fix this once we can trust the types.
// eslint-disable-next-line @typescript-eslint/no-unnecessary-template-expression
export const getPublicCollectionUID = (token: string) => `${token}`;
-export interface LocalSavedPublicCollectionFiles {
- collectionUID: string;
- files: EnteFile[];
-}
-
-export const syncPublicFiles = async (
- token: string,
- passwordToken: string,
- collection: Collection,
- setPublicFiles: (files: EnteFile[]) => void,
-) => {
- try {
- let files: EnteFile[] = [];
- const sortAsc = collection?.pubMagicMetadata?.data.asc ?? false;
- const collectionUID = getPublicCollectionUID(token);
- const localFiles = await savedPublicCollectionFiles(collectionUID);
-
- files = [...files, ...localFiles];
- try {
- if (!token) {
- return sortFiles(files, sortAsc);
- }
- const lastSyncTime =
- (await savedPublicCollectionLastSyncTime(collectionUID)) ?? 0;
- if (collection.updationTime === lastSyncTime) {
- return sortFiles(files, sortAsc);
- }
- const fetchedFiles = await getPublicFiles(
- token,
- passwordToken,
- collection,
- lastSyncTime,
- files,
- setPublicFiles,
- );
-
- files = [...files, ...fetchedFiles];
- const latestVersionFiles = new Map<string, EnteFile>();
- files.forEach((file) => {
- const uid = `${file.collectionID}-${file.id}`;
- if (
- !latestVersionFiles.has(uid) ||
- latestVersionFiles.get(uid).updationTime < file.updationTime
- ) {
- latestVersionFiles.set(uid, file);
- }
- });
- files = [];
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
- for (const [_, file] of latestVersionFiles) {
- // TODO(RE):
- if ("isDeleted" in file && file.isDeleted) {
- continue;
- }
- files.push(file);
- }
- await savePublicCollectionFiles(collectionUID, files);
- await savePublicCollectionLastSyncTime(
- collectionUID,
- collection.updationTime,
- );
- setPublicFiles([...sortFiles(files, sortAsc)]);
- } catch (e) {
- const parsedError = parseSharingErrorCodes(e);
- log.error("failed to sync shared collection files", e);
- if (parsedError.message === CustomError.TOKEN_EXPIRED) {
- throw e;
- }
- }
- return [...sortFiles(files, sortAsc)];
- } catch (e) {
- log.error("failed to get local or sync shared collection files", e);
- throw e;
- }
-};
-
-const getPublicFiles = async (
- token: string,
- passwordToken: string,
- collection: Collection,
- sinceTime: number,
- files: EnteFile[],
- setPublicFiles: (files: EnteFile[]) => void,
-): Promise<EnteFile[]> => {
- try {
- let decryptedFiles: EnteFile[] = [];
- let time = sinceTime;
- let resp;
- const sortAsc = collection?.pubMagicMetadata?.data.asc ?? false;
- do {
- if (!token) {
- break;
- }
- resp = await HTTPService.get(
- await apiURL("/public-collection/diff"),
- { sinceTime: time },
- {
- "X-Auth-Access-Token": token,
- ...(passwordToken && {
- "X-Auth-Access-Token-JWT": passwordToken,
- }),
- },
- );
- decryptedFiles = [
- ...decryptedFiles,
- ...(await Promise.all(
- resp.data.diff.map(async (file: RemoteEnteFile) => {
- if (!file.isDeleted) {
- return await decryptRemoteFile(
- file,
- collection.key,
- );
- } else {
- return file;
- }
- }) as Promise<EnteFile>[],
- )),
- ];
-
- if (resp.data.diff.length) {
- time = resp.data.diff.slice(-1)[0].updationTime;
- }
- setPublicFiles(
- sortFiles(
- [...(files || []), ...decryptedFiles].filter(
- // TODO(RE):
- // (item) => !item.isDeleted,
- (file) => !("isDeleted" in file && file.isDeleted),
- ),
- sortAsc,
- ),
- );
- } while (resp.data.hasMore);
- return decryptedFiles;
- } catch (e) {
- log.error("Get public files failed", e);
- throw e;
- }
-};
-
export const removePublicCollectionWithFiles = async (
collectionUID: string,
collectionKey: string,
diff --git a/web/packages/new/albums/services/public-collection.ts b/web/packages/new/albums/services/public-collection.ts
index cddc3c89f5..f6cf9ccfc2 100644
--- a/web/packages/new/albums/services/public-collection.ts
+++ b/web/packages/new/albums/services/public-collection.ts
@@ -93,7 +93,7 @@ export const verifyPublicAlbumPassword = async (
* (the fragment is a client side only portion that can be used to have local
* secrets that are not sent by the browser to the server).
*/
-export const fetchAndSavePublicCollection = async (
+export const pullCollection = async (
accessToken: string,
collectionKey: string,
): Promise<{ collection: Collection; referralCode: string }> => {
@@ -221,9 +221,6 @@ export const pullPublicCollectionFiles = async (
* (identified by its {@link credentials}) since {@link sinceTime}.
*
* Remote only, does not modify local state.
- *
- * @param credentials
- * @param number
*/
const getPublicCollectionDiff = async (
credentials: PublicAlbumsCredentials,