]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/process-undo.ts
Refractor videos AP functions
[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) {
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, actorUrl, activity)
30 }
31 }
32
33 if (activityToUndo.type === 'Follow') {
34 return retryTransactionWrapper(processUndoFollow, actorUrl, activityToUndo)
35 }
36
37 if (activityToUndo.type === 'Announce') {
38 return retryTransactionWrapper(processUndoAnnounce, actorUrl, 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 (actorUrl: string, 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 byActor = await ActorModel.loadByUrl(actorUrl)
109 if (!byActor) throw new Error('Unknown actor ' + actorUrl)
110
111 const cacheFile = await VideoRedundancyModel.loadByUrl(cacheFileObject.id)
112 if (!cacheFile) throw new Error('Unknown video cache ' + cacheFile.url)
113
114 await cacheFile.destroy()
115
116 if (video.isOwned()) {
117 // Don't resend the activity to the sender
118 const exceptions = [ byActor ]
119
120 await forwardVideoRelatedActivity(activity, t, exceptions, video)
121 }
122 })
123 }
124
125 function processUndoFollow (actorUrl: string, followActivity: ActivityFollow) {
126 return sequelizeTypescript.transaction(async t => {
127 const follower = await ActorModel.loadByUrl(actorUrl, t)
128 const following = await ActorModel.loadByUrl(followActivity.object, t)
129 const actorFollow = await ActorFollowModel.loadByActorAndTarget(follower.id, following.id, t)
130
131 if (!actorFollow) throw new Error(`'Unknown actor follow ${follower.id} -> ${following.id}.`)
132
133 await actorFollow.destroy({ transaction: t })
134
135 return undefined
136 })
137 }
138
139 function processUndoAnnounce (actorUrl: string, announceActivity: ActivityAnnounce) {
140 return sequelizeTypescript.transaction(async t => {
141 const byActor = await ActorModel.loadByUrl(actorUrl, t)
142 if (!byActor) throw new Error('Unknown actor ' + actorUrl)
143
144 const share = await VideoShareModel.loadByUrl(announceActivity.id, t)
145 if (!share) throw new Error(`Unknown video share ${announceActivity.id}.`)
146
147 if (share.actorId !== byActor.id) throw new Error(`${share.url} is not shared by ${byActor.url}.`)
148
149 await share.destroy({ transaction: t })
150
151 if (share.Video.isOwned()) {
152 // Don't resend the activity to the sender
153 const exceptions = [ byActor ]
154
155 await forwardVideoRelatedActivity(announceActivity, t, exceptions, share.Video)
156 }
157 })
158 }