diff options
author | Chocobozzz <me@florianbigard.com> | 2017-12-14 17:38:41 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2017-12-19 10:53:16 +0100 |
commit | 50d6de9c286abcb34ff4234d56d9cbb803db7665 (patch) | |
tree | f1732b27edcd05c7877a8358b8312f1e38c287ed /server/helpers/webfinger.ts | |
parent | fadf619ad61a016c1c7fc53de5a8f398a4f77519 (diff) | |
download | PeerTube-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.ts | 33 |
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 @@ | |||
1 | import * as WebFinger from 'webfinger.js' | 1 | import * as WebFinger from 'webfinger.js' |
2 | import { WebFingerData } from '../../shared' | 2 | import { WebFingerData } from '../../shared' |
3 | import { fetchRemoteAccount } from '../lib/activitypub' | 3 | import { ActorModel } from '../models/activitypub/actor' |
4 | import { isTestInstance } from './core-utils' | 4 | import { isTestInstance } from './core-utils' |
5 | import { isActivityPubUrlValid } from './custom-validators/activitypub' | 5 | import { 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 | ||
14 | async function getAccountFromWebfinger (nameWithHost: string) { | 14 | async 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 | ||
32 | export { | 24 | export { |
33 | getAccountFromWebfinger | 25 | loadActorUrlOrGetFromWebfinger |
34 | } | 26 | } |
35 | 27 | ||
36 | // --------------------------------------------------------------------------- | 28 | // --------------------------------------------------------------------------- |
37 | 29 | ||
30 | function 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 | |||
38 | function webfingerLookup (nameWithHost: string) { | 41 | function 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) => { |