1 import { logger, loggerTagsFactory } 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'
13 const lTags = loggerTagsFactory('ap', 'video')
21 refreshVideo?: boolean
24 async function syncVideoExternalAttributes (video: MVideo, fetchedVideo: VideoObject, syncParam: SyncParam) {
25 logger.info('Adding likes/dislikes/shares/comments of video %s.', video.uuid)
27 await syncRates('like', video, fetchedVideo, syncParam.likes)
28 await syncRates('dislike', video, fetchedVideo, syncParam.dislikes)
30 await syncShares(video, fetchedVideo, syncParam.shares)
32 await syncComments(video, fetchedVideo, syncParam.comments)
35 // ---------------------------------------------------------------------------
39 syncVideoExternalAttributes
42 // ---------------------------------------------------------------------------
44 function createJob (payload: ActivitypubHttpFetcherPayload) {
45 return JobQueue.Instance.createJobWithPromise({ type: 'activitypub-http-fetcher', payload })
48 function syncRates (type: 'like' | 'dislike', video: MVideo, fetchedVideo: VideoObject, isSync: boolean) {
49 const uri = type === 'like'
51 : fetchedVideo.dislikes
54 const jobType = type === 'like'
58 return createJob({ uri, videoId: video.id, type: jobType })
61 const handler = items => createRates(items, video, type)
62 const cleaner = crawlStartDate => AccountVideoRateModel.cleanOldRatesOf(video.id, type, crawlStartDate)
64 return crawlCollectionPage<string>(uri, handler, cleaner)
65 .catch(err => logger.error('Cannot add rate of video %s.', video.uuid, { err, rootUrl: uri, ...lTags(video.uuid, video.url) }))
68 function syncShares (video: MVideo, fetchedVideo: VideoObject, isSync: boolean) {
69 const uri = fetchedVideo.shares
72 return createJob({ uri, videoId: video.id, type: 'video-shares' })
75 const handler = items => addVideoShares(items, video)
76 const cleaner = crawlStartDate => VideoShareModel.cleanOldSharesOf(video.id, crawlStartDate)
78 return crawlCollectionPage<string>(uri, handler, cleaner)
79 .catch(err => logger.error('Cannot add shares of video %s.', video.uuid, { err, rootUrl: uri, ...lTags(video.uuid, video.url) }))
82 function syncComments (video: MVideo, fetchedVideo: VideoObject, isSync: boolean) {
83 const uri = fetchedVideo.comments
86 return createJob({ uri, videoId: video.id, type: 'video-comments' })
89 const handler = items => addVideoComments(items)
90 const cleaner = crawlStartDate => VideoCommentModel.cleanOldCommentsOf(video.id, crawlStartDate)
92 return crawlCollectionPage<string>(uri, handler, cleaner)
93 .catch(err => logger.error('Cannot add comments of video %s.', video.uuid, { err, rootUrl: uri, ...lTags(video.uuid, video.url) }))