PeerTube/server/lib/activitypub/process/process-delete.ts

154 lines
5.8 KiB
TypeScript
Raw Normal View History

2017-12-12 17:53:50 +01:00
import { ActivityDelete } from '../../../../shared/models/activitypub'
2017-12-28 11:16:08 +01:00
import { retryTransactionWrapper } from '../../../helpers/database-utils'
import { logger } from '../../../helpers/logger'
2020-05-07 14:58:24 +02:00
import { sequelizeTypescript } from '../../../initializers/database'
2021-05-11 11:15:29 +02:00
import { ActorModel } from '../../../models/actor/actor'
2017-12-12 17:53:50 +01:00
import { VideoModel } from '../../../models/video/video'
2018-01-04 11:19:16 +01:00
import { VideoCommentModel } from '../../../models/video/video-comment'
2019-03-05 10:58:44 +01:00
import { VideoPlaylistModel } from '../../../models/video/video-playlist'
2020-06-18 10:45:25 +02:00
import { APProcessorOptions } from '../../../types/activitypub-processor.model'
import {
MAccountActor,
MActor,
MActorFull,
MActorSignature,
MChannelAccountActor,
MChannelActor,
MCommentOwnerVideo
} from '../../../types/models'
2020-12-09 14:42:42 +01:00
import { forwardVideoRelatedActivity } from '../send/utils'
2019-08-02 10:53:36 +02:00
async function processDeleteActivity (options: APProcessorOptions<ActivityDelete>) {
const { activity, byActor } = options
2017-11-13 17:39:41 +01:00
2018-01-04 17:50:30 +01:00
const objectUrl = typeof activity.object === 'string' ? activity.object : activity.object.id
2017-11-13 17:39:41 +01:00
if (activity.actor === objectUrl) {
// We need more attributes (all the account and channel)
const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
if (byActorFull.type === 'Person') {
if (!byActorFull.Account) throw new Error('Actor ' + byActorFull.url + ' is a person but we cannot find it in database.')
2017-11-13 17:39:41 +01:00
2019-08-15 11:53:26 +02:00
const accountToDelete = byActorFull.Account as MAccountActor
accountToDelete.Actor = byActorFull
return retryTransactionWrapper(processDeleteAccount, accountToDelete)
} else if (byActorFull.type === 'Group') {
if (!byActorFull.VideoChannel) throw new Error('Actor ' + byActorFull.url + ' is a group but we cannot find it in database.')
2017-12-14 17:38:41 +01:00
const channelToDelete = byActorFull.VideoChannel as MChannelAccountActor & { Actor: MActorFull }
channelToDelete.Actor = byActorFull
2019-08-15 11:53:26 +02:00
return retryTransactionWrapper(processDeleteVideoChannel, channelToDelete)
2017-11-13 17:39:41 +01:00
}
}
{
const videoCommentInstance = await VideoCommentModel.loadByUrlAndPopulateAccountAndVideo(objectUrl)
2018-01-04 11:19:16 +01:00
if (videoCommentInstance) {
return retryTransactionWrapper(processDeleteVideoComment, byActor, videoCommentInstance, activity)
2018-01-04 11:19:16 +01:00
}
}
{
2018-01-04 17:50:30 +01:00
const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(objectUrl)
2018-01-04 11:19:16 +01:00
if (videoInstance) {
2018-09-14 16:51:35 +02:00
if (videoInstance.isOwned()) throw new Error(`Remote instance cannot delete owned video ${videoInstance.url}.`)
return retryTransactionWrapper(processDeleteVideo, byActor, videoInstance)
2017-11-13 17:39:41 +01:00
}
}
2019-03-05 10:58:44 +01:00
{
const videoPlaylist = await VideoPlaylistModel.loadByUrlAndPopulateAccount(objectUrl)
if (videoPlaylist) {
if (videoPlaylist.isOwned()) throw new Error(`Remote instance cannot delete owned playlist ${videoPlaylist.url}.`)
return retryTransactionWrapper(processDeleteVideoPlaylist, byActor, videoPlaylist)
}
}
2018-06-13 14:55:18 +02:00
return undefined
2017-11-13 17:39:41 +01:00
}
// ---------------------------------------------------------------------------
export {
processDeleteActivity
}
// ---------------------------------------------------------------------------
2019-08-15 11:53:26 +02:00
async function processDeleteVideo (actor: MActor, videoToDelete: VideoModel) {
2017-11-13 17:39:41 +01:00
logger.debug('Removing remote video "%s".', videoToDelete.uuid)
2017-12-12 17:53:50 +01:00
await sequelizeTypescript.transaction(async t => {
2017-12-14 17:38:41 +01:00
if (videoToDelete.VideoChannel.Account.Actor.id !== actor.id) {
throw new Error('Account ' + actor.url + ' does not own video channel ' + videoToDelete.VideoChannel.Actor.url)
2017-11-13 17:39:41 +01:00
}
await videoToDelete.destroy({ transaction: t })
})
logger.info('Remote video with uuid %s removed.', videoToDelete.uuid)
}
2019-08-15 11:53:26 +02:00
async function processDeleteVideoPlaylist (actor: MActor, playlistToDelete: VideoPlaylistModel) {
2019-03-05 10:58:44 +01:00
logger.debug('Removing remote video playlist "%s".', playlistToDelete.uuid)
await sequelizeTypescript.transaction(async t => {
if (playlistToDelete.OwnerAccount.Actor.id !== actor.id) {
throw new Error('Account ' + actor.url + ' does not own video playlist ' + playlistToDelete.url)
}
await playlistToDelete.destroy({ transaction: t })
})
logger.info('Remote video playlist with uuid %s removed.', playlistToDelete.uuid)
}
2019-08-15 11:53:26 +02:00
async function processDeleteAccount (accountToRemove: MAccountActor) {
2019-05-31 14:02:26 +02:00
logger.debug('Removing remote account "%s".', accountToRemove.Actor.url)
2017-11-13 17:39:41 +01:00
2017-12-12 17:53:50 +01:00
await sequelizeTypescript.transaction(async t => {
2017-12-14 17:38:41 +01:00
await accountToRemove.destroy({ transaction: t })
2017-11-13 17:39:41 +01:00
})
2019-05-31 14:02:26 +02:00
logger.info('Remote account %s removed.', accountToRemove.Actor.url)
2017-11-13 17:39:41 +01:00
}
2019-08-15 11:53:26 +02:00
async function processDeleteVideoChannel (videoChannelToRemove: MChannelActor) {
2019-05-31 14:02:26 +02:00
logger.debug('Removing remote video channel "%s".', videoChannelToRemove.Actor.url)
2017-11-13 17:39:41 +01:00
2017-12-12 17:53:50 +01:00
await sequelizeTypescript.transaction(async t => {
2017-12-14 17:38:41 +01:00
await videoChannelToRemove.destroy({ transaction: t })
2017-11-13 17:39:41 +01:00
})
2019-05-31 14:02:26 +02:00
logger.info('Remote video channel %s removed.', videoChannelToRemove.Actor.url)
2017-11-13 17:39:41 +01:00
}
2018-01-04 11:19:16 +01:00
2020-12-09 14:42:42 +01:00
function processDeleteVideoComment (byActor: MActorSignature, videoComment: MCommentOwnerVideo, activity: ActivityDelete) {
// Already deleted
2021-06-07 10:30:36 +02:00
if (videoComment.isDeleted()) return Promise.resolve()
2020-12-09 14:42:42 +01:00
2018-01-04 11:19:16 +01:00
logger.debug('Removing remote video comment "%s".', videoComment.url)
return sequelizeTypescript.transaction(async t => {
if (byActor.Account.id !== videoComment.Account.id && byActor.Account.id !== videoComment.Video.VideoChannel.accountId) {
throw new Error(`Account ${byActor.url} does not own video comment ${videoComment.url} or video ${videoComment.Video.url}`)
2018-09-19 15:47:55 +02:00
}
2021-06-15 09:17:19 +02:00
videoComment.markAsDeleted()
2021-06-15 09:17:19 +02:00
await videoComment.save({ transaction: t })
2018-01-04 11:19:16 +01:00
2018-03-27 10:26:52 +02:00
if (videoComment.Video.isOwned()) {
// Don't resend the activity to the sender
const exceptions = [ byActor ]
2018-10-02 11:33:18 +02:00
await forwardVideoRelatedActivity(activity, t, exceptions, videoComment.Video)
2018-03-27 10:26:52 +02:00
}
2018-01-04 11:19:16 +01:00
logger.info('Remote video comment %s removed.', videoComment.url)
})
}