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/database'
5 import { ActorModel } from '../../../models/actor/actor'
6 import { VideoModel } from '../../../models/video/video'
7 import { VideoCommentModel } from '../../../models/video/video-comment'
8 import { VideoPlaylistModel } from '../../../models/video/video-playlist'
9 import { APProcessorOptions } from '../../../types/activitypub-processor.model'
18 } from '../../../types/models'
19 import { forwardVideoRelatedActivity } from '../send/shared/send-utils'
21 async function processDeleteActivity (options: APProcessorOptions<ActivityDelete>) {
22 const { activity, byActor } = options
24 const objectUrl = typeof activity.object === 'string' ? activity.object : activity.object.id
26 if (activity.actor === objectUrl) {
27 // We need more attributes (all the account and channel)
28 const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
30 if (byActorFull.type === 'Person') {
31 if (!byActorFull.Account) throw new Error('Actor ' + byActorFull.url + ' is a person but we cannot find it in database.')
33 const accountToDelete = byActorFull.Account as MAccountActor
34 accountToDelete.Actor = byActorFull
36 return retryTransactionWrapper(processDeleteAccount, accountToDelete)
37 } else if (byActorFull.type === 'Group') {
38 if (!byActorFull.VideoChannel) throw new Error('Actor ' + byActorFull.url + ' is a group but we cannot find it in database.')
40 const channelToDelete = byActorFull.VideoChannel as MChannelAccountActor & { Actor: MActorFull }
41 channelToDelete.Actor = byActorFull
42 return retryTransactionWrapper(processDeleteVideoChannel, channelToDelete)
47 const videoCommentInstance = await VideoCommentModel.loadByUrlAndPopulateAccountAndVideo(objectUrl)
48 if (videoCommentInstance) {
49 return retryTransactionWrapper(processDeleteVideoComment, byActor, videoCommentInstance, activity)
54 const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(objectUrl)
56 if (videoInstance.isOwned()) throw new Error(`Remote instance cannot delete owned video ${videoInstance.url}.`)
58 return retryTransactionWrapper(processDeleteVideo, byActor, videoInstance)
63 const videoPlaylist = await VideoPlaylistModel.loadByUrlAndPopulateAccount(objectUrl)
65 if (videoPlaylist.isOwned()) throw new Error(`Remote instance cannot delete owned playlist ${videoPlaylist.url}.`)
67 return retryTransactionWrapper(processDeleteVideoPlaylist, byActor, videoPlaylist)
74 // ---------------------------------------------------------------------------
80 // ---------------------------------------------------------------------------
82 async function processDeleteVideo (actor: MActor, videoToDelete: VideoModel) {
83 logger.debug('Removing remote video "%s".', videoToDelete.uuid)
85 await sequelizeTypescript.transaction(async t => {
86 if (videoToDelete.VideoChannel.Account.Actor.id !== actor.id) {
87 throw new Error('Account ' + actor.url + ' does not own video channel ' + videoToDelete.VideoChannel.Actor.url)
90 await videoToDelete.destroy({ transaction: t })
93 logger.info('Remote video with uuid %s removed.', videoToDelete.uuid)
96 async function processDeleteVideoPlaylist (actor: MActor, playlistToDelete: VideoPlaylistModel) {
97 logger.debug('Removing remote video playlist "%s".', playlistToDelete.uuid)
99 await sequelizeTypescript.transaction(async t => {
100 if (playlistToDelete.OwnerAccount.Actor.id !== actor.id) {
101 throw new Error('Account ' + actor.url + ' does not own video playlist ' + playlistToDelete.url)
104 await playlistToDelete.destroy({ transaction: t })
107 logger.info('Remote video playlist with uuid %s removed.', playlistToDelete.uuid)
110 async function processDeleteAccount (accountToRemove: MAccountActor) {
111 logger.debug('Removing remote account "%s".', accountToRemove.Actor.url)
113 await sequelizeTypescript.transaction(async t => {
114 await accountToRemove.destroy({ transaction: t })
117 logger.info('Remote account %s removed.', accountToRemove.Actor.url)
120 async function processDeleteVideoChannel (videoChannelToRemove: MChannelActor) {
121 logger.debug('Removing remote video channel "%s".', videoChannelToRemove.Actor.url)
123 await sequelizeTypescript.transaction(async t => {
124 await videoChannelToRemove.destroy({ transaction: t })
127 logger.info('Remote video channel %s removed.', videoChannelToRemove.Actor.url)
130 function processDeleteVideoComment (byActor: MActorSignature, videoComment: MCommentOwnerVideo, activity: ActivityDelete) {
132 if (videoComment.isDeleted()) return Promise.resolve()
134 logger.debug('Removing remote video comment "%s".', videoComment.url)
136 return sequelizeTypescript.transaction(async t => {
137 if (byActor.Account.id !== videoComment.Account.id && byActor.Account.id !== videoComment.Video.VideoChannel.accountId) {
138 throw new Error(`Account ${byActor.url} does not own video comment ${videoComment.url} or video ${videoComment.Video.url}`)
141 videoComment.markAsDeleted()
143 await videoComment.save({ transaction: t })
145 if (videoComment.Video.isOwned()) {
146 // Don't resend the activity to the sender
147 const exceptions = [ byActor ]
148 await forwardVideoRelatedActivity(activity, t, exceptions, videoComment.Video)
151 logger.info('Remote video comment %s removed.', videoComment.url)