about summary refs log tree commit diff
path: root/src/components/live/queries.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/components/live/queries.ts')
-rw-r--r--src/components/live/queries.ts30
1 files changed, 28 insertions, 2 deletions
diff --git a/src/components/live/queries.ts b/src/components/live/queries.ts
index 1958ab49d..08cb0fc5a 100644
--- a/src/components/live/queries.ts
+++ b/src/components/live/queries.ts
@@ -7,17 +7,43 @@ import {
 import {retry} from '@atproto/common-web'
 import {msg} from '@lingui/macro'
 import {useLingui} from '@lingui/react'
-import {useMutation, useQueryClient} from '@tanstack/react-query'
+import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query'
 
 import {uploadBlob} from '#/lib/api'
 import {imageToThumb} from '#/lib/api/resolve'
-import {type LinkMeta} from '#/lib/link-meta/link-meta'
+import {getLinkMeta, type LinkMeta} from '#/lib/link-meta/link-meta'
 import {logger} from '#/logger'
 import {updateProfileShadow} from '#/state/cache/profile-shadow'
+import {useLiveNowConfig} from '#/state/service-config'
 import {useAgent, useSession} from '#/state/session'
 import * as Toast from '#/view/com/util/Toast'
 import {useDialogContext} from '#/components/Dialog'
 
+export function useLiveLinkMetaQuery(url: string | null) {
+  const liveNowConfig = useLiveNowConfig()
+  const {currentAccount} = useSession()
+  const {_} = useLingui()
+
+  const agent = useAgent()
+  return useQuery({
+    enabled: !!url,
+    queryKey: ['link-meta', url],
+    queryFn: async () => {
+      if (!url) return undefined
+      const config = liveNowConfig.find(cfg => cfg.did === currentAccount?.did)
+
+      if (!config) throw new Error(_(msg`You are not allowed to go live`))
+
+      const urlp = new URL(url)
+      if (!config.domains.includes(urlp.hostname)) {
+        throw new Error(_(msg`${urlp.hostname} is not a valid URL`))
+      }
+
+      return await getLinkMeta(agent, url)
+    },
+  })
+}
+
 export function useUpsertLiveStatusMutation(
   duration: number,
   linkMeta: LinkMeta | null | undefined,