]>
Commit | Line | Data |
---|---|---|
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 createJob (payload: ActivitypubHttpFetcherPayload) { | |
77 | return JobQueue.Instance.createJobWithPromise({ type: 'activitypub-http-fetcher', payload }) | |
78 | } | |
79 | ||
80 | function syncShares (video: MVideo, fetchedVideo: VideoObject, isSync: boolean) { | |
81 | const uri = fetchedVideo.shares | |
82 | ||
83 | if (!isSync) { | |
84 | return createJob({ uri, videoId: video.id, type: 'video-shares' }) | |
85 | } | |
86 | ||
87 | const handler = items => addVideoShares(items, video) | |
88 | const cleaner = crawlStartDate => VideoShareModel.cleanOldSharesOf(video.id, crawlStartDate) | |
89 | ||
90 | return crawlCollectionPage<string>(uri, handler, cleaner) | |
91 | .catch(err => logger.error('Cannot add shares of video %s.', video.uuid, { err, rootUrl: uri, ...lTags(video.uuid, video.url) })) | |
92 | } | |
93 | ||
94 | function syncComments (video: MVideo, fetchedVideo: VideoObject, isSync: boolean) { | |
95 | const uri = fetchedVideo.comments | |
96 | ||
97 | if (!isSync) { | |
98 | return createJob({ uri, videoId: video.id, type: 'video-comments' }) | |
99 | } | |
100 | ||
101 | const handler = items => addVideoComments(items) | |
102 | const cleaner = crawlStartDate => VideoCommentModel.cleanOldCommentsOf(video.id, crawlStartDate) | |
103 | ||
104 | return crawlCollectionPage<string>(uri, handler, cleaner) | |
105 | .catch(err => logger.error('Cannot add comments of video %s.', video.uuid, { err, rootUrl: uri, ...lTags(video.uuid, video.url) })) | |
106 | } |