X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Factivitypub%2Fprocess%2Fprocess-delete.ts;h=845a7b249a3384b9aa9988ebe2a60224f923a5b4;hb=1198edf4bb06ce5f1668b97cf9ca8fb483fe3f41;hp=b58f6c04a28a686f1fddb4ab365d16750e48cc6f;hpb=73c0809326670867642f8a36f68d8564e0e406b5;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/activitypub/process/process-delete.ts b/server/lib/activitypub/process/process-delete.ts index b58f6c04a..845a7b249 100644 --- a/server/lib/activitypub/process/process-delete.ts +++ b/server/lib/activitypub/process/process-delete.ts @@ -7,45 +7,58 @@ import { ActorModel } from '../../../models/activitypub/actor' import { VideoModel } from '../../../models/video/video' import { VideoChannelModel } from '../../../models/video/video-channel' import { VideoCommentModel } from '../../../models/video/video-comment' -import { getOrCreateActorAndServerAndModel } from '../actor' -import { forwardActivity } from '../send/misc' +import { forwardVideoRelatedActivity } from '../send/utils' +import { VideoPlaylistModel } from '../../../models/video/video-playlist' +import { APProcessorOptions } from '../../../typings/activitypub-processor.model' + +async function processDeleteActivity (options: APProcessorOptions) { + const { activity, byActor } = options -async function processDeleteActivity (activity: ActivityDelete) { const objectUrl = typeof activity.object === 'string' ? activity.object : activity.object.id if (activity.actor === objectUrl) { - let actor = await ActorModel.loadByUrl(activity.actor) - if (!actor) return + // We need more attributes (all the account and channel) + const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url) - if (actor.type === 'Person') { - if (!actor.Account) throw new Error('Actor ' + actor.url + ' is a person but we cannot find it in database.') + if (byActorFull.type === 'Person') { + if (!byActorFull.Account) throw new Error('Actor ' + byActorFull.url + ' is a person but we cannot find it in database.') - actor.Account.Actor = await actor.Account.$get('Actor') as ActorModel - 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.') + 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.') - actor.VideoChannel.Actor = await actor.VideoChannel.$get('Actor') as ActorModel - return processDeleteVideoChannel(actor.VideoChannel) + byActorFull.VideoChannel.Actor = await byActorFull.VideoChannel.$get('Actor') as ActorModel + return retryTransactionWrapper(processDeleteVideoChannel, byActorFull.VideoChannel) } } - const actor = await getOrCreateActorAndServerAndModel(activity.actor) { const videoCommentInstance = await VideoCommentModel.loadByUrlAndPopulateAccount(objectUrl) if (videoCommentInstance) { - return processDeleteVideoComment(actor, videoCommentInstance, activity) + return retryTransactionWrapper(processDeleteVideoComment, byActor, videoCommentInstance, activity) } } { const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(objectUrl) if (videoInstance) { - return processDeleteVideo(actor, videoInstance) + if (videoInstance.isOwned()) throw new Error(`Remote instance cannot delete owned video ${videoInstance.url}.`) + + return retryTransactionWrapper(processDeleteVideo, byActor, videoInstance) + } + } + + { + 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) } } - return + return undefined } // --------------------------------------------------------------------------- @@ -57,15 +70,6 @@ export { // --------------------------------------------------------------------------- async function processDeleteVideo (actor: ActorModel, videoToDelete: VideoModel) { - const options = { - arguments: [ actor, videoToDelete ], - errorMessage: 'Cannot remove the remote video with many retries.' - } - - await retryTransactionWrapper(deleteRemoteVideo, options) -} - -async function deleteRemoteVideo (actor: ActorModel, videoToDelete: VideoModel) { logger.debug('Removing remote video "%s".', videoToDelete.uuid) await sequelizeTypescript.transaction(async t => { @@ -79,63 +83,54 @@ async function deleteRemoteVideo (actor: ActorModel, videoToDelete: VideoModel) logger.info('Remote video with uuid %s removed.', videoToDelete.uuid) } -async function processDeleteAccount (accountToRemove: AccountModel) { - const options = { - arguments: [ accountToRemove ], - errorMessage: 'Cannot remove the remote account with many retries.' - } +async function processDeleteVideoPlaylist (actor: ActorModel, playlistToDelete: VideoPlaylistModel) { + logger.debug('Removing remote video playlist "%s".', playlistToDelete.uuid) - await retryTransactionWrapper(deleteRemoteAccount, options) + 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) } -async function deleteRemoteAccount (accountToRemove: AccountModel) { - logger.debug('Removing remote account "%s".', accountToRemove.Actor.uuid) +async function processDeleteAccount (accountToRemove: AccountModel) { + logger.debug('Removing remote account "%s".', accountToRemove.Actor.url) await sequelizeTypescript.transaction(async t => { await accountToRemove.destroy({ transaction: t }) }) - logger.info('Remote account with uuid %s removed.', accountToRemove.Actor.uuid) + logger.info('Remote account %s removed.', accountToRemove.Actor.url) } async function processDeleteVideoChannel (videoChannelToRemove: VideoChannelModel) { - const options = { - arguments: [ videoChannelToRemove ], - errorMessage: 'Cannot remove the remote video channel with many retries.' - } - - await retryTransactionWrapper(deleteRemoteVideoChannel, options) -} - -async function deleteRemoteVideoChannel (videoChannelToRemove: VideoChannelModel) { - logger.debug('Removing remote video channel "%s".', videoChannelToRemove.Actor.uuid) + logger.debug('Removing remote video channel "%s".', videoChannelToRemove.Actor.url) await sequelizeTypescript.transaction(async t => { await videoChannelToRemove.destroy({ transaction: t }) }) - logger.info('Remote video channel with uuid %s removed.', videoChannelToRemove.Actor.uuid) + logger.info('Remote video channel %s removed.', videoChannelToRemove.Actor.url) } -async function processDeleteVideoComment (byActor: ActorModel, videoComment: VideoCommentModel, activity: ActivityDelete) { - const options = { - arguments: [ byActor, videoComment, activity ], - errorMessage: 'Cannot remove the remote video comment with many retries.' - } - - await retryTransactionWrapper(deleteRemoteVideoComment, options) -} - -function deleteRemoteVideoComment (byActor: ActorModel, videoComment: VideoCommentModel, activity: ActivityDelete) { +function processDeleteVideoComment (byActor: ActorModel, videoComment: VideoCommentModel, activity: ActivityDelete) { logger.debug('Removing remote video comment "%s".', videoComment.url) return sequelizeTypescript.transaction(async t => { + if (videoComment.Account.id !== byActor.Account.id) { + throw new Error('Account ' + byActor.url + ' does not own video comment ' + videoComment.url) + } + await videoComment.destroy({ transaction: t }) if (videoComment.Video.isOwned()) { // Don't resend the activity to the sender const exceptions = [ byActor ] - await forwardActivity(activity, t, exceptions) + await forwardVideoRelatedActivity(activity, t, exceptions, videoComment.Video) } logger.info('Remote video comment %s removed.', videoComment.url)