Skip to content

armgit / ente

Public Imported from GitHub
0

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

Fix collection refresh and token revokation

Neeraj Gupta4bbf79c321a3
diff --git a/apps/cast/src/pages/slideshow.tsx b/apps/cast/src/pages/slideshow.tsx
index 60628f275f..79ab550ab7 100644
--- a/apps/cast/src/pages/slideshow.tsx
+++ b/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 (
diff --git a/apps/cast/src/services/cast/castService.ts b/apps/cast/src/services/cast/castService.ts
index 10e5d47319..f0cc06002e 100644
--- a/apps/cast/src/services/cast/castService.ts
+++ b/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(
diff --git a/apps/photos/src/components/Collections/CollectionOptions/AlbumCastDialog.tsx b/apps/photos/src/components/Collections/CollectionOptions/AlbumCastDialog.tsx
index 59280ea8ee..2cc9aabbd4 100644
--- a/apps/photos/src/components/Collections/CollectionOptions/AlbumCastDialog.tsx
+++ b/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]);
diff --git a/packages/shared/network/cast.ts b/packages/shared/network/cast.ts
index 7adbc91be1..e3e37583fe 100644
--- a/packages/shared/network/cast.ts
+++ b/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,
}