1
0
Fork 0
peertube/server/lib/activitypub/process/process-delete.ts

131 lines
4.5 KiB
TypeScript
Raw Normal View History

2017-12-12 16:53:50 +00:00
import { ActivityDelete } from '../../../../shared/models/activitypub'
2017-12-28 10:16:08 +00:00
import { retryTransactionWrapper } from '../../../helpers/database-utils'
import { logger } from '../../../helpers/logger'
2017-12-12 16:53:50 +00:00
import { sequelizeTypescript } from '../../../initializers'
import { AccountModel } from '../../../models/account/account'
2017-12-14 16:38:41 +00:00
import { ActorModel } from '../../../models/activitypub/actor'
2017-12-12 16:53:50 +00:00
import { VideoModel } from '../../../models/video/video'
import { VideoChannelModel } from '../../../models/video/video-channel'
2018-01-04 10:19:16 +00:00
import { VideoCommentModel } from '../../../models/video/video-comment'
2017-12-14 16:38:41 +00:00
import { getOrCreateActorAndServerAndModel } from '../actor'
2017-11-13 16:39:41 +00:00
async function processDeleteActivity (activity: ActivityDelete) {
2017-12-14 16:38:41 +00:00
const actor = await getOrCreateActorAndServerAndModel(activity.actor)
2017-11-13 16:39:41 +00:00
2018-01-04 15:56:36 +00:00
if (actor.url === activity.object) {
2017-12-14 16:38:41 +00:00
if (actor.type === 'Person') {
if (!actor.Account) throw new Error('Actor ' + actor.url + ' is a person but we cannot find it in database.')
2017-11-13 16:39:41 +00:00
2017-12-14 16:38:41 +00:00
return processDeleteAccount(actor.Account)
} else if (actor.type === 'Group') {
if (!actor.VideoChannel) throw new Error('Actor ' + actor.url + ' is a group but we cannot find it in database.')
return processDeleteVideoChannel(actor.VideoChannel)
2017-11-13 16:39:41 +00:00
}
}
{
2018-01-04 15:56:36 +00:00
const videoCommentInstance = await VideoCommentModel.loadByUrlAndPopulateAccount(activity.object)
2018-01-04 10:19:16 +00:00
if (videoCommentInstance) {
return processDeleteVideoComment(actor, videoCommentInstance)
}
}
{
2018-01-04 15:56:36 +00:00
const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(activity.object)
2018-01-04 10:19:16 +00:00
if (videoInstance) {
return processDeleteVideo(actor, videoInstance)
2017-11-13 16:39:41 +00:00
}
}
2017-11-17 14:52:26 +00:00
return
2017-11-13 16:39:41 +00:00
}
// ---------------------------------------------------------------------------
export {
processDeleteActivity
}
// ---------------------------------------------------------------------------
2017-12-14 16:38:41 +00:00
async function processDeleteVideo (actor: ActorModel, videoToDelete: VideoModel) {
2017-11-13 16:39:41 +00:00
const options = {
2017-12-14 16:38:41 +00:00
arguments: [ actor, videoToDelete ],
2017-11-13 16:39:41 +00:00
errorMessage: 'Cannot remove the remote video with many retries.'
}
await retryTransactionWrapper(deleteRemoteVideo, options)
}
2017-12-14 16:38:41 +00:00
async function deleteRemoteVideo (actor: ActorModel, videoToDelete: VideoModel) {
2017-11-13 16:39:41 +00:00
logger.debug('Removing remote video "%s".', videoToDelete.uuid)
2017-12-12 16:53:50 +00:00
await sequelizeTypescript.transaction(async t => {
2017-12-14 16:38:41 +00: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 16:39:41 +00:00
}
await videoToDelete.destroy({ transaction: t })
})
logger.info('Remote video with uuid %s removed.', videoToDelete.uuid)
}
2017-12-14 16:38:41 +00:00
async function processDeleteAccount (accountToRemove: AccountModel) {
2017-11-13 16:39:41 +00:00
const options = {
2017-12-14 16:38:41 +00:00
arguments: [ accountToRemove ],
errorMessage: 'Cannot remove the remote account with many retries.'
2017-11-13 16:39:41 +00:00
}
2017-12-14 16:38:41 +00:00
await retryTransactionWrapper(deleteRemoteAccount, options)
2017-11-13 16:39:41 +00:00
}
2017-12-14 16:38:41 +00:00
async function deleteRemoteAccount (accountToRemove: AccountModel) {
logger.debug('Removing remote account "%s".', accountToRemove.Actor.uuid)
2017-11-13 16:39:41 +00:00
2017-12-12 16:53:50 +00:00
await sequelizeTypescript.transaction(async t => {
2017-12-14 16:38:41 +00:00
await accountToRemove.destroy({ transaction: t })
2017-11-13 16:39:41 +00:00
})
2017-12-14 16:38:41 +00:00
logger.info('Remote account with uuid %s removed.', accountToRemove.Actor.uuid)
2017-11-13 16:39:41 +00:00
}
2017-12-14 16:38:41 +00:00
async function processDeleteVideoChannel (videoChannelToRemove: VideoChannelModel) {
2017-11-13 16:39:41 +00:00
const options = {
2017-12-14 16:38:41 +00:00
arguments: [ videoChannelToRemove ],
errorMessage: 'Cannot remove the remote video channel with many retries.'
2017-11-13 16:39:41 +00:00
}
2017-12-14 16:38:41 +00:00
await retryTransactionWrapper(deleteRemoteVideoChannel, options)
2017-11-13 16:39:41 +00:00
}
2017-12-14 16:38:41 +00:00
async function deleteRemoteVideoChannel (videoChannelToRemove: VideoChannelModel) {
logger.debug('Removing remote video channel "%s".', videoChannelToRemove.Actor.uuid)
2017-11-13 16:39:41 +00:00
2017-12-12 16:53:50 +00:00
await sequelizeTypescript.transaction(async t => {
2017-12-14 16:38:41 +00:00
await videoChannelToRemove.destroy({ transaction: t })
2017-11-13 16:39:41 +00:00
})
2017-12-14 16:38:41 +00:00
logger.info('Remote video channel with uuid %s removed.', videoChannelToRemove.Actor.uuid)
2017-11-13 16:39:41 +00:00
}
2018-01-04 10:19:16 +00:00
async function processDeleteVideoComment (actor: ActorModel, videoComment: VideoCommentModel) {
const options = {
arguments: [ actor, videoComment ],
errorMessage: 'Cannot remove the remote video comment with many retries.'
}
await retryTransactionWrapper(deleteRemoteVideoComment, options)
}
function deleteRemoteVideoComment (actor: ActorModel, videoComment: VideoCommentModel) {
logger.debug('Removing remote video comment "%s".', videoComment.url)
return sequelizeTypescript.transaction(async t => {
await videoComment.destroy({ transaction: t })
logger.info('Remote video comment %s removed.', videoComment.url)
})
}