activitypub-refresher.ts 1.9 KB

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