]>
Commit | Line | Data |
---|---|---|
46320694 | 1 | import { logger, loggerTagsFactory } from '@server/helpers/logger' |
304a84d5 C |
2 | import { PeerTubeRequestError } from '@server/helpers/requests' |
3 | import { VideoFetchByUrlType } from '@server/helpers/video' | |
4 | import { ActorFollowScoreCache } from '@server/lib/files-cache' | |
5 | import { VideoModel } from '@server/models/video/video' | |
6 | import { MVideoAccountLightBlacklistAllFiles, MVideoThumbnail } from '@server/types/models' | |
7 | import { HttpStatusCode } from '@shared/core-utils' | |
8 | import { fetchRemoteVideo, SyncParam, syncVideoExternalAttributes } from './shared' | |
9 | import { APVideoUpdater } from './updater' | |
10 | ||
46320694 C |
11 | const lTags = loggerTagsFactory('ap', 'video', 'refresh') |
12 | ||
304a84d5 C |
13 | async function refreshVideoIfNeeded (options: { |
14 | video: MVideoThumbnail | |
15 | fetchedType: VideoFetchByUrlType | |
16 | syncParam: SyncParam | |
17 | }): Promise<MVideoThumbnail> { | |
18 | if (!options.video.isOutdated()) return options.video | |
19 | ||
20 | // We need more attributes if the argument video was fetched with not enough joints | |
21 | const video = options.fetchedType === 'all' | |
22 | ? options.video as MVideoAccountLightBlacklistAllFiles | |
23 | : await VideoModel.loadByUrlAndPopulateAccount(options.video.url) | |
24 | ||
25 | try { | |
26 | const { videoObject } = await fetchRemoteVideo(video.url) | |
27 | ||
28 | if (videoObject === undefined) { | |
46320694 | 29 | logger.warn('Cannot refresh remote video %s: invalid body.', video.url, lTags(video.uuid)) |
304a84d5 C |
30 | |
31 | await video.setAsRefreshed() | |
32 | return video | |
33 | } | |
34 | ||
35 | const videoUpdater = new APVideoUpdater(videoObject, video) | |
36 | await videoUpdater.update() | |
37 | ||
38 | await syncVideoExternalAttributes(video, videoObject, options.syncParam) | |
39 | ||
40 | ActorFollowScoreCache.Instance.addGoodServerId(video.VideoChannel.Actor.serverId) | |
41 | ||
42 | return video | |
43 | } catch (err) { | |
44 | if ((err as PeerTubeRequestError).statusCode === HttpStatusCode.NOT_FOUND_404) { | |
46320694 | 45 | logger.info('Cannot refresh remote video %s: video does not exist anymore. Deleting it.', video.url, lTags(video.uuid)) |
304a84d5 C |
46 | |
47 | // Video does not exist anymore | |
48 | await video.destroy() | |
49 | return undefined | |
50 | } | |
51 | ||
46320694 | 52 | logger.warn('Cannot refresh video %s.', options.video.url, { err, ...lTags(video.uuid) }) |
304a84d5 C |
53 | |
54 | ActorFollowScoreCache.Instance.addBadServerId(video.VideoChannel.Actor.serverId) | |
55 | ||
56 | // Don't refresh in loop | |
57 | await video.setAsRefreshed() | |
58 | return video | |
59 | } | |
60 | } | |
61 | ||
62 | // --------------------------------------------------------------------------- | |
63 | ||
64 | export { | |
65 | refreshVideoIfNeeded | |
66 | } |