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/models'
8 import { fetchRemoteActor } from './shared'
9 import { APActorUpdater } from './updater'
10 import { getUrlFromWebfinger } from './webfinger'
12 type RefreshResult <T> = Promise<{ actor: T | MActorFull, refreshed: boolean }>
14 type RefreshOptions <T> = {
16 fetchedType: ActorLoadByUrlType
19 const promiseCache = new PromiseCache(doRefresh, (options: RefreshOptions<MActorFull | MActorAccountChannelId>) => options.actor.url)
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 })
25 return promiseCache.run(options)
32 // ---------------------------------------------------------------------------
34 async function doRefresh <T extends MActorFull | MActorAccountChannelId> (options: RefreshOptions<T>): RefreshResult <MActorFull> {
35 const { actor: actorArg, fetchedType } = options
37 // We need more attributes
38 const actor = fetchedType === 'all'
39 ? actorArg as MActorFull
40 : await ActorModel.loadByUrlAndPopulateAccountAndChannel(actorArg.url)
42 const lTags = loggerTagsFactory('ap', 'actor', 'refresh', actor.url)
44 logger.info('Refreshing actor %s.', actor.url, lTags())
47 const actorUrl = await getActorUrl(actor)
48 const { actorObject } = await fetchRemoteActor(actorUrl)
50 if (actorObject === undefined) {
51 logger.info('Cannot fetch remote actor %s in refresh actor.', actorUrl)
52 return { actor, refreshed: false }
55 const updater = new APActorUpdater(actorObject, actor)
56 await updater.update()
58 return { refreshed: true, actor }
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())
64 ? await actor.Account.destroy()
65 : await actor.VideoChannel.destroy()
67 return { actor: undefined, refreshed: false }
70 logger.info('Cannot refresh actor %s.', actor.url, { err, ...lTags() })
71 return { actor, refreshed: false }
75 function getActorUrl (actor: MActorFull) {
76 return getUrlFromWebfinger(actor.preferredUsername + '@' + actor.getHost())
78 logger.warn('Cannot get actor URL from webfinger, keeping the old one.', err)