1
0
Fork 0
peertube/server/lib/activitypub/process/process-undo.ts

159 lines
6 KiB
TypeScript
Raw Normal View History

2018-09-11 10:27:07 -04:00
import { ActivityAnnounce, ActivityFollow, ActivityLike, ActivityUndo, CacheFileObject } from '../../../../shared/models/activitypub'
2017-12-12 11:53:50 -05:00
import { DislikeObject } from '../../../../shared/models/activitypub/objects'
2017-12-28 05:16:08 -05:00
import { retryTransactionWrapper } from '../../../helpers/database-utils'
import { logger } from '../../../helpers/logger'
2017-12-12 11:53:50 -05:00
import { sequelizeTypescript } from '../../../initializers'
import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
2017-12-14 11:38:41 -05:00
import { ActorModel } from '../../../models/activitypub/actor'
import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
import { forwardVideoRelatedActivity } from '../send/utils'
2018-08-22 10:15:35 -04:00
import { getOrCreateVideoAndAccountAndChannel } from '../videos'
2018-05-11 09:10:13 -04:00
import { VideoShareModel } from '../../../models/video/video-share'
2018-09-11 10:27:07 -04:00
import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
2017-11-20 03:43:39 -05:00
async function processUndoActivity (activity: ActivityUndo, byActor: ActorModel) {
2017-11-20 03:43:39 -05:00
const activityToUndo = activity.object
2017-11-23 08:19:55 -05:00
if (activityToUndo.type === 'Like') {
2018-09-19 09:47:55 -04:00
return retryTransactionWrapper(processUndoLike, byActor, activity)
2018-09-11 10:27:07 -04:00
}
if (activityToUndo.type === 'Create') {
if (activityToUndo.object.type === 'Dislike') {
2018-09-19 09:47:55 -04:00
return retryTransactionWrapper(processUndoDislike, byActor, activity)
2018-09-11 10:27:07 -04:00
} else if (activityToUndo.object.type === 'CacheFile') {
return retryTransactionWrapper(processUndoCacheFile, byActor, activity)
2018-09-11 10:27:07 -04:00
}
}
if (activityToUndo.type === 'Dislike') {
return retryTransactionWrapper(processUndoDislike, byActor, activity)
}
2018-09-11 10:27:07 -04:00
if (activityToUndo.type === 'Follow') {
return retryTransactionWrapper(processUndoFollow, byActor, activityToUndo)
2018-09-11 10:27:07 -04:00
}
if (activityToUndo.type === 'Announce') {
return retryTransactionWrapper(processUndoAnnounce, byActor, activityToUndo)
2017-11-20 03:43:39 -05:00
}
logger.warn('Unknown activity object type %s -> %s when undo activity.', activityToUndo.type, { activity: activity.id })
return undefined
}
// ---------------------------------------------------------------------------
export {
processUndoActivity
}
// ---------------------------------------------------------------------------
2017-11-23 08:19:55 -05:00
2018-09-19 09:47:55 -04:00
async function processUndoLike (byActor: ActorModel, activity: ActivityUndo) {
const likeActivity = activity.object as ActivityLike
2018-09-19 05:16:23 -04:00
const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: likeActivity.object })
2018-01-10 11:18:12 -05:00
2017-12-12 11:53:50 -05:00
return sequelizeTypescript.transaction(async t => {
2018-09-19 09:47:55 -04:00
if (!byActor.Account) throw new Error('Unknown account ' + byActor.url)
2017-11-23 08:19:55 -05:00
2018-11-14 09:01:28 -05:00
let rate = await AccountVideoRateModel.loadByUrl(likeActivity.id, t)
if (!rate) rate = await AccountVideoRateModel.load(byActor.Account.id, video.id, t)
2018-09-19 09:47:55 -04:00
if (!rate) throw new Error(`Unknown rate by account ${byActor.Account.id} for video ${video.id}.`)
2017-11-23 08:19:55 -05:00
await rate.destroy({ transaction: t })
await video.decrement('likes', { transaction: t })
2017-11-23 08:19:55 -05:00
if (video.isOwned()) {
// Don't resend the activity to the sender
2018-09-19 09:47:55 -04:00
const exceptions = [ byActor ]
await forwardVideoRelatedActivity(activity, t, exceptions, video)
}
2017-11-23 08:19:55 -05:00
})
}
2018-09-19 09:47:55 -04:00
async function processUndoDislike (byActor: ActorModel, activity: ActivityUndo) {
const dislike = activity.object.type === 'Dislike'
? activity.object
: activity.object.object as DislikeObject
2018-09-19 05:16:23 -04:00
const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: dislike.object })
2018-01-10 11:18:12 -05:00
2017-12-12 11:53:50 -05:00
return sequelizeTypescript.transaction(async t => {
2018-09-19 09:47:55 -04:00
if (!byActor.Account) throw new Error('Unknown account ' + byActor.url)
2017-11-23 08:19:55 -05:00
2018-11-14 09:01:28 -05:00
let rate = await AccountVideoRateModel.loadByUrl(dislike.id, t)
if (!rate) rate = await AccountVideoRateModel.load(byActor.Account.id, video.id, t)
2018-09-19 09:47:55 -04:00
if (!rate) throw new Error(`Unknown rate by account ${byActor.Account.id} for video ${video.id}.`)
2017-11-23 08:19:55 -05:00
await rate.destroy({ transaction: t })
await video.decrement('dislikes', { transaction: t })
2017-11-23 08:19:55 -05:00
if (video.isOwned()) {
// Don't resend the activity to the sender
2018-09-19 09:47:55 -04:00
const exceptions = [ byActor ]
await forwardVideoRelatedActivity(activity, t, exceptions, video)
}
2017-11-23 08:19:55 -05:00
})
}
async function processUndoCacheFile (byActor: ActorModel, activity: ActivityUndo) {
2018-09-11 10:27:07 -04:00
const cacheFileObject = activity.object.object as CacheFileObject
2018-09-19 05:16:23 -04:00
const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.object })
2018-09-11 10:27:07 -04:00
return sequelizeTypescript.transaction(async t => {
const cacheFile = await VideoRedundancyModel.loadByUrl(cacheFileObject.id)
2019-04-24 04:28:57 -04:00
if (!cacheFile) {
logger.debug('Cannot undo unknown video cache %s.', cacheFileObject.id)
return
}
2018-09-11 10:27:07 -04:00
2018-09-19 09:47:55 -04:00
if (cacheFile.actorId !== byActor.id) throw new Error('Cannot delete redundancy ' + cacheFile.url + ' of another actor.')
2018-09-11 10:27:07 -04:00
await cacheFile.destroy()
if (video.isOwned()) {
// Don't resend the activity to the sender
const exceptions = [ byActor ]
await forwardVideoRelatedActivity(activity, t, exceptions, video)
}
})
}
function processUndoFollow (follower: ActorModel, followActivity: ActivityFollow) {
2017-12-12 11:53:50 -05:00
return sequelizeTypescript.transaction(async t => {
const following = await ActorModel.loadByUrlAndPopulateAccountAndChannel(followActivity.object, t)
2017-12-14 11:38:41 -05:00
const actorFollow = await ActorFollowModel.loadByActorAndTarget(follower.id, following.id, t)
2017-11-23 08:19:55 -05:00
2017-12-14 11:38:41 -05:00
if (!actorFollow) throw new Error(`'Unknown actor follow ${follower.id} -> ${following.id}.`)
2017-11-23 08:19:55 -05:00
2017-12-14 11:38:41 -05:00
await actorFollow.destroy({ transaction: t })
2017-11-23 08:19:55 -05:00
return undefined
})
}
2018-05-11 09:10:13 -04:00
function processUndoAnnounce (byActor: ActorModel, announceActivity: ActivityAnnounce) {
2018-05-11 09:10:13 -04:00
return sequelizeTypescript.transaction(async t => {
const share = await VideoShareModel.loadByUrl(announceActivity.id, t)
2018-09-04 04:22:10 -04:00
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}.`)
2018-05-11 09:10:13 -04:00
await share.destroy({ transaction: t })
if (share.Video.isOwned()) {
// Don't resend the activity to the sender
2018-09-04 04:22:10 -04:00
const exceptions = [ byActor ]
await forwardVideoRelatedActivity(announceActivity, t, exceptions, share.Video)
}
2018-05-11 09:10:13 -04:00
})
}