diff options
author | Chocobozzz <me@florianbigard.com> | 2021-06-02 15:57:30 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-06-02 16:57:53 +0200 |
commit | e872632091cbec15da05ce74d89fed4527ab93eb (patch) | |
tree | 2513c410644dd7865b725f15418789b49280bac4 | |
parent | 304a84d59c3a800b7f7aef48cf55f307534c0926 (diff) | |
download | PeerTube-e872632091cbec15da05ce74d89fed4527ab93eb.tar.gz PeerTube-e872632091cbec15da05ce74d89fed4527ab93eb.tar.zst PeerTube-e872632091cbec15da05ce74d89fed4527ab93eb.zip |
Refactor sync attributes
-rw-r--r-- | server/lib/activitypub/videos/shared/video-sync-attributes.ts | 87 |
1 files changed, 50 insertions, 37 deletions
diff --git a/server/lib/activitypub/videos/shared/video-sync-attributes.ts b/server/lib/activitypub/videos/shared/video-sync-attributes.ts index 181893c68..c1318abf7 100644 --- a/server/lib/activitypub/videos/shared/video-sync-attributes.ts +++ b/server/lib/activitypub/videos/shared/video-sync-attributes.ts | |||
@@ -10,8 +10,6 @@ import { addVideoShares } from '../../share' | |||
10 | import { addVideoComments } from '../../video-comments' | 10 | import { addVideoComments } from '../../video-comments' |
11 | import { createRates } from '../../video-rates' | 11 | import { createRates } from '../../video-rates' |
12 | 12 | ||
13 | import Bluebird = require('bluebird') | ||
14 | |||
15 | type SyncParam = { | 13 | type SyncParam = { |
16 | likes: boolean | 14 | likes: boolean |
17 | dislikes: boolean | 15 | dislikes: boolean |
@@ -24,52 +22,67 @@ type SyncParam = { | |||
24 | async function syncVideoExternalAttributes (video: MVideo, fetchedVideo: VideoObject, syncParam: SyncParam) { | 22 | async function syncVideoExternalAttributes (video: MVideo, fetchedVideo: VideoObject, syncParam: SyncParam) { |
25 | logger.info('Adding likes/dislikes/shares/comments of video %s.', video.uuid) | 23 | logger.info('Adding likes/dislikes/shares/comments of video %s.', video.uuid) |
26 | 24 | ||
27 | const jobPayloads: ActivitypubHttpFetcherPayload[] = [] | 25 | await syncRates('like', video, fetchedVideo, syncParam.likes) |
26 | await syncRates('dislike', video, fetchedVideo, syncParam.dislikes) | ||
28 | 27 | ||
29 | if (syncParam.likes === true) { | 28 | await syncShares(video, fetchedVideo, syncParam.shares) |
30 | const handler = items => createRates(items, video, 'like') | ||
31 | const cleaner = crawlStartDate => AccountVideoRateModel.cleanOldRatesOf(video.id, 'like' as 'like', crawlStartDate) | ||
32 | 29 | ||
33 | await crawlCollectionPage<string>(fetchedVideo.likes, handler, cleaner) | 30 | await syncComments(video, fetchedVideo, syncParam.comments) |
34 | .catch(err => logger.error('Cannot add likes of video %s.', video.uuid, { err, rootUrl: fetchedVideo.likes })) | 31 | } |
35 | } else { | ||
36 | jobPayloads.push({ uri: fetchedVideo.likes, videoId: video.id, type: 'video-likes' as 'video-likes' }) | ||
37 | } | ||
38 | 32 | ||
39 | if (syncParam.dislikes === true) { | 33 | // --------------------------------------------------------------------------- |
40 | const handler = items => createRates(items, video, 'dislike') | ||
41 | const cleaner = crawlStartDate => AccountVideoRateModel.cleanOldRatesOf(video.id, 'dislike' as 'dislike', crawlStartDate) | ||
42 | 34 | ||
43 | await crawlCollectionPage<string>(fetchedVideo.dislikes, handler, cleaner) | 35 | export { |
44 | .catch(err => logger.error('Cannot add dislikes of video %s.', video.uuid, { err, rootUrl: fetchedVideo.dislikes })) | 36 | SyncParam, |
45 | } else { | 37 | syncVideoExternalAttributes |
46 | jobPayloads.push({ uri: fetchedVideo.dislikes, videoId: video.id, type: 'video-dislikes' as 'video-dislikes' }) | 38 | } |
47 | } | 39 | |
40 | // --------------------------------------------------------------------------- | ||
41 | |||
42 | function createJob (payload: ActivitypubHttpFetcherPayload) { | ||
43 | return JobQueue.Instance.createJobWithPromise({ type: 'activitypub-http-fetcher', payload }) | ||
44 | } | ||
48 | 45 | ||
49 | if (syncParam.shares === true) { | 46 | function syncRates (type: 'like' | 'dislike', video: MVideo, fetchedVideo: VideoObject, isSync: boolean) { |
50 | const handler = items => addVideoShares(items, video) | 47 | const uri = type === 'like' |
51 | const cleaner = crawlStartDate => VideoShareModel.cleanOldSharesOf(video.id, crawlStartDate) | 48 | ? fetchedVideo.likes |
49 | : fetchedVideo.dislikes | ||
52 | 50 | ||
53 | await crawlCollectionPage<string>(fetchedVideo.shares, handler, cleaner) | 51 | if (!isSync) { |
54 | .catch(err => logger.error('Cannot add shares of video %s.', video.uuid, { err, rootUrl: fetchedVideo.shares })) | 52 | const jobType = type === 'like' |
55 | } else { | 53 | ? 'video-likes' |
56 | jobPayloads.push({ uri: fetchedVideo.shares, videoId: video.id, type: 'video-shares' as 'video-shares' }) | 54 | : 'video-dislikes' |
55 | |||
56 | return createJob({ uri, videoId: video.id, type: jobType }) | ||
57 | } | 57 | } |
58 | 58 | ||
59 | if (syncParam.comments === true) { | 59 | const handler = items => createRates(items, video, type) |
60 | const handler = items => addVideoComments(items) | 60 | const cleaner = crawlStartDate => AccountVideoRateModel.cleanOldRatesOf(video.id, type, crawlStartDate) |
61 | const cleaner = crawlStartDate => VideoCommentModel.cleanOldCommentsOf(video.id, crawlStartDate) | 61 | |
62 | return crawlCollectionPage<string>(uri, handler, cleaner) | ||
63 | .catch(err => logger.error('Cannot add rate of video %s.', video.uuid, { err, rootUrl: uri })) | ||
64 | } | ||
62 | 65 | ||
63 | await crawlCollectionPage<string>(fetchedVideo.comments, handler, cleaner) | 66 | function syncShares (video: MVideo, fetchedVideo: VideoObject, isSync: boolean) { |
64 | .catch(err => logger.error('Cannot add comments of video %s.', video.uuid, { err, rootUrl: fetchedVideo.comments })) | 67 | if (!isSync) { |
65 | } else { | 68 | return createJob({ uri: fetchedVideo.shares, videoId: video.id, type: 'video-shares' }) |
66 | jobPayloads.push({ uri: fetchedVideo.comments, videoId: video.id, type: 'video-comments' as 'video-comments' }) | ||
67 | } | 69 | } |
68 | 70 | ||
69 | await Bluebird.map(jobPayloads, payload => JobQueue.Instance.createJobWithPromise({ type: 'activitypub-http-fetcher', payload })) | 71 | const handler = items => addVideoShares(items, video) |
72 | const cleaner = crawlStartDate => VideoShareModel.cleanOldSharesOf(video.id, crawlStartDate) | ||
73 | |||
74 | return crawlCollectionPage<string>(fetchedVideo.shares, handler, cleaner) | ||
75 | .catch(err => logger.error('Cannot add shares of video %s.', video.uuid, { err, rootUrl: fetchedVideo.shares })) | ||
70 | } | 76 | } |
71 | 77 | ||
72 | export { | 78 | function syncComments (video: MVideo, fetchedVideo: VideoObject, isSync: boolean) { |
73 | SyncParam, | 79 | if (!isSync) { |
74 | syncVideoExternalAttributes | 80 | return createJob({ uri: fetchedVideo.comments, videoId: video.id, type: 'video-comments' }) |
81 | } | ||
82 | |||
83 | const handler = items => addVideoComments(items) | ||
84 | const cleaner = crawlStartDate => VideoCommentModel.cleanOldCommentsOf(video.id, crawlStartDate) | ||
85 | |||
86 | return crawlCollectionPage<string>(fetchedVideo.comments, handler, cleaner) | ||
87 | .catch(err => logger.error('Cannot add comments of video %s.', video.uuid, { err, rootUrl: fetchedVideo.comments })) | ||
75 | } | 88 | } |