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 { AccountVideoRateModel } from '../../../models/account/account-video-rate'
7 import { ActorModel } from '../../../models/activitypub/actor'
8 import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
9 import { VideoModel } from '../../../models/video/video'
10 import { forwardActivity } from '../send/misc'
12 async function processUndoActivity (activity: ActivityUndo) {
13 const activityToUndo = activity.object
15 if (activityToUndo.type === 'Like') {
16 return processUndoLike(activity.actor, activity)
17 } else if (activityToUndo.type === 'Create' && activityToUndo.object.type === 'Dislike') {
18 return processUndoDislike(activity.actor, activity)
19 } else if (activityToUndo.type === 'Follow') {
20 return processUndoFollow(activity.actor, activityToUndo)
23 logger.warn('Unknown activity object type %s -> %s when undo activity.', activityToUndo.type, { activity: activity.id })
28 // ---------------------------------------------------------------------------
34 // ---------------------------------------------------------------------------
36 function processUndoLike (actorUrl: string, activity: ActivityUndo) {
38 arguments: [ actorUrl, activity ],
39 errorMessage: 'Cannot undo like with many retries.'
42 return retryTransactionWrapper(undoLike, options)
45 function undoLike (actorUrl: string, activity: ActivityUndo) {
46 const likeActivity = activity.object as ActivityLike
48 return sequelizeTypescript.transaction(async t => {
49 const byAccount = await AccountModel.loadByUrl(actorUrl, t)
50 if (!byAccount) throw new Error('Unknown account ' + actorUrl)
52 const video = await VideoModel.loadByUrlAndPopulateAccount(likeActivity.object, t)
53 if (!video) throw new Error('Unknown video ' + likeActivity.actor)
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 function undoDislike (actorUrl: string, activity: ActivityUndo) {
79 const dislike = activity.object.object as DislikeObject
81 return sequelizeTypescript.transaction(async t => {
82 const byAccount = await AccountModel.loadByUrl(actorUrl, t)
83 if (!byAccount) throw new Error('Unknown account ' + actorUrl)
85 const video = await VideoModel.loadByUrlAndPopulateAccount(dislike.object, t)
86 if (!video) throw new Error('Unknown video ' + dislike.actor)
88 const rate = await AccountVideoRateModel.load(byAccount.id, video.id, t)
89 if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`)
91 await rate.destroy({ transaction: t })
92 await video.decrement('dislikes', { transaction: t })
94 if (video.isOwned()) {
95 // Don't resend the activity to the sender
96 const exceptions = [ byAccount.Actor ]
97 await forwardActivity(activity, t, exceptions)
102 function processUndoFollow (actorUrl: string, followActivity: ActivityFollow) {
104 arguments: [ actorUrl, followActivity ],
105 errorMessage: 'Cannot undo follow with many retries.'
108 return retryTransactionWrapper(undoFollow, options)
111 function undoFollow (actorUrl: string, followActivity: ActivityFollow) {
112 return sequelizeTypescript.transaction(async t => {
113 const follower = await ActorModel.loadByUrl(actorUrl, t)
114 const following = await ActorModel.loadByUrl(followActivity.object, t)
115 const actorFollow = await ActorFollowModel.loadByActorAndTarget(follower.id, following.id, t)
117 if (!actorFollow) throw new Error(`'Unknown actor follow ${follower.id} -> ${following.id}.`)
119 await actorFollow.destroy({ transaction: t })