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

108 lines
4.2 KiB
TypeScript
Raw Normal View History

2017-12-12 11:53:50 -05:00
import { ActivityDelete } from '../../../../shared/models/activitypub'
2017-12-28 05:16:08 -05:00
import { retryTransactionWrapper } from '../../../helpers/database-utils'
import { logger } from '../../../helpers/logger'
2017-12-12 11:53:50 -05:00
import { sequelizeTypescript } from '../../../initializers'
import { AccountModel } from '../../../models/account/account'
2017-12-14 11:38:41 -05:00
import { ActorModel } from '../../../models/activitypub/actor'
2017-12-12 11:53:50 -05:00
import { VideoModel } from '../../../models/video/video'
import { VideoChannelModel } from '../../../models/video/video-channel'
2018-01-04 05:19:16 -05:00
import { VideoCommentModel } from '../../../models/video/video-comment'
2018-05-25 05:32:36 -04:00
import { forwardActivity } from '../send/utils'
2017-11-13 11:39:41 -05:00
async function processDeleteActivity (activity: ActivityDelete, byActor: ActorModel) {
2018-01-04 11:50:30 -05:00
const objectUrl = typeof activity.object === 'string' ? activity.object : activity.object.id
2017-11-13 11:39:41 -05: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 11:39:41 -05:00
byActorFull.Account.Actor = await byActorFull.Account.$get('Actor') as ActorModel
return retryTransactionWrapper(processDeleteAccount, byActorFull.Account)
} 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 11:38:41 -05:00
byActorFull.VideoChannel.Actor = await byActorFull.VideoChannel.$get('Actor') as ActorModel
return retryTransactionWrapper(processDeleteVideoChannel, byActorFull.VideoChannel)
2017-11-13 11:39:41 -05:00
}
}
{
2018-01-04 11:50:30 -05:00
const videoCommentInstance = await VideoCommentModel.loadByUrlAndPopulateAccount(objectUrl)
2018-01-04 05:19:16 -05:00
if (videoCommentInstance) {
return retryTransactionWrapper(processDeleteVideoComment, byActor, videoCommentInstance, activity)
2018-01-04 05:19:16 -05:00
}
}
{
2018-01-04 11:50:30 -05:00
const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(objectUrl)
2018-01-04 05:19:16 -05:00
if (videoInstance) {
2018-09-14 10:51:35 -04:00
if (videoInstance.isOwned()) throw new Error(`Remote instance cannot delete owned video ${videoInstance.url}.`)
return retryTransactionWrapper(processDeleteVideo, byActor, videoInstance)
2017-11-13 11:39:41 -05:00
}
}
2018-06-13 08:55:18 -04:00
return undefined
2017-11-13 11:39:41 -05:00
}
// ---------------------------------------------------------------------------
export {
processDeleteActivity
}
// ---------------------------------------------------------------------------
2017-12-14 11:38:41 -05:00
async function processDeleteVideo (actor: ActorModel, videoToDelete: VideoModel) {
2017-11-13 11:39:41 -05:00
logger.debug('Removing remote video "%s".', videoToDelete.uuid)
2017-12-12 11:53:50 -05:00
await sequelizeTypescript.transaction(async t => {
2017-12-14 11:38:41 -05: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 11:39:41 -05:00
}
await videoToDelete.destroy({ transaction: t })
})
logger.info('Remote video with uuid %s removed.', videoToDelete.uuid)
}
2017-12-14 11:38:41 -05:00
async function processDeleteAccount (accountToRemove: AccountModel) {
logger.debug('Removing remote account "%s".', accountToRemove.Actor.uuid)
2017-11-13 11:39:41 -05:00
2017-12-12 11:53:50 -05:00
await sequelizeTypescript.transaction(async t => {
2017-12-14 11:38:41 -05:00
await accountToRemove.destroy({ transaction: t })
2017-11-13 11:39:41 -05:00
})
2017-12-14 11:38:41 -05:00
logger.info('Remote account with uuid %s removed.', accountToRemove.Actor.uuid)
2017-11-13 11:39:41 -05:00
}
2017-12-14 11:38:41 -05:00
async function processDeleteVideoChannel (videoChannelToRemove: VideoChannelModel) {
logger.debug('Removing remote video channel "%s".', videoChannelToRemove.Actor.uuid)
2017-11-13 11:39:41 -05:00
2017-12-12 11:53:50 -05:00
await sequelizeTypescript.transaction(async t => {
2017-12-14 11:38:41 -05:00
await videoChannelToRemove.destroy({ transaction: t })
2017-11-13 11:39:41 -05:00
})
2017-12-14 11:38:41 -05:00
logger.info('Remote video channel with uuid %s removed.', videoChannelToRemove.Actor.uuid)
2017-11-13 11:39:41 -05:00
}
2018-01-04 05:19:16 -05:00
2018-06-13 08:27:40 -04:00
function processDeleteVideoComment (byActor: ActorModel, videoComment: VideoCommentModel, activity: ActivityDelete) {
2018-01-04 05:19:16 -05:00
logger.debug('Removing remote video comment "%s".', videoComment.url)
return sequelizeTypescript.transaction(async t => {
await videoComment.destroy({ transaction: t })
2018-03-27 04:26:52 -04:00
if (videoComment.Video.isOwned()) {
// Don't resend the activity to the sender
const exceptions = [ byActor ]
await forwardActivity(activity, t, exceptions)
}
2018-01-04 05:19:16 -05:00
logger.info('Remote video comment %s removed.', videoComment.url)
})
}