diff options
author | Samuel Newman <mozzius@protonmail.com> | 2024-09-07 19:27:32 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-07 19:27:32 +0100 |
commit | 45a719b256173f98b20457cc80b4288e84f1c33f (patch) | |
tree | c377172ddaae5469ba6fb54457687f590339ed8e /src/state/queries/video/video-upload.shared.ts | |
parent | b7d78fe59b73294ac13baa51a6a7cd94698cb205 (diff) | |
download | voidsky-45a719b256173f98b20457cc80b4288e84f1c33f.tar.zst |
[Video] Check upload limits before uploading (#5153)
* DRY up video service auth code * throw error if over upload limits * use token * xmark on toast * errors with nice translatable error messages * Update src/state/queries/video/video.ts --------- Co-authored-by: Hailey <me@haileyok.com>
Diffstat (limited to 'src/state/queries/video/video-upload.shared.ts')
-rw-r--r-- | src/state/queries/video/video-upload.shared.ts | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/src/state/queries/video/video-upload.shared.ts b/src/state/queries/video/video-upload.shared.ts new file mode 100644 index 000000000..6b633bf21 --- /dev/null +++ b/src/state/queries/video/video-upload.shared.ts @@ -0,0 +1,73 @@ +import {useCallback} from 'react' +import {msg} from '@lingui/macro' +import {useLingui} from '@lingui/react' + +import {VIDEO_SERVICE_DID} from '#/lib/constants' +import {UploadLimitError} from '#/lib/media/video/errors' +import {getServiceAuthAudFromUrl} from '#/lib/strings/url-helpers' +import {useAgent} from '#/state/session' +import {useVideoAgent} from './util' + +export function useServiceAuthToken({ + aud, + lxm, + exp, +}: { + aud?: string + lxm: string + exp?: number +}) { + const agent = useAgent() + + return useCallback(async () => { + const pdsAud = getServiceAuthAudFromUrl(agent.dispatchUrl) + + if (!pdsAud) { + throw new Error('Agent does not have a PDS URL') + } + + const {data: serviceAuth} = await agent.com.atproto.server.getServiceAuth({ + aud: aud ?? pdsAud, + lxm, + exp, + }) + + return serviceAuth.token + }, [agent, aud, lxm, exp]) +} + +export function useVideoUploadLimits() { + const agent = useVideoAgent() + const getToken = useServiceAuthToken({ + lxm: 'app.bsky.video.getUploadLimits', + aud: VIDEO_SERVICE_DID, + }) + const {_} = useLingui() + + return useCallback(async () => { + const {data: limits} = await agent.app.bsky.video + .getUploadLimits( + {}, + {headers: {Authorization: `Bearer ${await getToken()}`}}, + ) + .catch(err => { + if (err instanceof Error) { + throw new UploadLimitError(err.message) + } else { + throw err + } + }) + + if (!limits.canUpload) { + if (limits.message) { + throw new UploadLimitError(limits.message) + } else { + throw new UploadLimitError( + _( + msg`You have temporarily reached the limit for video uploads. Please try again later.`, + ), + ) + } + } + }, [agent, _, getToken]) +} |