Fix collection refresh and token revokation

This commit is contained in:
Neeraj Gupta 2024-01-29 18:09:47 +05:30
parent 18c3369fc0
commit 4bbf79c321
4 changed files with 44 additions and 15 deletions

View file

@ -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
);
await syncPublicFiles(castToken, collection, () => {});
const files = await getLocalFiles(String(collection.id));
setCollectionFiles(
files.filter((file) => isFileEligibleForCast(file))
);
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');
alert('error, redirect to home' + e);
router.push('/');
}
};
const init = async () => {
try {
const castToken = window.localStorage.getItem('castToken');
setCastToken(castToken);
} catch (e) {
logError(e, 'error during sync');
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 (

View file

@ -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(

View file

@ -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]);

View file

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