X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Factivitypub%2Fprocess%2Fprocess-delete.ts;h=ac0e7e235aaeb4e116188c06c662448bbd1fe008;hb=2b621ac0ebe83693bba6354b3482a03ba58143e7;hp=41cdc236d1baacdd25e3cbbe9659957b4d288257;hpb=0f91ae62df8a37194fea84ce1efa9e733d9c1fd8;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/activitypub/process/process-delete.ts b/server/lib/activitypub/process/process-delete.ts index 41cdc236d..ac0e7e235 100644 --- a/server/lib/activitypub/process/process-delete.ts +++ b/server/lib/activitypub/process/process-delete.ts @@ -1,34 +1,74 @@ -import { ActivityDelete } from '../../../../shared/models/activitypub/activity' +import { ActivityDelete } from '../../../../shared/models/activitypub' import { retryTransactionWrapper } from '../../../helpers/database-utils' import { logger } from '../../../helpers/logger' -import { database as db } from '../../../initializers' -import { AccountInstance } from '../../../models/account/account-interface' -import { VideoChannelInstance } from '../../../models/video/video-channel-interface' -import { VideoInstance } from '../../../models/video/video-interface' -import { getOrCreateAccountAndServer } from '../account' - -async function processDeleteActivity (activity: ActivityDelete) { - const account = await getOrCreateAccountAndServer(activity.actor) +import { sequelizeTypescript } from '../../../initializers/database' +import { ActorModel } from '../../../models/actor/actor' +import { VideoModel } from '../../../models/video/video' +import { VideoCommentModel } from '../../../models/video/video-comment' +import { VideoPlaylistModel } from '../../../models/video/video-playlist' +import { APProcessorOptions } from '../../../types/activitypub-processor.model' +import { + MAccountActor, + MActor, + MActorFull, + MActorSignature, + MChannelAccountActor, + MChannelActor, + MCommentOwnerVideo +} from '../../../types/models' +import { forwardVideoRelatedActivity } from '../send/shared/send-utils' + +async function processDeleteActivity (options: APProcessorOptions) { + const { activity, byActor } = options + + const objectUrl = typeof activity.object === 'string' ? activity.object : activity.object.id + + 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.') + + 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.') + + const channelToDelete = byActorFull.VideoChannel as MChannelAccountActor & { Actor: MActorFull } + channelToDelete.Actor = byActorFull + return retryTransactionWrapper(processDeleteVideoChannel, channelToDelete) + } + } - if (account.url === activity.id) { - return processDeleteAccount(account) + { + const videoCommentInstance = await VideoCommentModel.loadByUrlAndPopulateAccountAndVideo(objectUrl) + if (videoCommentInstance) { + return retryTransactionWrapper(processDeleteVideoComment, byActor, videoCommentInstance, activity) + } } { - let videoObject = await db.Video.loadByUrlAndPopulateAccount(activity.id) - if (videoObject !== undefined) { - return processDeleteVideo(account, videoObject) + const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(objectUrl) + if (videoInstance) { + if (videoInstance.isOwned()) throw new Error(`Remote instance cannot delete owned video ${videoInstance.url}.`) + + return retryTransactionWrapper(processDeleteVideo, byActor, videoInstance) } } { - let videoChannelObject = await db.VideoChannel.loadByUrl(activity.id) - if (videoChannelObject !== undefined) { - return processDeleteVideoChannel(account, videoChannelObject) + 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 } // --------------------------------------------------------------------------- @@ -39,21 +79,12 @@ export { // --------------------------------------------------------------------------- -async function processDeleteVideo (account: AccountInstance, videoToDelete: VideoInstance) { - const options = { - arguments: [ account, videoToDelete ], - errorMessage: 'Cannot remove the remote video with many retries.' - } - - await retryTransactionWrapper(deleteRemoteVideo, options) -} - -async function deleteRemoteVideo (account: AccountInstance, videoToDelete: VideoInstance) { +async function processDeleteVideo (actor: MActor, videoToDelete: VideoModel) { logger.debug('Removing remote video "%s".', videoToDelete.uuid) - await db.sequelize.transaction(async t => { - if (videoToDelete.VideoChannel.Account.id !== account.id) { - throw new Error('Account ' + account.url + ' does not own video channel ' + videoToDelete.VideoChannel.url) + await sequelizeTypescript.transaction(async t => { + if (videoToDelete.VideoChannel.Account.Actor.id !== actor.id) { + throw new Error('Account ' + actor.url + ' does not own video channel ' + videoToDelete.VideoChannel.Actor.url) } await videoToDelete.destroy({ transaction: t }) @@ -62,44 +93,61 @@ async function deleteRemoteVideo (account: AccountInstance, videoToDelete: Video logger.info('Remote video with uuid %s removed.', videoToDelete.uuid) } -async function processDeleteVideoChannel (account: AccountInstance, videoChannelToRemove: VideoChannelInstance) { - const options = { - arguments: [ account, videoChannelToRemove ], - errorMessage: 'Cannot remove the remote video channel with many retries.' - } +async function processDeleteVideoPlaylist (actor: MActor, playlistToDelete: VideoPlaylistModel) { + 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 }) + }) - await retryTransactionWrapper(deleteRemoteVideoChannel, options) + logger.info('Remote video playlist with uuid %s removed.', playlistToDelete.uuid) } -async function deleteRemoteVideoChannel (account: AccountInstance, videoChannelToRemove: VideoChannelInstance) { - logger.debug('Removing remote video channel "%s".', videoChannelToRemove.uuid) +async function processDeleteAccount (accountToRemove: MAccountActor) { + logger.debug('Removing remote account "%s".', accountToRemove.Actor.url) - await db.sequelize.transaction(async t => { - if (videoChannelToRemove.Account.id !== account.id) { - throw new Error('Account ' + account.url + ' does not own video channel ' + videoChannelToRemove.url) - } + await sequelizeTypescript.transaction(async t => { + await accountToRemove.destroy({ transaction: t }) + }) + + logger.info('Remote account %s removed.', accountToRemove.Actor.url) +} + +async function processDeleteVideoChannel (videoChannelToRemove: MChannelActor) { + 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.uuid) + logger.info('Remote video channel %s removed.', videoChannelToRemove.Actor.url) } -async function processDeleteAccount (accountToRemove: AccountInstance) { - const options = { - arguments: [ accountToRemove ], - errorMessage: 'Cannot remove the remote account with many retries.' - } +function processDeleteVideoComment (byActor: MActorSignature, videoComment: MCommentOwnerVideo, activity: ActivityDelete) { + // Already deleted + if (videoComment.isDeleted()) return Promise.resolve() - await retryTransactionWrapper(deleteRemoteAccount, options) -} + logger.debug('Removing remote video comment "%s".', videoComment.url) -async function deleteRemoteAccount (accountToRemove: AccountInstance) { - logger.debug('Removing remote account "%s".', accountToRemove.uuid) + 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}`) + } - await db.sequelize.transaction(async t => { - await accountToRemove.destroy({ transaction: t }) - }) + videoComment.markAsDeleted() + + await videoComment.save({ transaction: t }) - logger.info('Remote account with uuid %s removed.', accountToRemove.uuid) + if (videoComment.Video.isOwned()) { + // Don't resend the activity to the sender + const exceptions = [ byActor ] + await forwardVideoRelatedActivity(activity, t, exceptions, videoComment.Video) + } + + logger.info('Remote video comment %s removed.', videoComment.url) + }) }