X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fwebfinger.ts;h=33367f651793fd3b053390e929d5f5f411162c9a;hb=2539932e16129992a2c0889b4ff527c265a8e2c7;hp=31417e72883469d31d904ddcba2d63b72d2b003a;hpb=608624252466acf9f1d9ee1c1170bd4fe4d18d18;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/webfinger.ts b/server/helpers/webfinger.ts index 31417e728..33367f651 100644 --- a/server/helpers/webfinger.ts +++ b/server/helpers/webfinger.ts @@ -1,9 +1,10 @@ import * as WebFinger from 'webfinger.js' - -import { isTestInstance } from './core-utils' -import { isActivityPubUrlValid } from './custom-validators' import { WebFingerData } from '../../shared' -import { fetchRemoteAccountAndCreateServer } from './activitypub' +import { WEBSERVER } from '../initializers/constants' +import { ActorModel } from '../models/actor/actor' +import { MActorFull } from '../types/models' +import { isTestInstance } from './core-utils' +import { isActivityPubUrlValid } from './custom-validators/activitypub/misc' const webfinger = new WebFinger({ webfist_fallback: false, @@ -12,30 +13,49 @@ const webfinger = new WebFinger({ request_timeout: 3000 }) -async function getAccountFromWebfinger (nameWithHost: string) { - const webfingerData: WebFingerData = await webfingerLookup(nameWithHost) +async function loadActorUrlOrGetFromWebfinger (uriArg: string) { + // Handle strings like @toto@example.com + const uri = uriArg.startsWith('@') ? uriArg.slice(1) : uriArg - if (Array.isArray(webfingerData.links) === false) throw new Error('WebFinger links is not an array.') + const [ name, host ] = uri.split('@') + let actor: MActorFull - 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.') + if (!host || host === WEBSERVER.HOST) { + actor = await ActorModel.loadLocalByName(name) + } else { + actor = await ActorModel.loadByNameAndHost(name, host) } - const res = await fetchRemoteAccountAndCreateServer(selfLink.href) - if (res === undefined) throw new Error('Cannot fetch and create server of remote account ' + selfLink.href) + if (actor) return actor.url + + return getUrlFromWebfinger(uri) +} - return res.account +async function getUrlFromWebfinger (uri: string) { + const webfingerData: WebFingerData = await webfingerLookup(uri) + 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) => {