1 import { logger, loggerTagsFactory } from '@server/helpers/logger'
2 import { PeerTubeRequestError } from '@server/helpers/requests'
3 import { VideoLoadByUrlType } from '@server/lib/model-loaders'
4 import { VideoModel } from '@server/models/video/video'
5 import { MVideoAccountLightBlacklistAllFiles, MVideoThumbnail } from '@server/types/models'
6 import { HttpStatusCode } from '@shared/models'
7 import { ActorFollowHealthCache } from '../../actor-follow-health-cache'
8 import { fetchRemoteVideo, SyncParam, syncVideoExternalAttributes } from './shared'
9 import { APVideoUpdater } from './updater'
11 async function refreshVideoIfNeeded (options: {
12 video: MVideoThumbnail
13 fetchedType: VideoLoadByUrlType
15 }): Promise<MVideoThumbnail> {
16 if (!options.video.isOutdated()) return options.video
18 // We need more attributes if the argument video was fetched with not enough joints
19 const video = options.fetchedType === 'all'
20 ? options.video as MVideoAccountLightBlacklistAllFiles
21 : await VideoModel.loadByUrlAndPopulateAccount(options.video.url)
23 const lTags = loggerTagsFactory('ap', 'video', 'refresh', video.uuid, video.url)
25 logger.info('Refreshing video %s.', video.url, lTags())
28 const { videoObject } = await fetchRemoteVideo(video.url)
30 if (videoObject === undefined) {
31 logger.warn('Cannot refresh remote video %s: invalid body.', video.url, lTags())
33 await video.setAsRefreshed()
37 const videoUpdater = new APVideoUpdater(videoObject, video)
38 await videoUpdater.update()
40 await syncVideoExternalAttributes(video, videoObject, options.syncParam)
42 ActorFollowHealthCache.Instance.addGoodServerId(video.VideoChannel.Actor.serverId)
46 if ((err as PeerTubeRequestError).statusCode === HttpStatusCode.NOT_FOUND_404) {
47 logger.info('Cannot refresh remote video %s: video does not exist anymore. Deleting it.', video.url, lTags())
49 // Video does not exist anymore
54 logger.warn('Cannot refresh video %s.', options.video.url, { err, ...lTags() })
56 ActorFollowHealthCache.Instance.addBadServerId(video.VideoChannel.Actor.serverId)
58 // Don't refresh in loop
59 await video.setAsRefreshed()
64 // ---------------------------------------------------------------------------