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