process-delete.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { ActivityDelete } from '../../../../shared/models/activitypub'
  2. import { retryTransactionWrapper } from '../../../helpers/database-utils'
  3. import { logger } from '../../../helpers/logger'
  4. import { sequelizeTypescript } from '../../../initializers'
  5. import { AccountModel } from '../../../models/account/account'
  6. import { ActorModel } from '../../../models/activitypub/actor'
  7. import { VideoModel } from '../../../models/video/video'
  8. import { VideoChannelModel } from '../../../models/video/video-channel'
  9. import { VideoCommentModel } from '../../../models/video/video-comment'
  10. import { forwardVideoRelatedActivity } from '../send/utils'
  11. import { VideoPlaylistModel } from '../../../models/video/video-playlist'
  12. async function processDeleteActivity (activity: ActivityDelete, byActor: ActorModel) {
  13. const objectUrl = typeof activity.object === 'string' ? activity.object : activity.object.id
  14. if (activity.actor === objectUrl) {
  15. // We need more attributes (all the account and channel)
  16. const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
  17. if (byActorFull.type === 'Person') {
  18. if (!byActorFull.Account) throw new Error('Actor ' + byActorFull.url + ' is a person but we cannot find it in database.')
  19. byActorFull.Account.Actor = await byActorFull.Account.$get('Actor') as ActorModel
  20. return retryTransactionWrapper(processDeleteAccount, byActorFull.Account)
  21. } else if (byActorFull.type === 'Group') {
  22. if (!byActorFull.VideoChannel) throw new Error('Actor ' + byActorFull.url + ' is a group but we cannot find it in database.')
  23. byActorFull.VideoChannel.Actor = await byActorFull.VideoChannel.$get('Actor') as ActorModel
  24. return retryTransactionWrapper(processDeleteVideoChannel, byActorFull.VideoChannel)
  25. }
  26. }
  27. {
  28. const videoCommentInstance = await VideoCommentModel.loadByUrlAndPopulateAccount(objectUrl)
  29. if (videoCommentInstance) {
  30. return retryTransactionWrapper(processDeleteVideoComment, byActor, videoCommentInstance, activity)
  31. }
  32. }
  33. {
  34. const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(objectUrl)
  35. if (videoInstance) {
  36. if (videoInstance.isOwned()) throw new Error(`Remote instance cannot delete owned video ${videoInstance.url}.`)
  37. return retryTransactionWrapper(processDeleteVideo, byActor, videoInstance)
  38. }
  39. }
  40. {
  41. const videoPlaylist = await VideoPlaylistModel.loadByUrlAndPopulateAccount(objectUrl)
  42. if (videoPlaylist) {
  43. if (videoPlaylist.isOwned()) throw new Error(`Remote instance cannot delete owned playlist ${videoPlaylist.url}.`)
  44. return retryTransactionWrapper(processDeleteVideoPlaylist, byActor, videoPlaylist)
  45. }
  46. }
  47. return undefined
  48. }
  49. // ---------------------------------------------------------------------------
  50. export {
  51. processDeleteActivity
  52. }
  53. // ---------------------------------------------------------------------------
  54. async function processDeleteVideo (actor: ActorModel, videoToDelete: VideoModel) {
  55. logger.debug('Removing remote video "%s".', videoToDelete.uuid)
  56. await sequelizeTypescript.transaction(async t => {
  57. if (videoToDelete.VideoChannel.Account.Actor.id !== actor.id) {
  58. throw new Error('Account ' + actor.url + ' does not own video channel ' + videoToDelete.VideoChannel.Actor.url)
  59. }
  60. await videoToDelete.destroy({ transaction: t })
  61. })
  62. logger.info('Remote video with uuid %s removed.', videoToDelete.uuid)
  63. }
  64. async function processDeleteVideoPlaylist (actor: ActorModel, playlistToDelete: VideoPlaylistModel) {
  65. logger.debug('Removing remote video playlist "%s".', playlistToDelete.uuid)
  66. await sequelizeTypescript.transaction(async t => {
  67. if (playlistToDelete.OwnerAccount.Actor.id !== actor.id) {
  68. throw new Error('Account ' + actor.url + ' does not own video playlist ' + playlistToDelete.url)
  69. }
  70. await playlistToDelete.destroy({ transaction: t })
  71. })
  72. logger.info('Remote video playlist with uuid %s removed.', playlistToDelete.uuid)
  73. }
  74. async function processDeleteAccount (accountToRemove: AccountModel) {
  75. logger.debug('Removing remote account "%s".', accountToRemove.Actor.url)
  76. await sequelizeTypescript.transaction(async t => {
  77. await accountToRemove.destroy({ transaction: t })
  78. })
  79. logger.info('Remote account %s removed.', accountToRemove.Actor.url)
  80. }
  81. async function processDeleteVideoChannel (videoChannelToRemove: VideoChannelModel) {
  82. logger.debug('Removing remote video channel "%s".', videoChannelToRemove.Actor.url)
  83. await sequelizeTypescript.transaction(async t => {
  84. await videoChannelToRemove.destroy({ transaction: t })
  85. })
  86. logger.info('Remote video channel %s removed.', videoChannelToRemove.Actor.url)
  87. }
  88. function processDeleteVideoComment (byActor: ActorModel, videoComment: VideoCommentModel, activity: ActivityDelete) {
  89. logger.debug('Removing remote video comment "%s".', videoComment.url)
  90. return sequelizeTypescript.transaction(async t => {
  91. if (videoComment.Account.id !== byActor.Account.id) {
  92. throw new Error('Account ' + byActor.url + ' does not own video comment ' + videoComment.url)
  93. }
  94. await videoComment.destroy({ transaction: t })
  95. if (videoComment.Video.isOwned()) {
  96. // Don't resend the activity to the sender
  97. const exceptions = [ byActor ]
  98. await forwardVideoRelatedActivity(activity, t, exceptions, videoComment.Video)
  99. }
  100. logger.info('Remote video comment %s removed.', videoComment.url)
  101. })
  102. }