Fix user notifications on new follow
[oweals/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
15 type FetchType = 'activity' | 'video-likes' | 'video-dislikes' | 'video-shares' | 'video-comments' | 'account-playlists'
16
17 export type ActivitypubHttpFetcherPayload = {
18   uri: string
19   type: FetchType
20   videoId?: number
21   accountId?: number
22 }
23
24 async function processActivityPubHttpFetcher (job: Bull.Job) {
25   logger.info('Processing ActivityPub fetcher in job %d.', job.id)
26
27   const payload = job.data as ActivitypubHttpFetcherPayload
28
29   let video: VideoModel
30   if (payload.videoId) video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoId)
31
32   let account: AccountModel
33   if (payload.accountId) account = await AccountModel.load(payload.accountId)
34
35   const fetcherType: { [ id in FetchType ]: (items: any[]) => Promise<any> } = {
36     'activity': items => processActivities(items, { outboxUrl: payload.uri, fromFetch: true }),
37     'video-likes': items => createRates(items, video, 'like'),
38     'video-dislikes': items => createRates(items, video, 'dislike'),
39     'video-shares': items => addVideoShares(items, video),
40     'video-comments': items => addVideoComments(items, video),
41     'account-playlists': items => createAccountPlaylists(items, account)
42   }
43
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])
52 }
53
54 // ---------------------------------------------------------------------------
55
56 export {
57   processActivityPubHttpFetcher
58 }