activitypub-refresher.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import * as Bull from 'bull'
  2. import { logger } from '../../../helpers/logger'
  3. import { fetchVideoByUrl } from '../../../helpers/video'
  4. import { refreshActorIfNeeded } from '../../activitypub/actor'
  5. import { refreshVideoIfNeeded } from '../../activitypub/videos'
  6. import { ActorModel } from '../../../models/activitypub/actor'
  7. import { VideoPlaylistModel } from '../../../models/video/video-playlist'
  8. import { RefreshPayload } from '@shared/models'
  9. import { refreshVideoPlaylistIfNeeded } from '@server/lib/activitypub/playlist'
  10. async function refreshAPObject (job: Bull.Job) {
  11. const payload = job.data as RefreshPayload
  12. logger.info('Processing AP refresher in job %d for %s.', job.id, payload.url)
  13. if (payload.type === 'video') return refreshVideo(payload.url)
  14. if (payload.type === 'video-playlist') return refreshVideoPlaylist(payload.url)
  15. if (payload.type === 'actor') return refreshActor(payload.url)
  16. }
  17. // ---------------------------------------------------------------------------
  18. export {
  19. refreshAPObject
  20. }
  21. // ---------------------------------------------------------------------------
  22. async function refreshVideo (videoUrl: string) {
  23. const fetchType = 'all' as 'all'
  24. const syncParam = { likes: true, dislikes: true, shares: true, comments: true, thumbnail: true }
  25. const videoFromDatabase = await fetchVideoByUrl(videoUrl, fetchType)
  26. if (videoFromDatabase) {
  27. const refreshOptions = {
  28. video: videoFromDatabase,
  29. fetchedType: fetchType,
  30. syncParam
  31. }
  32. await refreshVideoIfNeeded(refreshOptions)
  33. }
  34. }
  35. async function refreshActor (actorUrl: string) {
  36. const fetchType = 'all' as 'all'
  37. const actor = await ActorModel.loadByUrlAndPopulateAccountAndChannel(actorUrl)
  38. if (actor) {
  39. await refreshActorIfNeeded(actor, fetchType)
  40. }
  41. }
  42. async function refreshVideoPlaylist (playlistUrl: string) {
  43. const playlist = await VideoPlaylistModel.loadByUrlAndPopulateAccount(playlistUrl)
  44. if (playlist) {
  45. await refreshVideoPlaylistIfNeeded(playlist)
  46. }
  47. }