]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/videos/refresh.ts
Resolve object id from actors too
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / videos / refresh.ts
CommitLineData
46320694 1import { logger, loggerTagsFactory } from '@server/helpers/logger'
304a84d5 2import { PeerTubeRequestError } from '@server/helpers/requests'
304a84d5 3import { ActorFollowScoreCache } from '@server/lib/files-cache'
868fce62 4import { VideoLoadByUrlType } from '@server/lib/model-loaders'
304a84d5
C
5import { VideoModel } from '@server/models/video/video'
6import { MVideoAccountLightBlacklistAllFiles, MVideoThumbnail } from '@server/types/models'
7import { HttpStatusCode } from '@shared/core-utils'
8import { fetchRemoteVideo, SyncParam, syncVideoExternalAttributes } from './shared'
9import { APVideoUpdater } from './updater'
10
11async function refreshVideoIfNeeded (options: {
12 video: MVideoThumbnail
868fce62 13 fetchedType: VideoLoadByUrlType
304a84d5
C
14 syncParam: SyncParam
15}): Promise<MVideoThumbnail> {
16 if (!options.video.isOutdated()) return options.video
17
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)
22
908e6ead
C
23 const lTags = loggerTagsFactory('ap', 'video', 'refresh', video.uuid, video.url)
24
9de33c6b
C
25 logger.info('Refreshing video %s.', video.url, lTags())
26
304a84d5
C
27 try {
28 const { videoObject } = await fetchRemoteVideo(video.url)
29
30 if (videoObject === undefined) {
908e6ead 31 logger.warn('Cannot refresh remote video %s: invalid body.', video.url, lTags())
304a84d5
C
32
33 await video.setAsRefreshed()
34 return video
35 }
36
37 const videoUpdater = new APVideoUpdater(videoObject, video)
38 await videoUpdater.update()
39
40 await syncVideoExternalAttributes(video, videoObject, options.syncParam)
41
42 ActorFollowScoreCache.Instance.addGoodServerId(video.VideoChannel.Actor.serverId)
43
44 return video
45 } catch (err) {
46 if ((err as PeerTubeRequestError).statusCode === HttpStatusCode.NOT_FOUND_404) {
908e6ead 47 logger.info('Cannot refresh remote video %s: video does not exist anymore. Deleting it.', video.url, lTags())
304a84d5
C
48
49 // Video does not exist anymore
50 await video.destroy()
51 return undefined
52 }
53
908e6ead 54 logger.warn('Cannot refresh video %s.', options.video.url, { err, ...lTags() })
304a84d5
C
55
56 ActorFollowScoreCache.Instance.addBadServerId(video.VideoChannel.Actor.serverId)
57
58 // Don't refresh in loop
59 await video.setAsRefreshed()
60 return video
61 }
62}
63
64// ---------------------------------------------------------------------------
65
66export {
67 refreshVideoIfNeeded
68}