process-undo.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { ActivityAnnounce, ActivityFollow, ActivityLike, ActivityUndo, CacheFileObject } from '../../../../shared/models/activitypub'
  2. import { DislikeObject } from '../../../../shared/models/activitypub/objects'
  3. import { retryTransactionWrapper } from '../../../helpers/database-utils'
  4. import { logger } from '../../../helpers/logger'
  5. import { sequelizeTypescript } from '../../../initializers'
  6. import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
  7. import { ActorModel } from '../../../models/activitypub/actor'
  8. import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
  9. import { forwardVideoRelatedActivity } from '../send/utils'
  10. import { getOrCreateVideoAndAccountAndChannel } from '../videos'
  11. import { VideoShareModel } from '../../../models/video/video-share'
  12. import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
  13. async function processUndoActivity (activity: ActivityUndo, byActor: ActorModel) {
  14. const activityToUndo = activity.object
  15. if (activityToUndo.type === 'Like') {
  16. return retryTransactionWrapper(processUndoLike, byActor, activity)
  17. }
  18. if (activityToUndo.type === 'Create') {
  19. if (activityToUndo.object.type === 'Dislike') {
  20. return retryTransactionWrapper(processUndoDislike, byActor, activity)
  21. } else if (activityToUndo.object.type === 'CacheFile') {
  22. return retryTransactionWrapper(processUndoCacheFile, byActor, activity)
  23. }
  24. }
  25. if (activityToUndo.type === 'Dislike') {
  26. return retryTransactionWrapper(processUndoDislike, byActor, activity)
  27. }
  28. if (activityToUndo.type === 'Follow') {
  29. return retryTransactionWrapper(processUndoFollow, byActor, activityToUndo)
  30. }
  31. if (activityToUndo.type === 'Announce') {
  32. return retryTransactionWrapper(processUndoAnnounce, byActor, activityToUndo)
  33. }
  34. logger.warn('Unknown activity object type %s -> %s when undo activity.', activityToUndo.type, { activity: activity.id })
  35. return undefined
  36. }
  37. // ---------------------------------------------------------------------------
  38. export {
  39. processUndoActivity
  40. }
  41. // ---------------------------------------------------------------------------
  42. async function processUndoLike (byActor: ActorModel, activity: ActivityUndo) {
  43. const likeActivity = activity.object as ActivityLike
  44. const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: likeActivity.object })
  45. return sequelizeTypescript.transaction(async t => {
  46. if (!byActor.Account) throw new Error('Unknown account ' + byActor.url)
  47. let rate = await AccountVideoRateModel.loadByUrl(likeActivity.id, t)
  48. if (!rate) rate = await AccountVideoRateModel.load(byActor.Account.id, video.id, t)
  49. if (!rate) throw new Error(`Unknown rate by account ${byActor.Account.id} for video ${video.id}.`)
  50. await rate.destroy({ transaction: t })
  51. await video.decrement('likes', { transaction: t })
  52. if (video.isOwned()) {
  53. // Don't resend the activity to the sender
  54. const exceptions = [ byActor ]
  55. await forwardVideoRelatedActivity(activity, t, exceptions, video)
  56. }
  57. })
  58. }
  59. async function processUndoDislike (byActor: ActorModel, activity: ActivityUndo) {
  60. const dislike = activity.object.type === 'Dislike'
  61. ? activity.object
  62. : activity.object.object as DislikeObject
  63. const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: dislike.object })
  64. return sequelizeTypescript.transaction(async t => {
  65. if (!byActor.Account) throw new Error('Unknown account ' + byActor.url)
  66. let rate = await AccountVideoRateModel.loadByUrl(dislike.id, t)
  67. if (!rate) rate = await AccountVideoRateModel.load(byActor.Account.id, video.id, t)
  68. if (!rate) throw new Error(`Unknown rate by account ${byActor.Account.id} for video ${video.id}.`)
  69. await rate.destroy({ transaction: t })
  70. await video.decrement('dislikes', { transaction: t })
  71. if (video.isOwned()) {
  72. // Don't resend the activity to the sender
  73. const exceptions = [ byActor ]
  74. await forwardVideoRelatedActivity(activity, t, exceptions, video)
  75. }
  76. })
  77. }
  78. async function processUndoCacheFile (byActor: ActorModel, activity: ActivityUndo) {
  79. const cacheFileObject = activity.object.object as CacheFileObject
  80. const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.object })
  81. return sequelizeTypescript.transaction(async t => {
  82. const cacheFile = await VideoRedundancyModel.loadByUrl(cacheFileObject.id)
  83. if (!cacheFile) {
  84. logger.debug('Cannot undo unknown video cache %s.', cacheFileObject.id)
  85. return
  86. }
  87. if (cacheFile.actorId !== byActor.id) throw new Error('Cannot delete redundancy ' + cacheFile.url + ' of another actor.')
  88. await cacheFile.destroy()
  89. if (video.isOwned()) {
  90. // Don't resend the activity to the sender
  91. const exceptions = [ byActor ]
  92. await forwardVideoRelatedActivity(activity, t, exceptions, video)
  93. }
  94. })
  95. }
  96. function processUndoFollow (follower: ActorModel, followActivity: ActivityFollow) {
  97. return sequelizeTypescript.transaction(async t => {
  98. const following = await ActorModel.loadByUrlAndPopulateAccountAndChannel(followActivity.object, t)
  99. const actorFollow = await ActorFollowModel.loadByActorAndTarget(follower.id, following.id, t)
  100. if (!actorFollow) throw new Error(`'Unknown actor follow ${follower.id} -> ${following.id}.`)
  101. await actorFollow.destroy({ transaction: t })
  102. return undefined
  103. })
  104. }
  105. function processUndoAnnounce (byActor: ActorModel, announceActivity: ActivityAnnounce) {
  106. return sequelizeTypescript.transaction(async t => {
  107. const share = await VideoShareModel.loadByUrl(announceActivity.id, t)
  108. if (!share) throw new Error(`Unknown video share ${announceActivity.id}.`)
  109. if (share.actorId !== byActor.id) throw new Error(`${share.url} is not shared by ${byActor.url}.`)
  110. await share.destroy({ transaction: t })
  111. if (share.Video.isOwned()) {
  112. // Don't resend the activity to the sender
  113. const exceptions = [ byActor ]
  114. await forwardVideoRelatedActivity(announceActivity, t, exceptions, share.Video)
  115. }
  116. })
  117. }