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