]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/videos/shared/video-sync-attributes.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / videos / shared / video-sync-attributes.ts
1 import { runInReadCommittedTransaction } from '@server/helpers/database-utils'
2 import { logger, loggerTagsFactory } from '@server/helpers/logger'
3 import { doJSONRequest } from '@server/helpers/requests'
4 import { JobQueue } from '@server/lib/job-queue'
5 import { VideoModel } from '@server/models/video/video'
6 import { VideoCommentModel } from '@server/models/video/video-comment'
7 import { VideoShareModel } from '@server/models/video/video-share'
8 import { MVideo } from '@server/types/models'
9 import { ActivitypubHttpFetcherPayload, ActivityPubOrderedCollection, VideoObject } from '@shared/models'
10 import { crawlCollectionPage } from '../../crawl'
11 import { addVideoShares } from '../../share'
12 import { addVideoComments } from '../../video-comments'
13
14 const lTags = loggerTagsFactory('ap', 'video')
15
16 type SyncParam = {
17 rates: 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 ratePromise = updateVideoRates(video, fetchedVideo)
28 if (syncParam.rates) await ratePromise
29
30 await syncShares(video, fetchedVideo, syncParam.shares)
31
32 await syncComments(video, fetchedVideo, syncParam.comments)
33 }
34
35 async function updateVideoRates (video: MVideo, fetchedVideo: VideoObject) {
36 const [ likes, dislikes ] = await Promise.all([
37 getRatesCount('like', video, fetchedVideo),
38 getRatesCount('dislike', video, fetchedVideo)
39 ])
40
41 return runInReadCommittedTransaction(async t => {
42 await VideoModel.updateRatesOf(video.id, 'like', likes, t)
43 await VideoModel.updateRatesOf(video.id, 'dislike', dislikes, t)
44 })
45 }
46
47 // ---------------------------------------------------------------------------
48
49 export {
50 SyncParam,
51 syncVideoExternalAttributes,
52 updateVideoRates
53 }
54
55 // ---------------------------------------------------------------------------
56
57 async function getRatesCount (type: 'like' | 'dislike', video: MVideo, fetchedVideo: VideoObject) {
58 const uri = type === 'like'
59 ? fetchedVideo.likes
60 : fetchedVideo.dislikes
61
62 logger.info('Sync %s of video %s', type, video.url)
63 const options = { activityPub: true }
64
65 const response = await doJSONRequest<ActivityPubOrderedCollection<any>>(uri, options)
66 const totalItems = response.body.totalItems
67
68 if (isNaN(totalItems)) {
69 logger.error('Cannot sync %s of video %s, totalItems is not a number', type, video.url, { body: response.body })
70 return
71 }
72
73 return totalItems
74 }
75
76 function syncShares (video: MVideo, fetchedVideo: VideoObject, isSync: boolean) {
77 const uri = fetchedVideo.shares
78
79 if (!isSync) {
80 return createJob({ uri, videoId: video.id, type: 'video-shares' })
81 }
82
83 const handler = items => addVideoShares(items, video)
84 const cleaner = crawlStartDate => VideoShareModel.cleanOldSharesOf(video.id, crawlStartDate)
85
86 return crawlCollectionPage<string>(uri, handler, cleaner)
87 .catch(err => logger.error('Cannot add shares of video %s.', video.uuid, { err, rootUrl: uri, ...lTags(video.uuid, video.url) }))
88 }
89
90 function syncComments (video: MVideo, fetchedVideo: VideoObject, isSync: boolean) {
91 const uri = fetchedVideo.comments
92
93 if (!isSync) {
94 return createJob({ uri, videoId: video.id, type: 'video-comments' })
95 }
96
97 const handler = items => addVideoComments(items)
98 const cleaner = crawlStartDate => VideoCommentModel.cleanOldCommentsOf(video.id, crawlStartDate)
99
100 return crawlCollectionPage<string>(uri, handler, cleaner)
101 .catch(err => logger.error('Cannot add comments of video %s.', video.uuid, { err, rootUrl: uri, ...lTags(video.uuid, video.url) }))
102 }
103
104 function createJob (payload: ActivitypubHttpFetcherPayload) {
105 return JobQueue.Instance.createJob({ type: 'activitypub-http-fetcher', payload })
106 }