]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/lib/activitypub/process/process-undo.ts
Add ability to forbid followers
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-undo.ts
... / ...
CommitLineData
1import { ActivityAnnounce, ActivityFollow, ActivityLike, ActivityUndo, CacheFileObject } from '../../../../shared/models/activitypub'
2import { DislikeObject } from '../../../../shared/models/activitypub/objects'
3import { retryTransactionWrapper } from '../../../helpers/database-utils'
4import { logger } from '../../../helpers/logger'
5import { sequelizeTypescript } from '../../../initializers'
6import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
7import { ActorModel } from '../../../models/activitypub/actor'
8import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
9import { forwardVideoRelatedActivity } from '../send/utils'
10import { getOrCreateVideoAndAccountAndChannel } from '../videos'
11import { VideoShareModel } from '../../../models/video/video-share'
12import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
13
14async function processUndoActivity (activity: ActivityUndo, byActor: ActorModel) {
15 const activityToUndo = activity.object
16
17 if (activityToUndo.type === 'Like') {
18 return retryTransactionWrapper(processUndoLike, byActor, activity)
19 }
20
21 if (activityToUndo.type === 'Create') {
22 if (activityToUndo.object.type === 'Dislike') {
23 return retryTransactionWrapper(processUndoDislike, byActor, activity)
24 } else if (activityToUndo.object.type === 'CacheFile') {
25 return retryTransactionWrapper(processUndoCacheFile, byActor, activity)
26 }
27 }
28
29 if (activityToUndo.type === 'Dislike') {
30 return retryTransactionWrapper(processUndoDislike, byActor, activity)
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
48export {
49 processUndoActivity
50}
51
52// ---------------------------------------------------------------------------
53
54async function processUndoLike (byActor: ActorModel, 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 if (!byActor.Account) throw new Error('Unknown account ' + byActor.url)
61
62 let rate = await AccountVideoRateModel.loadByUrl(likeActivity.id, t)
63 if (!rate) rate = await AccountVideoRateModel.load(byActor.Account.id, video.id, t)
64 if (!rate) throw new Error(`Unknown rate by account ${byActor.Account.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 = [ byActor ]
72
73 await forwardVideoRelatedActivity(activity, t, exceptions, video)
74 }
75 })
76}
77
78async function processUndoDislike (byActor: ActorModel, activity: ActivityUndo) {
79 const dislike = activity.object.type === 'Dislike'
80 ? activity.object
81 : activity.object.object as DislikeObject
82
83 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: dislike.object })
84
85 return sequelizeTypescript.transaction(async t => {
86 if (!byActor.Account) throw new Error('Unknown account ' + byActor.url)
87
88 let rate = await AccountVideoRateModel.loadByUrl(dislike.id, t)
89 if (!rate) rate = await AccountVideoRateModel.load(byActor.Account.id, video.id, t)
90 if (!rate) throw new Error(`Unknown rate by account ${byActor.Account.id} for video ${video.id}.`)
91
92 await rate.destroy({ transaction: t })
93 await video.decrement('dislikes', { transaction: t })
94
95 if (video.isOwned()) {
96 // Don't resend the activity to the sender
97 const exceptions = [ byActor ]
98
99 await forwardVideoRelatedActivity(activity, t, exceptions, video)
100 }
101 })
102}
103
104async function processUndoCacheFile (byActor: ActorModel, activity: ActivityUndo) {
105 const cacheFileObject = activity.object.object as CacheFileObject
106
107 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.object })
108
109 return sequelizeTypescript.transaction(async t => {
110 const cacheFile = await VideoRedundancyModel.loadByUrl(cacheFileObject.id)
111 if (!cacheFile) throw new Error('Unknown video cache ' + cacheFileObject.id)
112
113 if (cacheFile.actorId !== byActor.id) throw new Error('Cannot delete redundancy ' + cacheFile.url + ' of another actor.')
114
115 await cacheFile.destroy()
116
117 if (video.isOwned()) {
118 // Don't resend the activity to the sender
119 const exceptions = [ byActor ]
120
121 await forwardVideoRelatedActivity(activity, t, exceptions, video)
122 }
123 })
124}
125
126function processUndoFollow (follower: ActorModel, followActivity: ActivityFollow) {
127 return sequelizeTypescript.transaction(async t => {
128 const following = await ActorModel.loadByUrlAndPopulateAccountAndChannel(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
139function processUndoAnnounce (byActor: ActorModel, announceActivity: ActivityAnnounce) {
140 return sequelizeTypescript.transaction(async t => {
141 const share = await VideoShareModel.loadByUrl(announceActivity.id, t)
142 if (!share) throw new Error(`Unknown video share ${announceActivity.id}.`)
143
144 if (share.actorId !== byActor.id) throw new Error(`${share.url} is not shared by ${byActor.url}.`)
145
146 await share.destroy({ transaction: t })
147
148 if (share.Video.isOwned()) {
149 // Don't resend the activity to the sender
150 const exceptions = [ byActor ]
151
152 await forwardVideoRelatedActivity(announceActivity, t, exceptions, share.Video)
153 }
154 })
155}