2
1

send-delete.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { Transaction } from 'sequelize'
  2. import { ActivityAudience, ActivityDelete } from '../../../../shared/models/activitypub'
  3. import { ActorModel } from '../../../models/activitypub/actor'
  4. import { VideoModel } from '../../../models/video/video'
  5. import { VideoCommentModel } from '../../../models/video/video-comment'
  6. import { VideoShareModel } from '../../../models/video/video-share'
  7. import { getDeleteActivityPubUrl } from '../url'
  8. import { broadcastToActors, broadcastToFollowers, sendVideoRelatedActivity, unicastTo } from './utils'
  9. import { audiencify, getActorsInvolvedInVideo, getVideoCommentAudience } from '../audience'
  10. import { logger } from '../../../helpers/logger'
  11. import { VideoPlaylistModel } from '../../../models/video/video-playlist'
  12. import { getServerActor } from '../../../helpers/utils'
  13. async function sendDeleteVideo (video: VideoModel, transaction: Transaction) {
  14. logger.info('Creating job to broadcast delete of video %s.', video.url)
  15. const byActor = video.VideoChannel.Account.Actor
  16. const activityBuilder = (audience: ActivityAudience) => {
  17. const url = getDeleteActivityPubUrl(video.url)
  18. return buildDeleteActivity(url, video.url, byActor, audience)
  19. }
  20. return sendVideoRelatedActivity(activityBuilder, { byActor, video, transaction })
  21. }
  22. async function sendDeleteActor (byActor: ActorModel, t: Transaction) {
  23. logger.info('Creating job to broadcast delete of actor %s.', byActor.url)
  24. const url = getDeleteActivityPubUrl(byActor.url)
  25. const activity = buildDeleteActivity(url, byActor.url, byActor)
  26. const actorsInvolved = await VideoShareModel.loadActorsWhoSharedVideosOf(byActor.id, t)
  27. // In case the actor did not have any videos
  28. const serverActor = await getServerActor()
  29. actorsInvolved.push(serverActor)
  30. actorsInvolved.push(byActor)
  31. return broadcastToFollowers(activity, byActor, actorsInvolved, t)
  32. }
  33. async function sendDeleteVideoComment (videoComment: VideoCommentModel, t: Transaction) {
  34. logger.info('Creating job to send delete of comment %s.', videoComment.url)
  35. const isVideoOrigin = videoComment.Video.isOwned()
  36. const url = getDeleteActivityPubUrl(videoComment.url)
  37. const byActor = videoComment.Account.Actor
  38. const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, t)
  39. const actorsInvolvedInComment = await getActorsInvolvedInVideo(videoComment.Video, t)
  40. actorsInvolvedInComment.push(byActor) // Add the actor that commented the video
  41. const audience = getVideoCommentAudience(videoComment, threadParentComments, actorsInvolvedInComment, isVideoOrigin)
  42. const activity = buildDeleteActivity(url, videoComment.url, byActor, audience)
  43. // This was a reply, send it to the parent actors
  44. const actorsException = [ byActor ]
  45. await broadcastToActors(activity, byActor, threadParentComments.map(c => c.Account.Actor), actorsException)
  46. // Broadcast to our followers
  47. await broadcastToFollowers(activity, byActor, [ byActor ], t)
  48. // Send to actors involved in the comment
  49. if (isVideoOrigin) return broadcastToFollowers(activity, byActor, actorsInvolvedInComment, t, actorsException)
  50. // Send to origin
  51. return unicastTo(activity, byActor, videoComment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
  52. }
  53. async function sendDeleteVideoPlaylist (videoPlaylist: VideoPlaylistModel, t: Transaction) {
  54. logger.info('Creating job to send delete of playlist %s.', videoPlaylist.url)
  55. const byActor = videoPlaylist.OwnerAccount.Actor
  56. const url = getDeleteActivityPubUrl(videoPlaylist.url)
  57. const activity = buildDeleteActivity(url, videoPlaylist.url, byActor)
  58. const serverActor = await getServerActor()
  59. const toFollowersOf = [ byActor, serverActor ]
  60. if (videoPlaylist.VideoChannel) toFollowersOf.push(videoPlaylist.VideoChannel.Actor)
  61. return broadcastToFollowers(activity, byActor, toFollowersOf, t)
  62. }
  63. // ---------------------------------------------------------------------------
  64. export {
  65. sendDeleteVideo,
  66. sendDeleteActor,
  67. sendDeleteVideoComment,
  68. sendDeleteVideoPlaylist
  69. }
  70. // ---------------------------------------------------------------------------
  71. function buildDeleteActivity (url: string, object: string, byActor: ActorModel, audience?: ActivityAudience): ActivityDelete {
  72. const activity = {
  73. type: 'Delete' as 'Delete',
  74. id: url,
  75. actor: byActor.url,
  76. object
  77. }
  78. if (audience) return audiencify(activity, audience)
  79. return activity
  80. }