aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-06-03 17:33:44 +0200
committerChocobozzz <me@florianbigard.com>2021-06-03 18:03:36 +0200
commit10363c74c1d869f0e0c7bc4d0367b1f34d1bb6a4 (patch)
tree008f8dad8032684f46105a261b27b2d6f05b36eb /server/lib/activitypub
parent5e08989ede1a340b9edb94465a11b1e04bf24094 (diff)
downloadPeerTube-10363c74c1d869f0e0c7bc4d0367b1f34d1bb6a4.tar.gz
PeerTube-10363c74c1d869f0e0c7bc4d0367b1f34d1bb6a4.tar.zst
PeerTube-10363c74c1d869f0e0c7bc4d0367b1f34d1bb6a4.zip
Move middleware utils in middlewares
helpers modules should not import models
Diffstat (limited to 'server/lib/activitypub')
-rw-r--r--server/lib/activitypub/actors/get.ts2
-rw-r--r--server/lib/activitypub/actors/index.ts1
-rw-r--r--server/lib/activitypub/actors/refresh.ts4
-rw-r--r--server/lib/activitypub/actors/shared/index.ts2
-rw-r--r--server/lib/activitypub/actors/webfinger.ts67
-rw-r--r--server/lib/activitypub/videos/get.ts2
-rw-r--r--server/lib/activitypub/videos/refresh.ts2
7 files changed, 74 insertions, 6 deletions
diff --git a/server/lib/activitypub/actors/get.ts b/server/lib/activitypub/actors/get.ts
index 0d5bea789..e7e87a967 100644
--- a/server/lib/activitypub/actors/get.ts
+++ b/server/lib/activitypub/actors/get.ts
@@ -1,9 +1,9 @@
1 1
2import { checkUrlsSameHost, getAPId } from '@server/helpers/activitypub' 2import { checkUrlsSameHost, getAPId } from '@server/helpers/activitypub'
3import { ActorFetchByUrlType, fetchActorByUrl } from '@server/helpers/actor'
4import { retryTransactionWrapper } from '@server/helpers/database-utils' 3import { retryTransactionWrapper } from '@server/helpers/database-utils'
5import { logger } from '@server/helpers/logger' 4import { logger } from '@server/helpers/logger'
6import { JobQueue } from '@server/lib/job-queue' 5import { JobQueue } from '@server/lib/job-queue'
6import { ActorFetchByUrlType, fetchActorByUrl } from '@server/lib/model-loaders'
7import { MActor, MActorAccountChannelId, MActorAccountChannelIdActor, MActorAccountId, MActorFullActor } from '@server/types/models' 7import { MActor, MActorAccountChannelId, MActorAccountChannelIdActor, MActorAccountId, MActorFullActor } from '@server/types/models'
8import { ActivityPubActor } from '@shared/models' 8import { ActivityPubActor } from '@shared/models'
9import { refreshActorIfNeeded } from './refresh' 9import { refreshActorIfNeeded } from './refresh'
diff --git a/server/lib/activitypub/actors/index.ts b/server/lib/activitypub/actors/index.ts
index a54da6798..5ee2a6f1a 100644
--- a/server/lib/activitypub/actors/index.ts
+++ b/server/lib/activitypub/actors/index.ts
@@ -3,3 +3,4 @@ export * from './image'
3export * from './keys' 3export * from './keys'
4export * from './refresh' 4export * from './refresh'
5export * from './updater' 5export * from './updater'
6export * from './webfinger'
diff --git a/server/lib/activitypub/actors/refresh.ts b/server/lib/activitypub/actors/refresh.ts
index ff3b249d0..9f2289bfa 100644
--- a/server/lib/activitypub/actors/refresh.ts
+++ b/server/lib/activitypub/actors/refresh.ts
@@ -1,12 +1,12 @@
1import { ActorFetchByUrlType } from '@server/helpers/actor'
2import { logger } from '@server/helpers/logger' 1import { logger } from '@server/helpers/logger'
3import { PeerTubeRequestError } from '@server/helpers/requests' 2import { PeerTubeRequestError } from '@server/helpers/requests'
4import { getUrlFromWebfinger } from '@server/helpers/webfinger' 3import { ActorFetchByUrlType } from '@server/lib/model-loaders'
5import { ActorModel } from '@server/models/actor/actor' 4import { ActorModel } from '@server/models/actor/actor'
6import { MActorAccountChannelId, MActorFull } from '@server/types/models' 5import { MActorAccountChannelId, MActorFull } from '@server/types/models'
7import { HttpStatusCode } from '@shared/core-utils' 6import { HttpStatusCode } from '@shared/core-utils'
8import { fetchRemoteActor } from './shared' 7import { fetchRemoteActor } from './shared'
9import { APActorUpdater } from './updater' 8import { APActorUpdater } from './updater'
9import { getUrlFromWebfinger } from './webfinger'
10 10
11async function refreshActorIfNeeded <T extends MActorFull | MActorAccountChannelId> ( 11async function refreshActorIfNeeded <T extends MActorFull | MActorAccountChannelId> (
12 actorArg: T, 12 actorArg: T,
diff --git a/server/lib/activitypub/actors/shared/index.ts b/server/lib/activitypub/actors/shared/index.ts
index a2ff468cf..52af1a8e1 100644
--- a/server/lib/activitypub/actors/shared/index.ts
+++ b/server/lib/activitypub/actors/shared/index.ts
@@ -1,3 +1,3 @@
1export * from './creator' 1export * from './creator'
2export * from './url-to-object'
3export * from './object-to-model-attributes' 2export * from './object-to-model-attributes'
3export * from './url-to-object'
diff --git a/server/lib/activitypub/actors/webfinger.ts b/server/lib/activitypub/actors/webfinger.ts
new file mode 100644
index 000000000..cf8eddfc7
--- /dev/null
+++ b/server/lib/activitypub/actors/webfinger.ts
@@ -0,0 +1,67 @@
1import * as WebFinger from 'webfinger.js'
2import { isTestInstance } from '@server/helpers/core-utils'
3import { isActivityPubUrlValid } from '@server/helpers/custom-validators/activitypub/misc'
4import { WEBSERVER } from '@server/initializers/constants'
5import { ActorModel } from '@server/models/actor/actor'
6import { MActorFull } from '@server/types/models'
7import { WebFingerData } from '@shared/models'
8
9const webfinger = new WebFinger({
10 webfist_fallback: false,
11 tls_only: isTestInstance(),
12 uri_fallback: false,
13 request_timeout: 3000
14})
15
16async function loadActorUrlOrGetFromWebfinger (uriArg: string) {
17 // Handle strings like @toto@example.com
18 const uri = uriArg.startsWith('@') ? uriArg.slice(1) : uriArg
19
20 const [ name, host ] = uri.split('@')
21 let actor: MActorFull
22
23 if (!host || host === WEBSERVER.HOST) {
24 actor = await ActorModel.loadLocalByName(name)
25 } else {
26 actor = await ActorModel.loadByNameAndHost(name, host)
27 }
28
29 if (actor) return actor.url
30
31 return getUrlFromWebfinger(uri)
32}
33
34async function getUrlFromWebfinger (uri: string) {
35 const webfingerData: WebFingerData = await webfingerLookup(uri)
36 return getLinkOrThrow(webfingerData)
37}
38
39// ---------------------------------------------------------------------------
40
41export {
42 getUrlFromWebfinger,
43 loadActorUrlOrGetFromWebfinger
44}
45
46// ---------------------------------------------------------------------------
47
48function getLinkOrThrow (webfingerData: WebFingerData) {
49 if (Array.isArray(webfingerData.links) === false) throw new Error('WebFinger links is not an array.')
50
51 const selfLink = webfingerData.links.find(l => l.rel === 'self')
52 if (selfLink === undefined || isActivityPubUrlValid(selfLink.href) === false) {
53 throw new Error('Cannot find self link or href is not a valid URL.')
54 }
55
56 return selfLink.href
57}
58
59function webfingerLookup (nameWithHost: string) {
60 return new Promise<WebFingerData>((res, rej) => {
61 webfinger.lookup(nameWithHost, (err, p) => {
62 if (err) return rej(err)
63
64 return res(p.object)
65 })
66 })
67}
diff --git a/server/lib/activitypub/videos/get.ts b/server/lib/activitypub/videos/get.ts
index a8c41e178..38ba4978c 100644
--- a/server/lib/activitypub/videos/get.ts
+++ b/server/lib/activitypub/videos/get.ts
@@ -1,7 +1,7 @@
1import { getAPId } from '@server/helpers/activitypub' 1import { getAPId } from '@server/helpers/activitypub'
2import { retryTransactionWrapper } from '@server/helpers/database-utils' 2import { retryTransactionWrapper } from '@server/helpers/database-utils'
3import { fetchVideoByUrl, VideoFetchByUrlType } from '@server/helpers/video'
4import { JobQueue } from '@server/lib/job-queue' 3import { JobQueue } from '@server/lib/job-queue'
4import { fetchVideoByUrl, VideoFetchByUrlType } from '@server/lib/model-loaders'
5import { MVideoAccountLightBlacklistAllFiles, MVideoImmutable, MVideoThumbnail } from '@server/types/models' 5import { MVideoAccountLightBlacklistAllFiles, MVideoImmutable, MVideoThumbnail } from '@server/types/models'
6import { refreshVideoIfNeeded } from './refresh' 6import { refreshVideoIfNeeded } from './refresh'
7import { APVideoCreator, fetchRemoteVideo, SyncParam, syncVideoExternalAttributes } from './shared' 7import { APVideoCreator, fetchRemoteVideo, SyncParam, syncVideoExternalAttributes } from './shared'
diff --git a/server/lib/activitypub/videos/refresh.ts b/server/lib/activitypub/videos/refresh.ts
index 71a4e75b0..f1a3a6fac 100644
--- a/server/lib/activitypub/videos/refresh.ts
+++ b/server/lib/activitypub/videos/refresh.ts
@@ -1,7 +1,7 @@
1import { logger, loggerTagsFactory } from '@server/helpers/logger' 1import { logger, loggerTagsFactory } from '@server/helpers/logger'
2import { PeerTubeRequestError } from '@server/helpers/requests' 2import { PeerTubeRequestError } from '@server/helpers/requests'
3import { VideoFetchByUrlType } from '@server/helpers/video'
4import { ActorFollowScoreCache } from '@server/lib/files-cache' 3import { ActorFollowScoreCache } from '@server/lib/files-cache'
4import { VideoFetchByUrlType } from '@server/lib/model-loaders'
5import { VideoModel } from '@server/models/video/video' 5import { VideoModel } from '@server/models/video/video'
6import { MVideoAccountLightBlacklistAllFiles, MVideoThumbnail } from '@server/types/models' 6import { MVideoAccountLightBlacklistAllFiles, MVideoThumbnail } from '@server/types/models'
7import { HttpStatusCode } from '@shared/core-utils' 7import { HttpStatusCode } from '@shared/core-utils'