2018-05-11 08:10:13 -05:00
|
|
|
import { ActivityAnnounce, ActivityFollow, ActivityLike, ActivityUndo } from '../../../../shared/models/activitypub'
|
2017-12-12 10:53:50 -06:00
|
|
|
import { DislikeObject } from '../../../../shared/models/activitypub/objects'
|
2018-03-22 12:40:33 -05:00
|
|
|
import { getActorUrl } from '../../../helpers/activitypub'
|
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'
|
2018-05-31 03:23:56 -05:00
|
|
|
import { forwardVideoRelatedActivity } from '../send/utils'
|
2018-08-22 09:15:35 -05:00
|
|
|
import { getOrCreateVideoAndAccountAndChannel } from '../videos'
|
2018-05-11 08:10:13 -05:00
|
|
|
import { VideoShareModel } from '../../../models/video/video-share'
|
2017-11-20 02:43:39 -06:00
|
|
|
|
|
|
|
async function processUndoActivity (activity: ActivityUndo) {
|
|
|
|
const activityToUndo = activity.object
|
|
|
|
|
2018-03-22 12:40:33 -05:00
|
|
|
const actorUrl = getActorUrl(activity.actor)
|
|
|
|
|
2017-11-23 07:19:55 -06:00
|
|
|
if (activityToUndo.type === 'Like') {
|
2018-06-13 07:27:40 -05:00
|
|
|
return retryTransactionWrapper(processUndoLike, actorUrl, activity)
|
2017-11-23 07:19:55 -06:00
|
|
|
} else if (activityToUndo.type === 'Create' && activityToUndo.object.type === 'Dislike') {
|
2018-06-13 07:27:40 -05:00
|
|
|
return retryTransactionWrapper(processUndoDislike, actorUrl, activity)
|
2017-11-23 07:19:55 -06:00
|
|
|
} else if (activityToUndo.type === 'Follow') {
|
2018-06-13 07:27:40 -05:00
|
|
|
return retryTransactionWrapper(processUndoFollow, actorUrl, activityToUndo)
|
2018-05-11 08:10:13 -05:00
|
|
|
} else if (activityToUndo.type === 'Announce') {
|
2018-06-13 07:27:40 -05:00
|
|
|
return retryTransactionWrapper(processUndoAnnounce, actorUrl, 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
|
|
|
|
2018-06-13 07:27:40 -05:00
|
|
|
async function processUndoLike (actorUrl: string, activity: ActivityUndo) {
|
2017-11-24 06:41:10 -06:00
|
|
|
const likeActivity = activity.object as ActivityLike
|
|
|
|
|
2018-08-22 09:15:35 -05:00
|
|
|
const { video } = await getOrCreateVideoAndAccountAndChannel(likeActivity.object)
|
2018-01-10 10:18:12 -06:00
|
|
|
|
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 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
|
2017-12-14 10:38:41 -06:00
|
|
|
const exceptions = [ byAccount.Actor ]
|
2018-05-31 03:23:56 -05:00
|
|
|
|
|
|
|
await forwardVideoRelatedActivity(activity, t, exceptions, video)
|
2017-11-24 06:41:10 -06:00
|
|
|
}
|
2017-11-23 07:19:55 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-06-13 07:27:40 -05:00
|
|
|
async function processUndoDislike (actorUrl: string, activity: ActivityUndo) {
|
2017-11-24 06:41:10 -06:00
|
|
|
const dislike = activity.object.object as DislikeObject
|
|
|
|
|
2018-08-22 09:15:35 -05:00
|
|
|
const { video } = await getOrCreateVideoAndAccountAndChannel(dislike.object)
|
2018-01-10 10:18:12 -06:00
|
|
|
|
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 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
|
2017-12-14 10:38:41 -06:00
|
|
|
const exceptions = [ byAccount.Actor ]
|
2018-05-31 03:23:56 -05:00
|
|
|
|
|
|
|
await forwardVideoRelatedActivity(activity, t, exceptions, video)
|
2017-11-24 06:41:10 -06:00
|
|
|
}
|
2017-11-23 07:19:55 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
function processUndoFollow (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
|
|
|
|
})
|
|
|
|
}
|
2018-05-11 08:10:13 -05:00
|
|
|
|
|
|
|
function processUndoAnnounce (actorUrl: string, announceActivity: ActivityAnnounce) {
|
|
|
|
return sequelizeTypescript.transaction(async t => {
|
2018-05-31 03:23:56 -05:00
|
|
|
const byAccount = await AccountModel.loadByUrl(actorUrl, t)
|
|
|
|
if (!byAccount) throw new Error('Unknown account ' + actorUrl)
|
|
|
|
|
2018-05-11 08:10:13 -05:00
|
|
|
const share = await VideoShareModel.loadByUrl(announceActivity.id, t)
|
|
|
|
if (!share) throw new Error(`'Unknown video share ${announceActivity.id}.`)
|
|
|
|
|
|
|
|
await share.destroy({ transaction: t })
|
|
|
|
|
2018-05-31 03:23:56 -05:00
|
|
|
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 08:10:13 -05:00
|
|
|
})
|
|
|
|
}
|