1
0
Fork 0

Refactor sync attributes

This commit is contained in:
Chocobozzz 2021-06-02 15:57:30 +02:00
parent 304a84d59c
commit e872632091
No known key found for this signature in database
GPG key ID: 583A612D890159BE

View file

@ -10,8 +10,6 @@ import { addVideoShares } from '../../share'
import { addVideoComments } from '../../video-comments' import { addVideoComments } from '../../video-comments'
import { createRates } from '../../video-rates' import { createRates } from '../../video-rates'
import Bluebird = require('bluebird')
type SyncParam = { type SyncParam = {
likes: boolean likes: boolean
dislikes: boolean dislikes: boolean
@ -24,52 +22,67 @@ type SyncParam = {
async function syncVideoExternalAttributes (video: MVideo, fetchedVideo: VideoObject, syncParam: SyncParam) { async function syncVideoExternalAttributes (video: MVideo, fetchedVideo: VideoObject, syncParam: SyncParam) {
logger.info('Adding likes/dislikes/shares/comments of video %s.', video.uuid) 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) { await syncShares(video, fetchedVideo, syncParam.shares)
const handler = items => createRates(items, video, 'like')
const cleaner = crawlStartDate => AccountVideoRateModel.cleanOldRatesOf(video.id, 'like' as 'like', crawlStartDate)
await crawlCollectionPage<string>(fetchedVideo.likes, handler, cleaner) await syncComments(video, fetchedVideo, syncParam.comments)
.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' })
}
if (syncParam.dislikes === true) {
const handler = items => createRates(items, video, 'dislike')
const cleaner = crawlStartDate => AccountVideoRateModel.cleanOldRatesOf(video.id, 'dislike' as 'dislike', crawlStartDate)
await crawlCollectionPage<string>(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' })
}
if (syncParam.shares === true) {
const handler = items => addVideoShares(items, video)
const cleaner = crawlStartDate => VideoShareModel.cleanOldSharesOf(video.id, crawlStartDate)
await crawlCollectionPage<string>(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 (syncParam.comments === true) {
const handler = items => addVideoComments(items)
const cleaner = crawlStartDate => VideoCommentModel.cleanOldCommentsOf(video.id, crawlStartDate)
await crawlCollectionPage<string>(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' })
}
await Bluebird.map(jobPayloads, payload => JobQueue.Instance.createJobWithPromise({ type: 'activitypub-http-fetcher', payload }))
} }
// ---------------------------------------------------------------------------
export { export {
SyncParam, SyncParam,
syncVideoExternalAttributes syncVideoExternalAttributes
} }
// ---------------------------------------------------------------------------
function createJob (payload: ActivitypubHttpFetcherPayload) {
return JobQueue.Instance.createJobWithPromise({ type: 'activitypub-http-fetcher', payload })
}
function syncRates (type: 'like' | 'dislike', video: MVideo, fetchedVideo: VideoObject, isSync: boolean) {
const uri = type === 'like'
? fetchedVideo.likes
: fetchedVideo.dislikes
if (!isSync) {
const jobType = type === 'like'
? 'video-likes'
: 'video-dislikes'
return createJob({ uri, videoId: video.id, type: jobType })
}
const handler = items => createRates(items, video, type)
const cleaner = crawlStartDate => AccountVideoRateModel.cleanOldRatesOf(video.id, type, crawlStartDate)
return crawlCollectionPage<string>(uri, handler, cleaner)
.catch(err => logger.error('Cannot add rate of video %s.', video.uuid, { err, rootUrl: uri }))
}
function syncShares (video: MVideo, fetchedVideo: VideoObject, isSync: boolean) {
if (!isSync) {
return createJob({ uri: fetchedVideo.shares, videoId: video.id, type: 'video-shares' })
}
const handler = items => addVideoShares(items, video)
const cleaner = crawlStartDate => VideoShareModel.cleanOldSharesOf(video.id, crawlStartDate)
return crawlCollectionPage<string>(fetchedVideo.shares, handler, cleaner)
.catch(err => logger.error('Cannot add shares of video %s.', video.uuid, { err, rootUrl: fetchedVideo.shares }))
}
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<string>(fetchedVideo.comments, handler, cleaner)
.catch(err => logger.error('Cannot add comments of video %s.', video.uuid, { err, rootUrl: fetchedVideo.comments }))
}