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

123 lines
4.9 KiB
TypeScript
Raw Normal View History

2018-05-11 15:10:13 +02:00
import { ActivityAnnounce, ActivityFollow, ActivityLike, ActivityUndo } from '../../../../shared/models/activitypub'
2017-12-12 17:53:50 +01:00
import { DislikeObject } from '../../../../shared/models/activitypub/objects'
import { getActorUrl } from '../../../helpers/activitypub'
2017-12-28 11:16:08 +01:00
import { retryTransactionWrapper } from '../../../helpers/database-utils'
import { logger } from '../../../helpers/logger'
2017-12-12 17:53:50 +01:00
import { sequelizeTypescript } from '../../../initializers'
import { AccountModel } from '../../../models/account/account'
import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
2017-12-14 17:38:41 +01:00
import { ActorModel } from '../../../models/activitypub/actor'
import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
import { forwardVideoRelatedActivity } from '../send/utils'
2018-01-10 17:18:12 +01:00
import { getOrCreateAccountAndVideoAndChannel } from '../videos'
2018-05-11 15:10:13 +02:00
import { VideoShareModel } from '../../../models/video/video-share'
2017-11-20 09:43:39 +01:00
async function processUndoActivity (activity: ActivityUndo) {
const activityToUndo = activity.object
const actorUrl = getActorUrl(activity.actor)
2017-11-23 14:19:55 +01:00
if (activityToUndo.type === 'Like') {
2018-06-13 14:27:40 +02:00
return retryTransactionWrapper(processUndoLike, actorUrl, activity)
2017-11-23 14:19:55 +01:00
} else if (activityToUndo.type === 'Create' && activityToUndo.object.type === 'Dislike') {
2018-06-13 14:27:40 +02:00
return retryTransactionWrapper(processUndoDislike, actorUrl, activity)
2017-11-23 14:19:55 +01:00
} else if (activityToUndo.type === 'Follow') {
2018-06-13 14:27:40 +02:00
return retryTransactionWrapper(processUndoFollow, actorUrl, activityToUndo)
2018-05-11 15:10:13 +02:00
} else if (activityToUndo.type === 'Announce') {
2018-06-13 14:27:40 +02:00
return retryTransactionWrapper(processUndoAnnounce, actorUrl, activityToUndo)
2017-11-20 09:43:39 +01:00
}
logger.warn('Unknown activity object type %s -> %s when undo activity.', activityToUndo.type, { activity: activity.id })
return undefined
}
// ---------------------------------------------------------------------------
export {
processUndoActivity
}
// ---------------------------------------------------------------------------
2017-11-23 14:19:55 +01:00
2018-06-13 14:27:40 +02:00
async function processUndoLike (actorUrl: string, activity: ActivityUndo) {
const likeActivity = activity.object as ActivityLike
2018-01-10 17:18:12 +01:00
const { video } = await getOrCreateAccountAndVideoAndChannel(likeActivity.object)
2017-12-12 17:53:50 +01:00
return sequelizeTypescript.transaction(async t => {
2017-12-14 17:38:41 +01:00
const byAccount = await AccountModel.loadByUrl(actorUrl, t)
if (!byAccount) throw new Error('Unknown account ' + actorUrl)
2017-11-23 14:19:55 +01:00
2017-12-12 17:53:50 +01:00
const rate = await AccountVideoRateModel.load(byAccount.id, video.id, t)
2017-11-23 14:19:55 +01: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 14:19:55 +01:00
if (video.isOwned()) {
// Don't resend the activity to the sender
2017-12-14 17:38:41 +01:00
const exceptions = [ byAccount.Actor ]
await forwardVideoRelatedActivity(activity, t, exceptions, video)
}
2017-11-23 14:19:55 +01:00
})
}
2018-06-13 14:27:40 +02:00
async function processUndoDislike (actorUrl: string, activity: ActivityUndo) {
const dislike = activity.object.object as DislikeObject
2018-01-10 17:18:12 +01:00
const { video } = await getOrCreateAccountAndVideoAndChannel(dislike.object)
2017-12-12 17:53:50 +01:00
return sequelizeTypescript.transaction(async t => {
2017-12-14 17:38:41 +01:00
const byAccount = await AccountModel.loadByUrl(actorUrl, t)
if (!byAccount) throw new Error('Unknown account ' + actorUrl)
2017-11-23 14:19:55 +01:00
2017-12-12 17:53:50 +01:00
const rate = await AccountVideoRateModel.load(byAccount.id, video.id, t)
2017-11-23 14:19:55 +01: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 14:19:55 +01:00
if (video.isOwned()) {
// Don't resend the activity to the sender
2017-12-14 17:38:41 +01:00
const exceptions = [ byAccount.Actor ]
await forwardVideoRelatedActivity(activity, t, exceptions, video)
}
2017-11-23 14:19:55 +01:00
})
}
2017-12-14 17:38:41 +01:00
function processUndoFollow (actorUrl: string, followActivity: ActivityFollow) {
2017-12-12 17:53:50 +01:00
return sequelizeTypescript.transaction(async t => {
2017-12-14 17:38:41 +01: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 14:19:55 +01:00
2017-12-14 17:38:41 +01:00
if (!actorFollow) throw new Error(`'Unknown actor follow ${follower.id} -> ${following.id}.`)
2017-11-23 14:19:55 +01:00
2017-12-14 17:38:41 +01:00
await actorFollow.destroy({ transaction: t })
2017-11-23 14:19:55 +01:00
return undefined
})
}
2018-05-11 15:10:13 +02:00
function processUndoAnnounce (actorUrl: string, announceActivity: ActivityAnnounce) {
return sequelizeTypescript.transaction(async t => {
const byAccount = await AccountModel.loadByUrl(actorUrl, t)
if (!byAccount) throw new Error('Unknown account ' + actorUrl)
2018-05-11 15:10:13 +02:00
const share = await VideoShareModel.loadByUrl(announceActivity.id, t)
if (!share) throw new Error(`'Unknown video share ${announceActivity.id}.`)
await share.destroy({ transaction: t })
if (share.Video.isOwned()) {
// Don't resend the activity to the sender
const exceptions = [ byAccount.Actor ]
await forwardVideoRelatedActivity(announceActivity, t, exceptions, share.Video)
}
2018-05-11 15:10:13 +02:00
})
}