2
1

send-undo.ts 4.9 KB

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