1 import { ActivityAnnounce, ActivityFollow, ActivityLike, ActivityUndo, CacheFileObject } 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'
14 import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
16 async function processUndoActivity (activity: ActivityUndo, byActor: ActorModel) {
17 const activityToUndo = activity.object
19 const actorUrl = getActorUrl(activity.actor)
21 if (activityToUndo.type === 'Like') {
22 return retryTransactionWrapper(processUndoLike, actorUrl, activity)
25 if (activityToUndo.type === 'Create') {
26 if (activityToUndo.object.type === 'Dislike') {
27 return retryTransactionWrapper(processUndoDislike, actorUrl, activity)
28 } else if (activityToUndo.object.type === 'CacheFile') {
29 return retryTransactionWrapper(processUndoCacheFile, byActor, activity)
33 if (activityToUndo.type === 'Follow') {
34 return retryTransactionWrapper(processUndoFollow, byActor, activityToUndo)
37 if (activityToUndo.type === 'Announce') {
38 return retryTransactionWrapper(processUndoAnnounce, byActor, activityToUndo)
41 logger.warn('Unknown activity object type %s -> %s when undo activity.', activityToUndo.type, { activity: activity.id })
46 // ---------------------------------------------------------------------------
52 // ---------------------------------------------------------------------------
54 async function processUndoLike (actorUrl: string, activity: ActivityUndo) {
55 const likeActivity = activity.object as ActivityLike
57 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: likeActivity.object })
59 return sequelizeTypescript.transaction(async t => {
60 const byAccount = await AccountModel.loadByUrl(actorUrl, t)
61 if (!byAccount) throw new Error('Unknown account ' + actorUrl)
63 const rate = await AccountVideoRateModel.load(byAccount.id, video.id, t)
64 if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`)
66 await rate.destroy({ transaction: t })
67 await video.decrement('likes', { transaction: t })
69 if (video.isOwned()) {
70 // Don't resend the activity to the sender
71 const exceptions = [ byAccount.Actor ]
73 await forwardVideoRelatedActivity(activity, t, exceptions, video)
78 async function processUndoDislike (actorUrl: string, activity: ActivityUndo) {
79 const dislike = activity.object.object as DislikeObject
81 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: 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 ]
97 await forwardVideoRelatedActivity(activity, t, exceptions, video)
102 async function processUndoCacheFile (byActor: ActorModel, activity: ActivityUndo) {
103 const cacheFileObject = activity.object.object as CacheFileObject
105 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.object })
107 return sequelizeTypescript.transaction(async t => {
108 const cacheFile = await VideoRedundancyModel.loadByUrl(cacheFileObject.id)
109 if (!cacheFile) throw new Error('Unknown video cache ' + cacheFile.url)
111 await cacheFile.destroy()
113 if (video.isOwned()) {
114 // Don't resend the activity to the sender
115 const exceptions = [ byActor ]
117 await forwardVideoRelatedActivity(activity, t, exceptions, video)
122 function processUndoFollow (follower: ActorModel, followActivity: ActivityFollow) {
123 return sequelizeTypescript.transaction(async t => {
124 const following = await ActorModel.loadByUrlAndPopulateAccountAndChannel(followActivity.object, t)
125 const actorFollow = await ActorFollowModel.loadByActorAndTarget(follower.id, following.id, t)
127 if (!actorFollow) throw new Error(`'Unknown actor follow ${follower.id} -> ${following.id}.`)
129 await actorFollow.destroy({ transaction: t })
135 function processUndoAnnounce (byActor: ActorModel, announceActivity: ActivityAnnounce) {
136 return sequelizeTypescript.transaction(async t => {
137 const share = await VideoShareModel.loadByUrl(announceActivity.id, t)
138 if (!share) throw new Error(`Unknown video share ${announceActivity.id}.`)
140 if (share.actorId !== byActor.id) throw new Error(`${share.url} is not shared by ${byActor.url}.`)
142 await share.destroy({ transaction: t })
144 if (share.Video.isOwned()) {
145 // Don't resend the activity to the sender
146 const exceptions = [ byActor ]
148 await forwardVideoRelatedActivity(announceActivity, t, exceptions, share.Video)