activitypub-http-fetcher.ts 2.7 KB

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