]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/webfinger.ts
Fetch video likes/dislikes too
[github/Chocobozzz/PeerTube.git] / server / helpers / webfinger.ts
index 164ae4951697cf65c298a038792f96e4ffe10df1..a5b4785feb17e8ecaa1015959c161cf638704a68 100644 (file)
@@ -1,9 +1,9 @@
 import * as WebFinger from 'webfinger.js'
+import { WebFingerData } from '../../shared'
+import { fetchRemoteAccount } from '../lib/activitypub/account'
 
 import { isTestInstance } from './core-utils'
 import { isActivityPubUrlValid } from './custom-validators'
-import { WebFingerData } from '../../shared'
-import { fetchRemoteAccountAndCreatePod } from './activitypub'
 
 const webfinger = new WebFinger({
   webfist_fallback: false,
@@ -12,15 +12,18 @@ const webfinger = new WebFinger({
   request_timeout: 3000
 })
 
-async function getAccountFromWebfinger (url: string) {
-  const webfingerData: WebFingerData = await webfingerLookup(url)
+async function getAccountFromWebfinger (nameWithHost: string) {
+  const webfingerData: WebFingerData = await webfingerLookup(nameWithHost)
 
-  if (Array.isArray(webfingerData.links) === false) return undefined
+  if (Array.isArray(webfingerData.links) === false) throw new Error('WebFinger links is not an array.')
 
   const selfLink = webfingerData.links.find(l => l.rel === 'self')
-  if (selfLink === undefined || isActivityPubUrlValid(selfLink.href) === false) return undefined
+  if (selfLink === undefined || isActivityPubUrlValid(selfLink.href) === false) {
+    throw new Error('Cannot find self link or href is not a valid URL.')
+  }
 
-  const { account } = await fetchRemoteAccountAndCreatePod(selfLink.href)
+  const account = await fetchRemoteAccount(selfLink.href)
+  if (account === undefined) throw new Error('Cannot fetch remote account ' + selfLink.href)
 
   return account
 }
@@ -33,12 +36,12 @@ export {
 
 // ---------------------------------------------------------------------------
 
-function webfingerLookup (url: string) {
+function webfingerLookup (nameWithHost: string) {
   return new Promise<WebFingerData>((res, rej) => {
-    webfinger.lookup(url, (err, p) => {
+    webfinger.lookup(nameWithHost, (err, p) => {
       if (err) return rej(err)
 
-      return p
+      return res(p.object)
     })
   })
 }