]>
Commit | Line | Data |
---|---|---|
46320694 | 1 | import { logger, loggerTagsFactory } from '@server/helpers/logger' |
69290ab3 C |
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 | ||
46320694 C |
13 | const lTags = loggerTagsFactory('ap', 'video') |
14 | ||
69290ab3 C |
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 | ||
e8726320 C |
27 | await syncRates('like', video, fetchedVideo, syncParam.likes) |
28 | await syncRates('dislike', video, fetchedVideo, syncParam.dislikes) | |
69290ab3 | 29 | |
e8726320 | 30 | await syncShares(video, fetchedVideo, syncParam.shares) |
69290ab3 | 31 | |
e8726320 C |
32 | await syncComments(video, fetchedVideo, syncParam.comments) |
33 | } | |
69290ab3 | 34 | |
e8726320 | 35 | // --------------------------------------------------------------------------- |
69290ab3 | 36 | |
e8726320 C |
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 | } | |
69290ab3 | 47 | |
e8726320 C |
48 | function syncRates (type: 'like' | 'dislike', video: MVideo, fetchedVideo: VideoObject, isSync: boolean) { |
49 | const uri = type === 'like' | |
50 | ? fetchedVideo.likes | |
51 | : fetchedVideo.dislikes | |
69290ab3 | 52 | |
e8726320 C |
53 | if (!isSync) { |
54 | const jobType = type === 'like' | |
55 | ? 'video-likes' | |
56 | : 'video-dislikes' | |
57 | ||
58 | return createJob({ uri, videoId: video.id, type: jobType }) | |
69290ab3 C |
59 | } |
60 | ||
e8726320 C |
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) | |
46320694 | 65 | .catch(err => logger.error('Cannot add rate of video %s.', video.uuid, { err, rootUrl: uri, ...lTags(video.uuid) })) |
e8726320 | 66 | } |
69290ab3 | 67 | |
e8726320 | 68 | function syncShares (video: MVideo, fetchedVideo: VideoObject, isSync: boolean) { |
46320694 C |
69 | const uri = fetchedVideo.shares |
70 | ||
e8726320 | 71 | if (!isSync) { |
46320694 | 72 | return createJob({ uri, videoId: video.id, type: 'video-shares' }) |
69290ab3 C |
73 | } |
74 | ||
e8726320 C |
75 | const handler = items => addVideoShares(items, video) |
76 | const cleaner = crawlStartDate => VideoShareModel.cleanOldSharesOf(video.id, crawlStartDate) | |
77 | ||
46320694 C |
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) })) | |
69290ab3 C |
80 | } |
81 | ||
e8726320 | 82 | function syncComments (video: MVideo, fetchedVideo: VideoObject, isSync: boolean) { |
46320694 C |
83 | const uri = fetchedVideo.comments |
84 | ||
e8726320 | 85 | if (!isSync) { |
46320694 | 86 | return createJob({ uri, videoId: video.id, type: 'video-comments' }) |
e8726320 C |
87 | } |
88 | ||
89 | const handler = items => addVideoComments(items) | |
90 | const cleaner = crawlStartDate => VideoCommentModel.cleanOldCommentsOf(video.id, crawlStartDate) | |
91 | ||
46320694 C |
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) })) | |
69290ab3 | 94 | } |