]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-undo.ts
Add ability to forbid followers
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-undo.ts
CommitLineData
c48e82b5 1import { ActivityAnnounce, ActivityFollow, ActivityLike, ActivityUndo, CacheFileObject } from '../../../../shared/models/activitypub'
3fd3ab2d 2import { DislikeObject } from '../../../../shared/models/activitypub/objects'
da854ddd
C
3import { retryTransactionWrapper } from '../../../helpers/database-utils'
4import { logger } from '../../../helpers/logger'
3fd3ab2d 5import { sequelizeTypescript } from '../../../initializers'
3fd3ab2d 6import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
50d6de9c
C
7import { ActorModel } from '../../../models/activitypub/actor'
8import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
9588d4f4 9import { forwardVideoRelatedActivity } from '../send/utils'
1297eb5d 10import { getOrCreateVideoAndAccountAndChannel } from '../videos'
0f320037 11import { VideoShareModel } from '../../../models/video/video-share'
c48e82b5 12import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
54141398 13
e587e0ec 14async function processUndoActivity (activity: ActivityUndo, byActor: ActorModel) {
54141398
C
15 const activityToUndo = activity.object
16
0032ebe9 17 if (activityToUndo.type === 'Like') {
12ba460e 18 return retryTransactionWrapper(processUndoLike, byActor, activity)
c48e82b5
C
19 }
20
21 if (activityToUndo.type === 'Create') {
22 if (activityToUndo.object.type === 'Dislike') {
12ba460e 23 return retryTransactionWrapper(processUndoDislike, byActor, activity)
c48e82b5 24 } else if (activityToUndo.object.type === 'CacheFile') {
e587e0ec 25 return retryTransactionWrapper(processUndoCacheFile, byActor, activity)
c48e82b5
C
26 }
27 }
28
848f499d
C
29 if (activityToUndo.type === 'Dislike') {
30 return retryTransactionWrapper(processUndoDislike, byActor, activity)
31 }
32
c48e82b5 33 if (activityToUndo.type === 'Follow') {
e587e0ec 34 return retryTransactionWrapper(processUndoFollow, byActor, activityToUndo)
c48e82b5
C
35 }
36
37 if (activityToUndo.type === 'Announce') {
e587e0ec 38 return retryTransactionWrapper(processUndoAnnounce, byActor, activityToUndo)
54141398
C
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// ---------------------------------------------------------------------------
0032ebe9 53
12ba460e 54async function processUndoLike (byActor: ActorModel, activity: ActivityUndo) {
63c93323
C
55 const likeActivity = activity.object as ActivityLike
56
4157cdb1 57 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: likeActivity.object })
2ccaeeb3 58
3fd3ab2d 59 return sequelizeTypescript.transaction(async t => {
12ba460e 60 if (!byActor.Account) throw new Error('Unknown account ' + byActor.url)
0032ebe9 61
5c6d985f
C
62 let rate = await AccountVideoRateModel.loadByUrl(likeActivity.id, t)
63 if (!rate) rate = await AccountVideoRateModel.load(byActor.Account.id, video.id, t)
12ba460e 64 if (!rate) throw new Error(`Unknown rate by account ${byActor.Account.id} for video ${video.id}.`)
0032ebe9
C
65
66 await rate.destroy({ transaction: t })
63c93323 67 await video.decrement('likes', { transaction: t })
0032ebe9 68
63c93323
C
69 if (video.isOwned()) {
70 // Don't resend the activity to the sender
12ba460e 71 const exceptions = [ byActor ]
9588d4f4
C
72
73 await forwardVideoRelatedActivity(activity, t, exceptions, video)
63c93323 74 }
0032ebe9
C
75 })
76}
77
12ba460e 78async function processUndoDislike (byActor: ActorModel, activity: ActivityUndo) {
848f499d
C
79 const dislike = activity.object.type === 'Dislike'
80 ? activity.object
81 : activity.object.object as DislikeObject
63c93323 82
4157cdb1 83 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: dislike.object })
2ccaeeb3 84
3fd3ab2d 85 return sequelizeTypescript.transaction(async t => {
12ba460e 86 if (!byActor.Account) throw new Error('Unknown account ' + byActor.url)
0032ebe9 87
5c6d985f
C
88 let rate = await AccountVideoRateModel.loadByUrl(dislike.id, t)
89 if (!rate) rate = await AccountVideoRateModel.load(byActor.Account.id, video.id, t)
12ba460e 90 if (!rate) throw new Error(`Unknown rate by account ${byActor.Account.id} for video ${video.id}.`)
0032ebe9
C
91
92 await rate.destroy({ transaction: t })
63c93323 93 await video.decrement('dislikes', { transaction: t })
0032ebe9 94
63c93323
C
95 if (video.isOwned()) {
96 // Don't resend the activity to the sender
12ba460e 97 const exceptions = [ byActor ]
9588d4f4
C
98
99 await forwardVideoRelatedActivity(activity, t, exceptions, video)
63c93323 100 }
0032ebe9
C
101 })
102}
103
e587e0ec 104async function processUndoCacheFile (byActor: ActorModel, activity: ActivityUndo) {
c48e82b5
C
105 const cacheFileObject = activity.object.object as CacheFileObject
106
4157cdb1 107 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.object })
c48e82b5
C
108
109 return sequelizeTypescript.transaction(async t => {
c48e82b5 110 const cacheFile = await VideoRedundancyModel.loadByUrl(cacheFileObject.id)
e5565833 111 if (!cacheFile) throw new Error('Unknown video cache ' + cacheFileObject.id)
c48e82b5 112
12ba460e
C
113 if (cacheFile.actorId !== byActor.id) throw new Error('Cannot delete redundancy ' + cacheFile.url + ' of another actor.')
114
c48e82b5
C
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
e587e0ec 126function processUndoFollow (follower: ActorModel, followActivity: ActivityFollow) {
3fd3ab2d 127 return sequelizeTypescript.transaction(async t => {
e587e0ec 128 const following = await ActorModel.loadByUrlAndPopulateAccountAndChannel(followActivity.object, t)
50d6de9c 129 const actorFollow = await ActorFollowModel.loadByActorAndTarget(follower.id, following.id, t)
0032ebe9 130
50d6de9c 131 if (!actorFollow) throw new Error(`'Unknown actor follow ${follower.id} -> ${following.id}.`)
0032ebe9 132
50d6de9c 133 await actorFollow.destroy({ transaction: t })
0032ebe9
C
134
135 return undefined
136 })
137}
0f320037 138
e587e0ec 139function processUndoAnnounce (byActor: ActorModel, announceActivity: ActivityAnnounce) {
0f320037
C
140 return sequelizeTypescript.transaction(async t => {
141 const share = await VideoShareModel.loadByUrl(announceActivity.id, t)
5cf84858
C
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}.`)
0f320037
C
145
146 await share.destroy({ transaction: t })
147
9588d4f4
C
148 if (share.Video.isOwned()) {
149 // Don't resend the activity to the sender
5cf84858 150 const exceptions = [ byActor ]
9588d4f4
C
151
152 await forwardVideoRelatedActivity(announceActivity, t, exceptions, share.Video)
153 }
0f320037
C
154 })
155}