X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fwebfinger.ts;h=688bf2bab8106ed1f2983e4f511799e5d062d2d3;hb=73c695919c6569bfb667c36fc5a6b9b862130a0d;hp=d98068cd772d039fc7e81a22ac353de4689a1e31;hpb=3fd3ab2d34d512b160a5e6084d7609be7b4f4452;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/webfinger.ts b/server/helpers/webfinger.ts index d98068cd7..688bf2bab 100644 --- a/server/helpers/webfinger.ts +++ b/server/helpers/webfinger.ts @@ -1,8 +1,8 @@ import * as WebFinger from 'webfinger.js' import { WebFingerData } from '../../shared' -import { fetchRemoteAccount } from '../lib/activitypub' +import { ActorModel } from '../models/activitypub/actor' import { isTestInstance } from './core-utils' -import { isActivityPubUrlValid } from './custom-validators/activitypub' +import { isActivityPubUrlValid } from './custom-validators/activitypub/misc' const webfinger = new WebFinger({ webfist_fallback: false, @@ -11,30 +11,38 @@ const webfinger = new WebFinger({ request_timeout: 3000 }) -async function getAccountFromWebfinger (nameWithHost: string) { - const webfingerData: WebFingerData = await webfingerLookup(nameWithHost) +async function loadActorUrlOrGetFromWebfinger (name: string, host: string) { + const actor = await ActorModel.loadByNameAndHost(name, host) + if (actor) return actor.url - 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) { - throw new Error('Cannot find self link or href is not a valid URL.') - } - - const account = await fetchRemoteAccount(selfLink.href) - if (account === undefined) throw new Error('Cannot fetch remote account ' + selfLink.href) + return getUrlFromWebfinger(name, host) +} - return account +async function getUrlFromWebfinger (name: string, host: string) { + const webfingerData: WebFingerData = await webfingerLookup(name + '@' + host) + return getLinkOrThrow(webfingerData) } // --------------------------------------------------------------------------- export { - getAccountFromWebfinger + getUrlFromWebfinger, + loadActorUrlOrGetFromWebfinger } // --------------------------------------------------------------------------- +function getLinkOrThrow (webfingerData: WebFingerData) { + 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) { + throw new Error('Cannot find self link or href is not a valid URL.') + } + + return selfLink.href +} + function webfingerLookup (nameWithHost: string) { return new Promise((res, rej) => { webfinger.lookup(nameWithHost, (err, p) => {