]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/process-undo.ts
778a38e05fba2aeffa24f0722e04d4fdf4ed25cd
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-undo.ts
1 import { VideoModel } from '@server/models/video/video'
2 import { ActivityAnnounce, ActivityFollow, ActivityLike, ActivityUndo, CacheFileObject } from '../../../../shared/models/activitypub'
3 import { DislikeObject } from '../../../../shared/models/activitypub/objects'
4 import { retryTransactionWrapper } from '../../../helpers/database-utils'
5 import { logger } from '../../../helpers/logger'
6 import { sequelizeTypescript } from '../../../initializers/database'
7 import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
8 import { ActorModel } from '../../../models/actor/actor'
9 import { ActorFollowModel } from '../../../models/actor/actor-follow'
10 import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
11 import { VideoShareModel } from '../../../models/video/video-share'
12 import { APProcessorOptions } from '../../../types/activitypub-processor.model'
13 import { MActorSignature } from '../../../types/models'
14 import { forwardVideoRelatedActivity } from '../send/shared/send-utils'
15 import { federateVideoIfNeeded, getOrCreateAPVideo } from '../videos'
16
17 async function processUndoActivity (options: APProcessorOptions<ActivityUndo>) {
18 const { activity, byActor } = options
19 const activityToUndo = activity.object
20
21 if (activityToUndo.type === 'Like') {
22 return retryTransactionWrapper(processUndoLike, byActor, activity)
23 }
24
25 if (activityToUndo.type === 'Create') {
26 if (activityToUndo.object.type === 'CacheFile') {
27 return retryTransactionWrapper(processUndoCacheFile, byActor, activity)
28 }
29 }
30
31 if (activityToUndo.type === 'Dislike') {
32 return retryTransactionWrapper(processUndoDislike, byActor, activity)
33 }
34
35 if (activityToUndo.type === 'Follow') {
36 return retryTransactionWrapper(processUndoFollow, byActor, activityToUndo)
37 }
38
39 if (activityToUndo.type === 'Announce') {
40 return retryTransactionWrapper(processUndoAnnounce, byActor, activityToUndo)
41 }
42
43 logger.warn('Unknown activity object type %s -> %s when undo activity.', activityToUndo.type, { activity: activity.id })
44
45 return undefined
46 }
47
48 // ---------------------------------------------------------------------------
49
50 export {
51 processUndoActivity
52 }
53
54 // ---------------------------------------------------------------------------
55
56 async function processUndoLike (byActor: MActorSignature, activity: ActivityUndo) {
57 const likeActivity = activity.object as ActivityLike
58
59 const { video: onlyVideo } = await getOrCreateAPVideo({ videoObject: likeActivity.object })
60 // We don't care about likes of remote videos
61 if (!onlyVideo.isOwned()) return
62
63 return sequelizeTypescript.transaction(async t => {
64 if (!byActor.Account) throw new Error('Unknown account ' + byActor.url)
65
66 const video = await VideoModel.loadFull(onlyVideo.id, t)
67 const rate = await AccountVideoRateModel.loadByAccountAndVideoOrUrl(byActor.Account.id, video.id, likeActivity.id, t)
68 if (!rate || rate.type !== 'like') throw new Error(`Unknown like by account ${byActor.Account.id} for video ${video.id}.`)
69
70 await rate.destroy({ transaction: t })
71 await video.decrement('likes', { transaction: t })
72
73 video.likes--
74 await federateVideoIfNeeded(video, false, t)
75 })
76 }
77
78 async function processUndoDislike (byActor: MActorSignature, activity: ActivityUndo) {
79 const dislike = activity.object.type === 'Dislike'
80 ? activity.object
81 : activity.object.object as DislikeObject
82
83 const { video: onlyVideo } = await getOrCreateAPVideo({ videoObject: dislike.object })
84 // We don't care about likes of remote videos
85 if (!onlyVideo.isOwned()) return
86
87 return sequelizeTypescript.transaction(async t => {
88 if (!byActor.Account) throw new Error('Unknown account ' + byActor.url)
89
90 const video = await VideoModel.loadFull(onlyVideo.id, t)
91 const rate = await AccountVideoRateModel.loadByAccountAndVideoOrUrl(byActor.Account.id, video.id, dislike.id, t)
92 if (!rate || rate.type !== 'dislike') throw new Error(`Unknown dislike by account ${byActor.Account.id} for video ${video.id}.`)
93
94 await rate.destroy({ transaction: t })
95 await video.decrement('dislikes', { transaction: t })
96 video.dislikes--
97
98 await federateVideoIfNeeded(video, false, t)
99 })
100 }
101
102 // ---------------------------------------------------------------------------
103
104 async function processUndoCacheFile (byActor: MActorSignature, activity: ActivityUndo) {
105 const cacheFileObject = activity.object.object as CacheFileObject
106
107 const { video } = await getOrCreateAPVideo({ videoObject: cacheFileObject.object })
108
109 return sequelizeTypescript.transaction(async t => {
110 const cacheFile = await VideoRedundancyModel.loadByUrl(cacheFileObject.id, t)
111 if (!cacheFile) {
112 logger.debug('Cannot undo unknown video cache %s.', cacheFileObject.id)
113 return
114 }
115
116 if (cacheFile.actorId !== byActor.id) throw new Error('Cannot delete redundancy ' + cacheFile.url + ' of another actor.')
117
118 await cacheFile.destroy({ transaction: t })
119
120 if (video.isOwned()) {
121 // Don't resend the activity to the sender
122 const exceptions = [ byActor ]
123
124 await forwardVideoRelatedActivity(activity, t, exceptions, video)
125 }
126 })
127 }
128
129 function processUndoAnnounce (byActor: MActorSignature, announceActivity: ActivityAnnounce) {
130 return sequelizeTypescript.transaction(async t => {
131 const share = await VideoShareModel.loadByUrl(announceActivity.id, t)
132 if (!share) throw new Error(`Unknown video share ${announceActivity.id}.`)
133
134 if (share.actorId !== byActor.id) throw new Error(`${share.url} is not shared by ${byActor.url}.`)
135
136 await share.destroy({ transaction: t })
137
138 if (share.Video.isOwned()) {
139 // Don't resend the activity to the sender
140 const exceptions = [ byActor ]
141
142 await forwardVideoRelatedActivity(announceActivity, t, exceptions, share.Video)
143 }
144 })
145 }
146
147 // ---------------------------------------------------------------------------
148
149 function processUndoFollow (follower: MActorSignature, followActivity: ActivityFollow) {
150 return sequelizeTypescript.transaction(async t => {
151 const following = await ActorModel.loadByUrlAndPopulateAccountAndChannel(followActivity.object, t)
152 const actorFollow = await ActorFollowModel.loadByActorAndTarget(follower.id, following.id, t)
153
154 if (!actorFollow) throw new Error(`'Unknown actor follow ${follower.id} -> ${following.id}.`)
155
156 await actorFollow.destroy({ transaction: t })
157
158 return undefined
159 })
160 }