PeerTube/server/lib/job-queue/handlers/activitypub-http-fetcher.ts

59 lines
2.7 KiB
TypeScript
Raw Normal View History

2018-07-10 10:02:20 -05:00
import * as Bull from 'bull'
2019-03-19 10:23:02 -05:00
import * as Bluebird from 'bluebird'
2017-12-28 04:16:08 -06:00
import { logger } from '../../../helpers/logger'
2017-12-12 10:53:50 -06:00
import { processActivities } from '../../activitypub/process'
import { addVideoComments } from '../../activitypub/video-comments'
import { crawlCollectionPage } from '../../activitypub/crawl'
2018-09-19 04:16:23 -05:00
import { VideoModel } from '../../../models/video/video'
import { addVideoShares, createRates } from '../../activitypub'
2019-02-26 03:55:40 -06:00
import { createAccountPlaylists } from '../../activitypub/playlist'
import { AccountModel } from '../../../models/account/account'
2019-03-19 10:23:02 -05:00
import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
import { VideoShareModel } from '../../../models/video/video-share'
import { VideoCommentModel } from '../../../models/video/video-comment'
2019-02-26 03:55:40 -06:00
type FetchType = 'activity' | 'video-likes' | 'video-dislikes' | 'video-shares' | 'video-comments' | 'account-playlists'
2017-11-22 03:29:55 -06:00
export type ActivitypubHttpFetcherPayload = {
uri: string
type: FetchType
videoId?: number
2019-02-26 03:55:40 -06:00
accountId?: number
}
2018-07-10 10:02:20 -05:00
async function processActivityPubHttpFetcher (job: Bull.Job) {
logger.info('Processing ActivityPub fetcher in job %d.', job.id)
const payload = job.data as ActivitypubHttpFetcherPayload
let video: VideoModel
if (payload.videoId) video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoId)
2017-11-22 03:29:55 -06:00
2019-02-26 03:55:40 -06:00
let account: AccountModel
if (payload.accountId) account = await AccountModel.load(payload.accountId)
const fetcherType: { [ id in FetchType ]: (items: any[]) => Promise<any> } = {
2018-11-14 08:01:28 -06:00
'activity': items => processActivities(items, { outboxUrl: payload.uri }),
'video-likes': items => createRates(items, video, 'like'),
'video-dislikes': items => createRates(items, video, 'dislike'),
'video-shares': items => addVideoShares(items, video),
2019-02-26 03:55:40 -06:00
'video-comments': items => addVideoComments(items, video),
'account-playlists': items => createAccountPlaylists(items, account)
2017-11-22 03:29:55 -06:00
}
2019-03-19 10:23:02 -05:00
const cleanerType: { [ id in FetchType ]?: (crawlStartDate: Date) => Bluebird<any> } = {
'video-likes': crawlStartDate => AccountVideoRateModel.cleanOldRatesOf(video.id, 'like' as 'like', crawlStartDate),
'video-dislikes': crawlStartDate => AccountVideoRateModel.cleanOldRatesOf(video.id, 'dislike' as 'dislike', crawlStartDate),
'video-shares': crawlStartDate => VideoShareModel.cleanOldSharesOf(video.id, crawlStartDate),
'video-comments': crawlStartDate => VideoCommentModel.cleanOldCommentsOf(video.id, crawlStartDate)
}
return crawlCollectionPage(payload.uri, fetcherType[payload.type], cleanerType[payload.type])
2017-11-22 03:29:55 -06:00
}
// ---------------------------------------------------------------------------
export {
processActivityPubHttpFetcher
2017-11-22 03:29:55 -06:00
}