diff --git a/web/apps/cast/src/pages/slideshow.tsx b/web/apps/cast/src/pages/slideshow.tsx index 60e07b725..85cf7ad23 100644 --- a/web/apps/cast/src/pages/slideshow.tsx +++ b/web/apps/cast/src/pages/slideshow.tsx @@ -8,6 +8,7 @@ import { getCastCollection, getLocalFiles, isFileEligibleForCast, + readCastData, syncPublicFiles, } from "services/cast"; import { Collection } from "types/collection"; @@ -28,13 +29,10 @@ export default function Slideshow() { const syncCastFiles = async (token: string) => { try { - console.log("syncCastFiles"); - const castToken = window.localStorage.getItem("castToken"); - const requestedCollectionKey = - window.localStorage.getItem("collectionKey"); + const { castToken, collectionKey } = readCastData(); const collection = await getCastCollection( + collectionKey, castToken, - requestedCollectionKey, ); if ( castCollection === undefined || diff --git a/web/apps/cast/src/services/cast.ts b/web/apps/cast/src/services/cast.ts index fb19d2bfd..6e0282f79 100644 --- a/web/apps/cast/src/services/cast.ts +++ b/web/apps/cast/src/services/cast.ts @@ -3,6 +3,7 @@ import { isNonWebImageFileExtension } from "@/media/formats"; import { decodeLivePhoto } from "@/media/live-photo"; import { nameAndExtension } from "@/next/file"; import log from "@/next/log"; +import { ensure } from "@/utils/ensure"; import ComlinkCryptoWorker from "@ente/shared/crypto"; import { CustomError, parseSharingErrorCodes } from "@ente/shared/error"; import HTTPService from "@ente/shared/network/HTTPService"; @@ -26,6 +27,41 @@ const ENDPOINT = getEndpoint(); const COLLECTION_FILES_TABLE = "collection-files"; const COLLECTIONS_TABLE = "collections"; +/** + * Save the data received after pairing with a sender into local storage. + * + * We will read in back when we start the slideshow. + */ +export const storeCastData = (payload: unknown) => { + if (!payload || typeof payload != "object") + throw new Error("Unexpected cast data"); + + // Iterate through all the keys of the payload object and save them to + // localStorage. We don't validate here, we'll validate when we read these + // values back in `readCastData`. + for (const key in payload) { + window.localStorage.setItem(key, payload[key]); + } +}; + +interface CastData { + /** A key to decrypt the collection we are casting. */ + collectionKey: string; + /** A credential to use for fetching media files for this cast session. */ + castToken: string; +} + +/** + * Read back the cast data we got after pairing. + * + * Sibling of {@link storeCastData} + */ +export const readCastData = (): CastData => { + const collectionKey = ensure(window.localStorage.getItem("collectionKey")); + const castToken = ensure(window.localStorage.getItem("castToken")); + return { collectionKey, castToken }; +}; + const getLastSyncKey = (collectionUID: string) => `${collectionUID}-time`; export const getLocalFiles = async ( @@ -232,8 +268,8 @@ const fetchFiles = async ( }; export const getCastCollection = async ( - castToken: string, collectionKey: string, + castToken: string, ): Promise => { try { const resp = await HTTPService.get(`${ENDPOINT}/cast/info`, null, { @@ -278,13 +314,6 @@ export const getCastCollection = async ( } }; -export const storeCastData = (payloadObj: Object) => { - // iterate through all the keys in the payload object and set them in localStorage. - for (const key in payloadObj) { - window.localStorage.setItem(key, payloadObj[key]); - } -}; - export function sortFiles(files: EnteFile[], sortAsc = false) { // sort based on the time of creation time of the file, // for files with same creation time, sort based on the time of last modification