Refresh remote actors on GET enpoints

This commit is contained in:
Chocobozzz 2019-01-14 11:30:15 +01:00
parent bb8f7872f5
commit 744d0eca19
No known key found for this signature in database
GPG key ID: 583A612D890159BE
8 changed files with 99 additions and 62 deletions

View file

@ -201,6 +201,62 @@ async function addFetchOutboxJob (actor: ActorModel) {
return JobQueue.Instance.createJob({ type: 'activitypub-http-fetcher', payload })
}
async function refreshActorIfNeeded (
actorArg: ActorModel,
fetchedType: ActorFetchByUrlType
): Promise<{ actor: ActorModel, refreshed: boolean }> {
if (!actorArg.isOutdated()) return { actor: actorArg, refreshed: false }
// We need more attributes
const actor = fetchedType === 'all' ? actorArg : await ActorModel.loadByUrlAndPopulateAccountAndChannel(actorArg.url)
try {
const actorUrl = await getUrlFromWebfinger(actor.preferredUsername + '@' + actor.getHost())
const { result, statusCode } = await fetchRemoteActor(actorUrl)
if (statusCode === 404) {
logger.info('Deleting actor %s because there is a 404 in refresh actor.', actor.url)
actor.Account ? actor.Account.destroy() : actor.VideoChannel.destroy()
return { actor: undefined, refreshed: false }
}
if (result === undefined) {
logger.warn('Cannot fetch remote actor in refresh actor.')
return { actor, refreshed: false }
}
return sequelizeTypescript.transaction(async t => {
updateInstanceWithAnother(actor, result.actor)
if (result.avatarName !== undefined) {
await updateActorAvatarInstance(actor, result.avatarName, t)
}
// Force update
actor.setDataValue('updatedAt', new Date())
await actor.save({ transaction: t })
if (actor.Account) {
actor.Account.set('name', result.name)
actor.Account.set('description', result.summary)
await actor.Account.save({ transaction: t })
} else if (actor.VideoChannel) {
actor.VideoChannel.set('name', result.name)
actor.VideoChannel.set('description', result.summary)
actor.VideoChannel.set('support', result.support)
await actor.VideoChannel.save({ transaction: t })
}
return { refreshed: true, actor }
})
} catch (err) {
logger.warn('Cannot refresh actor.', { err })
return { actor, refreshed: false }
}
}
export {
getOrCreateActorAndServerAndModel,
buildActorInstance,
@ -208,6 +264,7 @@ export {
fetchActorTotalItems,
fetchAvatarIfExists,
updateActorInstance,
refreshActorIfNeeded,
updateActorAvatarInstance,
addFetchOutboxJob
}
@ -373,58 +430,4 @@ async function saveVideoChannel (actor: ActorModel, result: FetchRemoteActorResu
return videoChannelCreated
}
async function refreshActorIfNeeded (
actorArg: ActorModel,
fetchedType: ActorFetchByUrlType
): Promise<{ actor: ActorModel, refreshed: boolean }> {
if (!actorArg.isOutdated()) return { actor: actorArg, refreshed: false }
// We need more attributes
const actor = fetchedType === 'all' ? actorArg : await ActorModel.loadByUrlAndPopulateAccountAndChannel(actorArg.url)
try {
const actorUrl = await getUrlFromWebfinger(actor.preferredUsername + '@' + actor.getHost())
const { result, statusCode } = await fetchRemoteActor(actorUrl)
if (statusCode === 404) {
logger.info('Deleting actor %s because there is a 404 in refresh actor.', actor.url)
actor.Account ? actor.Account.destroy() : actor.VideoChannel.destroy()
return { actor: undefined, refreshed: false }
}
if (result === undefined) {
logger.warn('Cannot fetch remote actor in refresh actor.')
return { actor, refreshed: false }
}
return sequelizeTypescript.transaction(async t => {
updateInstanceWithAnother(actor, result.actor)
if (result.avatarName !== undefined) {
await updateActorAvatarInstance(actor, result.avatarName, t)
}
// Force update
actor.setDataValue('updatedAt', new Date())
await actor.save({ transaction: t })
if (actor.Account) {
actor.Account.set('name', result.name)
actor.Account.set('description', result.summary)
await actor.Account.save({ transaction: t })
} else if (actor.VideoChannel) {
actor.VideoChannel.set('name', result.name)
actor.VideoChannel.set('description', result.summary)
actor.VideoChannel.set('support', result.support)
await actor.VideoChannel.save({ transaction: t })
}
return { refreshed: true, actor }
})
} catch (err) {
logger.warn('Cannot refresh actor.', { err })
return { actor, refreshed: false }
}
}