瀏覽代碼

Fix collection refresh and token revokation

Neeraj Gupta 1 年之前
父節點
當前提交
4bbf79c321

+ 36 - 10
apps/cast/src/pages/slideshow.tsx

@@ -9,6 +9,7 @@ import {
     getLocalFiles,
     syncPublicFiles,
 } from 'services/cast/castService';
+import { Collection } from 'types/collection';
 import { EnteFile } from 'types/file';
 import { downloadFileAsBlob, isRawFileFromFileName } from 'utils/file';
 
@@ -26,35 +27,60 @@ export default function Slideshow() {
 
     const [loading, setLoading] = useState(true);
     const [castToken, setCastToken] = useState<string>('');
+    const [castCollection, setCastCollection] = useState<
+        Collection | undefined
+    >(undefined);
 
     const [renderableFileURLCache, setRenderableFileURLCache] = useState<
         Record<string, string>
     >({});
 
-    const init = async () => {
+    const syncCastFiles = async (token: string) => {
         try {
+            const castToken = window.localStorage.getItem('castToken');
             const requestedCollectionKey =
                 window.localStorage.getItem('collectionKey');
-            const castToken = window.localStorage.getItem('castToken');
-            setCastToken(castToken);
-
             const collection = await getCastCollection(
                 castToken,
                 requestedCollectionKey
             );
+            if (
+                castCollection === undefined ||
+                castCollection.updationTime !== collection.updationTime
+            ) {
+                setCastCollection(collection);
+                await syncPublicFiles(token, collection, () => {});
+                const files = await getLocalFiles(String(collection.id));
+                setCollectionFiles(
+                    files.filter((file) => isFileEligibleForCast(file))
+                );
+            }
+        } catch (e) {
+            logError(e, 'error during sync');
+            router.push('/');
+        }
+    };
 
-            await syncPublicFiles(castToken, collection, () => {});
-            const files = await getLocalFiles(String(collection.id));
-            setCollectionFiles(
-                files.filter((file) => isFileEligibleForCast(file))
-            );
+    const init = async () => {
+        try {
+            const castToken = window.localStorage.getItem('castToken');
+            setCastToken(castToken);
         } catch (e) {
             logError(e, 'error during sync');
-            alert('error, redirect to home' + e);
             router.push('/');
         }
     };
 
+    useEffect(() => {
+        if (castToken) {
+            const intervalId = setInterval(() => {
+                syncCastFiles(castToken);
+            }, 5000);
+
+            return () => clearInterval(intervalId);
+        }
+    }, [castToken]);
+
     const isFileEligibleForCast = (file: EnteFile) => {
         const fileType = file.metadata.fileType;
         if (

+ 1 - 1
apps/cast/src/services/cast/castService.ts

@@ -113,7 +113,6 @@ export const syncPublicFiles = async (
         const collectionUID = String(collection.id);
         const localFiles = await getLocalFiles(collectionUID);
         files = [...files, ...localFiles];
-        console.log('found local files', files);
         try {
             const lastSyncTime = await getSyncTime(collectionUID);
             if (collection.updationTime === lastSyncTime) {
@@ -177,6 +176,7 @@ const fetchFiles = async (
         const sortAsc = collection?.pubMagicMetadata?.data.asc ?? false;
         do {
             if (!castToken) {
+                console.log('WTF no token');
                 break;
             }
             resp = await HTTPService.get(

+ 5 - 3
apps/photos/src/components/Collections/CollectionOptions/AlbumCastDialog.tsx

@@ -39,10 +39,9 @@ export default function AlbumCastDialog(props: Props) {
     const [browserCanCast, setBrowserCanCast] = useState(false);
     // Make API call on component mount
     useEffect(() => {
-        (async () => {
-            await castGateway.revokeAllTokens();
-        })();
+        castGateway.revokeAllTokens();
     }, []);
+
     const onSubmit: SingleInputFormProps['callback'] = async (
         value,
         setFieldError
@@ -145,6 +144,9 @@ export default function AlbumCastDialog(props: Props) {
     }, [view]);
 
     useEffect(() => {
+        if (props.show) {
+            castGateway.revokeAllTokens();
+        }
         setBrowserCanCast(!!window.chrome);
     }, [props.show]);
 

+ 1 - 0
packages/shared/network/cast.ts

@@ -26,6 +26,7 @@ class CastGateway {
             await HTTPService.delete(
                 getEndpoint() + '/cast/revoke-all-tokens/',
                 undefined,
+                undefined,
                 {
                     'X-Auth-Token': token,
                 }