blob: 1c7ec47174e421d8184f2a15f1ef926ff96fc006 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
import * as WebFinger from 'webfinger.js'
import { isProdInstance } from '@server/helpers/core-utils'
import { isActivityPubUrlValid } from '@server/helpers/custom-validators/activitypub/misc'
import { REQUEST_TIMEOUT, WEBSERVER } from '@server/initializers/constants'
import { ActorModel } from '@server/models/actor/actor'
import { MActorFull } from '@server/types/models'
import { WebFingerData } from '@shared/models'
const webfinger = new WebFinger({
webfist_fallback: false,
tls_only: isProdInstance(),
uri_fallback: false,
request_timeout: REQUEST_TIMEOUT
})
async function loadActorUrlOrGetFromWebfinger (uriArg: string) {
// Handle strings like @toto@example.com
const uri = uriArg.startsWith('@') ? uriArg.slice(1) : uriArg
const [ name, host ] = uri.split('@')
let actor: MActorFull
if (!host || host === WEBSERVER.HOST) {
actor = await ActorModel.loadLocalByName(name)
} else {
actor = await ActorModel.loadByNameAndHost(name, host)
}
if (actor) return actor.url
return getUrlFromWebfinger(uri)
}
async function getUrlFromWebfinger (uri: string) {
const webfingerData: WebFingerData = await webfingerLookup(uri)
return getLinkOrThrow(webfingerData)
}
// ---------------------------------------------------------------------------
export {
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<WebFingerData>((res, rej) => {
webfinger.lookup(nameWithHost, (err, p) => {
if (err) return rej(err)
return res(p.object)
})
})
}
|