1 import { ActivityAnnounce, ActivityFollow, ActivityLike, ActivityUndo } from '../../../../shared/models/activitypub'
2 import { DislikeObject } from '../../../../shared/models/activitypub/objects'
3 import { getActorUrl } from '../../../helpers/activitypub'
4 import { retryTransactionWrapper } from '../../../helpers/database-utils'
5 import { logger } from '../../../helpers/logger'
6 import { sequelizeTypescript } from '../../../initializers'
7 import { AccountModel } from '../../../models/account/account'
8 import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
9 import { ActorModel } from '../../../models/activitypub/actor'
10 import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
11 import { forwardVideoRelatedActivity } from '../send/utils'
12 import { getOrCreateVideoAndAccountAndChannel } from '../videos'
13 import { VideoShareModel } from '../../../models/video/video-share'
15 async function processUndoActivity (activity: ActivityUndo) {
16 const activityToUndo = activity.object
18 const actorUrl = getActorUrl(activity.actor)
20 if (activityToUndo.type === 'Like') {
21 return retryTransactionWrapper(processUndoLike, actorUrl, activity)
22 } else if (activityToUndo.type === 'Create' && activityToUndo.object.type === 'Dislike') {
23 return retryTransactionWrapper(processUndoDislike, actorUrl, activity)
24 } else if (activityToUndo.type === 'Follow') {
25 return retryTransactionWrapper(processUndoFollow, actorUrl, activityToUndo)
26 } else if (activityToUndo.type === 'Announce') {
27 return retryTransactionWrapper(processUndoAnnounce, actorUrl, activityToUndo)
30 logger.warn('Unknown activity object type %s -> %s when undo activity.', activityToUndo.type, { activity: activity.id })
35 // ---------------------------------------------------------------------------
41 // ---------------------------------------------------------------------------
43 async function processUndoLike (actorUrl: string, activity: ActivityUndo) {
44 const likeActivity = activity.object as ActivityLike
46 const { video } = await getOrCreateVideoAndAccountAndChannel(likeActivity.object)
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 rate = await AccountVideoRateModel.load(byAccount.id, video.id, t)
53 if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`)
55 await rate.destroy({ transaction: t })
56 await video.decrement('likes', { transaction: t })
58 if (video.isOwned()) {
59 // Don't resend the activity to the sender
60 const exceptions = [ byAccount.Actor ]
62 await forwardVideoRelatedActivity(activity, t, exceptions, video)
67 async function processUndoDislike (actorUrl: string, activity: ActivityUndo) {
68 const dislike = activity.object.object as DislikeObject
70 const { video } = await getOrCreateVideoAndAccountAndChannel(dislike.object)
72 return sequelizeTypescript.transaction(async t => {
73 const byAccount = await AccountModel.loadByUrl(actorUrl, t)
74 if (!byAccount) throw new Error('Unknown account ' + actorUrl)
76 const rate = await AccountVideoRateModel.load(byAccount.id, video.id, t)
77 if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`)
79 await rate.destroy({ transaction: t })
80 await video.decrement('dislikes', { transaction: t })
82 if (video.isOwned()) {
83 // Don't resend the activity to the sender
84 const exceptions = [ byAccount.Actor ]
86 await forwardVideoRelatedActivity(activity, t, exceptions, video)
91 function processUndoFollow (actorUrl: string, followActivity: ActivityFollow) {
92 return sequelizeTypescript.transaction(async t => {
93 const follower = await ActorModel.loadByUrl(actorUrl, t)
94 const following = await ActorModel.loadByUrl(followActivity.object, t)
95 const actorFollow = await ActorFollowModel.loadByActorAndTarget(follower.id, following.id, t)
97 if (!actorFollow) throw new Error(`'Unknown actor follow ${follower.id} -> ${following.id}.`)
99 await actorFollow.destroy({ transaction: t })
105 function processUndoAnnounce (actorUrl: string, announceActivity: ActivityAnnounce) {
106 return sequelizeTypescript.transaction(async t => {
107 const byAccount = await AccountModel.loadByUrl(actorUrl, t)
108 if (!byAccount) throw new Error('Unknown account ' + actorUrl)
110 const share = await VideoShareModel.loadByUrl(announceActivity.id, t)
111 if (!share) throw new Error(`'Unknown video share ${announceActivity.id}.`)
113 await share.destroy({ transaction: t })
115 if (share.Video.isOwned()) {
116 // Don't resend the activity to the sender
117 const exceptions = [ byAccount.Actor ]
119 await forwardVideoRelatedActivity(announceActivity, t, exceptions, share.Video)