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 { ActorModel } from '../../../models/activitypub/actor'
6 import { VideoModel } from '../../../models/video/video'
7 import { VideoCommentModel } from '../../../models/video/video-comment'
8 import { forwardVideoRelatedActivity } from '../send/utils'
9 import { VideoPlaylistModel } from '../../../models/video/video-playlist'
10 import { APProcessorOptions } from '../../../typings/activitypub-processor.model'
11 import { MAccountActor, MActor, MActorSignature, MChannelActor, MChannelActorAccountActor } from '../../../typings/models'
13 async function processDeleteActivity (options: APProcessorOptions<ActivityDelete>) {
14 const { activity, byActor } = options
16 const objectUrl = typeof activity.object === 'string' ? activity.object : activity.object.id
18 if (activity.actor === objectUrl) {
19 // We need more attributes (all the account and channel)
20 const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
22 if (byActorFull.type === 'Person') {
23 if (!byActorFull.Account) throw new Error('Actor ' + byActorFull.url + ' is a person but we cannot find it in database.')
25 const accountToDelete = byActorFull.Account as MAccountActor
26 accountToDelete.Actor = byActorFull
28 return retryTransactionWrapper(processDeleteAccount, accountToDelete)
29 } else if (byActorFull.type === 'Group') {
30 if (!byActorFull.VideoChannel) throw new Error('Actor ' + byActorFull.url + ' is a group but we cannot find it in database.')
32 const channelToDelete = byActorFull.VideoChannel as MChannelActorAccountActor
33 channelToDelete.Actor = byActorFull
35 return retryTransactionWrapper(processDeleteVideoChannel, channelToDelete)
40 const videoCommentInstance = await VideoCommentModel.loadByUrlAndPopulateAccountAndVideo(objectUrl)
41 if (videoCommentInstance) {
42 return retryTransactionWrapper(processDeleteVideoComment, byActor, videoCommentInstance, activity)
47 const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(objectUrl)
49 if (videoInstance.isOwned()) throw new Error(`Remote instance cannot delete owned video ${videoInstance.url}.`)
51 return retryTransactionWrapper(processDeleteVideo, byActor, videoInstance)
56 const videoPlaylist = await VideoPlaylistModel.loadByUrlAndPopulateAccount(objectUrl)
58 if (videoPlaylist.isOwned()) throw new Error(`Remote instance cannot delete owned playlist ${videoPlaylist.url}.`)
60 return retryTransactionWrapper(processDeleteVideoPlaylist, byActor, videoPlaylist)
67 // ---------------------------------------------------------------------------
73 // ---------------------------------------------------------------------------
75 async function processDeleteVideo (actor: MActor, videoToDelete: VideoModel) {
76 logger.debug('Removing remote video "%s".', videoToDelete.uuid)
78 await sequelizeTypescript.transaction(async t => {
79 if (videoToDelete.VideoChannel.Account.Actor.id !== actor.id) {
80 throw new Error('Account ' + actor.url + ' does not own video channel ' + videoToDelete.VideoChannel.Actor.url)
83 await videoToDelete.destroy({ transaction: t })
86 logger.info('Remote video with uuid %s removed.', videoToDelete.uuid)
89 async function processDeleteVideoPlaylist (actor: MActor, playlistToDelete: VideoPlaylistModel) {
90 logger.debug('Removing remote video playlist "%s".', playlistToDelete.uuid)
92 await sequelizeTypescript.transaction(async t => {
93 if (playlistToDelete.OwnerAccount.Actor.id !== actor.id) {
94 throw new Error('Account ' + actor.url + ' does not own video playlist ' + playlistToDelete.url)
97 await playlistToDelete.destroy({ transaction: t })
100 logger.info('Remote video playlist with uuid %s removed.', playlistToDelete.uuid)
103 async function processDeleteAccount (accountToRemove: MAccountActor) {
104 logger.debug('Removing remote account "%s".', accountToRemove.Actor.url)
106 await sequelizeTypescript.transaction(async t => {
107 await accountToRemove.destroy({ transaction: t })
110 logger.info('Remote account %s removed.', accountToRemove.Actor.url)
113 async function processDeleteVideoChannel (videoChannelToRemove: MChannelActor) {
114 logger.debug('Removing remote video channel "%s".', videoChannelToRemove.Actor.url)
116 await sequelizeTypescript.transaction(async t => {
117 await videoChannelToRemove.destroy({ transaction: t })
120 logger.info('Remote video channel %s removed.', videoChannelToRemove.Actor.url)
123 function processDeleteVideoComment (byActor: MActorSignature, videoComment: VideoCommentModel, activity: ActivityDelete) {
124 logger.debug('Removing remote video comment "%s".', videoComment.url)
126 return sequelizeTypescript.transaction(async t => {
127 if (byActor.Account.id !== videoComment.Account.id && byActor.Account.id !== videoComment.Video.VideoChannel.accountId) {
128 throw new Error(`Account ${byActor.url} does not own video comment ${videoComment.url} or video ${videoComment.Video.url}`)
131 await videoComment.destroy({ transaction: t })
133 if (videoComment.Video.isOwned()) {
134 // Don't resend the activity to the sender
135 const exceptions = [ byActor ]
136 await forwardVideoRelatedActivity(activity, t, exceptions, videoComment.Video)
139 logger.info('Remote video comment %s removed.', videoComment.url)