]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/actors/refresh.ts
Merge branch 'master' into release/3.3.0
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / actors / refresh.ts
1 import { logger, loggerTagsFactory } from '@server/helpers/logger'
2 import { PromiseCache } from '@server/helpers/promise-cache'
3 import { PeerTubeRequestError } from '@server/helpers/requests'
4 import { ActorLoadByUrlType } from '@server/lib/model-loaders'
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 import { getUrlFromWebfinger } from './webfinger'
11
12 type RefreshResult <T> = Promise<{ actor: T | MActorFull, refreshed: boolean }>
13
14 type RefreshOptions <T> = {
15 actor: T
16 fetchedType: ActorLoadByUrlType
17 }
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
36
37 // We need more attributes
38 const actor = fetchedType === 'all'
39 ? actorArg as MActorFull
40 : await ActorModel.loadByUrlAndPopulateAccountAndChannel(actorArg.url)
41
42 const lTags = loggerTagsFactory('ap', 'actor', 'refresh', actor.url)
43
44 logger.info('Refreshing actor %s.', actor.url, lTags())
45
46 try {
47 const actorUrl = await getActorUrl(actor)
48 const { actorObject } = await fetchRemoteActor(actorUrl)
49
50 if (actorObject === undefined) {
51 logger.warn('Cannot fetch remote actor in refresh actor.')
52 return { actor, refreshed: false }
53 }
54
55 const updater = new APActorUpdater(actorObject, actor)
56 await updater.update()
57
58 return { refreshed: true, actor }
59 } catch (err) {
60 if ((err as PeerTubeRequestError).statusCode === HttpStatusCode.NOT_FOUND_404) {
61 logger.info('Deleting actor %s because there is a 404 in refresh actor.', actor.url, lTags())
62
63 actor.Account
64 ? await actor.Account.destroy()
65 : await actor.VideoChannel.destroy()
66
67 return { actor: undefined, refreshed: false }
68 }
69
70 logger.warn('Cannot refresh actor %s.', actor.url, { err, ...lTags() })
71 return { actor, refreshed: false }
72 }
73 }
74
75 function getActorUrl (actor: MActorFull) {
76 return getUrlFromWebfinger(actor.preferredUsername + '@' + actor.getHost())
77 .catch(err => {
78 logger.warn('Cannot get actor URL from webfinger, keeping the old one.', err)
79 return actor.url
80 })
81 }