PeerTube/server/lib/activitypub/process/process-undo.ts

125 lines
4.6 KiB
TypeScript
Raw Normal View History

2017-12-12 10:53:50 -06:00
import { ActivityFollow, ActivityLike, ActivityUndo } from '../../../../shared/models/activitypub'
import { DislikeObject } from '../../../../shared/models/activitypub/objects'
2017-12-28 04:16:08 -06:00
import { retryTransactionWrapper } from '../../../helpers/database-utils'
import { logger } from '../../../helpers/logger'
2017-12-12 10:53:50 -06:00
import { sequelizeTypescript } from '../../../initializers'
import { AccountModel } from '../../../models/account/account'
import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
2017-12-14 10:38:41 -06:00
import { ActorModel } from '../../../models/activitypub/actor'
import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
2017-12-12 10:53:50 -06:00
import { VideoModel } from '../../../models/video/video'
import { forwardActivity } from '../send/misc'
2017-11-20 02:43:39 -06:00
async function processUndoActivity (activity: ActivityUndo) {
const activityToUndo = activity.object
2017-11-23 07:19:55 -06:00
if (activityToUndo.type === 'Like') {
return processUndoLike(activity.actor, activity)
2017-11-23 07:19:55 -06:00
} else if (activityToUndo.type === 'Create' && activityToUndo.object.type === 'Dislike') {
return processUndoDislike(activity.actor, activity)
2017-11-23 07:19:55 -06:00
} else if (activityToUndo.type === 'Follow') {
return processUndoFollow(activity.actor, activityToUndo)
2017-11-20 02:43:39 -06:00
}
logger.warn('Unknown activity object type %s -> %s when undo activity.', activityToUndo.type, { activity: activity.id })
return undefined
}
// ---------------------------------------------------------------------------
export {
processUndoActivity
}
// ---------------------------------------------------------------------------
2017-11-23 07:19:55 -06:00
2017-12-14 10:38:41 -06:00
function processUndoLike (actorUrl: string, activity: ActivityUndo) {
2017-11-23 07:19:55 -06:00
const options = {
2017-12-14 10:38:41 -06:00
arguments: [ actorUrl, activity ],
2017-11-23 07:19:55 -06:00
errorMessage: 'Cannot undo like with many retries.'
}
return retryTransactionWrapper(undoLike, options)
}
2017-12-14 10:38:41 -06:00
function undoLike (actorUrl: string, activity: ActivityUndo) {
const likeActivity = activity.object as ActivityLike
2017-12-12 10:53:50 -06:00
return sequelizeTypescript.transaction(async t => {
2017-12-14 10:38:41 -06:00
const byAccount = await AccountModel.loadByUrl(actorUrl, t)
if (!byAccount) throw new Error('Unknown account ' + actorUrl)
2017-11-23 07:19:55 -06:00
2017-12-12 10:53:50 -06:00
const video = await VideoModel.loadByUrlAndPopulateAccount(likeActivity.object, t)
2017-11-23 07:19:55 -06:00
if (!video) throw new Error('Unknown video ' + likeActivity.actor)
2017-12-12 10:53:50 -06:00
const rate = await AccountVideoRateModel.load(byAccount.id, video.id, t)
2017-11-23 07:19:55 -06:00
if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`)
await rate.destroy({ transaction: t })
await video.decrement('likes', { transaction: t })
2017-11-23 07:19:55 -06:00
if (video.isOwned()) {
// Don't resend the activity to the sender
2017-12-14 10:38:41 -06:00
const exceptions = [ byAccount.Actor ]
await forwardActivity(activity, t, exceptions)
}
2017-11-23 07:19:55 -06:00
})
}
2017-12-14 10:38:41 -06:00
function processUndoDislike (actorUrl: string, activity: ActivityUndo) {
2017-11-23 07:19:55 -06:00
const options = {
2017-12-14 10:38:41 -06:00
arguments: [ actorUrl, activity ],
2017-11-23 07:19:55 -06:00
errorMessage: 'Cannot undo dislike with many retries.'
}
return retryTransactionWrapper(undoDislike, options)
}
2017-12-14 10:38:41 -06:00
function undoDislike (actorUrl: string, activity: ActivityUndo) {
const dislike = activity.object.object as DislikeObject
2017-12-12 10:53:50 -06:00
return sequelizeTypescript.transaction(async t => {
2017-12-14 10:38:41 -06:00
const byAccount = await AccountModel.loadByUrl(actorUrl, t)
if (!byAccount) throw new Error('Unknown account ' + actorUrl)
2017-11-23 07:19:55 -06:00
2017-12-12 10:53:50 -06:00
const video = await VideoModel.loadByUrlAndPopulateAccount(dislike.object, t)
2017-11-23 07:19:55 -06:00
if (!video) throw new Error('Unknown video ' + dislike.actor)
2017-12-12 10:53:50 -06:00
const rate = await AccountVideoRateModel.load(byAccount.id, video.id, t)
2017-11-23 07:19:55 -06:00
if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`)
await rate.destroy({ transaction: t })
await video.decrement('dislikes', { transaction: t })
2017-11-23 07:19:55 -06:00
if (video.isOwned()) {
// Don't resend the activity to the sender
2017-12-14 10:38:41 -06:00
const exceptions = [ byAccount.Actor ]
await forwardActivity(activity, t, exceptions)
}
2017-11-23 07:19:55 -06:00
})
}
2017-12-14 10:38:41 -06:00
function processUndoFollow (actorUrl: string, followActivity: ActivityFollow) {
2017-11-23 07:19:55 -06:00
const options = {
2017-12-14 10:38:41 -06:00
arguments: [ actorUrl, followActivity ],
2017-11-23 07:19:55 -06:00
errorMessage: 'Cannot undo follow with many retries.'
}
return retryTransactionWrapper(undoFollow, options)
}
2017-12-14 10:38:41 -06:00
function undoFollow (actorUrl: string, followActivity: ActivityFollow) {
2017-12-12 10:53:50 -06:00
return sequelizeTypescript.transaction(async t => {
2017-12-14 10:38:41 -06:00
const follower = await ActorModel.loadByUrl(actorUrl, t)
const following = await ActorModel.loadByUrl(followActivity.object, t)
const actorFollow = await ActorFollowModel.loadByActorAndTarget(follower.id, following.id, t)
2017-11-23 07:19:55 -06:00
2017-12-14 10:38:41 -06:00
if (!actorFollow) throw new Error(`'Unknown actor follow ${follower.id} -> ${following.id}.`)
2017-11-23 07:19:55 -06:00
2017-12-14 10:38:41 -06:00
await actorFollow.destroy({ transaction: t })
2017-11-23 07:19:55 -06:00
return undefined
})
}