activitypub-http-fetcher.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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/playlist'
  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. async function processActivityPubHttpFetcher (job: Bull.Job) {
  17. logger.info('Processing ActivityPub fetcher in job %d.', job.id)
  18. const payload = job.data as ActivitypubHttpFetcherPayload
  19. let video: MVideoFullLight
  20. if (payload.videoId) video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoId)
  21. let account: MAccountDefault
  22. if (payload.accountId) account = await AccountModel.load(payload.accountId)
  23. const fetcherType: { [ id in FetchType ]: (items: any[]) => Promise<any> } = {
  24. 'activity': items => processActivities(items, { outboxUrl: payload.uri, fromFetch: true }),
  25. 'video-likes': items => createRates(items, video, 'like'),
  26. 'video-dislikes': items => createRates(items, video, 'dislike'),
  27. 'video-shares': items => addVideoShares(items, video),
  28. 'video-comments': items => addVideoComments(items),
  29. 'account-playlists': items => createAccountPlaylists(items, account)
  30. }
  31. const cleanerType: { [ id in FetchType ]?: (crawlStartDate: Date) => Promise<any> } = {
  32. 'video-likes': crawlStartDate => AccountVideoRateModel.cleanOldRatesOf(video.id, 'like' as 'like', crawlStartDate),
  33. 'video-dislikes': crawlStartDate => AccountVideoRateModel.cleanOldRatesOf(video.id, 'dislike' as 'dislike', crawlStartDate),
  34. 'video-shares': crawlStartDate => VideoShareModel.cleanOldSharesOf(video.id, crawlStartDate),
  35. 'video-comments': crawlStartDate => VideoCommentModel.cleanOldCommentsOf(video.id, crawlStartDate)
  36. }
  37. return crawlCollectionPage(payload.uri, fetcherType[payload.type], cleanerType[payload.type])
  38. }
  39. // ---------------------------------------------------------------------------
  40. export {
  41. processActivityPubHttpFetcher
  42. }