change video type
[oweals/peertube.git] / server / lib / job-queue / handlers / activitypub-http-fetcher.ts
1 import * as Bull from 'bull'
2 import { logger } from '../../../helpers/logger'
3 import { processActivities } from '../../activitypub/process'
4 import { addVideoComments } from '../../activitypub/video-comments'
5 import { crawlCollectionPage } from '../../activitypub/crawl'
6 import { VideoModel } from '../../../models/video/video'
7 import { addVideoShares, createRates } from '../../activitypub'
8
9 type FetchType = 'activity' | 'video-likes' | 'video-dislikes' | 'video-shares' | 'video-comments'
10
11 export type ActivitypubHttpFetcherPayload = {
12   uri: string
13   type: FetchType
14   videoId?: number
15 }
16
17 async function processActivityPubHttpFetcher (job: Bull.Job) {
18   logger.info('Processing ActivityPub fetcher in job %d.', job.id)
19
20   const payload = job.data as ActivitypubHttpFetcherPayload
21
22   let video: VideoModel
23   if (payload.videoId) video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoId)
24
25   const fetcherType: { [ id in FetchType ]: (items: any[]) => Promise<any> } = {
26     'activity': items => processActivities(items),
27     'video-likes': items => createRates(items, video, 'like'),
28     'video-dislikes': items => createRates(items, video, 'dislike'),
29     'video-shares': items => addVideoShares(items, video),
30     'video-comments': items => addVideoComments(items, video)
31   }
32
33   return crawlCollectionPage(payload.uri, fetcherType[payload.type])
34 }
35
36 // ---------------------------------------------------------------------------
37
38 export {
39   processActivityPubHttpFetcher
40 }