1 import { ActivityFollow, ActivityLike, ActivityUndo } from '../../../../shared/models/activitypub'
2 import { DislikeObject } from '../../../../shared/models/activitypub/objects'
3 import { logger, retryTransactionWrapper } from '../../../helpers'
4 import { sequelizeTypescript } from '../../../initializers'
5 import { AccountModel } from '../../../models/account/account'
6 import { AccountFollowModel } from '../../../models/account/account-follow'
7 import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
8 import { VideoModel } from '../../../models/video/video'
9 import { forwardActivity } from '../send/misc'
11 async function processUndoActivity (activity: ActivityUndo) {
12 const activityToUndo = activity.object
14 if (activityToUndo.type === 'Like') {
15 return processUndoLike(activity.actor, activity)
16 } else if (activityToUndo.type === 'Create' && activityToUndo.object.type === 'Dislike') {
17 return processUndoDislike(activity.actor, activity)
18 } else if (activityToUndo.type === 'Follow') {
19 return processUndoFollow(activity.actor, activityToUndo)
22 logger.warn('Unknown activity object type %s -> %s when undo activity.', activityToUndo.type, { activity: activity.id })
27 // ---------------------------------------------------------------------------
33 // ---------------------------------------------------------------------------
35 function processUndoLike (actor: string, activity: ActivityUndo) {
37 arguments: [ actor, activity ],
38 errorMessage: 'Cannot undo like with many retries.'
41 return retryTransactionWrapper(undoLike, options)
44 function undoLike (actor: string, activity: ActivityUndo) {
45 const likeActivity = activity.object as ActivityLike
47 return sequelizeTypescript.transaction(async t => {
48 const byAccount = await AccountModel.loadByUrl(actor, t)
49 if (!byAccount) throw new Error('Unknown account ' + actor)
51 const video = await VideoModel.loadByUrlAndPopulateAccount(likeActivity.object, t)
52 if (!video) throw new Error('Unknown video ' + likeActivity.actor)
54 const rate = await AccountVideoRateModel.load(byAccount.id, video.id, t)
55 if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`)
57 await rate.destroy({ transaction: t })
58 await video.decrement('likes', { transaction: t })
60 if (video.isOwned()) {
61 // Don't resend the activity to the sender
62 const exceptions = [ byAccount ]
63 await forwardActivity(activity, t, exceptions)
68 function processUndoDislike (actor: string, activity: ActivityUndo) {
70 arguments: [ actor, activity ],
71 errorMessage: 'Cannot undo dislike with many retries.'
74 return retryTransactionWrapper(undoDislike, options)
77 function undoDislike (actor: string, activity: ActivityUndo) {
78 const dislike = activity.object.object as DislikeObject
80 return sequelizeTypescript.transaction(async t => {
81 const byAccount = await AccountModel.loadByUrl(actor, t)
82 if (!byAccount) throw new Error('Unknown account ' + actor)
84 const video = await VideoModel.loadByUrlAndPopulateAccount(dislike.object, t)
85 if (!video) throw new Error('Unknown video ' + dislike.actor)
87 const rate = await AccountVideoRateModel.load(byAccount.id, video.id, t)
88 if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`)
90 await rate.destroy({ transaction: t })
91 await video.decrement('dislikes', { transaction: t })
93 if (video.isOwned()) {
94 // Don't resend the activity to the sender
95 const exceptions = [ byAccount ]
96 await forwardActivity(activity, t, exceptions)
101 function processUndoFollow (actor: string, followActivity: ActivityFollow) {
103 arguments: [ actor, followActivity ],
104 errorMessage: 'Cannot undo follow with many retries.'
107 return retryTransactionWrapper(undoFollow, options)
110 function undoFollow (actor: string, followActivity: ActivityFollow) {
111 return sequelizeTypescript.transaction(async t => {
112 const follower = await AccountModel.loadByUrl(actor, t)
113 const following = await AccountModel.loadByUrl(followActivity.object, t)
114 const accountFollow = await AccountFollowModel.loadByAccountAndTarget(follower.id, following.id, t)
116 if (!accountFollow) throw new Error(`'Unknown account follow ${follower.id} -> ${following.id}.`)
118 await accountFollow.destroy({ transaction: t })