forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1import {type BskyAgent} from '@atproto/api'
2import {type I18n} from '@lingui/core'
3import {msg} from '@lingui/core/macro'
4
5import {VIDEO_SERVICE_DID} from '#/lib/constants'
6import {UploadLimitError} from '#/lib/media/video/errors'
7import {getServiceAuthAudFromUrl} from '#/lib/strings/url-helpers'
8import {pdsAgent} from '#/state/session/agent'
9import {createVideoAgent} from './util'
10
11export async function getServiceAuthToken({
12 agent,
13 aud,
14 lxm,
15 exp,
16}: {
17 agent: BskyAgent
18 aud?: string
19 lxm: string
20 exp?: number
21}) {
22 const pdsAud = getServiceAuthAudFromUrl(agent.dispatchUrl)
23 if (!pdsAud) {
24 throw new Error('Agent does not have a PDS URL')
25 }
26 const {data: serviceAuth} = await pdsAgent(
27 agent,
28 ).com.atproto.server.getServiceAuth({
29 aud: aud ?? pdsAud,
30 lxm,
31 exp,
32 })
33 return serviceAuth.token
34}
35
36export async function getVideoUploadLimits(agent: BskyAgent, _: I18n['_']) {
37 const token = await getServiceAuthToken({
38 agent,
39 lxm: 'app.bsky.video.getUploadLimits',
40 aud: VIDEO_SERVICE_DID,
41 })
42 const videoAgent = createVideoAgent()
43 const {data: limits} = await videoAgent.app.bsky.video
44 .getUploadLimits({}, {headers: {Authorization: `Bearer ${token}`}})
45 .catch(err => {
46 if (err instanceof Error) {
47 throw new UploadLimitError(err.message)
48 } else {
49 throw err
50 }
51 })
52
53 if (!limits.canUpload) {
54 if (limits.message) {
55 throw new UploadLimitError(limits.message)
56 } else {
57 throw new UploadLimitError(
58 _(
59 msg`You have temporarily reached the limit for video uploads. Please try again later.`,
60 ),
61 )
62 }
63 }
64}