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 { getOrCreateActorAndServerAndModel } from '../actor'
11 import { forwardActivity } from '../send/misc'
13 async function processDeleteActivity (activity: ActivityDelete) {
14 const objectUrl = typeof activity.object === 'string' ? activity.object : activity.object.id
16 if (activity.actor === objectUrl) {
17 let actor = await ActorModel.loadByUrl(activity.actor)
20 if (actor.type === 'Person') {
21 if (!actor.Account) throw new Error('Actor ' + actor.url + ' is a person but we cannot find it in database.')
23 actor.Account.Actor = await actor.Account.$get('Actor') as ActorModel
24 return processDeleteAccount(actor.Account)
25 } else if (actor.type === 'Group') {
26 if (!actor.VideoChannel) throw new Error('Actor ' + actor.url + ' is a group but we cannot find it in database.')
28 actor.VideoChannel.Actor = await actor.VideoChannel.$get('Actor') as ActorModel
29 return processDeleteVideoChannel(actor.VideoChannel)
33 const actor = await getOrCreateActorAndServerAndModel(activity.actor)
35 const videoCommentInstance = await VideoCommentModel.loadByUrlAndPopulateAccount(objectUrl)
36 if (videoCommentInstance) {
37 return processDeleteVideoComment(actor, videoCommentInstance, activity)
42 const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(objectUrl)
44 return processDeleteVideo(actor, videoInstance)
51 // ---------------------------------------------------------------------------
57 // ---------------------------------------------------------------------------
59 async function processDeleteVideo (actor: ActorModel, videoToDelete: VideoModel) {
61 arguments: [ actor, videoToDelete ],
62 errorMessage: 'Cannot remove the remote video with many retries.'
65 await retryTransactionWrapper(deleteRemoteVideo, options)
68 async function deleteRemoteVideo (actor: ActorModel, videoToDelete: VideoModel) {
69 logger.debug('Removing remote video "%s".', videoToDelete.uuid)
71 await sequelizeTypescript.transaction(async t => {
72 if (videoToDelete.VideoChannel.Account.Actor.id !== actor.id) {
73 throw new Error('Account ' + actor.url + ' does not own video channel ' + videoToDelete.VideoChannel.Actor.url)
76 await videoToDelete.destroy({ transaction: t })
79 logger.info('Remote video with uuid %s removed.', videoToDelete.uuid)
82 async function processDeleteAccount (accountToRemove: AccountModel) {
84 arguments: [ accountToRemove ],
85 errorMessage: 'Cannot remove the remote account with many retries.'
88 await retryTransactionWrapper(deleteRemoteAccount, options)
91 async function deleteRemoteAccount (accountToRemove: AccountModel) {
92 logger.debug('Removing remote account "%s".', accountToRemove.Actor.uuid)
94 await sequelizeTypescript.transaction(async t => {
95 await accountToRemove.destroy({ transaction: t })
98 logger.info('Remote account with uuid %s removed.', accountToRemove.Actor.uuid)
101 async function processDeleteVideoChannel (videoChannelToRemove: VideoChannelModel) {
103 arguments: [ videoChannelToRemove ],
104 errorMessage: 'Cannot remove the remote video channel with many retries.'
107 await retryTransactionWrapper(deleteRemoteVideoChannel, options)
110 async function deleteRemoteVideoChannel (videoChannelToRemove: VideoChannelModel) {
111 logger.debug('Removing remote video channel "%s".', videoChannelToRemove.Actor.uuid)
113 await sequelizeTypescript.transaction(async t => {
114 await videoChannelToRemove.destroy({ transaction: t })
117 logger.info('Remote video channel with uuid %s removed.', videoChannelToRemove.Actor.uuid)
120 async function processDeleteVideoComment (byActor: ActorModel, videoComment: VideoCommentModel, activity: ActivityDelete) {
122 arguments: [ byActor, videoComment, activity ],
123 errorMessage: 'Cannot remove the remote video comment with many retries.'
126 await retryTransactionWrapper(deleteRemoteVideoComment, options)
129 function deleteRemoteVideoComment (byActor: ActorModel, videoComment: VideoCommentModel, activity: ActivityDelete) {
130 logger.debug('Removing remote video comment "%s".', videoComment.url)
132 return sequelizeTypescript.transaction(async t => {
133 await videoComment.destroy({ transaction: t })
135 if (videoComment.Video.isOwned()) {
136 // Don't resend the activity to the sender
137 const exceptions = [ byActor ]
138 await forwardActivity(activity, t, exceptions)
141 logger.info('Remote video comment %s removed.', videoComment.url)