PeerTube/server/lib/activitypub/videos/shared/video-sync-attributes.ts

107 lines
3.8 KiB
TypeScript
Raw Normal View History

import { runInReadCommittedTransaction } from '@server/helpers/database-utils'
2021-06-02 16:49:59 +02:00
import { logger, loggerTagsFactory } from '@server/helpers/logger'
import { doJSONRequest } from '@server/helpers/requests'
2021-06-02 09:35:01 +02:00
import { JobQueue } from '@server/lib/job-queue'
import { VideoModel } from '@server/models/video/video'
2021-06-02 09:35:01 +02:00
import { VideoCommentModel } from '@server/models/video/video-comment'
import { VideoShareModel } from '@server/models/video/video-share'
import { MVideo } from '@server/types/models'
import { ActivitypubHttpFetcherPayload, ActivityPubOrderedCollection, VideoObject } from '@shared/models'
2021-06-02 09:35:01 +02:00
import { crawlCollectionPage } from '../../crawl'
import { addVideoShares } from '../../share'
import { addVideoComments } from '../../video-comments'
2021-06-02 16:49:59 +02:00
const lTags = loggerTagsFactory('ap', 'video')
2021-06-02 09:35:01 +02:00
type SyncParam = {
rates: boolean
2021-06-02 09:35:01 +02:00
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)
const ratePromise = updateVideoRates(video, fetchedVideo)
if (syncParam.rates) await ratePromise
2021-06-02 09:35:01 +02:00
2021-06-02 15:57:30 +02:00
await syncShares(video, fetchedVideo, syncParam.shares)
2021-06-02 09:35:01 +02:00
2021-06-02 15:57:30 +02:00
await syncComments(video, fetchedVideo, syncParam.comments)
}
2021-06-02 09:35:01 +02:00
async function updateVideoRates (video: MVideo, fetchedVideo: VideoObject) {
const [ likes, dislikes ] = await Promise.all([
getRatesCount('like', video, fetchedVideo),
getRatesCount('dislike', video, fetchedVideo)
])
return runInReadCommittedTransaction(async t => {
await VideoModel.updateRatesOf(video.id, 'like', likes, t)
await VideoModel.updateRatesOf(video.id, 'dislike', dislikes, t)
})
}
2021-06-02 15:57:30 +02:00
// ---------------------------------------------------------------------------
2021-06-02 09:35:01 +02:00
2021-06-02 15:57:30 +02:00
export {
SyncParam,
syncVideoExternalAttributes,
updateVideoRates
2021-06-02 15:57:30 +02:00
}
// ---------------------------------------------------------------------------
async function getRatesCount (type: 'like' | 'dislike', video: MVideo, fetchedVideo: VideoObject) {
2021-06-02 15:57:30 +02:00
const uri = type === 'like'
? fetchedVideo.likes
: fetchedVideo.dislikes
2021-06-02 09:35:01 +02:00
logger.info('Sync %s of video %s', type, video.url)
const options = { activityPub: true }
const response = await doJSONRequest<ActivityPubOrderedCollection<any>>(uri, options)
const totalItems = response.body.totalItems
2021-06-02 15:57:30 +02:00
if (isNaN(totalItems)) {
logger.error('Cannot sync %s of video %s, totalItems is not a number', type, video.url, { body: response.body })
return
2021-06-02 09:35:01 +02:00
}
return totalItems
}
2021-06-02 15:57:30 +02:00
function createJob (payload: ActivitypubHttpFetcherPayload) {
return JobQueue.Instance.createJobWithPromise({ type: 'activitypub-http-fetcher', payload })
2021-06-02 15:57:30 +02:00
}
2021-06-02 09:35:01 +02:00
2021-06-02 15:57:30 +02:00
function syncShares (video: MVideo, fetchedVideo: VideoObject, isSync: boolean) {
2021-06-02 16:49:59 +02:00
const uri = fetchedVideo.shares
2021-06-02 15:57:30 +02:00
if (!isSync) {
2021-06-02 16:49:59 +02:00
return createJob({ uri, videoId: video.id, type: 'video-shares' })
2021-06-02 09:35:01 +02:00
}
2021-06-02 15:57:30 +02:00
const handler = items => addVideoShares(items, video)
const cleaner = crawlStartDate => VideoShareModel.cleanOldSharesOf(video.id, crawlStartDate)
2021-06-02 16:49:59 +02:00
return crawlCollectionPage<string>(uri, handler, cleaner)
2021-06-03 16:56:42 +02:00
.catch(err => logger.error('Cannot add shares of video %s.', video.uuid, { err, rootUrl: uri, ...lTags(video.uuid, video.url) }))
2021-06-02 09:35:01 +02:00
}
2021-06-02 15:57:30 +02:00
function syncComments (video: MVideo, fetchedVideo: VideoObject, isSync: boolean) {
2021-06-02 16:49:59 +02:00
const uri = fetchedVideo.comments
2021-06-02 15:57:30 +02:00
if (!isSync) {
2021-06-02 16:49:59 +02:00
return createJob({ uri, videoId: video.id, type: 'video-comments' })
2021-06-02 15:57:30 +02:00
}
const handler = items => addVideoComments(items)
const cleaner = crawlStartDate => VideoCommentModel.cleanOldCommentsOf(video.id, crawlStartDate)
2021-06-02 16:49:59 +02:00
return crawlCollectionPage<string>(uri, handler, cleaner)
2021-06-03 16:56:42 +02:00
.catch(err => logger.error('Cannot add comments of video %s.', video.uuid, { err, rootUrl: uri, ...lTags(video.uuid, video.url) }))
2021-06-02 09:35:01 +02:00
}