]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-undo.ts
Fix user notifications on new follow
[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'
1198edf4 13import { APProcessorOptions } from '../../../typings/activitypub-processor.model'
54141398 14
1198edf4
C
15async function processUndoActivity (options: APProcessorOptions<ActivityUndo>) {
16 const { activity, byActor } = options
54141398
C
17 const activityToUndo = activity.object
18
0032ebe9 19 if (activityToUndo.type === 'Like') {
12ba460e 20 return retryTransactionWrapper(processUndoLike, byActor, activity)
c48e82b5
C
21 }
22
23 if (activityToUndo.type === 'Create') {
24 if (activityToUndo.object.type === 'Dislike') {
12ba460e 25 return retryTransactionWrapper(processUndoDislike, byActor, activity)
c48e82b5 26 } else if (activityToUndo.object.type === 'CacheFile') {
e587e0ec 27 return retryTransactionWrapper(processUndoCacheFile, byActor, activity)
c48e82b5
C
28 }
29 }
30
848f499d
C
31 if (activityToUndo.type === 'Dislike') {
32 return retryTransactionWrapper(processUndoDislike, byActor, activity)
33 }
34
c48e82b5 35 if (activityToUndo.type === 'Follow') {
e587e0ec 36 return retryTransactionWrapper(processUndoFollow, byActor, activityToUndo)
c48e82b5
C
37 }
38
39 if (activityToUndo.type === 'Announce') {
e587e0ec 40 return retryTransactionWrapper(processUndoAnnounce, byActor, activityToUndo)
54141398
C
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
50export {
51 processUndoActivity
52}
53
54// ---------------------------------------------------------------------------
0032ebe9 55
12ba460e 56async function processUndoLike (byActor: ActorModel, activity: ActivityUndo) {
63c93323
C
57 const likeActivity = activity.object as ActivityLike
58
4157cdb1 59 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: likeActivity.object })
2ccaeeb3 60
3fd3ab2d 61 return sequelizeTypescript.transaction(async t => {
12ba460e 62 if (!byActor.Account) throw new Error('Unknown account ' + byActor.url)
0032ebe9 63
29d4e137
C
64 const rate = await AccountVideoRateModel.loadByAccountAndVideoOrUrl(byActor.Account.id, video.id, likeActivity.id, t)
65 if (!rate || rate.type !== 'like') throw new Error(`Unknown like by account ${byActor.Account.id} for video ${video.id}.`)
0032ebe9
C
66
67 await rate.destroy({ transaction: t })
63c93323 68 await video.decrement('likes', { transaction: t })
0032ebe9 69
63c93323
C
70 if (video.isOwned()) {
71 // Don't resend the activity to the sender
12ba460e 72 const exceptions = [ byActor ]
9588d4f4
C
73
74 await forwardVideoRelatedActivity(activity, t, exceptions, video)
63c93323 75 }
0032ebe9
C
76 })
77}
78
12ba460e 79async function processUndoDislike (byActor: ActorModel, activity: ActivityUndo) {
848f499d
C
80 const dislike = activity.object.type === 'Dislike'
81 ? activity.object
82 : activity.object.object as DislikeObject
63c93323 83
4157cdb1 84 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: dislike.object })
2ccaeeb3 85
3fd3ab2d 86 return sequelizeTypescript.transaction(async t => {
12ba460e 87 if (!byActor.Account) throw new Error('Unknown account ' + byActor.url)
0032ebe9 88
29d4e137
C
89 const rate = await AccountVideoRateModel.loadByAccountAndVideoOrUrl(byActor.Account.id, video.id, dislike.id, t)
90 if (!rate || rate.type !== 'dislike') throw new Error(`Unknown dislike 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)
9cc8d43e
C
111 if (!cacheFile) {
112 logger.debug('Cannot undo unknown video cache %s.', cacheFileObject.id)
113 return
114 }
c48e82b5 115
12ba460e
C
116 if (cacheFile.actorId !== byActor.id) throw new Error('Cannot delete redundancy ' + cacheFile.url + ' of another actor.')
117
c48e82b5
C
118 await cacheFile.destroy()
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
e587e0ec 129function processUndoFollow (follower: ActorModel, followActivity: ActivityFollow) {
3fd3ab2d 130 return sequelizeTypescript.transaction(async t => {
e587e0ec 131 const following = await ActorModel.loadByUrlAndPopulateAccountAndChannel(followActivity.object, t)
50d6de9c 132 const actorFollow = await ActorFollowModel.loadByActorAndTarget(follower.id, following.id, t)
0032ebe9 133
50d6de9c 134 if (!actorFollow) throw new Error(`'Unknown actor follow ${follower.id} -> ${following.id}.`)
0032ebe9 135
50d6de9c 136 await actorFollow.destroy({ transaction: t })
0032ebe9
C
137
138 return undefined
139 })
140}
0f320037 141
e587e0ec 142function processUndoAnnounce (byActor: ActorModel, announceActivity: ActivityAnnounce) {
0f320037
C
143 return sequelizeTypescript.transaction(async t => {
144 const share = await VideoShareModel.loadByUrl(announceActivity.id, t)
5cf84858
C
145 if (!share) throw new Error(`Unknown video share ${announceActivity.id}.`)
146
147 if (share.actorId !== byActor.id) throw new Error(`${share.url} is not shared by ${byActor.url}.`)
0f320037
C
148
149 await share.destroy({ transaction: t })
150
9588d4f4
C
151 if (share.Video.isOwned()) {
152 // Don't resend the activity to the sender
5cf84858 153 const exceptions = [ byActor ]
9588d4f4
C
154
155 await forwardVideoRelatedActivity(announceActivity, t, exceptions, share.Video)
156 }
0f320037
C
157 })
158}