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'
12 async function processDeleteActivity (activity: ActivityDelete, byActor: ActorModel) {
13 const objectUrl = typeof activity.object === 'string' ? activity.object : activity.object.id
15 if (activity.actor === objectUrl) {
16 // We need more attributes (all the account and channel)
17 const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
19 if (byActorFull.type === 'Person') {
20 if (!byActorFull.Account) throw new Error('Actor ' + byActorFull.url + ' is a person but we cannot find it in database.')
22 byActorFull.Account.Actor = await byActorFull.Account.$get('Actor') as ActorModel
23 return retryTransactionWrapper(processDeleteAccount, byActorFull.Account)
24 } else if (byActorFull.type === 'Group') {
25 if (!byActorFull.VideoChannel) throw new Error('Actor ' + byActorFull.url + ' is a group but we cannot find it in database.')
27 byActorFull.VideoChannel.Actor = await byActorFull.VideoChannel.$get('Actor') as ActorModel
28 return retryTransactionWrapper(processDeleteVideoChannel, byActorFull.VideoChannel)
33 const videoCommentInstance = await VideoCommentModel.loadByUrlAndPopulateAccount(objectUrl)
34 if (videoCommentInstance) {
35 return retryTransactionWrapper(processDeleteVideoComment, byActor, videoCommentInstance, activity)
40 const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(objectUrl)
42 if (videoInstance.isOwned()) throw new Error(`Remote instance cannot delete owned video ${videoInstance.url}.`)
44 return retryTransactionWrapper(processDeleteVideo, byActor, videoInstance)
51 // ---------------------------------------------------------------------------
57 // ---------------------------------------------------------------------------
59 async function processDeleteVideo (actor: ActorModel, videoToDelete: VideoModel) {
60 logger.debug('Removing remote video "%s".', videoToDelete.uuid)
62 await sequelizeTypescript.transaction(async t => {
63 if (videoToDelete.VideoChannel.Account.Actor.id !== actor.id) {
64 throw new Error('Account ' + actor.url + ' does not own video channel ' + videoToDelete.VideoChannel.Actor.url)
67 await videoToDelete.destroy({ transaction: t })
70 logger.info('Remote video with uuid %s removed.', videoToDelete.uuid)
73 async function processDeleteAccount (accountToRemove: AccountModel) {
74 logger.debug('Removing remote account "%s".', accountToRemove.Actor.uuid)
76 await sequelizeTypescript.transaction(async t => {
77 await accountToRemove.destroy({ transaction: t })
80 logger.info('Remote account with uuid %s removed.', accountToRemove.Actor.uuid)
83 async function processDeleteVideoChannel (videoChannelToRemove: VideoChannelModel) {
84 logger.debug('Removing remote video channel "%s".', videoChannelToRemove.Actor.uuid)
86 await sequelizeTypescript.transaction(async t => {
87 await videoChannelToRemove.destroy({ transaction: t })
90 logger.info('Remote video channel with uuid %s removed.', videoChannelToRemove.Actor.uuid)
93 function processDeleteVideoComment (byActor: ActorModel, videoComment: VideoCommentModel, activity: ActivityDelete) {
94 logger.debug('Removing remote video comment "%s".', videoComment.url)
96 return sequelizeTypescript.transaction(async t => {
97 if (videoComment.Account.id !== byActor.Account.id) {
98 throw new Error('Account ' + byActor.url + ' does not own video comment ' + videoComment.url)
101 await videoComment.destroy({ transaction: t })
103 if (videoComment.Video.isOwned()) {
104 // Don't resend the activity to the sender
105 const exceptions = [ byActor ]
106 await forwardVideoRelatedActivity(activity, t, exceptions, videoComment.Video)
109 logger.info('Remote video comment %s removed.', videoComment.url)