]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/job-queue/handlers/activitypub-http-fetcher.ts
Merge branch 'release/1.4.0' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / activitypub-http-fetcher.ts
1 import * as Bull from 'bull'
2 import * as Bluebird from 'bluebird'
3 import { logger } from '../../../helpers/logger'
4 import { processActivities } from '../../activitypub/process'
5 import { addVideoComments } from '../../activitypub/video-comments'
6 import { crawlCollectionPage } from '../../activitypub/crawl'
7 import { VideoModel } from '../../../models/video/video'
8 import { addVideoShares, createRates } from '../../activitypub'
9 import { createAccountPlaylists } from '../../activitypub/playlist'
10 import { AccountModel } from '../../../models/account/account'
11 import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
12 import { VideoShareModel } from '../../../models/video/video-share'
13 import { VideoCommentModel } from '../../../models/video/video-comment'
14 import { MAccountDefault, MVideoFullLight } from '../../../typings/models'
15
16 type FetchType = 'activity' | 'video-likes' | 'video-dislikes' | 'video-shares' | 'video-comments' | 'account-playlists'
17
18 export type ActivitypubHttpFetcherPayload = {
19 uri: string
20 type: FetchType
21 videoId?: number
22 accountId?: number
23 }
24
25 async function processActivityPubHttpFetcher (job: Bull.Job) {
26 logger.info('Processing ActivityPub fetcher in job %d.', job.id)
27
28 const payload = job.data as ActivitypubHttpFetcherPayload
29
30 let video: MVideoFullLight
31 if (payload.videoId) video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoId)
32
33 let account: MAccountDefault
34 if (payload.accountId) account = await AccountModel.load(payload.accountId)
35
36 const fetcherType: { [ id in FetchType ]: (items: any[]) => Promise<any> } = {
37 'activity': items => processActivities(items, { outboxUrl: payload.uri, fromFetch: true }),
38 'video-likes': items => createRates(items, video, 'like'),
39 'video-dislikes': items => createRates(items, video, 'dislike'),
40 'video-shares': items => addVideoShares(items, video),
41 'video-comments': items => addVideoComments(items),
42 'account-playlists': items => createAccountPlaylists(items, account)
43 }
44
45 const cleanerType: { [ id in FetchType ]?: (crawlStartDate: Date) => Bluebird<any> } = {
46 'video-likes': crawlStartDate => AccountVideoRateModel.cleanOldRatesOf(video.id, 'like' as 'like', crawlStartDate),
47 'video-dislikes': crawlStartDate => AccountVideoRateModel.cleanOldRatesOf(video.id, 'dislike' as 'dislike', crawlStartDate),
48 'video-shares': crawlStartDate => VideoShareModel.cleanOldSharesOf(video.id, crawlStartDate),
49 'video-comments': crawlStartDate => VideoCommentModel.cleanOldCommentsOf(video.id, crawlStartDate)
50 }
51
52 return crawlCollectionPage(payload.uri, fetcherType[payload.type], cleanerType[payload.type])
53 }
54
55 // ---------------------------------------------------------------------------
56
57 export {
58 processActivityPubHttpFetcher
59 }