1 import { ActivityFollow, ActivityLike, ActivityUndo } from '../../../../shared/models/activitypub/activity'
2 import { DislikeObject } from '../../../../shared/models/activitypub/objects/dislike-object'
3 import { retryTransactionWrapper } from '../../../helpers/database-utils'
4 import { logger } from '../../../helpers/logger'
5 import { database as db } from '../../../initializers'
6 import { forwardActivity } from '../send/misc'
8 async function processUndoActivity (activity: ActivityUndo) {
9 const activityToUndo = activity.object
11 if (activityToUndo.type === 'Like') {
12 return processUndoLike(activity.actor, activity)
13 } else if (activityToUndo.type === 'Create' && activityToUndo.object.type === 'Dislike') {
14 return processUndoDislike(activity.actor, activity)
15 } else if (activityToUndo.type === 'Follow') {
16 return processUndoFollow(activity.actor, activityToUndo)
19 logger.warn('Unknown activity object type %s -> %s when undo activity.', activityToUndo.type, { activity: activity.id })
24 // ---------------------------------------------------------------------------
30 // ---------------------------------------------------------------------------
32 function processUndoLike (actor: string, activity: ActivityUndo) {
34 arguments: [ actor, activity ],
35 errorMessage: 'Cannot undo like with many retries.'
38 return retryTransactionWrapper(undoLike, options)
41 function undoLike (actor: string, activity: ActivityUndo) {
42 const likeActivity = activity.object as ActivityLike
44 return db.sequelize.transaction(async t => {
45 const byAccount = await db.Account.loadByUrl(actor, t)
46 if (!byAccount) throw new Error('Unknown account ' + actor)
48 const video = await db.Video.loadByUrlAndPopulateAccount(likeActivity.object, t)
49 if (!video) throw new Error('Unknown video ' + likeActivity.actor)
51 const rate = await db.AccountVideoRate.load(byAccount.id, video.id, t)
52 if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`)
54 await rate.destroy({ transaction: t })
55 await video.decrement('likes', { transaction: t })
57 if (video.isOwned()) {
58 // Don't resend the activity to the sender
59 const exceptions = [ byAccount ]
60 await forwardActivity(activity, t, exceptions)
65 function processUndoDislike (actor: string, activity: ActivityUndo) {
67 arguments: [ actor, activity ],
68 errorMessage: 'Cannot undo dislike with many retries.'
71 return retryTransactionWrapper(undoDislike, options)
74 function undoDislike (actor: string, activity: ActivityUndo) {
75 const dislike = activity.object.object as DislikeObject
77 return db.sequelize.transaction(async t => {
78 const byAccount = await db.Account.loadByUrl(actor, t)
79 if (!byAccount) throw new Error('Unknown account ' + actor)
81 const video = await db.Video.loadByUrlAndPopulateAccount(dislike.object, t)
82 if (!video) throw new Error('Unknown video ' + dislike.actor)
84 const rate = await db.AccountVideoRate.load(byAccount.id, video.id, t)
85 if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`)
87 await rate.destroy({ transaction: t })
88 await video.decrement('dislikes', { transaction: t })
90 if (video.isOwned()) {
91 // Don't resend the activity to the sender
92 const exceptions = [ byAccount ]
93 await forwardActivity(activity, t, exceptions)
98 function processUndoFollow (actor: string, followActivity: ActivityFollow) {
100 arguments: [ actor, followActivity ],
101 errorMessage: 'Cannot undo follow with many retries.'
104 return retryTransactionWrapper(undoFollow, options)
107 function undoFollow (actor: string, followActivity: ActivityFollow) {
108 return db.sequelize.transaction(async t => {
109 const follower = await db.Account.loadByUrl(actor, t)
110 const following = await db.Account.loadByUrl(followActivity.object, t)
111 const accountFollow = await db.AccountFollow.loadByAccountAndTarget(follower.id, following.id, t)
113 if (!accountFollow) throw new Error(`'Unknown account follow ${follower.id} -> ${following.id}.`)
115 await accountFollow.destroy({ transaction: t })