aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-06-09 13:34:40 +0200
committerChocobozzz <me@florianbigard.com>2021-06-09 13:34:40 +0200
commit4ead40e7766f5964ecd9a11766ff56b95090fe1c (patch)
treef1fd6b2e1604c4025eae86fb87cf81daf609850f /server/lib/activitypub
parenta6a12dae10658a1fa7120ad39702c07137143954 (diff)
downloadPeerTube-4ead40e7766f5964ecd9a11766ff56b95090fe1c.tar.gz
PeerTube-4ead40e7766f5964ecd9a11766ff56b95090fe1c.tar.zst
PeerTube-4ead40e7766f5964ecd9a11766ff56b95090fe1c.zip
Cache refresh actor promise
Diffstat (limited to 'server/lib/activitypub')
-rw-r--r--server/lib/activitypub/actors/get.ts2
-rw-r--r--server/lib/activitypub/actors/refresh.ts34
2 files changed, 25 insertions, 11 deletions
diff --git a/server/lib/activitypub/actors/get.ts b/server/lib/activitypub/actors/get.ts
index de93aa964..501ff74e3 100644
--- a/server/lib/activitypub/actors/get.ts
+++ b/server/lib/activitypub/actors/get.ts
@@ -56,7 +56,7 @@ async function getOrCreateAPActor (
56 if (actor.Account) (actor as MActorAccountChannelIdActor).Account.Actor = actor 56 if (actor.Account) (actor as MActorAccountChannelIdActor).Account.Actor = actor
57 if (actor.VideoChannel) (actor as MActorAccountChannelIdActor).VideoChannel.Actor = actor 57 if (actor.VideoChannel) (actor as MActorAccountChannelIdActor).VideoChannel.Actor = actor
58 58
59 const { actor: actorRefreshed, refreshed } = await refreshActorIfNeeded(actor, fetchType) 59 const { actor: actorRefreshed, refreshed } = await refreshActorIfNeeded({ actor, fetchedType: fetchType })
60 if (!actorRefreshed) throw new Error('Actor ' + actor.url + ' does not exist anymore.') 60 if (!actorRefreshed) throw new Error('Actor ' + actor.url + ' does not exist anymore.')
61 61
62 await scheduleOutboxFetchIfNeeded(actor, created, refreshed, updateCollections) 62 await scheduleOutboxFetchIfNeeded(actor, created, refreshed, updateCollections)
diff --git a/server/lib/activitypub/actors/refresh.ts b/server/lib/activitypub/actors/refresh.ts
index 1f7cfcd8f..b2fe3932f 100644
--- a/server/lib/activitypub/actors/refresh.ts
+++ b/server/lib/activitypub/actors/refresh.ts
@@ -1,4 +1,5 @@
1import { logger, loggerTagsFactory } from '@server/helpers/logger' 1import { logger, loggerTagsFactory } from '@server/helpers/logger'
2import { PromiseCache } from '@server/helpers/promise-cache'
2import { PeerTubeRequestError } from '@server/helpers/requests' 3import { PeerTubeRequestError } from '@server/helpers/requests'
3import { ActorLoadByUrlType } from '@server/lib/model-loaders' 4import { ActorLoadByUrlType } from '@server/lib/model-loaders'
4import { ActorModel } from '@server/models/actor/actor' 5import { ActorModel } from '@server/models/actor/actor'
@@ -8,11 +9,30 @@ import { fetchRemoteActor } from './shared'
8import { APActorUpdater } from './updater' 9import { APActorUpdater } from './updater'
9import { getUrlFromWebfinger } from './webfinger' 10import { getUrlFromWebfinger } from './webfinger'
10 11
11async function refreshActorIfNeeded <T extends MActorFull | MActorAccountChannelId> ( 12type RefreshResult <T> = Promise<{ actor: T | MActorFull, refreshed: boolean }>
12 actorArg: T, 13
14type RefreshOptions <T> = {
15 actor: T
13 fetchedType: ActorLoadByUrlType 16 fetchedType: ActorLoadByUrlType
14): Promise<{ actor: T | MActorFull, refreshed: boolean }> { 17}
15 if (!actorArg.isOutdated()) return { actor: actorArg, refreshed: false } 18
19const promiseCache = new PromiseCache(doRefresh, (options: RefreshOptions<MActorFull | MActorAccountChannelId>) => options.actor.url)
20
21function refreshActorIfNeeded <T extends MActorFull | MActorAccountChannelId> (options: RefreshOptions<T>): RefreshResult <T> {
22 const actorArg = options.actor
23 if (!actorArg.isOutdated()) return Promise.resolve({ actor: actorArg, refreshed: false })
24
25 return promiseCache.run(options)
26}
27
28export {
29 refreshActorIfNeeded
30}
31
32// ---------------------------------------------------------------------------
33
34async function doRefresh <T extends MActorFull | MActorAccountChannelId> (options: RefreshOptions<T>): RefreshResult <MActorFull> {
35 const { actor: actorArg, fetchedType } = options
16 36
17 // We need more attributes 37 // We need more attributes
18 const actor = fetchedType === 'all' 38 const actor = fetchedType === 'all'
@@ -52,12 +72,6 @@ async function refreshActorIfNeeded <T extends MActorFull | MActorAccountChannel
52 } 72 }
53} 73}
54 74
55export {
56 refreshActorIfNeeded
57}
58
59// ---------------------------------------------------------------------------
60
61function getActorUrl (actor: MActorFull) { 75function getActorUrl (actor: MActorFull) {
62 return getUrlFromWebfinger(actor.preferredUsername + '@' + actor.getHost()) 76 return getUrlFromWebfinger(actor.preferredUsername + '@' + actor.getHost())
63 .catch(err => { 77 .catch(err => {