X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Factivitypub%2Fprocess%2Fprocess-undo.ts;h=c091d96788e8de7084c481c8821f4b96f5e922fb;hb=e587e0ecee5bec43a225995948faaa4bc97f080a;hp=efa63122b5ef5e67d0af4b5d0a86b8ef1e1adbae;hpb=3fd3ab2d34d512b160a5e6084d7609be7b4f4452;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/activitypub/process/process-undo.ts b/server/lib/activitypub/process/process-undo.ts index efa63122b..c091d9678 100644 --- a/server/lib/activitypub/process/process-undo.ts +++ b/server/lib/activitypub/process/process-undo.ts @@ -1,22 +1,41 @@ -import { ActivityFollow, ActivityLike, ActivityUndo } from '../../../../shared/models/activitypub' +import { ActivityAnnounce, ActivityFollow, ActivityLike, ActivityUndo, CacheFileObject } from '../../../../shared/models/activitypub' import { DislikeObject } from '../../../../shared/models/activitypub/objects' -import { logger, retryTransactionWrapper } from '../../../helpers' +import { getActorUrl } from '../../../helpers/activitypub' +import { retryTransactionWrapper } from '../../../helpers/database-utils' +import { logger } from '../../../helpers/logger' import { sequelizeTypescript } from '../../../initializers' import { AccountModel } from '../../../models/account/account' -import { AccountFollowModel } from '../../../models/account/account-follow' import { AccountVideoRateModel } from '../../../models/account/account-video-rate' -import { VideoModel } from '../../../models/video/video' -import { forwardActivity } from '../send/misc' - -async function processUndoActivity (activity: ActivityUndo) { +import { ActorModel } from '../../../models/activitypub/actor' +import { ActorFollowModel } from '../../../models/activitypub/actor-follow' +import { forwardVideoRelatedActivity } from '../send/utils' +import { getOrCreateVideoAndAccountAndChannel } from '../videos' +import { VideoShareModel } from '../../../models/video/video-share' +import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy' + +async function processUndoActivity (activity: ActivityUndo, byActor: ActorModel) { const activityToUndo = activity.object + const actorUrl = getActorUrl(activity.actor) + if (activityToUndo.type === 'Like') { - return processUndoLike(activity.actor, activity) - } else if (activityToUndo.type === 'Create' && activityToUndo.object.type === 'Dislike') { - return processUndoDislike(activity.actor, activity) - } else if (activityToUndo.type === 'Follow') { - return processUndoFollow(activity.actor, activityToUndo) + return retryTransactionWrapper(processUndoLike, actorUrl, activity) + } + + if (activityToUndo.type === 'Create') { + if (activityToUndo.object.type === 'Dislike') { + return retryTransactionWrapper(processUndoDislike, actorUrl, activity) + } else if (activityToUndo.object.type === 'CacheFile') { + return retryTransactionWrapper(processUndoCacheFile, byActor, activity) + } + } + + if (activityToUndo.type === 'Follow') { + return retryTransactionWrapper(processUndoFollow, byActor, activityToUndo) + } + + if (activityToUndo.type === 'Announce') { + return retryTransactionWrapper(processUndoAnnounce, byActor, activityToUndo) } logger.warn('Unknown activity object type %s -> %s when undo activity.', activityToUndo.type, { activity: activity.id }) @@ -32,24 +51,14 @@ export { // --------------------------------------------------------------------------- -function processUndoLike (actor: string, activity: ActivityUndo) { - const options = { - arguments: [ actor, activity ], - errorMessage: 'Cannot undo like with many retries.' - } - - return retryTransactionWrapper(undoLike, options) -} - -function undoLike (actor: string, activity: ActivityUndo) { +async function processUndoLike (actorUrl: string, activity: ActivityUndo) { const likeActivity = activity.object as ActivityLike - return sequelizeTypescript.transaction(async t => { - const byAccount = await AccountModel.loadByUrl(actor, t) - if (!byAccount) throw new Error('Unknown account ' + actor) + const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: likeActivity.object }) - const video = await VideoModel.loadByUrlAndPopulateAccount(likeActivity.object, t) - if (!video) throw new Error('Unknown video ' + likeActivity.actor) + return sequelizeTypescript.transaction(async t => { + const byAccount = await AccountModel.loadByUrl(actorUrl, t) + if (!byAccount) throw new Error('Unknown account ' + actorUrl) const rate = await AccountVideoRateModel.load(byAccount.id, video.id, t) if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`) @@ -59,30 +68,21 @@ function undoLike (actor: string, activity: ActivityUndo) { if (video.isOwned()) { // Don't resend the activity to the sender - const exceptions = [ byAccount ] - await forwardActivity(activity, t, exceptions) + const exceptions = [ byAccount.Actor ] + + await forwardVideoRelatedActivity(activity, t, exceptions, video) } }) } -function processUndoDislike (actor: string, activity: ActivityUndo) { - const options = { - arguments: [ actor, activity ], - errorMessage: 'Cannot undo dislike with many retries.' - } - - return retryTransactionWrapper(undoDislike, options) -} - -function undoDislike (actor: string, activity: ActivityUndo) { +async function processUndoDislike (actorUrl: string, activity: ActivityUndo) { const dislike = activity.object.object as DislikeObject - return sequelizeTypescript.transaction(async t => { - const byAccount = await AccountModel.loadByUrl(actor, t) - if (!byAccount) throw new Error('Unknown account ' + actor) + const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: dislike.object }) - const video = await VideoModel.loadByUrlAndPopulateAccount(dislike.object, t) - if (!video) throw new Error('Unknown video ' + dislike.actor) + return sequelizeTypescript.transaction(async t => { + const byAccount = await AccountModel.loadByUrl(actorUrl, t) + if (!byAccount) throw new Error('Unknown account ' + actorUrl) const rate = await AccountVideoRateModel.load(byAccount.id, video.id, t) if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`) @@ -92,31 +92,60 @@ function undoDislike (actor: string, activity: ActivityUndo) { if (video.isOwned()) { // Don't resend the activity to the sender - const exceptions = [ byAccount ] - await forwardActivity(activity, t, exceptions) + const exceptions = [ byAccount.Actor ] + + await forwardVideoRelatedActivity(activity, t, exceptions, video) } }) } -function processUndoFollow (actor: string, followActivity: ActivityFollow) { - const options = { - arguments: [ actor, followActivity ], - errorMessage: 'Cannot undo follow with many retries.' - } +async function processUndoCacheFile (byActor: ActorModel, activity: ActivityUndo) { + const cacheFileObject = activity.object.object as CacheFileObject + + const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.object }) + + return sequelizeTypescript.transaction(async t => { + const cacheFile = await VideoRedundancyModel.loadByUrl(cacheFileObject.id) + if (!cacheFile) throw new Error('Unknown video cache ' + cacheFile.url) + + await cacheFile.destroy() - return retryTransactionWrapper(undoFollow, options) + if (video.isOwned()) { + // Don't resend the activity to the sender + const exceptions = [ byActor ] + + await forwardVideoRelatedActivity(activity, t, exceptions, video) + } + }) } -function undoFollow (actor: string, followActivity: ActivityFollow) { +function processUndoFollow (follower: ActorModel, followActivity: ActivityFollow) { return sequelizeTypescript.transaction(async t => { - const follower = await AccountModel.loadByUrl(actor, t) - const following = await AccountModel.loadByUrl(followActivity.object, t) - const accountFollow = await AccountFollowModel.loadByAccountAndTarget(follower.id, following.id, t) + const following = await ActorModel.loadByUrlAndPopulateAccountAndChannel(followActivity.object, t) + const actorFollow = await ActorFollowModel.loadByActorAndTarget(follower.id, following.id, t) - if (!accountFollow) throw new Error(`'Unknown account follow ${follower.id} -> ${following.id}.`) + if (!actorFollow) throw new Error(`'Unknown actor follow ${follower.id} -> ${following.id}.`) - await accountFollow.destroy({ transaction: t }) + await actorFollow.destroy({ transaction: t }) return undefined }) } + +function processUndoAnnounce (byActor: ActorModel, announceActivity: ActivityAnnounce) { + return sequelizeTypescript.transaction(async t => { + const share = await VideoShareModel.loadByUrl(announceActivity.id, t) + if (!share) throw new Error(`Unknown video share ${announceActivity.id}.`) + + if (share.actorId !== byActor.id) throw new Error(`${share.url} is not shared by ${byActor.url}.`) + + await share.destroy({ transaction: t }) + + if (share.Video.isOwned()) { + // Don't resend the activity to the sender + const exceptions = [ byActor ] + + await forwardVideoRelatedActivity(announceActivity, t, exceptions, share.Video) + } + }) +}