]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/job-queue/handlers/activitypub-http-fetcher.ts
Avoir some circular dependencies
[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 7import { VideoModel } from '../../../models/video/video'
8dc8a34e
C
8import { addVideoShares } from '../../activitypub/share'
9import { createRates } from '../../activitypub/video-rates'
418d092a
C
10import { createAccountPlaylists } from '../../activitypub/playlist'
11import { AccountModel } from '../../../models/account/account'
2ba92871
C
12import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
13import { VideoShareModel } from '../../../models/video/video-share'
14import { VideoCommentModel } from '../../../models/video/video-comment'
453e83ea 15import { MAccountDefault, MVideoFullLight } from '../../../typings/models'
8dc8a34e 16import { ActivitypubHttpFetcherPayload, FetchType } from '@shared/models'
94a5ff8a 17
94831479 18async function processActivityPubHttpFetcher (job: Bull.Job) {
94a5ff8a
C
19 logger.info('Processing ActivityPub fetcher in job %d.', job.id)
20
f6eebcb3
C
21 const payload = job.data as ActivitypubHttpFetcherPayload
22
453e83ea 23 let video: MVideoFullLight
f6eebcb3 24 if (payload.videoId) video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoId)
c986175d 25
453e83ea 26 let account: MAccountDefault
418d092a
C
27 if (payload.accountId) account = await AccountModel.load(payload.accountId)
28
f6eebcb3 29 const fetcherType: { [ id in FetchType ]: (items: any[]) => Promise<any> } = {
1198edf4 30 'activity': items => processActivities(items, { outboxUrl: payload.uri, fromFetch: true }),
f6eebcb3
C
31 'video-likes': items => createRates(items, video, 'like'),
32 'video-dislikes': items => createRates(items, video, 'dislike'),
33 'video-shares': items => addVideoShares(items, video),
6b9c966f 34 'video-comments': items => addVideoComments(items),
418d092a 35 'account-playlists': items => createAccountPlaylists(items, account)
c986175d 36 }
f6eebcb3 37
2ba92871
C
38 const cleanerType: { [ id in FetchType ]?: (crawlStartDate: Date) => Bluebird<any> } = {
39 'video-likes': crawlStartDate => AccountVideoRateModel.cleanOldRatesOf(video.id, 'like' as 'like', crawlStartDate),
40 'video-dislikes': crawlStartDate => AccountVideoRateModel.cleanOldRatesOf(video.id, 'dislike' as 'dislike', crawlStartDate),
41 'video-shares': crawlStartDate => VideoShareModel.cleanOldSharesOf(video.id, crawlStartDate),
42 'video-comments': crawlStartDate => VideoCommentModel.cleanOldCommentsOf(video.id, crawlStartDate)
43 }
44
45 return crawlCollectionPage(payload.uri, fetcherType[payload.type], cleanerType[payload.type])
c986175d
C
46}
47
c986175d
C
48// ---------------------------------------------------------------------------
49
50export {
94a5ff8a 51 processActivityPubHttpFetcher
c986175d 52}