1 import { ActivityFollow, ActivityLike, ActivityUndo } from '../../../../shared/models/activitypub'
2 import { DislikeObject } from '../../../../shared/models/activitypub/objects'
3 import { retryTransactionWrapper } from '../../../helpers/database-utils'
4 import { logger } from '../../../helpers/logger'
5 import { sequelizeTypescript } from '../../../initializers'
6 import { AccountModel } from '../../../models/account/account'
7 import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
8 import { ActorModel } from '../../../models/activitypub/actor'
9 import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
10 import { forwardActivity } from '../send/misc'
11 import { getOrCreateAccountAndVideoAndChannel } from '../videos'
13 async function processUndoActivity (activity: ActivityUndo) {
14 const activityToUndo = activity.object
16 if (activityToUndo.type === 'Like') {
17 return processUndoLike(activity.actor, activity)
18 } else if (activityToUndo.type === 'Create' && activityToUndo.object.type === 'Dislike') {
19 return processUndoDislike(activity.actor, activity)
20 } else if (activityToUndo.type === 'Follow') {
21 return processUndoFollow(activity.actor, activityToUndo)
24 logger.warn('Unknown activity object type %s -> %s when undo activity.', activityToUndo.type, { activity: activity.id })
29 // ---------------------------------------------------------------------------
35 // ---------------------------------------------------------------------------
37 function processUndoLike (actorUrl: string, activity: ActivityUndo) {
39 arguments: [ actorUrl, activity ],
40 errorMessage: 'Cannot undo like with many retries.'
43 return retryTransactionWrapper(undoLike, options)
46 async function undoLike (actorUrl: string, activity: ActivityUndo) {
47 const likeActivity = activity.object as ActivityLike
49 const { video } = await getOrCreateAccountAndVideoAndChannel(likeActivity.object)
51 return sequelizeTypescript.transaction(async t => {
52 const byAccount = await AccountModel.loadByUrl(actorUrl, t)
53 if (!byAccount) throw new Error('Unknown account ' + actorUrl)
55 const rate = await AccountVideoRateModel.load(byAccount.id, video.id, t)
56 if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`)
58 await rate.destroy({ transaction: t })
59 await video.decrement('likes', { transaction: t })
61 if (video.isOwned()) {
62 // Don't resend the activity to the sender
63 const exceptions = [ byAccount.Actor ]
64 await forwardActivity(activity, t, exceptions)
69 function processUndoDislike (actorUrl: string, activity: ActivityUndo) {
71 arguments: [ actorUrl, activity ],
72 errorMessage: 'Cannot undo dislike with many retries.'
75 return retryTransactionWrapper(undoDislike, options)
78 async function undoDislike (actorUrl: string, activity: ActivityUndo) {
79 const dislike = activity.object.object as DislikeObject
81 const { video } = await getOrCreateAccountAndVideoAndChannel(dislike.object)
83 return sequelizeTypescript.transaction(async t => {
84 const byAccount = await AccountModel.loadByUrl(actorUrl, t)
85 if (!byAccount) throw new Error('Unknown account ' + actorUrl)
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.Actor ]
96 await forwardActivity(activity, t, exceptions)
101 function processUndoFollow (actorUrl: string, followActivity: ActivityFollow) {
103 arguments: [ actorUrl, followActivity ],
104 errorMessage: 'Cannot undo follow with many retries.'
107 return retryTransactionWrapper(undoFollow, options)
110 function undoFollow (actorUrl: string, followActivity: ActivityFollow) {
111 return sequelizeTypescript.transaction(async t => {
112 const follower = await ActorModel.loadByUrl(actorUrl, t)
113 const following = await ActorModel.loadByUrl(followActivity.object, t)
114 const actorFollow = await ActorFollowModel.loadByActorAndTarget(follower.id, following.id, t)
116 if (!actorFollow) throw new Error(`'Unknown actor follow ${follower.id} -> ${following.id}.`)
118 await actorFollow.destroy({ transaction: t })