2021-06-08 09:19:09 -05:00
|
|
|
import { logger, loggerTagsFactory } from '@server/helpers/logger'
|
2021-06-09 06:34:40 -05:00
|
|
|
import { PromiseCache } from '@server/helpers/promise-cache'
|
2021-06-03 09:02:29 -05:00
|
|
|
import { PeerTubeRequestError } from '@server/helpers/requests'
|
2021-06-03 11:10:56 -05:00
|
|
|
import { ActorLoadByUrlType } from '@server/lib/model-loaders'
|
2021-06-03 09:02:29 -05:00
|
|
|
import { ActorModel } from '@server/models/actor/actor'
|
|
|
|
import { MActorAccountChannelId, MActorFull } from '@server/types/models'
|
2021-07-16 03:42:24 -05:00
|
|
|
import { HttpStatusCode } from '@shared/models'
|
2021-06-03 09:02:29 -05:00
|
|
|
import { fetchRemoteActor } from './shared'
|
|
|
|
import { APActorUpdater } from './updater'
|
2021-06-03 10:33:44 -05:00
|
|
|
import { getUrlFromWebfinger } from './webfinger'
|
2021-06-03 09:02:29 -05:00
|
|
|
|
2021-06-09 06:34:40 -05:00
|
|
|
type RefreshResult <T> = Promise<{ actor: T | MActorFull, refreshed: boolean }>
|
|
|
|
|
|
|
|
type RefreshOptions <T> = {
|
|
|
|
actor: T
|
2021-06-03 11:10:56 -05:00
|
|
|
fetchedType: ActorLoadByUrlType
|
2021-06-09 06:34:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const promiseCache = new PromiseCache(doRefresh, (options: RefreshOptions<MActorFull | MActorAccountChannelId>) => options.actor.url)
|
|
|
|
|
|
|
|
function refreshActorIfNeeded <T extends MActorFull | MActorAccountChannelId> (options: RefreshOptions<T>): RefreshResult <T> {
|
|
|
|
const actorArg = options.actor
|
|
|
|
if (!actorArg.isOutdated()) return Promise.resolve({ actor: actorArg, refreshed: false })
|
|
|
|
|
|
|
|
return promiseCache.run(options)
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
refreshActorIfNeeded
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
async function doRefresh <T extends MActorFull | MActorAccountChannelId> (options: RefreshOptions<T>): RefreshResult <MActorFull> {
|
|
|
|
const { actor: actorArg, fetchedType } = options
|
2021-06-03 09:02:29 -05:00
|
|
|
|
|
|
|
// We need more attributes
|
|
|
|
const actor = fetchedType === 'all'
|
|
|
|
? actorArg as MActorFull
|
|
|
|
: await ActorModel.loadByUrlAndPopulateAccountAndChannel(actorArg.url)
|
|
|
|
|
2021-06-08 09:19:09 -05:00
|
|
|
const lTags = loggerTagsFactory('ap', 'actor', 'refresh', actor.url)
|
|
|
|
|
|
|
|
logger.info('Refreshing actor %s.', actor.url, lTags())
|
|
|
|
|
2021-06-03 09:02:29 -05:00
|
|
|
try {
|
|
|
|
const actorUrl = await getActorUrl(actor)
|
|
|
|
const { actorObject } = await fetchRemoteActor(actorUrl)
|
|
|
|
|
|
|
|
if (actorObject === undefined) {
|
2022-07-13 03:04:22 -05:00
|
|
|
logger.info('Cannot fetch remote actor %s in refresh actor.', actorUrl)
|
2021-06-03 09:02:29 -05:00
|
|
|
return { actor, refreshed: false }
|
|
|
|
}
|
|
|
|
|
|
|
|
const updater = new APActorUpdater(actorObject, actor)
|
|
|
|
await updater.update()
|
|
|
|
|
|
|
|
return { refreshed: true, actor }
|
|
|
|
} catch (err) {
|
|
|
|
if ((err as PeerTubeRequestError).statusCode === HttpStatusCode.NOT_FOUND_404) {
|
2021-06-08 09:19:09 -05:00
|
|
|
logger.info('Deleting actor %s because there is a 404 in refresh actor.', actor.url, lTags())
|
2021-06-03 09:02:29 -05:00
|
|
|
|
|
|
|
actor.Account
|
|
|
|
? await actor.Account.destroy()
|
|
|
|
: await actor.VideoChannel.destroy()
|
|
|
|
|
|
|
|
return { actor: undefined, refreshed: false }
|
|
|
|
}
|
|
|
|
|
2022-07-13 03:04:22 -05:00
|
|
|
logger.info('Cannot refresh actor %s.', actor.url, { err, ...lTags() })
|
2021-06-03 09:02:29 -05:00
|
|
|
return { actor, refreshed: false }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getActorUrl (actor: MActorFull) {
|
|
|
|
return getUrlFromWebfinger(actor.preferredUsername + '@' + actor.getHost())
|
|
|
|
.catch(err => {
|
2022-07-18 04:55:13 -05:00
|
|
|
logger.warn('Cannot get actor URL from webfinger, keeping the old one.', { err })
|
2021-06-03 09:02:29 -05:00
|
|
|
return actor.url
|
|
|
|
})
|
|
|
|
}
|