]>
Commit | Line | Data |
---|---|---|
e4f97bab | 1 | import * as WebFinger from 'webfinger.js' |
892211e8 | 2 | import { WebFingerData } from '../../shared' |
50d6de9c | 3 | import { ActorModel } from '../models/activitypub/actor' |
e4f97bab | 4 | import { isTestInstance } from './core-utils' |
da854ddd | 5 | import { isActivityPubUrlValid } from './custom-validators/activitypub/misc' |
e4f97bab C |
6 | |
7 | const webfinger = new WebFinger({ | |
8 | webfist_fallback: false, | |
9 | tls_only: isTestInstance(), | |
10 | uri_fallback: false, | |
11 | request_timeout: 3000 | |
12 | }) | |
13 | ||
06a05d5f C |
14 | async function loadActorUrlOrGetFromWebfinger (uri: string) { |
15 | const [ name, host ] = uri.split('@') | |
16 | ||
50d6de9c C |
17 | const actor = await ActorModel.loadByNameAndHost(name, host) |
18 | if (actor) return actor.url | |
e4f97bab | 19 | |
06a05d5f | 20 | return getUrlFromWebfinger(uri) |
a5625b41 C |
21 | } |
22 | ||
06a05d5f C |
23 | async function getUrlFromWebfinger (uri: string) { |
24 | const webfingerData: WebFingerData = await webfingerLookup(uri) | |
50d6de9c | 25 | return getLinkOrThrow(webfingerData) |
e4f97bab C |
26 | } |
27 | ||
28 | // --------------------------------------------------------------------------- | |
29 | ||
30 | export { | |
a5625b41 | 31 | getUrlFromWebfinger, |
50d6de9c | 32 | loadActorUrlOrGetFromWebfinger |
e4f97bab C |
33 | } |
34 | ||
35 | // --------------------------------------------------------------------------- | |
36 | ||
50d6de9c C |
37 | function getLinkOrThrow (webfingerData: WebFingerData) { |
38 | if (Array.isArray(webfingerData.links) === false) throw new Error('WebFinger links is not an array.') | |
39 | ||
40 | const selfLink = webfingerData.links.find(l => l.rel === 'self') | |
41 | if (selfLink === undefined || isActivityPubUrlValid(selfLink.href) === false) { | |
42 | throw new Error('Cannot find self link or href is not a valid URL.') | |
43 | } | |
44 | ||
45 | return selfLink.href | |
46 | } | |
47 | ||
350e31d6 | 48 | function webfingerLookup (nameWithHost: string) { |
e4f97bab | 49 | return new Promise<WebFingerData>((res, rej) => { |
350e31d6 | 50 | webfinger.lookup(nameWithHost, (err, p) => { |
e4f97bab C |
51 | if (err) return rej(err) |
52 | ||
350e31d6 | 53 | return res(p.object) |
e4f97bab C |
54 | }) |
55 | }) | |
56 | } |