send-create.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { Transaction } from 'sequelize'
  2. import { ActivityAudience, ActivityCreate } from '../../../../shared/models/activitypub'
  3. import { VideoPrivacy } from '../../../../shared/models/videos'
  4. import { ActorModel } from '../../../models/activitypub/actor'
  5. import { VideoModel } from '../../../models/video/video'
  6. import { VideoCommentModel } from '../../../models/video/video-comment'
  7. import { broadcastToActors, broadcastToFollowers, sendVideoRelatedActivity, unicastTo } from './utils'
  8. import { audiencify, getActorsInvolvedInVideo, getAudience, getAudienceFromFollowersOf, getVideoCommentAudience } from '../audience'
  9. import { logger } from '../../../helpers/logger'
  10. import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
  11. import { VideoPlaylistModel } from '../../../models/video/video-playlist'
  12. import { VideoPlaylistPrivacy } from '../../../../shared/models/videos/playlist/video-playlist-privacy.model'
  13. import { getServerActor } from '../../../helpers/utils'
  14. async function sendCreateVideo (video: VideoModel, t: Transaction) {
  15. if (video.privacy === VideoPrivacy.PRIVATE) return undefined
  16. logger.info('Creating job to send video creation of %s.', video.url)
  17. const byActor = video.VideoChannel.Account.Actor
  18. const videoObject = video.toActivityPubObject()
  19. const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC)
  20. const createActivity = buildCreateActivity(video.url, byActor, videoObject, audience)
  21. return broadcastToFollowers(createActivity, byActor, [ byActor ], t)
  22. }
  23. async function sendCreateCacheFile (byActor: ActorModel, video: VideoModel, fileRedundancy: VideoRedundancyModel) {
  24. logger.info('Creating job to send file cache of %s.', fileRedundancy.url)
  25. return sendVideoRelatedCreateActivity({
  26. byActor,
  27. video,
  28. url: fileRedundancy.url,
  29. object: fileRedundancy.toActivityPubObject()
  30. })
  31. }
  32. async function sendCreateVideoPlaylist (playlist: VideoPlaylistModel, t: Transaction) {
  33. if (playlist.privacy === VideoPlaylistPrivacy.PRIVATE) return undefined
  34. logger.info('Creating job to send create video playlist of %s.', playlist.url)
  35. const byActor = playlist.OwnerAccount.Actor
  36. const audience = getAudience(byActor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC)
  37. const object = await playlist.toActivityPubObject(null, t)
  38. const createActivity = buildCreateActivity(playlist.url, byActor, object, audience)
  39. const serverActor = await getServerActor()
  40. const toFollowersOf = [ byActor, serverActor ]
  41. if (playlist.VideoChannel) toFollowersOf.push(playlist.VideoChannel.Actor)
  42. return broadcastToFollowers(createActivity, byActor, toFollowersOf, t)
  43. }
  44. async function sendCreateVideoComment (comment: VideoCommentModel, t: Transaction) {
  45. logger.info('Creating job to send comment %s.', comment.url)
  46. const isOrigin = comment.Video.isOwned()
  47. const byActor = comment.Account.Actor
  48. const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
  49. const commentObject = comment.toActivityPubObject(threadParentComments)
  50. const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
  51. // Add the actor that commented too
  52. actorsInvolvedInComment.push(byActor)
  53. const parentsCommentActors = threadParentComments.map(c => c.Account.Actor)
  54. let audience: ActivityAudience
  55. if (isOrigin) {
  56. audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, isOrigin)
  57. } else {
  58. audience = getAudienceFromFollowersOf(actorsInvolvedInComment.concat(parentsCommentActors))
  59. }
  60. const createActivity = buildCreateActivity(comment.url, byActor, commentObject, audience)
  61. // This was a reply, send it to the parent actors
  62. const actorsException = [ byActor ]
  63. await broadcastToActors(createActivity, byActor, parentsCommentActors, actorsException)
  64. // Broadcast to our followers
  65. await broadcastToFollowers(createActivity, byActor, [ byActor ], t)
  66. // Send to actors involved in the comment
  67. if (isOrigin) return broadcastToFollowers(createActivity, byActor, actorsInvolvedInComment, t, actorsException)
  68. // Send to origin
  69. return unicastTo(createActivity, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
  70. }
  71. function buildCreateActivity (url: string, byActor: ActorModel, object: any, audience?: ActivityAudience): ActivityCreate {
  72. if (!audience) audience = getAudience(byActor)
  73. return audiencify(
  74. {
  75. type: 'Create' as 'Create',
  76. id: url + '/activity',
  77. actor: byActor.url,
  78. object: audiencify(object, audience)
  79. },
  80. audience
  81. )
  82. }
  83. // ---------------------------------------------------------------------------
  84. export {
  85. sendCreateVideo,
  86. buildCreateActivity,
  87. sendCreateVideoComment,
  88. sendCreateVideoPlaylist,
  89. sendCreateCacheFile
  90. }
  91. // ---------------------------------------------------------------------------
  92. async function sendVideoRelatedCreateActivity (options: {
  93. byActor: ActorModel,
  94. video: VideoModel,
  95. url: string,
  96. object: any,
  97. transaction?: Transaction
  98. }) {
  99. const activityBuilder = (audience: ActivityAudience) => {
  100. return buildCreateActivity(options.url, options.byActor, options.object, audience)
  101. }
  102. return sendVideoRelatedActivity(activityBuilder, options)
  103. }