2
1

send-undo.ts 4.8 KB

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