process-delete.ts 5.8 KB

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