send-update.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { Transaction } from 'sequelize'
  2. import { ActivityAudience, ActivityUpdate } from '../../../../shared/models/activitypub'
  3. import { VideoPrivacy } from '../../../../shared/models/videos'
  4. import { AccountModel } from '../../../models/account/account'
  5. import { ActorModel } from '../../../models/activitypub/actor'
  6. import { VideoModel } from '../../../models/video/video'
  7. import { VideoChannelModel } from '../../../models/video/video-channel'
  8. import { VideoShareModel } from '../../../models/video/video-share'
  9. import { getUpdateActivityPubUrl } from '../url'
  10. import { broadcastToFollowers, sendVideoRelatedActivity } from './utils'
  11. import { audiencify, getActorsInvolvedInVideo, getAudience } from '../audience'
  12. import { logger } from '../../../helpers/logger'
  13. import { VideoCaptionModel } from '../../../models/video/video-caption'
  14. import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
  15. import { VideoPlaylistModel } from '../../../models/video/video-playlist'
  16. import { VideoPlaylistPrivacy } from '../../../../shared/models/videos/playlist/video-playlist-privacy.model'
  17. import { getServerActor } from '../../../helpers/utils'
  18. async function sendUpdateVideo (video: VideoModel, t: Transaction, overrodeByActor?: ActorModel) {
  19. if (video.privacy === VideoPrivacy.PRIVATE) return undefined
  20. logger.info('Creating job to update video %s.', video.url)
  21. const byActor = overrodeByActor ? overrodeByActor : video.VideoChannel.Account.Actor
  22. const url = getUpdateActivityPubUrl(video.url, video.updatedAt.toISOString())
  23. // Needed to build the AP object
  24. if (!video.VideoCaptions) video.VideoCaptions = await video.$get('VideoCaptions') as VideoCaptionModel[]
  25. const videoObject = video.toActivityPubObject()
  26. const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC)
  27. const updateActivity = buildUpdateActivity(url, byActor, videoObject, audience)
  28. const actorsInvolved = await getActorsInvolvedInVideo(video, t)
  29. if (overrodeByActor) actorsInvolved.push(overrodeByActor)
  30. return broadcastToFollowers(updateActivity, byActor, actorsInvolved, t)
  31. }
  32. async function sendUpdateActor (accountOrChannel: AccountModel | VideoChannelModel, t: Transaction) {
  33. const byActor = accountOrChannel.Actor
  34. logger.info('Creating job to update actor %s.', byActor.url)
  35. const url = getUpdateActivityPubUrl(byActor.url, byActor.updatedAt.toISOString())
  36. const accountOrChannelObject = accountOrChannel.toActivityPubObject()
  37. const audience = getAudience(byActor)
  38. const updateActivity = buildUpdateActivity(url, byActor, accountOrChannelObject, audience)
  39. let actorsInvolved: ActorModel[]
  40. if (accountOrChannel instanceof AccountModel) {
  41. // Actors that shared my videos are involved too
  42. actorsInvolved = await VideoShareModel.loadActorsWhoSharedVideosOf(byActor.id, t)
  43. } else {
  44. // Actors that shared videos of my channel are involved too
  45. actorsInvolved = await VideoShareModel.loadActorsByVideoChannel(accountOrChannel.id, t)
  46. }
  47. actorsInvolved.push(byActor)
  48. return broadcastToFollowers(updateActivity, byActor, actorsInvolved, t)
  49. }
  50. async function sendUpdateCacheFile (byActor: ActorModel, redundancyModel: VideoRedundancyModel) {
  51. logger.info('Creating job to update cache file %s.', redundancyModel.url)
  52. const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(redundancyModel.getVideo().id)
  53. const activityBuilder = (audience: ActivityAudience) => {
  54. const redundancyObject = redundancyModel.toActivityPubObject()
  55. const url = getUpdateActivityPubUrl(redundancyModel.url, redundancyModel.updatedAt.toISOString())
  56. return buildUpdateActivity(url, byActor, redundancyObject, audience)
  57. }
  58. return sendVideoRelatedActivity(activityBuilder, { byActor, video })
  59. }
  60. async function sendUpdateVideoPlaylist (videoPlaylist: VideoPlaylistModel, t: Transaction) {
  61. if (videoPlaylist.privacy === VideoPlaylistPrivacy.PRIVATE) return undefined
  62. const byActor = videoPlaylist.OwnerAccount.Actor
  63. logger.info('Creating job to update video playlist %s.', videoPlaylist.url)
  64. const url = getUpdateActivityPubUrl(videoPlaylist.url, videoPlaylist.updatedAt.toISOString())
  65. const object = await videoPlaylist.toActivityPubObject(null, t)
  66. const audience = getAudience(byActor, videoPlaylist.privacy === VideoPlaylistPrivacy.PUBLIC)
  67. const updateActivity = buildUpdateActivity(url, byActor, object, audience)
  68. const serverActor = await getServerActor()
  69. const toFollowersOf = [ byActor, serverActor ]
  70. if (videoPlaylist.VideoChannel) toFollowersOf.push(videoPlaylist.VideoChannel.Actor)
  71. return broadcastToFollowers(updateActivity, byActor, toFollowersOf, t)
  72. }
  73. // ---------------------------------------------------------------------------
  74. export {
  75. sendUpdateActor,
  76. sendUpdateVideo,
  77. sendUpdateCacheFile,
  78. sendUpdateVideoPlaylist
  79. }
  80. // ---------------------------------------------------------------------------
  81. function buildUpdateActivity (url: string, byActor: ActorModel, object: any, audience?: ActivityAudience): ActivityUpdate {
  82. if (!audience) audience = getAudience(byActor)
  83. return audiencify(
  84. {
  85. type: 'Update' as 'Update',
  86. id: url,
  87. actor: byActor.url,
  88. object: audiencify(object, audience
  89. )
  90. },
  91. audience
  92. )
  93. }