]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/job-queue/handlers/activitypub-http-fetcher.ts
52225f64fd316cc894d98806f8f80d16694e2716
[github/Chocobozzz/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 import { createAccountPlaylists } from '../../activitypub/playlist'
9 import { AccountModel } from '../../../models/account/account'
10
11 type FetchType = 'activity' | 'video-likes' | 'video-dislikes' | 'video-shares' | 'video-comments' | 'account-playlists'
12
13 export type ActivitypubHttpFetcherPayload = {
14 uri: string
15 type: FetchType
16 videoId?: number
17 accountId?: number
18 }
19
20 async function processActivityPubHttpFetcher (job: Bull.Job) {
21 logger.info('Processing ActivityPub fetcher in job %d.', job.id)
22
23 const payload = job.data as ActivitypubHttpFetcherPayload
24
25 let video: VideoModel
26 if (payload.videoId) video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoId)
27
28 let account: AccountModel
29 if (payload.accountId) account = await AccountModel.load(payload.accountId)
30
31 const fetcherType: { [ id in FetchType ]: (items: any[]) => Promise<any> } = {
32 'activity': items => processActivities(items, { outboxUrl: payload.uri }),
33 'video-likes': items => createRates(items, video, 'like'),
34 'video-dislikes': items => createRates(items, video, 'dislike'),
35 'video-shares': items => addVideoShares(items, video),
36 'video-comments': items => addVideoComments(items, video),
37 'account-playlists': items => createAccountPlaylists(items, account)
38 }
39
40 return crawlCollectionPage(payload.uri, fetcherType[payload.type])
41 }
42
43 // ---------------------------------------------------------------------------
44
45 export {
46 processActivityPubHttpFetcher
47 }