diff options
Diffstat (limited to 'server/lib/activitypub/videos/refresh.ts')
-rw-r--r-- | server/lib/activitypub/videos/refresh.ts | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/server/lib/activitypub/videos/refresh.ts b/server/lib/activitypub/videos/refresh.ts new file mode 100644 index 000000000..205a3ccb1 --- /dev/null +++ b/server/lib/activitypub/videos/refresh.ts | |||
@@ -0,0 +1,64 @@ | |||
1 | import { logger } from '@server/helpers/logger' | ||
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 | |||
11 | async function refreshVideoIfNeeded (options: { | ||
12 | video: MVideoThumbnail | ||
13 | fetchedType: VideoFetchByUrlType | ||
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 | |||
23 | try { | ||
24 | const { videoObject } = await fetchRemoteVideo(video.url) | ||
25 | |||
26 | if (videoObject === undefined) { | ||
27 | logger.warn('Cannot refresh remote video %s: invalid body.', video.url) | ||
28 | |||
29 | await video.setAsRefreshed() | ||
30 | return video | ||
31 | } | ||
32 | |||
33 | const videoUpdater = new APVideoUpdater(videoObject, video) | ||
34 | await videoUpdater.update() | ||
35 | |||
36 | await syncVideoExternalAttributes(video, videoObject, options.syncParam) | ||
37 | |||
38 | ActorFollowScoreCache.Instance.addGoodServerId(video.VideoChannel.Actor.serverId) | ||
39 | |||
40 | return video | ||
41 | } catch (err) { | ||
42 | if ((err as PeerTubeRequestError).statusCode === HttpStatusCode.NOT_FOUND_404) { | ||
43 | logger.info('Cannot refresh remote video %s: video does not exist anymore. Deleting it.', video.url) | ||
44 | |||
45 | // Video does not exist anymore | ||
46 | await video.destroy() | ||
47 | return undefined | ||
48 | } | ||
49 | |||
50 | logger.warn('Cannot refresh video %s.', options.video.url, { err }) | ||
51 | |||
52 | ActorFollowScoreCache.Instance.addBadServerId(video.VideoChannel.Actor.serverId) | ||
53 | |||
54 | // Don't refresh in loop | ||
55 | await video.setAsRefreshed() | ||
56 | return video | ||
57 | } | ||
58 | } | ||
59 | |||
60 | // --------------------------------------------------------------------------- | ||
61 | |||
62 | export { | ||
63 | refreshVideoIfNeeded | ||
64 | } | ||