]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/videos/shared/video-sync-attributes.ts
Clearer video creation from API regarding rates
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / videos / shared / video-sync-attributes.ts
CommitLineData
57e4e1c1 1import { runInReadCommittedTransaction } from '@server/helpers/database-utils'
46320694 2import { logger, loggerTagsFactory } from '@server/helpers/logger'
57e4e1c1 3import { doJSONRequest } from '@server/helpers/requests'
69290ab3 4import { JobQueue } from '@server/lib/job-queue'
57e4e1c1 5import { VideoModel } from '@server/models/video/video'
69290ab3
C
6import { VideoCommentModel } from '@server/models/video/video-comment'
7import { VideoShareModel } from '@server/models/video/video-share'
8import { MVideo } from '@server/types/models'
57e4e1c1 9import { ActivitypubHttpFetcherPayload, ActivityPubOrderedCollection, VideoObject } from '@shared/models'
69290ab3
C
10import { crawlCollectionPage } from '../../crawl'
11import { addVideoShares } from '../../share'
12import { addVideoComments } from '../../video-comments'
69290ab3 13
46320694
C
14const lTags = loggerTagsFactory('ap', 'video')
15
69290ab3 16type SyncParam = {
57e4e1c1 17 rates: boolean
69290ab3
C
18 shares: boolean
19 comments: boolean
20 thumbnail: boolean
21 refreshVideo?: boolean
22}
23
24async function syncVideoExternalAttributes (video: MVideo, fetchedVideo: VideoObject, syncParam: SyncParam) {
25 logger.info('Adding likes/dislikes/shares/comments of video %s.', video.uuid)
26
57e4e1c1
C
27 const ratePromise = updateVideoRates(video, fetchedVideo)
28 if (syncParam.rates) await ratePromise
69290ab3 29
e8726320 30 await syncShares(video, fetchedVideo, syncParam.shares)
69290ab3 31
e8726320
C
32 await syncComments(video, fetchedVideo, syncParam.comments)
33}
69290ab3 34
57e4e1c1
C
35async 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
e8726320 47// ---------------------------------------------------------------------------
69290ab3 48
e8726320
C
49export {
50 SyncParam,
57e4e1c1
C
51 syncVideoExternalAttributes,
52 updateVideoRates
e8726320
C
53}
54
55// ---------------------------------------------------------------------------
56
57e4e1c1 57async function getRatesCount (type: 'like' | 'dislike', video: MVideo, fetchedVideo: VideoObject) {
e8726320
C
58 const uri = type === 'like'
59 ? fetchedVideo.likes
60 : fetchedVideo.dislikes
69290ab3 61
57e4e1c1
C
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
e8726320 67
57e4e1c1
C
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
69290ab3
C
71 }
72
57e4e1c1
C
73 return totalItems
74}
e8726320 75
57e4e1c1
C
76function createJob (payload: ActivitypubHttpFetcherPayload) {
77 return JobQueue.Instance.createJobWithPromise({ type: 'activitypub-http-fetcher', payload })
e8726320 78}
69290ab3 79
e8726320 80function syncShares (video: MVideo, fetchedVideo: VideoObject, isSync: boolean) {
46320694
C
81 const uri = fetchedVideo.shares
82
e8726320 83 if (!isSync) {
46320694 84 return createJob({ uri, videoId: video.id, type: 'video-shares' })
69290ab3
C
85 }
86
e8726320
C
87 const handler = items => addVideoShares(items, video)
88 const cleaner = crawlStartDate => VideoShareModel.cleanOldSharesOf(video.id, crawlStartDate)
89
46320694 90 return crawlCollectionPage<string>(uri, handler, cleaner)
908e6ead 91 .catch(err => logger.error('Cannot add shares of video %s.', video.uuid, { err, rootUrl: uri, ...lTags(video.uuid, video.url) }))
69290ab3
C
92}
93
e8726320 94function syncComments (video: MVideo, fetchedVideo: VideoObject, isSync: boolean) {
46320694
C
95 const uri = fetchedVideo.comments
96
e8726320 97 if (!isSync) {
46320694 98 return createJob({ uri, videoId: video.id, type: 'video-comments' })
e8726320
C
99 }
100
101 const handler = items => addVideoComments(items)
102 const cleaner = crawlStartDate => VideoCommentModel.cleanOldCommentsOf(video.id, crawlStartDate)
103
46320694 104 return crawlCollectionPage<string>(uri, handler, cleaner)
908e6ead 105 .catch(err => logger.error('Cannot add comments of video %s.', video.uuid, { err, rootUrl: uri, ...lTags(video.uuid, video.url) }))
69290ab3 106}