1 import { ActivityAnnounce, ActivityFollow, ActivityLike, ActivityUndo, CacheFileObject } 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 { AccountVideoRateModel } from '../../../models/account/account-video-rate'
7 import { ActorModel } from '../../../models/activitypub/actor'
8 import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
9 import { forwardVideoRelatedActivity } from '../send/utils'
10 import { getOrCreateVideoAndAccountAndChannel } from '../videos'
11 import { VideoShareModel } from '../../../models/video/video-share'
12 import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
14 async function processUndoActivity (activity: ActivityUndo, byActor: ActorModel) {
15 const activityToUndo = activity.object
17 if (activityToUndo.type === 'Like') {
18 return retryTransactionWrapper(processUndoLike, byActor, activity)
21 if (activityToUndo.type === 'Create') {
22 if (activityToUndo.object.type === 'Dislike') {
23 return retryTransactionWrapper(processUndoDislike, byActor, activity)
24 } else if (activityToUndo.object.type === 'CacheFile') {
25 return retryTransactionWrapper(processUndoCacheFile, byActor, activity)
29 if (activityToUndo.type === 'Follow') {
30 return retryTransactionWrapper(processUndoFollow, byActor, activityToUndo)
33 if (activityToUndo.type === 'Announce') {
34 return retryTransactionWrapper(processUndoAnnounce, byActor, activityToUndo)
37 logger.warn('Unknown activity object type %s -> %s when undo activity.', activityToUndo.type, { activity: activity.id })
42 // ---------------------------------------------------------------------------
48 // ---------------------------------------------------------------------------
50 async function processUndoLike (byActor: ActorModel, activity: ActivityUndo) {
51 const likeActivity = activity.object as ActivityLike
53 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: likeActivity.object })
55 return sequelizeTypescript.transaction(async t => {
56 if (!byActor.Account) throw new Error('Unknown account ' + byActor.url)
58 let rate = await AccountVideoRateModel.loadByUrl(likeActivity.id, t)
59 if (!rate) rate = await AccountVideoRateModel.load(byActor.Account.id, video.id, t)
60 if (!rate) throw new Error(`Unknown rate by account ${byActor.Account.id} for video ${video.id}.`)
62 await rate.destroy({ transaction: t })
63 await video.decrement('likes', { transaction: t })
65 if (video.isOwned()) {
66 // Don't resend the activity to the sender
67 const exceptions = [ byActor ]
69 await forwardVideoRelatedActivity(activity, t, exceptions, video)
74 async function processUndoDislike (byActor: ActorModel, activity: ActivityUndo) {
75 const dislike = activity.object.object as DislikeObject
77 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: dislike.object })
79 return sequelizeTypescript.transaction(async t => {
80 if (!byActor.Account) throw new Error('Unknown account ' + byActor.url)
82 let rate = await AccountVideoRateModel.loadByUrl(dislike.id, t)
83 if (!rate) rate = await AccountVideoRateModel.load(byActor.Account.id, video.id, t)
84 if (!rate) throw new Error(`Unknown rate by account ${byActor.Account.id} for video ${video.id}.`)
86 await rate.destroy({ transaction: t })
87 await video.decrement('dislikes', { transaction: t })
89 if (video.isOwned()) {
90 // Don't resend the activity to the sender
91 const exceptions = [ byActor ]
93 await forwardVideoRelatedActivity(activity, t, exceptions, video)
98 async function processUndoCacheFile (byActor: ActorModel, activity: ActivityUndo) {
99 const cacheFileObject = activity.object.object as CacheFileObject
101 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.object })
103 return sequelizeTypescript.transaction(async t => {
104 const cacheFile = await VideoRedundancyModel.loadByUrl(cacheFileObject.id)
105 if (!cacheFile) throw new Error('Unknown video cache ' + cacheFileObject.id)
107 if (cacheFile.actorId !== byActor.id) throw new Error('Cannot delete redundancy ' + cacheFile.url + ' of another actor.')
109 await cacheFile.destroy()
111 if (video.isOwned()) {
112 // Don't resend the activity to the sender
113 const exceptions = [ byActor ]
115 await forwardVideoRelatedActivity(activity, t, exceptions, video)
120 function processUndoFollow (follower: ActorModel, followActivity: ActivityFollow) {
121 return sequelizeTypescript.transaction(async t => {
122 const following = await ActorModel.loadByUrlAndPopulateAccountAndChannel(followActivity.object, t)
123 const actorFollow = await ActorFollowModel.loadByActorAndTarget(follower.id, following.id, t)
125 if (!actorFollow) throw new Error(`'Unknown actor follow ${follower.id} -> ${following.id}.`)
127 await actorFollow.destroy({ transaction: t })
133 function processUndoAnnounce (byActor: ActorModel, announceActivity: ActivityAnnounce) {
134 return sequelizeTypescript.transaction(async t => {
135 const share = await VideoShareModel.loadByUrl(announceActivity.id, t)
136 if (!share) throw new Error(`Unknown video share ${announceActivity.id}.`)
138 if (share.actorId !== byActor.id) throw new Error(`${share.url} is not shared by ${byActor.url}.`)
140 await share.destroy({ transaction: t })
142 if (share.Video.isOwned()) {
143 // Don't resend the activity to the sender
144 const exceptions = [ byActor ]
146 await forwardVideoRelatedActivity(announceActivity, t, exceptions, share.Video)