diff options
Diffstat (limited to 'server/lib/activitypub/actors/refresh.ts')
-rw-r--r-- | server/lib/activitypub/actors/refresh.ts | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/server/lib/activitypub/actors/refresh.ts b/server/lib/activitypub/actors/refresh.ts new file mode 100644 index 000000000..ff3b249d0 --- /dev/null +++ b/server/lib/activitypub/actors/refresh.ts | |||
@@ -0,0 +1,63 @@ | |||
1 | import { ActorFetchByUrlType } from '@server/helpers/actor' | ||
2 | import { logger } from '@server/helpers/logger' | ||
3 | import { PeerTubeRequestError } from '@server/helpers/requests' | ||
4 | import { getUrlFromWebfinger } from '@server/helpers/webfinger' | ||
5 | import { ActorModel } from '@server/models/actor/actor' | ||
6 | import { MActorAccountChannelId, MActorFull } from '@server/types/models' | ||
7 | import { HttpStatusCode } from '@shared/core-utils' | ||
8 | import { fetchRemoteActor } from './shared' | ||
9 | import { APActorUpdater } from './updater' | ||
10 | |||
11 | async function refreshActorIfNeeded <T extends MActorFull | MActorAccountChannelId> ( | ||
12 | actorArg: T, | ||
13 | fetchedType: ActorFetchByUrlType | ||
14 | ): Promise<{ actor: T | MActorFull, refreshed: boolean }> { | ||
15 | if (!actorArg.isOutdated()) return { actor: actorArg, refreshed: false } | ||
16 | |||
17 | // We need more attributes | ||
18 | const actor = fetchedType === 'all' | ||
19 | ? actorArg as MActorFull | ||
20 | : await ActorModel.loadByUrlAndPopulateAccountAndChannel(actorArg.url) | ||
21 | |||
22 | try { | ||
23 | const actorUrl = await getActorUrl(actor) | ||
24 | const { actorObject } = await fetchRemoteActor(actorUrl) | ||
25 | |||
26 | if (actorObject === undefined) { | ||
27 | logger.warn('Cannot fetch remote actor in refresh actor.') | ||
28 | return { actor, refreshed: false } | ||
29 | } | ||
30 | |||
31 | const updater = new APActorUpdater(actorObject, actor) | ||
32 | await updater.update() | ||
33 | |||
34 | return { refreshed: true, actor } | ||
35 | } catch (err) { | ||
36 | if ((err as PeerTubeRequestError).statusCode === HttpStatusCode.NOT_FOUND_404) { | ||
37 | logger.info('Deleting actor %s because there is a 404 in refresh actor.', actor.url) | ||
38 | |||
39 | actor.Account | ||
40 | ? await actor.Account.destroy() | ||
41 | : await actor.VideoChannel.destroy() | ||
42 | |||
43 | return { actor: undefined, refreshed: false } | ||
44 | } | ||
45 | |||
46 | logger.warn('Cannot refresh actor %s.', actor.url, { err }) | ||
47 | return { actor, refreshed: false } | ||
48 | } | ||
49 | } | ||
50 | |||
51 | export { | ||
52 | refreshActorIfNeeded | ||
53 | } | ||
54 | |||
55 | // --------------------------------------------------------------------------- | ||
56 | |||
57 | function getActorUrl (actor: MActorFull) { | ||
58 | return getUrlFromWebfinger(actor.preferredUsername + '@' + actor.getHost()) | ||
59 | .catch(err => { | ||
60 | logger.warn('Cannot get actor URL from webfinger, keeping the old one.', err) | ||
61 | return actor.url | ||
62 | }) | ||
63 | } | ||