import { isTestInstance } from './core-utils'
import { isActivityPubUrlValid } from './custom-validators'
import { WebFingerData } from '../../shared'
-import { fetchRemoteAccountAndCreatePod } from './activitypub'
+import { fetchRemoteAccountAndCreateServer } from './activitypub'
const webfinger = new WebFinger({
webfist_fallback: false,
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 res = await fetchRemoteAccountAndCreateServer(selfLink.href)
+ if (res === undefined) throw new Error('Cannot fetch and create server of remote account ' + selfLink.href)
- return account
+ return res.account
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
-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)
})
})
}