]>
Commit | Line | Data |
---|---|---|
94831479 | 1 | import * as Bull from 'bull' |
2ba92871 | 2 | import * as Bluebird from 'bluebird' |
da854ddd | 3 | import { logger } from '../../../helpers/logger' |
3fd3ab2d | 4 | import { processActivities } from '../../activitypub/process' |
f6eebcb3 | 5 | import { addVideoComments } from '../../activitypub/video-comments' |
8fffe21a | 6 | import { crawlCollectionPage } from '../../activitypub/crawl' |
4157cdb1 C |
7 | import { VideoModel } from '../../../models/video/video' |
8 | import { addVideoShares, createRates } from '../../activitypub' | |
418d092a C |
9 | import { createAccountPlaylists } from '../../activitypub/playlist' |
10 | import { AccountModel } from '../../../models/account/account' | |
2ba92871 C |
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' | |
f6eebcb3 | 14 | |
418d092a | 15 | type FetchType = 'activity' | 'video-likes' | 'video-dislikes' | 'video-shares' | 'video-comments' | 'account-playlists' |
c986175d | 16 | |
94a5ff8a | 17 | export type ActivitypubHttpFetcherPayload = { |
f6eebcb3 C |
18 | uri: string |
19 | type: FetchType | |
20 | videoId?: number | |
418d092a | 21 | accountId?: number |
94a5ff8a C |
22 | } |
23 | ||
94831479 | 24 | async function processActivityPubHttpFetcher (job: Bull.Job) { |
94a5ff8a C |
25 | logger.info('Processing ActivityPub fetcher in job %d.', job.id) |
26 | ||
f6eebcb3 C |
27 | const payload = job.data as ActivitypubHttpFetcherPayload |
28 | ||
29 | let video: VideoModel | |
30 | if (payload.videoId) video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoId) | |
c986175d | 31 | |
418d092a C |
32 | let account: AccountModel |
33 | if (payload.accountId) account = await AccountModel.load(payload.accountId) | |
34 | ||
f6eebcb3 | 35 | const fetcherType: { [ id in FetchType ]: (items: any[]) => Promise<any> } = { |
5c6d985f | 36 | 'activity': items => processActivities(items, { outboxUrl: payload.uri }), |
f6eebcb3 C |
37 | 'video-likes': items => createRates(items, video, 'like'), |
38 | 'video-dislikes': items => createRates(items, video, 'dislike'), | |
39 | 'video-shares': items => addVideoShares(items, video), | |
418d092a C |
40 | 'video-comments': items => addVideoComments(items, video), |
41 | 'account-playlists': items => createAccountPlaylists(items, account) | |
c986175d | 42 | } |
f6eebcb3 | 43 | |
2ba92871 C |
44 | const cleanerType: { [ id in FetchType ]?: (crawlStartDate: Date) => Bluebird<any> } = { |
45 | 'video-likes': crawlStartDate => AccountVideoRateModel.cleanOldRatesOf(video.id, 'like' as 'like', crawlStartDate), | |
46 | 'video-dislikes': crawlStartDate => AccountVideoRateModel.cleanOldRatesOf(video.id, 'dislike' as 'dislike', crawlStartDate), | |
47 | 'video-shares': crawlStartDate => VideoShareModel.cleanOldSharesOf(video.id, crawlStartDate), | |
48 | 'video-comments': crawlStartDate => VideoCommentModel.cleanOldCommentsOf(video.id, crawlStartDate) | |
49 | } | |
50 | ||
51 | return crawlCollectionPage(payload.uri, fetcherType[payload.type], cleanerType[payload.type]) | |
c986175d C |
52 | } |
53 | ||
c986175d C |
54 | // --------------------------------------------------------------------------- |
55 | ||
56 | export { | |
94a5ff8a | 57 | processActivityPubHttpFetcher |
c986175d | 58 | } |