aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/webfinger.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2017-12-14 17:38:41 +0100
committerChocobozzz <me@florianbigard.com>2017-12-19 10:53:16 +0100
commit50d6de9c286abcb34ff4234d56d9cbb803db7665 (patch)
treef1732b27edcd05c7877a8358b8312f1e38c287ed /server/helpers/webfinger.ts
parentfadf619ad61a016c1c7fc53de5a8f398a4f77519 (diff)
downloadPeerTube-50d6de9c286abcb34ff4234d56d9cbb803db7665.tar.gz
PeerTube-50d6de9c286abcb34ff4234d56d9cbb803db7665.tar.zst
PeerTube-50d6de9c286abcb34ff4234d56d9cbb803db7665.zip
Begin moving video channel to actor
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) => {