2021-06-02 09:49:59 -05:00
|
|
|
import { logger, loggerTagsFactory } from '@server/helpers/logger'
|
2021-06-02 02:35:01 -05:00
|
|
|
import { JobQueue } from '@server/lib/job-queue'
|
|
|
|
import { AccountVideoRateModel } from '@server/models/account/account-video-rate'
|
|
|
|
import { VideoCommentModel } from '@server/models/video/video-comment'
|
|
|
|
import { VideoShareModel } from '@server/models/video/video-share'
|
|
|
|
import { MVideo } from '@server/types/models'
|
|
|
|
import { ActivitypubHttpFetcherPayload, VideoObject } from '@shared/models'
|
|
|
|
import { crawlCollectionPage } from '../../crawl'
|
|
|
|
import { addVideoShares } from '../../share'
|
|
|
|
import { addVideoComments } from '../../video-comments'
|
|
|
|
import { createRates } from '../../video-rates'
|
|
|
|
|
2021-06-02 09:49:59 -05:00
|
|
|
const lTags = loggerTagsFactory('ap', 'video')
|
|
|
|
|
2021-06-02 02:35:01 -05:00
|
|
|
type SyncParam = {
|
|
|
|
likes: boolean
|
|
|
|
dislikes: boolean
|
|
|
|
shares: boolean
|
|
|
|
comments: boolean
|
|
|
|
thumbnail: boolean
|
|
|
|
refreshVideo?: boolean
|
|
|
|
}
|
|
|
|
|
|
|
|
async function syncVideoExternalAttributes (video: MVideo, fetchedVideo: VideoObject, syncParam: SyncParam) {
|
|
|
|
logger.info('Adding likes/dislikes/shares/comments of video %s.', video.uuid)
|
|
|
|
|
2021-06-02 08:57:30 -05:00
|
|
|
await syncRates('like', video, fetchedVideo, syncParam.likes)
|
|
|
|
await syncRates('dislike', video, fetchedVideo, syncParam.dislikes)
|
2021-06-02 02:35:01 -05:00
|
|
|
|
2021-06-02 08:57:30 -05:00
|
|
|
await syncShares(video, fetchedVideo, syncParam.shares)
|
2021-06-02 02:35:01 -05:00
|
|
|
|
2021-06-02 08:57:30 -05:00
|
|
|
await syncComments(video, fetchedVideo, syncParam.comments)
|
|
|
|
}
|
2021-06-02 02:35:01 -05:00
|
|
|
|
2021-06-02 08:57:30 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
2021-06-02 02:35:01 -05:00
|
|
|
|
2021-06-02 08:57:30 -05:00
|
|
|
export {
|
|
|
|
SyncParam,
|
|
|
|
syncVideoExternalAttributes
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
function createJob (payload: ActivitypubHttpFetcherPayload) {
|
|
|
|
return JobQueue.Instance.createJobWithPromise({ type: 'activitypub-http-fetcher', payload })
|
|
|
|
}
|
2021-06-02 02:35:01 -05:00
|
|
|
|
2021-06-02 08:57:30 -05:00
|
|
|
function syncRates (type: 'like' | 'dislike', video: MVideo, fetchedVideo: VideoObject, isSync: boolean) {
|
|
|
|
const uri = type === 'like'
|
|
|
|
? fetchedVideo.likes
|
|
|
|
: fetchedVideo.dislikes
|
2021-06-02 02:35:01 -05:00
|
|
|
|
2021-06-02 08:57:30 -05:00
|
|
|
if (!isSync) {
|
|
|
|
const jobType = type === 'like'
|
|
|
|
? 'video-likes'
|
|
|
|
: 'video-dislikes'
|
|
|
|
|
|
|
|
return createJob({ uri, videoId: video.id, type: jobType })
|
2021-06-02 02:35:01 -05:00
|
|
|
}
|
|
|
|
|
2021-06-02 08:57:30 -05:00
|
|
|
const handler = items => createRates(items, video, type)
|
|
|
|
const cleaner = crawlStartDate => AccountVideoRateModel.cleanOldRatesOf(video.id, type, crawlStartDate)
|
|
|
|
|
|
|
|
return crawlCollectionPage<string>(uri, handler, cleaner)
|
2021-06-03 09:56:42 -05:00
|
|
|
.catch(err => logger.error('Cannot add rate of video %s.', video.uuid, { err, rootUrl: uri, ...lTags(video.uuid, video.url) }))
|
2021-06-02 08:57:30 -05:00
|
|
|
}
|
2021-06-02 02:35:01 -05:00
|
|
|
|
2021-06-02 08:57:30 -05:00
|
|
|
function syncShares (video: MVideo, fetchedVideo: VideoObject, isSync: boolean) {
|
2021-06-02 09:49:59 -05:00
|
|
|
const uri = fetchedVideo.shares
|
|
|
|
|
2021-06-02 08:57:30 -05:00
|
|
|
if (!isSync) {
|
2021-06-02 09:49:59 -05:00
|
|
|
return createJob({ uri, videoId: video.id, type: 'video-shares' })
|
2021-06-02 02:35:01 -05:00
|
|
|
}
|
|
|
|
|
2021-06-02 08:57:30 -05:00
|
|
|
const handler = items => addVideoShares(items, video)
|
|
|
|
const cleaner = crawlStartDate => VideoShareModel.cleanOldSharesOf(video.id, crawlStartDate)
|
|
|
|
|
2021-06-02 09:49:59 -05:00
|
|
|
return crawlCollectionPage<string>(uri, handler, cleaner)
|
2021-06-03 09:56:42 -05:00
|
|
|
.catch(err => logger.error('Cannot add shares of video %s.', video.uuid, { err, rootUrl: uri, ...lTags(video.uuid, video.url) }))
|
2021-06-02 02:35:01 -05:00
|
|
|
}
|
|
|
|
|
2021-06-02 08:57:30 -05:00
|
|
|
function syncComments (video: MVideo, fetchedVideo: VideoObject, isSync: boolean) {
|
2021-06-02 09:49:59 -05:00
|
|
|
const uri = fetchedVideo.comments
|
|
|
|
|
2021-06-02 08:57:30 -05:00
|
|
|
if (!isSync) {
|
2021-06-02 09:49:59 -05:00
|
|
|
return createJob({ uri, videoId: video.id, type: 'video-comments' })
|
2021-06-02 08:57:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const handler = items => addVideoComments(items)
|
|
|
|
const cleaner = crawlStartDate => VideoCommentModel.cleanOldCommentsOf(video.id, crawlStartDate)
|
|
|
|
|
2021-06-02 09:49:59 -05:00
|
|
|
return crawlCollectionPage<string>(uri, handler, cleaner)
|
2021-06-03 09:56:42 -05:00
|
|
|
.catch(err => logger.error('Cannot add comments of video %s.', video.uuid, { err, rootUrl: uri, ...lTags(video.uuid, video.url) }))
|
2021-06-02 02:35:01 -05:00
|
|
|
}
|