]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/job-queue/handlers/activitypub-http-fetcher.ts
Merge branch 'release/2.1.0' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / activitypub-http-fetcher.ts
CommitLineData
94831479 1import * as Bull from 'bull'
2ba92871 2import * as Bluebird from 'bluebird'
da854ddd 3import { logger } from '../../../helpers/logger'
3fd3ab2d 4import { processActivities } from '../../activitypub/process'
f6eebcb3 5import { addVideoComments } from '../../activitypub/video-comments'
8fffe21a 6import { crawlCollectionPage } from '../../activitypub/crawl'
4157cdb1
C
7import { VideoModel } from '../../../models/video/video'
8import { addVideoShares, createRates } from '../../activitypub'
418d092a
C
9import { createAccountPlaylists } from '../../activitypub/playlist'
10import { AccountModel } from '../../../models/account/account'
2ba92871
C
11import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
12import { VideoShareModel } from '../../../models/video/video-share'
13import { VideoCommentModel } from '../../../models/video/video-comment'
453e83ea 14import { MAccountDefault, MVideoFullLight } from '../../../typings/models'
f6eebcb3 15
418d092a 16type FetchType = 'activity' | 'video-likes' | 'video-dislikes' | 'video-shares' | 'video-comments' | 'account-playlists'
c986175d 17
94a5ff8a 18export type ActivitypubHttpFetcherPayload = {
f6eebcb3
C
19 uri: string
20 type: FetchType
21 videoId?: number
418d092a 22 accountId?: number
94a5ff8a
C
23}
24
94831479 25async function processActivityPubHttpFetcher (job: Bull.Job) {
94a5ff8a
C
26 logger.info('Processing ActivityPub fetcher in job %d.', job.id)
27
f6eebcb3
C
28 const payload = job.data as ActivitypubHttpFetcherPayload
29
453e83ea 30 let video: MVideoFullLight
f6eebcb3 31 if (payload.videoId) video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoId)
c986175d 32
453e83ea 33 let account: MAccountDefault
418d092a
C
34 if (payload.accountId) account = await AccountModel.load(payload.accountId)
35
f6eebcb3 36 const fetcherType: { [ id in FetchType ]: (items: any[]) => Promise<any> } = {
1198edf4 37 'activity': items => processActivities(items, { outboxUrl: payload.uri, fromFetch: true }),
f6eebcb3
C
38 'video-likes': items => createRates(items, video, 'like'),
39 'video-dislikes': items => createRates(items, video, 'dislike'),
40 'video-shares': items => addVideoShares(items, video),
6b9c966f 41 'video-comments': items => addVideoComments(items),
418d092a 42 'account-playlists': items => createAccountPlaylists(items, account)
c986175d 43 }
f6eebcb3 44
2ba92871
C
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])
c986175d
C
53}
54
c986175d
C
55// ---------------------------------------------------------------------------
56
57export {
94a5ff8a 58 processActivityPubHttpFetcher
c986175d 59}