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'
11 import { VideoPlaylistModel } from '../../../models/video/video-playlist'
12 import { APProcessorOptions } from '../../../typings/activitypub-processor.model'
13 import { SignatureActorModel } from '../../../typings/models'
15 async function processDeleteActivity (options: APProcessorOptions<ActivityDelete>) {
16 const { activity, byActor } = options
18 const objectUrl = typeof activity.object === 'string' ? activity.object : activity.object.id
20 if (activity.actor === objectUrl) {
21 // We need more attributes (all the account and channel)
22 const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
24 if (byActorFull.type === 'Person') {
25 if (!byActorFull.Account) throw new Error('Actor ' + byActorFull.url + ' is a person but we cannot find it in database.')
27 byActorFull.Account.Actor = await byActorFull.Account.$get('Actor') as ActorModel
28 return retryTransactionWrapper(processDeleteAccount, byActorFull.Account)
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 byActorFull.VideoChannel.Actor = await byActorFull.VideoChannel.$get('Actor') as ActorModel
33 return retryTransactionWrapper(processDeleteVideoChannel, byActorFull.VideoChannel)
38 const videoCommentInstance = await VideoCommentModel.loadByUrlAndPopulateAccountAndVideo(objectUrl)
39 if (videoCommentInstance) {
40 return retryTransactionWrapper(processDeleteVideoComment, byActor, videoCommentInstance, activity)
45 const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(objectUrl)
47 if (videoInstance.isOwned()) throw new Error(`Remote instance cannot delete owned video ${videoInstance.url}.`)
49 return retryTransactionWrapper(processDeleteVideo, byActor, videoInstance)
54 const videoPlaylist = await VideoPlaylistModel.loadByUrlAndPopulateAccount(objectUrl)
56 if (videoPlaylist.isOwned()) throw new Error(`Remote instance cannot delete owned playlist ${videoPlaylist.url}.`)
58 return retryTransactionWrapper(processDeleteVideoPlaylist, byActor, videoPlaylist)
65 // ---------------------------------------------------------------------------
71 // ---------------------------------------------------------------------------
73 async function processDeleteVideo (actor: ActorModel, videoToDelete: VideoModel) {
74 logger.debug('Removing remote video "%s".', videoToDelete.uuid)
76 await sequelizeTypescript.transaction(async t => {
77 if (videoToDelete.VideoChannel.Account.Actor.id !== actor.id) {
78 throw new Error('Account ' + actor.url + ' does not own video channel ' + videoToDelete.VideoChannel.Actor.url)
81 await videoToDelete.destroy({ transaction: t })
84 logger.info('Remote video with uuid %s removed.', videoToDelete.uuid)
87 async function processDeleteVideoPlaylist (actor: ActorModel, playlistToDelete: VideoPlaylistModel) {
88 logger.debug('Removing remote video playlist "%s".', playlistToDelete.uuid)
90 await sequelizeTypescript.transaction(async t => {
91 if (playlistToDelete.OwnerAccount.Actor.id !== actor.id) {
92 throw new Error('Account ' + actor.url + ' does not own video playlist ' + playlistToDelete.url)
95 await playlistToDelete.destroy({ transaction: t })
98 logger.info('Remote video playlist with uuid %s removed.', playlistToDelete.uuid)
101 async function processDeleteAccount (accountToRemove: AccountModel) {
102 logger.debug('Removing remote account "%s".', accountToRemove.Actor.url)
104 await sequelizeTypescript.transaction(async t => {
105 await accountToRemove.destroy({ transaction: t })
108 logger.info('Remote account %s removed.', accountToRemove.Actor.url)
111 async function processDeleteVideoChannel (videoChannelToRemove: VideoChannelModel) {
112 logger.debug('Removing remote video channel "%s".', videoChannelToRemove.Actor.url)
114 await sequelizeTypescript.transaction(async t => {
115 await videoChannelToRemove.destroy({ transaction: t })
118 logger.info('Remote video channel %s removed.', videoChannelToRemove.Actor.url)
121 function processDeleteVideoComment (byActor: SignatureActorModel, videoComment: VideoCommentModel, activity: ActivityDelete) {
122 logger.debug('Removing remote video comment "%s".', videoComment.url)
124 return sequelizeTypescript.transaction(async t => {
125 if (byActor.Account.id !== videoComment.Account.id && byActor.Account.id !== videoComment.Video.VideoChannel.accountId) {
126 throw new Error(`Account ${byActor.url} does not own video comment ${videoComment.url} or video ${videoComment.Video.url}`)
129 await videoComment.destroy({ transaction: t })
131 if (videoComment.Video.isOwned()) {
132 // Don't resend the activity to the sender
133 const exceptions = [ byActor ]
134 await forwardVideoRelatedActivity(activity, t, exceptions, videoComment.Video)
137 logger.info('Remote video comment %s removed.', videoComment.url)