2017-12-12 10:53:50 -06:00
|
|
|
import { ActivityFollow, ActivityLike, ActivityUndo } from '../../../../shared/models/activitypub'
|
|
|
|
import { DislikeObject } from '../../../../shared/models/activitypub/objects'
|
|
|
|
import { logger, retryTransactionWrapper } from '../../../helpers'
|
|
|
|
import { sequelizeTypescript } from '../../../initializers'
|
|
|
|
import { AccountModel } from '../../../models/account/account'
|
|
|
|
import { AccountFollowModel } from '../../../models/account/account-follow'
|
|
|
|
import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
|
|
|
|
import { VideoModel } from '../../../models/video/video'
|
2017-11-24 06:41:10 -06:00
|
|
|
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') {
|
2017-11-24 06:41:10 -06:00
|
|
|
return processUndoLike(activity.actor, activity)
|
2017-11-23 07:19:55 -06:00
|
|
|
} else if (activityToUndo.type === 'Create' && activityToUndo.object.type === 'Dislike') {
|
2017-11-24 06:41:10 -06:00
|
|
|
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-11-24 06:41:10 -06:00
|
|
|
function processUndoLike (actor: string, activity: ActivityUndo) {
|
2017-11-23 07:19:55 -06:00
|
|
|
const options = {
|
2017-11-24 06:41:10 -06:00
|
|
|
arguments: [ actor, activity ],
|
2017-11-23 07:19:55 -06:00
|
|
|
errorMessage: 'Cannot undo like with many retries.'
|
|
|
|
}
|
|
|
|
|
|
|
|
return retryTransactionWrapper(undoLike, options)
|
|
|
|
}
|
|
|
|
|
2017-11-24 06:41:10 -06:00
|
|
|
function undoLike (actor: string, activity: ActivityUndo) {
|
|
|
|
const likeActivity = activity.object as ActivityLike
|
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
return sequelizeTypescript.transaction(async t => {
|
|
|
|
const byAccount = await AccountModel.loadByUrl(actor, t)
|
2017-11-23 07:19:55 -06:00
|
|
|
if (!byAccount) throw new Error('Unknown account ' + actor)
|
|
|
|
|
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 })
|
2017-11-24 06:41:10 -06:00
|
|
|
await video.decrement('likes', { transaction: t })
|
2017-11-23 07:19:55 -06:00
|
|
|
|
2017-11-24 06:41:10 -06:00
|
|
|
if (video.isOwned()) {
|
|
|
|
// Don't resend the activity to the sender
|
|
|
|
const exceptions = [ byAccount ]
|
|
|
|
await forwardActivity(activity, t, exceptions)
|
|
|
|
}
|
2017-11-23 07:19:55 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-11-24 06:41:10 -06:00
|
|
|
function processUndoDislike (actor: string, activity: ActivityUndo) {
|
2017-11-23 07:19:55 -06:00
|
|
|
const options = {
|
2017-11-24 06:41:10 -06:00
|
|
|
arguments: [ actor, activity ],
|
2017-11-23 07:19:55 -06:00
|
|
|
errorMessage: 'Cannot undo dislike with many retries.'
|
|
|
|
}
|
|
|
|
|
|
|
|
return retryTransactionWrapper(undoDislike, options)
|
|
|
|
}
|
|
|
|
|
2017-11-24 06:41:10 -06:00
|
|
|
function undoDislike (actor: string, activity: ActivityUndo) {
|
|
|
|
const dislike = activity.object.object as DislikeObject
|
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
return sequelizeTypescript.transaction(async t => {
|
|
|
|
const byAccount = await AccountModel.loadByUrl(actor, t)
|
2017-11-23 07:19:55 -06:00
|
|
|
if (!byAccount) throw new Error('Unknown account ' + actor)
|
|
|
|
|
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 })
|
2017-11-24 06:41:10 -06:00
|
|
|
await video.decrement('dislikes', { transaction: t })
|
2017-11-23 07:19:55 -06:00
|
|
|
|
2017-11-24 06:41:10 -06:00
|
|
|
if (video.isOwned()) {
|
|
|
|
// Don't resend the activity to the sender
|
|
|
|
const exceptions = [ byAccount ]
|
|
|
|
await forwardActivity(activity, t, exceptions)
|
|
|
|
}
|
2017-11-23 07:19:55 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function processUndoFollow (actor: string, followActivity: ActivityFollow) {
|
|
|
|
const options = {
|
|
|
|
arguments: [ actor, followActivity ],
|
|
|
|
errorMessage: 'Cannot undo follow with many retries.'
|
|
|
|
}
|
|
|
|
|
|
|
|
return retryTransactionWrapper(undoFollow, options)
|
|
|
|
}
|
|
|
|
|
|
|
|
function undoFollow (actor: string, followActivity: ActivityFollow) {
|
2017-12-12 10:53:50 -06:00
|
|
|
return sequelizeTypescript.transaction(async t => {
|
|
|
|
const follower = await AccountModel.loadByUrl(actor, t)
|
|
|
|
const following = await AccountModel.loadByUrl(followActivity.object, t)
|
|
|
|
const accountFollow = await AccountFollowModel.loadByAccountAndTarget(follower.id, following.id, t)
|
2017-11-23 07:19:55 -06:00
|
|
|
|
|
|
|
if (!accountFollow) throw new Error(`'Unknown account follow ${follower.id} -> ${following.id}.`)
|
|
|
|
|
|
|
|
await accountFollow.destroy({ transaction: t })
|
|
|
|
|
|
|
|
return undefined
|
|
|
|
})
|
|
|
|
}
|