]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/job-queue/handlers/activitypub-http-fetcher.ts
Fix user notifications on new follow
[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'
f6eebcb3 14
418d092a 15type FetchType = 'activity' | 'video-likes' | 'video-dislikes' | 'video-shares' | 'video-comments' | 'account-playlists'
c986175d 16
94a5ff8a 17export type ActivitypubHttpFetcherPayload = {
f6eebcb3
C
18 uri: string
19 type: FetchType
20 videoId?: number
418d092a 21 accountId?: number
94a5ff8a
C
22}
23
94831479 24async 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> } = {
1198edf4 36 'activity': items => processActivities(items, { outboxUrl: payload.uri, fromFetch: true }),
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
56export {
94a5ff8a 57 processActivityPubHttpFetcher
c986175d 58}