aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/webfinger.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers/webfinger.ts')
-rw-r--r--server/helpers/webfinger.ts33
1 files changed, 18 insertions, 15 deletions
diff --git a/server/helpers/webfinger.ts b/server/helpers/webfinger.ts
index d98068cd7..76444fbe3 100644
--- a/server/helpers/webfinger.ts
+++ b/server/helpers/webfinger.ts
@@ -1,6 +1,6 @@
1import * as WebFinger from 'webfinger.js' 1import * as WebFinger from 'webfinger.js'
2import { WebFingerData } from '../../shared' 2import { WebFingerData } from '../../shared'
3import { fetchRemoteAccount } from '../lib/activitypub' 3import { ActorModel } from '../models/activitypub/actor'
4import { isTestInstance } from './core-utils' 4import { isTestInstance } from './core-utils'
5import { isActivityPubUrlValid } from './custom-validators/activitypub' 5import { isActivityPubUrlValid } from './custom-validators/activitypub'
6 6
@@ -11,30 +11,33 @@ const webfinger = new WebFinger({
11 request_timeout: 3000 11 request_timeout: 3000
12}) 12})
13 13
14async function getAccountFromWebfinger (nameWithHost: string) { 14async function loadActorUrlOrGetFromWebfinger (name: string, host: string) {
15 const webfingerData: WebFingerData = await webfingerLookup(nameWithHost) 15 const actor = await ActorModel.loadByNameAndHost(name, host)
16 if (actor) return actor.url
16 17
17 if (Array.isArray(webfingerData.links) === false) throw new Error('WebFinger links is not an array.') 18 const webfingerData: WebFingerData = await webfingerLookup(name + '@' + host)
18 19 return getLinkOrThrow(webfingerData)
19 const selfLink = webfingerData.links.find(l => l.rel === 'self')
20 if (selfLink === undefined || isActivityPubUrlValid(selfLink.href) === false) {
21 throw new Error('Cannot find self link or href is not a valid URL.')
22 }
23
24 const account = await fetchRemoteAccount(selfLink.href)
25 if (account === undefined) throw new Error('Cannot fetch remote account ' + selfLink.href)
26
27 return account
28} 20}
29 21
30// --------------------------------------------------------------------------- 22// ---------------------------------------------------------------------------
31 23
32export { 24export {
33 getAccountFromWebfinger 25 loadActorUrlOrGetFromWebfinger
34} 26}
35 27
36// --------------------------------------------------------------------------- 28// ---------------------------------------------------------------------------
37 29
30function getLinkOrThrow (webfingerData: WebFingerData) {
31 if (Array.isArray(webfingerData.links) === false) throw new Error('WebFinger links is not an array.')
32
33 const selfLink = webfingerData.links.find(l => l.rel === 'self')
34 if (selfLink === undefined || isActivityPubUrlValid(selfLink.href) === false) {
35 throw new Error('Cannot find self link or href is not a valid URL.')
36 }
37
38 return selfLink.href
39}
40
38function webfingerLookup (nameWithHost: string) { 41function webfingerLookup (nameWithHost: string) {
39 return new Promise<WebFingerData>((res, rej) => { 42 return new Promise<WebFingerData>((res, rej) => {
40 webfinger.lookup(nameWithHost, (err, p) => { 43 webfinger.lookup(nameWithHost, (err, p) => {