From e872632091cbec15da05ce74d89fed4527ab93eb Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 2 Jun 2021 15:57:30 +0200 Subject: [PATCH] Refactor sync attributes --- .../videos/shared/video-sync-attributes.ts | 87 +++++++++++-------- 1 file 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' import { addVideoComments } from '../../video-comments' import { createRates } from '../../video-rates' -import Bluebird = require('bluebird') - type SyncParam = { likes: boolean dislikes: boolean @@ -24,52 +22,67 @@ type SyncParam = { async function syncVideoExternalAttributes (video: MVideo, fetchedVideo: VideoObject, syncParam: SyncParam) { logger.info('Adding likes/dislikes/shares/comments of video %s.', video.uuid) - const jobPayloads: ActivitypubHttpFetcherPayload[] = [] + await syncRates('like', video, fetchedVideo, syncParam.likes) + await syncRates('dislike', video, fetchedVideo, syncParam.dislikes) - if (syncParam.likes === true) { - const handler = items => createRates(items, video, 'like') - const cleaner = crawlStartDate => AccountVideoRateModel.cleanOldRatesOf(video.id, 'like' as 'like', crawlStartDate) + await syncShares(video, fetchedVideo, syncParam.shares) - await crawlCollectionPage(fetchedVideo.likes, handler, cleaner) - .catch(err => logger.error('Cannot add likes of video %s.', video.uuid, { err, rootUrl: fetchedVideo.likes })) - } else { - jobPayloads.push({ uri: fetchedVideo.likes, videoId: video.id, type: 'video-likes' as 'video-likes' }) - } + await syncComments(video, fetchedVideo, syncParam.comments) +} - if (syncParam.dislikes === true) { - const handler = items => createRates(items, video, 'dislike') - const cleaner = crawlStartDate => AccountVideoRateModel.cleanOldRatesOf(video.id, 'dislike' as 'dislike', crawlStartDate) +// --------------------------------------------------------------------------- - await crawlCollectionPage(fetchedVideo.dislikes, handler, cleaner) - .catch(err => logger.error('Cannot add dislikes of video %s.', video.uuid, { err, rootUrl: fetchedVideo.dislikes })) - } else { - jobPayloads.push({ uri: fetchedVideo.dislikes, videoId: video.id, type: 'video-dislikes' as 'video-dislikes' }) - } +export { + SyncParam, + syncVideoExternalAttributes +} + +// --------------------------------------------------------------------------- + +function createJob (payload: ActivitypubHttpFetcherPayload) { + return JobQueue.Instance.createJobWithPromise({ type: 'activitypub-http-fetcher', payload }) +} - if (syncParam.shares === true) { - const handler = items => addVideoShares(items, video) - const cleaner = crawlStartDate => VideoShareModel.cleanOldSharesOf(video.id, crawlStartDate) +function syncRates (type: 'like' | 'dislike', video: MVideo, fetchedVideo: VideoObject, isSync: boolean) { + const uri = type === 'like' + ? fetchedVideo.likes + : fetchedVideo.dislikes - await crawlCollectionPage(fetchedVideo.shares, handler, cleaner) - .catch(err => logger.error('Cannot add shares of video %s.', video.uuid, { err, rootUrl: fetchedVideo.shares })) - } else { - jobPayloads.push({ uri: fetchedVideo.shares, videoId: video.id, type: 'video-shares' as 'video-shares' }) + if (!isSync) { + const jobType = type === 'like' + ? 'video-likes' + : 'video-dislikes' + + return createJob({ uri, videoId: video.id, type: jobType }) } - if (syncParam.comments === true) { - const handler = items => addVideoComments(items) - const cleaner = crawlStartDate => VideoCommentModel.cleanOldCommentsOf(video.id, crawlStartDate) + const handler = items => createRates(items, video, type) + const cleaner = crawlStartDate => AccountVideoRateModel.cleanOldRatesOf(video.id, type, crawlStartDate) + + return crawlCollectionPage(uri, handler, cleaner) + .catch(err => logger.error('Cannot add rate of video %s.', video.uuid, { err, rootUrl: uri })) +} - await crawlCollectionPage(fetchedVideo.comments, handler, cleaner) - .catch(err => logger.error('Cannot add comments of video %s.', video.uuid, { err, rootUrl: fetchedVideo.comments })) - } else { - jobPayloads.push({ uri: fetchedVideo.comments, videoId: video.id, type: 'video-comments' as 'video-comments' }) +function syncShares (video: MVideo, fetchedVideo: VideoObject, isSync: boolean) { + if (!isSync) { + return createJob({ uri: fetchedVideo.shares, videoId: video.id, type: 'video-shares' }) } - await Bluebird.map(jobPayloads, payload => JobQueue.Instance.createJobWithPromise({ type: 'activitypub-http-fetcher', payload })) + const handler = items => addVideoShares(items, video) + const cleaner = crawlStartDate => VideoShareModel.cleanOldSharesOf(video.id, crawlStartDate) + + return crawlCollectionPage(fetchedVideo.shares, handler, cleaner) + .catch(err => logger.error('Cannot add shares of video %s.', video.uuid, { err, rootUrl: fetchedVideo.shares })) } -export { - SyncParam, - syncVideoExternalAttributes +function syncComments (video: MVideo, fetchedVideo: VideoObject, isSync: boolean) { + if (!isSync) { + return createJob({ uri: fetchedVideo.comments, videoId: video.id, type: 'video-comments' }) + } + + const handler = items => addVideoComments(items) + const cleaner = crawlStartDate => VideoCommentModel.cleanOldCommentsOf(video.id, crawlStartDate) + + return crawlCollectionPage(fetchedVideo.comments, handler, cleaner) + .catch(err => logger.error('Cannot add comments of video %s.', video.uuid, { err, rootUrl: fetchedVideo.comments })) } -- 2.41.0