diff options
author | Chocobozzz <me@florianbigard.com> | 2021-06-09 13:34:40 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-06-09 13:34:40 +0200 |
commit | 4ead40e7766f5964ecd9a11766ff56b95090fe1c (patch) | |
tree | f1fd6b2e1604c4025eae86fb87cf81daf609850f /server/lib/activitypub | |
parent | a6a12dae10658a1fa7120ad39702c07137143954 (diff) | |
download | PeerTube-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.ts | 2 | ||||
-rw-r--r-- | server/lib/activitypub/actors/refresh.ts | 34 |
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 @@ | |||
1 | import { logger, loggerTagsFactory } from '@server/helpers/logger' | 1 | import { logger, loggerTagsFactory } from '@server/helpers/logger' |
2 | import { PromiseCache } from '@server/helpers/promise-cache' | ||
2 | import { PeerTubeRequestError } from '@server/helpers/requests' | 3 | import { PeerTubeRequestError } from '@server/helpers/requests' |
3 | import { ActorLoadByUrlType } from '@server/lib/model-loaders' | 4 | import { ActorLoadByUrlType } from '@server/lib/model-loaders' |
4 | import { ActorModel } from '@server/models/actor/actor' | 5 | import { ActorModel } from '@server/models/actor/actor' |
@@ -8,11 +9,30 @@ import { fetchRemoteActor } from './shared' | |||
8 | import { APActorUpdater } from './updater' | 9 | import { APActorUpdater } from './updater' |
9 | import { getUrlFromWebfinger } from './webfinger' | 10 | import { getUrlFromWebfinger } from './webfinger' |
10 | 11 | ||
11 | async function refreshActorIfNeeded <T extends MActorFull | MActorAccountChannelId> ( | 12 | type RefreshResult <T> = Promise<{ actor: T | MActorFull, refreshed: boolean }> |
12 | actorArg: T, | 13 | |
14 | type 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 | |
19 | const promiseCache = new PromiseCache(doRefresh, (options: RefreshOptions<MActorFull | MActorAccountChannelId>) => options.actor.url) | ||
20 | |||
21 | function 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 | |||
28 | export { | ||
29 | refreshActorIfNeeded | ||
30 | } | ||
31 | |||
32 | // --------------------------------------------------------------------------- | ||
33 | |||
34 | async 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 | ||
55 | export { | ||
56 | refreshActorIfNeeded | ||
57 | } | ||
58 | |||
59 | // --------------------------------------------------------------------------- | ||
60 | |||
61 | function getActorUrl (actor: MActorFull) { | 75 | function getActorUrl (actor: MActorFull) { |
62 | return getUrlFromWebfinger(actor.preferredUsername + '@' + actor.getHost()) | 76 | return getUrlFromWebfinger(actor.preferredUsername + '@' + actor.getHost()) |
63 | .catch(err => { | 77 | .catch(err => { |