send-undo.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { Transaction } from 'sequelize'
  2. import {
  3. ActivityAnnounce,
  4. ActivityAudience,
  5. ActivityCreate,
  6. ActivityFollow,
  7. ActivityLike,
  8. ActivityUndo
  9. } from '../../../../shared/models/activitypub'
  10. import { ActorModel } from '../../../models/activitypub/actor'
  11. import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
  12. import { VideoModel } from '../../../models/video/video'
  13. import { getActorFollowActivityPubUrl, getUndoActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoLikeActivityPubUrl } from '../url'
  14. import { broadcastToFollowers, sendVideoRelatedActivity, unicastTo } from './utils'
  15. import { audiencify, getAudience } from '../audience'
  16. import { buildCreateActivity, buildDislikeActivity } from './send-create'
  17. import { buildFollowActivity } from './send-follow'
  18. import { buildLikeActivity } from './send-like'
  19. import { VideoShareModel } from '../../../models/video/video-share'
  20. import { buildAnnounceWithVideoAudience } from './send-announce'
  21. import { logger } from '../../../helpers/logger'
  22. import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
  23. async function sendUndoFollow (actorFollow: ActorFollowModel, t: Transaction) {
  24. const me = actorFollow.ActorFollower
  25. const following = actorFollow.ActorFollowing
  26. // Same server as ours
  27. if (!following.serverId) return
  28. logger.info('Creating job to send an unfollow request to %s.', following.url)
  29. const followUrl = getActorFollowActivityPubUrl(actorFollow)
  30. const undoUrl = getUndoActivityPubUrl(followUrl)
  31. const followActivity = buildFollowActivity(followUrl, me, following)
  32. const undoActivity = undoActivityData(undoUrl, me, followActivity)
  33. return unicastTo(undoActivity, me, following.inboxUrl)
  34. }
  35. async function sendUndoAnnounce (byActor: ActorModel, videoShare: VideoShareModel, video: VideoModel, t: Transaction) {
  36. logger.info('Creating job to undo announce %s.', videoShare.url)
  37. const undoUrl = getUndoActivityPubUrl(videoShare.url)
  38. const { activity: announceActivity, actorsInvolvedInVideo } = await buildAnnounceWithVideoAudience(byActor, videoShare, video, t)
  39. const undoActivity = undoActivityData(undoUrl, byActor, announceActivity)
  40. const followersException = [ byActor ]
  41. return broadcastToFollowers(undoActivity, byActor, actorsInvolvedInVideo, t, followersException)
  42. }
  43. async function sendUndoLike (byActor: ActorModel, video: VideoModel, t: Transaction) {
  44. logger.info('Creating job to undo a like of video %s.', video.url)
  45. const likeUrl = getVideoLikeActivityPubUrl(byActor, video)
  46. const likeActivity = buildLikeActivity(likeUrl, byActor, video)
  47. return sendUndoVideoRelatedActivity({ byActor, video, url: likeUrl, activity: likeActivity, transaction: t })
  48. }
  49. async function sendUndoDislike (byActor: ActorModel, video: VideoModel, t: Transaction) {
  50. logger.info('Creating job to undo a dislike of video %s.', video.url)
  51. const dislikeUrl = getVideoDislikeActivityPubUrl(byActor, video)
  52. const dislikeActivity = buildDislikeActivity(dislikeUrl, byActor, video)
  53. const createDislikeActivity = buildCreateActivity(dislikeUrl, byActor, dislikeActivity)
  54. return sendUndoVideoRelatedActivity({ byActor, video, url: dislikeUrl, activity: createDislikeActivity, transaction: t })
  55. }
  56. async function sendUndoCacheFile (byActor: ActorModel, redundancyModel: VideoRedundancyModel, t: Transaction) {
  57. logger.info('Creating job to undo cache file %s.', redundancyModel.url)
  58. const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(redundancyModel.VideoFile.Video.id)
  59. const createActivity = buildCreateActivity(redundancyModel.url, byActor, redundancyModel.toActivityPubObject())
  60. return sendUndoVideoRelatedActivity({ byActor, video, url: redundancyModel.url, activity: createActivity, transaction: t })
  61. }
  62. // ---------------------------------------------------------------------------
  63. export {
  64. sendUndoFollow,
  65. sendUndoLike,
  66. sendUndoDislike,
  67. sendUndoAnnounce,
  68. sendUndoCacheFile
  69. }
  70. // ---------------------------------------------------------------------------
  71. function undoActivityData (
  72. url: string,
  73. byActor: ActorModel,
  74. object: ActivityFollow | ActivityLike | ActivityCreate | ActivityAnnounce,
  75. audience?: ActivityAudience
  76. ): ActivityUndo {
  77. if (!audience) audience = getAudience(byActor)
  78. return audiencify(
  79. {
  80. type: 'Undo' as 'Undo',
  81. id: url,
  82. actor: byActor.url,
  83. object
  84. },
  85. audience
  86. )
  87. }
  88. async function sendUndoVideoRelatedActivity (options: {
  89. byActor: ActorModel,
  90. video: VideoModel,
  91. url: string,
  92. activity: ActivityFollow | ActivityLike | ActivityCreate | ActivityAnnounce,
  93. transaction: Transaction
  94. }) {
  95. const activityBuilder = (audience: ActivityAudience) => {
  96. const undoUrl = getUndoActivityPubUrl(options.url)
  97. return undoActivityData(undoUrl, options.byActor, options.activity, audience)
  98. }
  99. return sendVideoRelatedActivity(activityBuilder, options)
  100. }