]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/videos/shared/video-sync-attributes.ts
Refactor AP video update
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / videos / shared / video-sync-attributes.ts
1 import { logger } from '@server/helpers/logger'
2 import { JobQueue } from '@server/lib/job-queue'
3 import { AccountVideoRateModel } from '@server/models/account/account-video-rate'
4 import { VideoCommentModel } from '@server/models/video/video-comment'
5 import { VideoShareModel } from '@server/models/video/video-share'
6 import { MVideo } from '@server/types/models'
7 import { ActivitypubHttpFetcherPayload, VideoObject } from '@shared/models'
8 import { crawlCollectionPage } from '../../crawl'
9 import { addVideoShares } from '../../share'
10 import { addVideoComments } from '../../video-comments'
11 import { createRates } from '../../video-rates'
12
13 import Bluebird = require('bluebird')
14
15 type SyncParam = {
16 likes: boolean
17 dislikes: boolean
18 shares: boolean
19 comments: boolean
20 thumbnail: boolean
21 refreshVideo?: boolean
22 }
23
24 async function syncVideoExternalAttributes (video: MVideo, fetchedVideo: VideoObject, syncParam: SyncParam) {
25 logger.info('Adding likes/dislikes/shares/comments of video %s.', video.uuid)
26
27 const jobPayloads: ActivitypubHttpFetcherPayload[] = []
28
29 if (syncParam.likes === true) {
30 const handler = items => createRates(items, video, 'like')
31 const cleaner = crawlStartDate => AccountVideoRateModel.cleanOldRatesOf(video.id, 'like' as 'like', crawlStartDate)
32
33 await crawlCollectionPage<string>(fetchedVideo.likes, handler, cleaner)
34 .catch(err => logger.error('Cannot add likes of video %s.', video.uuid, { err, rootUrl: fetchedVideo.likes }))
35 } else {
36 jobPayloads.push({ uri: fetchedVideo.likes, videoId: video.id, type: 'video-likes' as 'video-likes' })
37 }
38
39 if (syncParam.dislikes === true) {
40 const handler = items => createRates(items, video, 'dislike')
41 const cleaner = crawlStartDate => AccountVideoRateModel.cleanOldRatesOf(video.id, 'dislike' as 'dislike', crawlStartDate)
42
43 await crawlCollectionPage<string>(fetchedVideo.dislikes, handler, cleaner)
44 .catch(err => logger.error('Cannot add dislikes of video %s.', video.uuid, { err, rootUrl: fetchedVideo.dislikes }))
45 } else {
46 jobPayloads.push({ uri: fetchedVideo.dislikes, videoId: video.id, type: 'video-dislikes' as 'video-dislikes' })
47 }
48
49 if (syncParam.shares === true) {
50 const handler = items => addVideoShares(items, video)
51 const cleaner = crawlStartDate => VideoShareModel.cleanOldSharesOf(video.id, crawlStartDate)
52
53 await crawlCollectionPage<string>(fetchedVideo.shares, handler, cleaner)
54 .catch(err => logger.error('Cannot add shares of video %s.', video.uuid, { err, rootUrl: fetchedVideo.shares }))
55 } else {
56 jobPayloads.push({ uri: fetchedVideo.shares, videoId: video.id, type: 'video-shares' as 'video-shares' })
57 }
58
59 if (syncParam.comments === true) {
60 const handler = items => addVideoComments(items)
61 const cleaner = crawlStartDate => VideoCommentModel.cleanOldCommentsOf(video.id, crawlStartDate)
62
63 await crawlCollectionPage<string>(fetchedVideo.comments, handler, cleaner)
64 .catch(err => logger.error('Cannot add comments of video %s.', video.uuid, { err, rootUrl: fetchedVideo.comments }))
65 } else {
66 jobPayloads.push({ uri: fetchedVideo.comments, videoId: video.id, type: 'video-comments' as 'video-comments' })
67 }
68
69 await Bluebird.map(jobPayloads, payload => JobQueue.Instance.createJobWithPromise({ type: 'activitypub-http-fetcher', payload }))
70 }
71
72 export {
73 SyncParam,
74 syncVideoExternalAttributes
75 }