X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Factivitypub%2Fprocess%2Fprocess-undo.ts;h=c091d96788e8de7084c481c8821f4b96f5e922fb;hb=e587e0ecee5bec43a225995948faaa4bc97f080a;hp=cc221045f972c453f2d4a19a46fd7b2fe7f5f86f;hpb=63c93323ecdeaa4b6183d75dd3f13469e1ef3ebd;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/activitypub/process/process-undo.ts b/server/lib/activitypub/process/process-undo.ts index cc221045f..c091d9678 100644 --- a/server/lib/activitypub/process/process-undo.ts +++ b/server/lib/activitypub/process/process-undo.ts @@ -1,19 +1,41 @@ -import { ActivityFollow, ActivityLike, ActivityUndo } from '../../../../shared/models/activitypub/activity' -import { DislikeObject } from '../../../../shared/models/activitypub/objects/dislike-object' +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' import { logger } from '../../../helpers/logger' -import { database as db } from '../../../initializers' -import { forwardActivity } from '../send/misc' - -async function processUndoActivity (activity: ActivityUndo) { +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 { 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 }) @@ -29,26 +51,16 @@ 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 db.sequelize.transaction(async t => { - const byAccount = await db.Account.loadByUrl(actor, t) - if (!byAccount) throw new Error('Unknown account ' + actor) + const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: likeActivity.object }) - const video = await db.Video.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 db.AccountVideoRate.load(byAccount.id, video.id, t) + 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}.`) await rate.destroy({ transaction: t }) @@ -56,32 +68,23 @@ 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 db.sequelize.transaction(async t => { - const byAccount = await db.Account.loadByUrl(actor, t) - if (!byAccount) throw new Error('Unknown account ' + actor) + const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: dislike.object }) - const video = await db.Video.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 db.AccountVideoRate.load(byAccount.id, video.id, t) + 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}.`) await rate.destroy({ transaction: t }) @@ -89,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) - return retryTransactionWrapper(undoFollow, options) + 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 (actor: string, followActivity: ActivityFollow) { - return db.sequelize.transaction(async t => { - const follower = await db.Account.loadByUrl(actor, t) - const following = await db.Account.loadByUrl(followActivity.object, t) - const accountFollow = await db.AccountFollow.loadByAccountAndTarget(follower.id, following.id, t) +function processUndoFollow (follower: ActorModel, followActivity: ActivityFollow) { + return sequelizeTypescript.transaction(async 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) + } + }) +}