]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/videos/shared/video-sync-attributes.ts
c4e1010054c6a0a1171fc355afba80fd947c41d3
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / videos / shared / video-sync-attributes.ts
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'
12
13 const lTags = loggerTagsFactory('ap', 'video')
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 await syncRates('like', video, fetchedVideo, syncParam.likes)
28 await syncRates('dislike', video, fetchedVideo, syncParam.dislikes)
29
30 await syncShares(video, fetchedVideo, syncParam.shares)
31
32 await syncComments(video, fetchedVideo, syncParam.comments)
33 }
34
35 // ---------------------------------------------------------------------------
36
37 export {
38 SyncParam,
39 syncVideoExternalAttributes
40 }
41
42 // ---------------------------------------------------------------------------
43
44 function createJob (payload: ActivitypubHttpFetcherPayload) {
45 return JobQueue.Instance.createJobWithPromise({ type: 'activitypub-http-fetcher', payload })
46 }
47
48 function syncRates (type: 'like' | 'dislike', video: MVideo, fetchedVideo: VideoObject, isSync: boolean) {
49 const uri = type === 'like'
50 ? fetchedVideo.likes
51 : fetchedVideo.dislikes
52
53 if (!isSync) {
54 const jobType = type === 'like'
55 ? 'video-likes'
56 : 'video-dislikes'
57
58 return createJob({ uri, videoId: video.id, type: jobType })
59 }
60
61 const handler = items => createRates(items, video, type)
62 const cleaner = crawlStartDate => AccountVideoRateModel.cleanOldRatesOf(video.id, type, crawlStartDate)
63
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) }))
66 }
67
68 function syncShares (video: MVideo, fetchedVideo: VideoObject, isSync: boolean) {
69 const uri = fetchedVideo.shares
70
71 if (!isSync) {
72 return createJob({ uri, videoId: video.id, type: 'video-shares' })
73 }
74
75 const handler = items => addVideoShares(items, video)
76 const cleaner = crawlStartDate => VideoShareModel.cleanOldSharesOf(video.id, crawlStartDate)
77
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) }))
80 }
81
82 function syncComments (video: MVideo, fetchedVideo: VideoObject, isSync: boolean) {
83 const uri = fetchedVideo.comments
84
85 if (!isSync) {
86 return createJob({ uri, videoId: video.id, type: 'video-comments' })
87 }
88
89 const handler = items => addVideoComments(items)
90 const cleaner = crawlStartDate => VideoCommentModel.cleanOldCommentsOf(video.id, crawlStartDate)
91
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) }))
94 }