]>
Commit | Line | Data |
---|---|---|
57e4e1c1 | 1 | import { runInReadCommittedTransaction } from '@server/helpers/database-utils' |
46320694 | 2 | import { logger, loggerTagsFactory } from '@server/helpers/logger' |
57e4e1c1 | 3 | import { doJSONRequest } from '@server/helpers/requests' |
69290ab3 | 4 | import { JobQueue } from '@server/lib/job-queue' |
57e4e1c1 | 5 | import { VideoModel } from '@server/models/video/video' |
69290ab3 C |
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' | |
57e4e1c1 | 9 | import { ActivitypubHttpFetcherPayload, ActivityPubOrderedCollection, VideoObject } from '@shared/models' |
69290ab3 C |
10 | import { crawlCollectionPage } from '../../crawl' |
11 | import { addVideoShares } from '../../share' | |
12 | import { addVideoComments } from '../../video-comments' | |
69290ab3 | 13 | |
46320694 C |
14 | const lTags = loggerTagsFactory('ap', 'video') |
15 | ||
69290ab3 | 16 | type SyncParam = { |
57e4e1c1 | 17 | rates: boolean |
69290ab3 C |
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 | ||
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 |
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 | ||
e8726320 | 47 | // --------------------------------------------------------------------------- |
69290ab3 | 48 | |
e8726320 C |
49 | export { |
50 | SyncParam, | |
57e4e1c1 C |
51 | syncVideoExternalAttributes, |
52 | updateVideoRates | |
e8726320 C |
53 | } |
54 | ||
55 | // --------------------------------------------------------------------------- | |
56 | ||
57e4e1c1 | 57 | async 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 | 76 | function createJob (payload: ActivitypubHttpFetcherPayload) { |
bd911b54 | 77 | return JobQueue.Instance.createJob({ type: 'activitypub-http-fetcher', payload }) |
e8726320 | 78 | } |
69290ab3 | 79 | |
e8726320 | 80 | function 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 | 94 | function 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 | } |