1
0
Fork 0
peertube/server/lib/activitypub/videos/refresh.ts

69 lines
2.4 KiB
TypeScript
Raw Normal View History

2021-06-02 14:49:59 +00:00
import { logger, loggerTagsFactory } from '@server/helpers/logger'
2021-06-02 13:47:05 +00:00
import { PeerTubeRequestError } from '@server/helpers/requests'
import { VideoLoadByUrlType } from '@server/lib/model-loaders'
2021-06-02 13:47:05 +00:00
import { VideoModel } from '@server/models/video/video'
import { MVideoAccountLightBlacklistAllFiles, MVideoThumbnail } from '@server/types/models'
2021-07-16 08:42:24 +00:00
import { HttpStatusCode } from '@shared/models'
import { ActorFollowHealthCache } from '../../actor-follow-health-cache'
2021-06-02 13:47:05 +00:00
import { fetchRemoteVideo, SyncParam, syncVideoExternalAttributes } from './shared'
import { APVideoUpdater } from './updater'
async function refreshVideoIfNeeded (options: {
video: MVideoThumbnail
fetchedType: VideoLoadByUrlType
2021-06-02 13:47:05 +00:00
syncParam: SyncParam
}): Promise<MVideoThumbnail> {
if (!options.video.isOutdated()) return options.video
// We need more attributes if the argument video was fetched with not enough joints
const video = options.fetchedType === 'all'
? options.video as MVideoAccountLightBlacklistAllFiles
: await VideoModel.loadByUrlAndPopulateAccount(options.video.url)
2021-06-03 14:56:42 +00:00
const lTags = loggerTagsFactory('ap', 'video', 'refresh', video.uuid, video.url)
2021-06-08 14:19:09 +00:00
logger.info('Refreshing video %s.', video.url, lTags())
2021-06-02 13:47:05 +00:00
try {
const { videoObject } = await fetchRemoteVideo(video.url)
if (videoObject === undefined) {
2021-06-03 14:56:42 +00:00
logger.warn('Cannot refresh remote video %s: invalid body.', video.url, lTags())
2021-06-02 13:47:05 +00:00
await video.setAsRefreshed()
return video
}
const videoUpdater = new APVideoUpdater(videoObject, video)
await videoUpdater.update()
await syncVideoExternalAttributes(video, videoObject, options.syncParam)
ActorFollowHealthCache.Instance.addGoodServerId(video.VideoChannel.Actor.serverId)
2021-06-02 13:47:05 +00:00
return video
} catch (err) {
if ((err as PeerTubeRequestError).statusCode === HttpStatusCode.NOT_FOUND_404) {
2021-06-03 14:56:42 +00:00
logger.info('Cannot refresh remote video %s: video does not exist anymore. Deleting it.', video.url, lTags())
2021-06-02 13:47:05 +00:00
// Video does not exist anymore
await video.destroy()
return undefined
}
2021-06-03 14:56:42 +00:00
logger.warn('Cannot refresh video %s.', options.video.url, { err, ...lTags() })
2021-06-02 13:47:05 +00:00
ActorFollowHealthCache.Instance.addBadServerId(video.VideoChannel.Actor.serverId)
2021-06-02 13:47:05 +00:00
// Don't refresh in loop
await video.setAsRefreshed()
return video
}
}
// ---------------------------------------------------------------------------
export {
refreshVideoIfNeeded
}