]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-undo.ts
Fix like/dislike federation
[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
29d4e137
C
62 const rate = await AccountVideoRateModel.loadByAccountAndVideoOrUrl(byActor.Account.id, video.id, likeActivity.id, t)
63 if (!rate || rate.type !== 'like') throw new Error(`Unknown like by account ${byActor.Account.id} for video ${video.id}.`)
0032ebe9
C
64
65 await rate.destroy({ transaction: t })
63c93323 66 await video.decrement('likes', { transaction: t })
0032ebe9 67
63c93323
C
68 if (video.isOwned()) {
69 // Don't resend the activity to the sender
12ba460e 70 const exceptions = [ byActor ]
9588d4f4
C
71
72 await forwardVideoRelatedActivity(activity, t, exceptions, video)
63c93323 73 }
0032ebe9
C
74 })
75}
76
12ba460e 77async function processUndoDislike (byActor: ActorModel, activity: ActivityUndo) {
848f499d
C
78 const dislike = activity.object.type === 'Dislike'
79 ? activity.object
80 : activity.object.object as DislikeObject
63c93323 81
4157cdb1 82 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: dislike.object })
2ccaeeb3 83
3fd3ab2d 84 return sequelizeTypescript.transaction(async t => {
12ba460e 85 if (!byActor.Account) throw new Error('Unknown account ' + byActor.url)
0032ebe9 86
29d4e137
C
87 const rate = await AccountVideoRateModel.loadByAccountAndVideoOrUrl(byActor.Account.id, video.id, dislike.id, t)
88 if (!rate || rate.type !== 'dislike') throw new Error(`Unknown dislike by account ${byActor.Account.id} for video ${video.id}.`)
0032ebe9
C
89
90 await rate.destroy({ transaction: t })
63c93323 91 await video.decrement('dislikes', { transaction: t })
0032ebe9 92
63c93323
C
93 if (video.isOwned()) {
94 // Don't resend the activity to the sender
12ba460e 95 const exceptions = [ byActor ]
9588d4f4
C
96
97 await forwardVideoRelatedActivity(activity, t, exceptions, video)
63c93323 98 }
0032ebe9
C
99 })
100}
101
e587e0ec 102async function processUndoCacheFile (byActor: ActorModel, activity: ActivityUndo) {
c48e82b5
C
103 const cacheFileObject = activity.object.object as CacheFileObject
104
4157cdb1 105 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.object })
c48e82b5
C
106
107 return sequelizeTypescript.transaction(async t => {
c48e82b5 108 const cacheFile = await VideoRedundancyModel.loadByUrl(cacheFileObject.id)
9cc8d43e
C
109 if (!cacheFile) {
110 logger.debug('Cannot undo unknown video cache %s.', cacheFileObject.id)
111 return
112 }
c48e82b5 113
12ba460e
C
114 if (cacheFile.actorId !== byActor.id) throw new Error('Cannot delete redundancy ' + cacheFile.url + ' of another actor.')
115
c48e82b5
C
116 await cacheFile.destroy()
117
118 if (video.isOwned()) {
119 // Don't resend the activity to the sender
120 const exceptions = [ byActor ]
121
122 await forwardVideoRelatedActivity(activity, t, exceptions, video)
123 }
124 })
125}
126
e587e0ec 127function processUndoFollow (follower: ActorModel, followActivity: ActivityFollow) {
3fd3ab2d 128 return sequelizeTypescript.transaction(async t => {
e587e0ec 129 const following = await ActorModel.loadByUrlAndPopulateAccountAndChannel(followActivity.object, t)
50d6de9c 130 const actorFollow = await ActorFollowModel.loadByActorAndTarget(follower.id, following.id, t)
0032ebe9 131
50d6de9c 132 if (!actorFollow) throw new Error(`'Unknown actor follow ${follower.id} -> ${following.id}.`)
0032ebe9 133
50d6de9c 134 await actorFollow.destroy({ transaction: t })
0032ebe9
C
135
136 return undefined
137 })
138}
0f320037 139
e587e0ec 140function processUndoAnnounce (byActor: ActorModel, announceActivity: ActivityAnnounce) {
0f320037
C
141 return sequelizeTypescript.transaction(async t => {
142 const share = await VideoShareModel.loadByUrl(announceActivity.id, t)
5cf84858
C
143 if (!share) throw new Error(`Unknown video share ${announceActivity.id}.`)
144
145 if (share.actorId !== byActor.id) throw new Error(`${share.url} is not shared by ${byActor.url}.`)
0f320037
C
146
147 await share.destroy({ transaction: t })
148
9588d4f4
C
149 if (share.Video.isOwned()) {
150 // Don't resend the activity to the sender
5cf84858 151 const exceptions = [ byActor ]
9588d4f4
C
152
153 await forwardVideoRelatedActivity(announceActivity, t, exceptions, share.Video)
154 }
0f320037
C
155 })
156}