]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/process-undo.ts
Optimize activity actor load in AP processors
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-undo.ts
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'
15
16 async function processUndoActivity (activity: ActivityUndo, byActor: ActorModel) {
17 const activityToUndo = activity.object
18
19 const actorUrl = getActorUrl(activity.actor)
20
21 if (activityToUndo.type === 'Like') {
22 return retryTransactionWrapper(processUndoLike, actorUrl, activity)
23 }
24
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)
30 }
31 }
32
33 if (activityToUndo.type === 'Follow') {
34 return retryTransactionWrapper(processUndoFollow, byActor, activityToUndo)
35 }
36
37 if (activityToUndo.type === 'Announce') {
38 return retryTransactionWrapper(processUndoAnnounce, byActor, activityToUndo)
39 }
40
41 logger.warn('Unknown activity object type %s -> %s when undo activity.', activityToUndo.type, { activity: activity.id })
42
43 return undefined
44 }
45
46 // ---------------------------------------------------------------------------
47
48 export {
49 processUndoActivity
50 }
51
52 // ---------------------------------------------------------------------------
53
54 async function processUndoLike (actorUrl: string, activity: ActivityUndo) {
55 const likeActivity = activity.object as ActivityLike
56
57 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: likeActivity.object })
58
59 return sequelizeTypescript.transaction(async t => {
60 const byAccount = await AccountModel.loadByUrl(actorUrl, t)
61 if (!byAccount) throw new Error('Unknown account ' + actorUrl)
62
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}.`)
65
66 await rate.destroy({ transaction: t })
67 await video.decrement('likes', { transaction: t })
68
69 if (video.isOwned()) {
70 // Don't resend the activity to the sender
71 const exceptions = [ byAccount.Actor ]
72
73 await forwardVideoRelatedActivity(activity, t, exceptions, video)
74 }
75 })
76 }
77
78 async function processUndoDislike (actorUrl: string, activity: ActivityUndo) {
79 const dislike = activity.object.object as DislikeObject
80
81 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: dislike.object })
82
83 return sequelizeTypescript.transaction(async t => {
84 const byAccount = await AccountModel.loadByUrl(actorUrl, t)
85 if (!byAccount) throw new Error('Unknown account ' + actorUrl)
86
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}.`)
89
90 await rate.destroy({ transaction: t })
91 await video.decrement('dislikes', { transaction: t })
92
93 if (video.isOwned()) {
94 // Don't resend the activity to the sender
95 const exceptions = [ byAccount.Actor ]
96
97 await forwardVideoRelatedActivity(activity, t, exceptions, video)
98 }
99 })
100 }
101
102 async function processUndoCacheFile (byActor: ActorModel, activity: ActivityUndo) {
103 const cacheFileObject = activity.object.object as CacheFileObject
104
105 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.object })
106
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)
110
111 await cacheFile.destroy()
112
113 if (video.isOwned()) {
114 // Don't resend the activity to the sender
115 const exceptions = [ byActor ]
116
117 await forwardVideoRelatedActivity(activity, t, exceptions, video)
118 }
119 })
120 }
121
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)
126
127 if (!actorFollow) throw new Error(`'Unknown actor follow ${follower.id} -> ${following.id}.`)
128
129 await actorFollow.destroy({ transaction: t })
130
131 return undefined
132 })
133 }
134
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}.`)
139
140 if (share.actorId !== byActor.id) throw new Error(`${share.url} is not shared by ${byActor.url}.`)
141
142 await share.destroy({ transaction: t })
143
144 if (share.Video.isOwned()) {
145 // Don't resend the activity to the sender
146 const exceptions = [ byActor ]
147
148 await forwardVideoRelatedActivity(announceActivity, t, exceptions, share.Video)
149 }
150 })
151 }