]>
Commit | Line | Data |
---|---|---|
1 | import * as Bull from 'bull' | |
2 | import { ActivitypubHttpFetcherPayload, FetchType } from '@shared/models' | |
3 | import { logger } from '../../../helpers/logger' | |
4 | import { AccountModel } from '../../../models/account/account' | |
5 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' | |
6 | import { VideoModel } from '../../../models/video/video' | |
7 | import { VideoCommentModel } from '../../../models/video/video-comment' | |
8 | import { VideoShareModel } from '../../../models/video/video-share' | |
9 | import { MAccountDefault, MVideoFullLight } from '../../../types/models' | |
10 | import { crawlCollectionPage } from '../../activitypub/crawl' | |
11 | import { createAccountPlaylists } from '../../activitypub/playlists' | |
12 | import { processActivities } from '../../activitypub/process' | |
13 | import { addVideoShares } from '../../activitypub/share' | |
14 | import { addVideoComments } from '../../activitypub/video-comments' | |
15 | import { createRates } from '../../activitypub/video-rates' | |
16 | ||
17 | async function processActivityPubHttpFetcher (job: Bull.Job) { | |
18 | logger.info('Processing ActivityPub fetcher in job %d.', job.id) | |
19 | ||
20 | const payload = job.data as ActivitypubHttpFetcherPayload | |
21 | ||
22 | let video: MVideoFullLight | |
23 | if (payload.videoId) video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoId) | |
24 | ||
25 | let account: MAccountDefault | |
26 | if (payload.accountId) account = await AccountModel.load(payload.accountId) | |
27 | ||
28 | const fetcherType: { [ id in FetchType ]: (items: any[]) => Promise<any> } = { | |
29 | 'activity': items => processActivities(items, { outboxUrl: payload.uri, fromFetch: true }), | |
30 | 'video-likes': items => createRates(items, video, 'like'), | |
31 | 'video-dislikes': items => createRates(items, video, 'dislike'), | |
32 | 'video-shares': items => addVideoShares(items, video), | |
33 | 'video-comments': items => addVideoComments(items), | |
34 | 'account-playlists': items => createAccountPlaylists(items, account) | |
35 | } | |
36 | ||
37 | const cleanerType: { [ id in FetchType ]?: (crawlStartDate: Date) => Promise<any> } = { | |
38 | 'video-likes': crawlStartDate => AccountVideoRateModel.cleanOldRatesOf(video.id, 'like' as 'like', crawlStartDate), | |
39 | 'video-dislikes': crawlStartDate => AccountVideoRateModel.cleanOldRatesOf(video.id, 'dislike' as 'dislike', crawlStartDate), | |
40 | 'video-shares': crawlStartDate => VideoShareModel.cleanOldSharesOf(video.id, crawlStartDate), | |
41 | 'video-comments': crawlStartDate => VideoCommentModel.cleanOldCommentsOf(video.id, crawlStartDate) | |
42 | } | |
43 | ||
44 | return crawlCollectionPage(payload.uri, fetcherType[payload.type], cleanerType[payload.type]) | |
45 | } | |
46 | ||
47 | // --------------------------------------------------------------------------- | |
48 | ||
49 | export { | |
50 | processActivityPubHttpFetcher | |
51 | } |