X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Factivitypub%2Fprocess%2Fprocess-undo.ts;h=c091d96788e8de7084c481c8821f4b96f5e922fb;hb=e587e0ecee5bec43a225995948faaa4bc97f080a;hp=4a0181137f465a2a87aa43fced11772ccdbcf3bd;hpb=50d6de9c286abcb34ff4234d56d9cbb803db7665;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/activitypub/process/process-undo.ts b/server/lib/activitypub/process/process-undo.ts index 4a0181137..c091d9678 100644 --- a/server/lib/activitypub/process/process-undo.ts +++ b/server/lib/activitypub/process/process-undo.ts @@ -1,23 +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 { AccountVideoRateModel } from '../../../models/account/account-video-rate' import { ActorModel } from '../../../models/activitypub/actor' import { ActorFollowModel } from '../../../models/activitypub/actor-follow' -import { VideoModel } from '../../../models/video/video' -import { forwardActivity } from '../send/misc' +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(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 }) @@ -33,25 +51,15 @@ export { // --------------------------------------------------------------------------- -function processUndoLike (actorUrl: string, activity: ActivityUndo) { - const options = { - arguments: [ actorUrl, activity ], - errorMessage: 'Cannot undo like with many retries.' - } - - return retryTransactionWrapper(undoLike, options) -} - -function undoLike (actorUrl: string, activity: ActivityUndo) { +async function processUndoLike (actorUrl: string, activity: ActivityUndo) { const likeActivity = activity.object as ActivityLike + const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: likeActivity.object }) + return sequelizeTypescript.transaction(async t => { const byAccount = await AccountModel.loadByUrl(actorUrl, t) if (!byAccount) throw new Error('Unknown account ' + actorUrl) - const video = await VideoModel.loadByUrlAndPopulateAccount(likeActivity.object, t) - if (!video) throw new Error('Unknown video ' + likeActivity.actor) - 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}.`) @@ -61,30 +69,21 @@ 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) -} - -function undoDislike (actorUrl: string, activity: ActivityUndo) { +async function processUndoDislike (actorUrl: string, activity: ActivityUndo) { const dislike = activity.object.object as DislikeObject + const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: dislike.object }) + return sequelizeTypescript.transaction(async t => { const byAccount = await AccountModel.loadByUrl(actorUrl, t) if (!byAccount) throw new Error('Unknown account ' + actorUrl) - const video = await VideoModel.loadByUrlAndPopulateAccount(dislike.object, t) - if (!video) throw new Error('Unknown video ' + dislike.actor) - 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}.`) @@ -94,24 +93,35 @@ 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 + + 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 (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}.`) @@ -121,3 +131,21 @@ function undoFollow (actorUrl: string, followActivity: ActivityFollow) { 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) + } + }) +}