]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/process-undo.ts
Merge branch 'release/5.1.0' into develop
[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') {
69 logger.warn('Unknown like by account %d for video %d.', byActor.Account.id, video.id)
70 return
71 }
72
73 await rate.destroy({ transaction: t })
74 await video.decrement('likes', { transaction: t })
75
76 video.likes--
77 await federateVideoIfNeeded(video, false, t)
78 })
79 }
80
81 async function processUndoDislike (byActor: MActorSignature, activity: ActivityUndo) {
82 const dislike = activity.object.type === 'Dislike'
83 ? activity.object
84 : activity.object.object as DislikeObject
85
86 const { video: onlyVideo } = await getOrCreateAPVideo({ videoObject: dislike.object })
87 // We don't care about likes of remote videos
88 if (!onlyVideo.isOwned()) return
89
90 return sequelizeTypescript.transaction(async t => {
91 if (!byActor.Account) throw new Error('Unknown account ' + byActor.url)
92
93 const video = await VideoModel.loadFull(onlyVideo.id, t)
94 const rate = await AccountVideoRateModel.loadByAccountAndVideoOrUrl(byActor.Account.id, video.id, dislike.id, t)
95 if (!rate || rate.type !== 'dislike') {
96 logger.warn(`Unknown dislike by account %d for video %d.`, byActor.Account.id, video.id)
97 return
98 }
99
100 await rate.destroy({ transaction: t })
101 await video.decrement('dislikes', { transaction: t })
102 video.dislikes--
103
104 await federateVideoIfNeeded(video, false, t)
105 })
106 }
107
108 // ---------------------------------------------------------------------------
109
110 async function processUndoCacheFile (byActor: MActorSignature, activity: ActivityUndo) {
111 const cacheFileObject = activity.object.object as CacheFileObject
112
113 const { video } = await getOrCreateAPVideo({ videoObject: cacheFileObject.object })
114
115 return sequelizeTypescript.transaction(async t => {
116 const cacheFile = await VideoRedundancyModel.loadByUrl(cacheFileObject.id, t)
117 if (!cacheFile) {
118 logger.debug('Cannot undo unknown video cache %s.', cacheFileObject.id)
119 return
120 }
121
122 if (cacheFile.actorId !== byActor.id) throw new Error('Cannot delete redundancy ' + cacheFile.url + ' of another actor.')
123
124 await cacheFile.destroy({ transaction: t })
125
126 if (video.isOwned()) {
127 // Don't resend the activity to the sender
128 const exceptions = [ byActor ]
129
130 await forwardVideoRelatedActivity(activity, t, exceptions, video)
131 }
132 })
133 }
134
135 function processUndoAnnounce (byActor: MActorSignature, announceActivity: ActivityAnnounce) {
136 return sequelizeTypescript.transaction(async t => {
137 const share = await VideoShareModel.loadByUrl(announceActivity.id, t)
138 if (!share) {
139 logger.warn('Unknown video share %d', announceActivity.id)
140 return
141 }
142
143 if (share.actorId !== byActor.id) throw new Error(`${share.url} is not shared by ${byActor.url}.`)
144
145 await share.destroy({ transaction: t })
146
147 if (share.Video.isOwned()) {
148 // Don't resend the activity to the sender
149 const exceptions = [ byActor ]
150
151 await forwardVideoRelatedActivity(announceActivity, t, exceptions, share.Video)
152 }
153 })
154 }
155
156 // ---------------------------------------------------------------------------
157
158 function processUndoFollow (follower: MActorSignature, followActivity: ActivityFollow) {
159 return sequelizeTypescript.transaction(async t => {
160 const following = await ActorModel.loadByUrlAndPopulateAccountAndChannel(followActivity.object, t)
161 const actorFollow = await ActorFollowModel.loadByActorAndTarget(follower.id, following.id, t)
162
163 if (!actorFollow) {
164 logger.warn('Unknown actor follow %d -> %d.', follower.id, following.id)
165 return
166 }
167
168 await actorFollow.destroy({ transaction: t })
169
170 return undefined
171 })
172 }