X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Factivitypub%2Fprocess%2Fprocess-undo.ts;h=c091d96788e8de7084c481c8821f4b96f5e922fb;hb=e587e0ecee5bec43a225995948faaa4bc97f080a;hp=9b024d15f5c2cd802eb463ebb3a9f1d731ef0a8a;hpb=0f320037e689b2778959c12ddd4ce790f6e4ae4f;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/activitypub/process/process-undo.ts b/server/lib/activitypub/process/process-undo.ts index 9b024d15f..c091d9678 100644 --- a/server/lib/activitypub/process/process-undo.ts +++ b/server/lib/activitypub/process/process-undo.ts @@ -1,4 +1,4 @@ -import { ActivityAnnounce, 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 { getActorUrl } from '../../../helpers/activitypub' import { retryTransactionWrapper } from '../../../helpers/database-utils' @@ -8,23 +8,34 @@ import { AccountModel } from '../../../models/account/account' import { AccountVideoRateModel } from '../../../models/account/account-video-rate' import { ActorModel } from '../../../models/activitypub/actor' import { ActorFollowModel } from '../../../models/activitypub/actor-follow' -import { forwardActivity } from '../send/misc' -import { getOrCreateAccountAndVideoAndChannel } from '../videos' +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) { +async function processUndoActivity (activity: ActivityUndo, byActor: ActorModel) { const activityToUndo = activity.object const actorUrl = getActorUrl(activity.actor) if (activityToUndo.type === 'Like') { - return processUndoLike(actorUrl, activity) - } else if (activityToUndo.type === 'Create' && activityToUndo.object.type === 'Dislike') { - return processUndoDislike(actorUrl, activity) - } else if (activityToUndo.type === 'Follow') { - return processUndoFollow(actorUrl, activityToUndo) - } else if (activityToUndo.type === 'Announce') { - return processUndoAnnounce(actorUrl, 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 }) @@ -40,19 +51,10 @@ export { // --------------------------------------------------------------------------- -function processUndoLike (actorUrl: string, activity: ActivityUndo) { - const options = { - arguments: [ actorUrl, activity ], - errorMessage: 'Cannot undo like with many retries.' - } - - return retryTransactionWrapper(undoLike, options) -} - -async function undoLike (actorUrl: string, activity: ActivityUndo) { +async function processUndoLike (actorUrl: string, activity: ActivityUndo) { const likeActivity = activity.object as ActivityLike - const { video } = await getOrCreateAccountAndVideoAndChannel(likeActivity.object) + const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: likeActivity.object }) return sequelizeTypescript.transaction(async t => { const byAccount = await AccountModel.loadByUrl(actorUrl, t) @@ -67,24 +69,16 @@ async function undoLike (actorUrl: string, activity: ActivityUndo) { if (video.isOwned()) { // Don't resend the activity to the sender const exceptions = [ byAccount.Actor ] - await forwardActivity(activity, t, exceptions) + + await forwardVideoRelatedActivity(activity, t, exceptions, video) } }) } -function processUndoDislike (actorUrl: string, activity: ActivityUndo) { - const options = { - arguments: [ actorUrl, activity ], - errorMessage: 'Cannot undo dislike with many retries.' - } - - return retryTransactionWrapper(undoDislike, options) -} - -async function undoDislike (actorUrl: string, activity: ActivityUndo) { +async function processUndoDislike (actorUrl: string, activity: ActivityUndo) { const dislike = activity.object.object as DislikeObject - const { video } = await getOrCreateAccountAndVideoAndChannel(dislike.object) + const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: dislike.object }) return sequelizeTypescript.transaction(async t => { const byAccount = await AccountModel.loadByUrl(actorUrl, t) @@ -99,24 +93,35 @@ async function undoDislike (actorUrl: string, activity: ActivityUndo) { if (video.isOwned()) { // Don't resend the activity to the sender const exceptions = [ byAccount.Actor ] - await forwardActivity(activity, t, exceptions) + + await forwardVideoRelatedActivity(activity, t, exceptions, video) } }) } -function processUndoFollow (actorUrl: string, followActivity: ActivityFollow) { - const options = { - arguments: [ actorUrl, followActivity ], - errorMessage: 'Cannot undo follow with many retries.' - } +async function processUndoCacheFile (byActor: ActorModel, activity: ActivityUndo) { + const cacheFileObject = activity.object.object as CacheFileObject - return retryTransactionWrapper(undoFollow, options) + 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() + + if (video.isOwned()) { + // Don't resend the activity to the sender + const exceptions = [ byActor ] + + await forwardVideoRelatedActivity(activity, t, exceptions, video) + } + }) } -function undoFollow (actorUrl: string, followActivity: ActivityFollow) { +function processUndoFollow (follower: ActorModel, followActivity: ActivityFollow) { return sequelizeTypescript.transaction(async t => { - const follower = await ActorModel.loadByUrl(actorUrl, t) - const following = await ActorModel.loadByUrl(followActivity.object, t) + const following = await ActorModel.loadByUrlAndPopulateAccountAndChannel(followActivity.object, t) const actorFollow = await ActorFollowModel.loadByActorAndTarget(follower.id, following.id, t) if (!actorFollow) throw new Error(`'Unknown actor follow ${follower.id} -> ${following.id}.`) @@ -127,22 +132,20 @@ function undoFollow (actorUrl: string, followActivity: ActivityFollow) { }) } -function processUndoAnnounce (actorUrl: string, announceActivity: ActivityAnnounce) { - const options = { - arguments: [ actorUrl, announceActivity ], - errorMessage: 'Cannot undo announce with many retries.' - } - - return retryTransactionWrapper(undoAnnounce, options) -} - -function undoAnnounce (actorUrl: string, announceActivity: ActivityAnnounce) { +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) 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 }) - return undefined + if (share.Video.isOwned()) { + // Don't resend the activity to the sender + const exceptions = [ byActor ] + + await forwardVideoRelatedActivity(announceActivity, t, exceptions, share.Video) + } }) }