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'
12 async function processDeleteActivity (activity: ActivityDelete) {
13 const actor = await getOrCreateActorAndServerAndModel(activity.actor)
15 if (actor.url === activity.object) {
16 if (actor.type === 'Person') {
17 if (!actor.Account) throw new Error('Actor ' + actor.url + ' is a person but we cannot find it in database.')
19 return processDeleteAccount(actor.Account)
20 } else if (actor.type === 'Group') {
21 if (!actor.VideoChannel) throw new Error('Actor ' + actor.url + ' is a group but we cannot find it in database.')
23 return processDeleteVideoChannel(actor.VideoChannel)
28 const videoCommentInstance = await VideoCommentModel.loadByUrlAndPopulateAccount(activity.object)
29 if (videoCommentInstance) {
30 return processDeleteVideoComment(actor, videoCommentInstance)
35 const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(activity.object)
37 return processDeleteVideo(actor, videoInstance)
44 // ---------------------------------------------------------------------------
50 // ---------------------------------------------------------------------------
52 async function processDeleteVideo (actor: ActorModel, videoToDelete: VideoModel) {
54 arguments: [ actor, videoToDelete ],
55 errorMessage: 'Cannot remove the remote video with many retries.'
58 await retryTransactionWrapper(deleteRemoteVideo, options)
61 async function deleteRemoteVideo (actor: ActorModel, videoToDelete: VideoModel) {
62 logger.debug('Removing remote video "%s".', videoToDelete.uuid)
64 await sequelizeTypescript.transaction(async t => {
65 if (videoToDelete.VideoChannel.Account.Actor.id !== actor.id) {
66 throw new Error('Account ' + actor.url + ' does not own video channel ' + videoToDelete.VideoChannel.Actor.url)
69 await videoToDelete.destroy({ transaction: t })
72 logger.info('Remote video with uuid %s removed.', videoToDelete.uuid)
75 async function processDeleteAccount (accountToRemove: AccountModel) {
77 arguments: [ accountToRemove ],
78 errorMessage: 'Cannot remove the remote account with many retries.'
81 await retryTransactionWrapper(deleteRemoteAccount, options)
84 async function deleteRemoteAccount (accountToRemove: AccountModel) {
85 logger.debug('Removing remote account "%s".', accountToRemove.Actor.uuid)
87 await sequelizeTypescript.transaction(async t => {
88 await accountToRemove.destroy({ transaction: t })
91 logger.info('Remote account with uuid %s removed.', accountToRemove.Actor.uuid)
94 async function processDeleteVideoChannel (videoChannelToRemove: VideoChannelModel) {
96 arguments: [ videoChannelToRemove ],
97 errorMessage: 'Cannot remove the remote video channel with many retries.'
100 await retryTransactionWrapper(deleteRemoteVideoChannel, options)
103 async function deleteRemoteVideoChannel (videoChannelToRemove: VideoChannelModel) {
104 logger.debug('Removing remote video channel "%s".', videoChannelToRemove.Actor.uuid)
106 await sequelizeTypescript.transaction(async t => {
107 await videoChannelToRemove.destroy({ transaction: t })
110 logger.info('Remote video channel with uuid %s removed.', videoChannelToRemove.Actor.uuid)
113 async function processDeleteVideoComment (actor: ActorModel, videoComment: VideoCommentModel) {
115 arguments: [ actor, videoComment ],
116 errorMessage: 'Cannot remove the remote video comment with many retries.'
119 await retryTransactionWrapper(deleteRemoteVideoComment, options)
122 function deleteRemoteVideoComment (actor: ActorModel, videoComment: VideoCommentModel) {
123 logger.debug('Removing remote video comment "%s".', videoComment.url)
125 return sequelizeTypescript.transaction(async t => {
126 await videoComment.destroy({ transaction: t })
128 logger.info('Remote video comment %s removed.', videoComment.url)