]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-undo.ts
Translated using Weblate (Persian)
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-undo.ts
CommitLineData
57e4e1c1 1import { VideoModel } from '@server/models/video/video'
c48e82b5 2import { ActivityAnnounce, ActivityFollow, ActivityLike, ActivityUndo, CacheFileObject } from '../../../../shared/models/activitypub'
3fd3ab2d 3import { DislikeObject } from '../../../../shared/models/activitypub/objects'
da854ddd
C
4import { retryTransactionWrapper } from '../../../helpers/database-utils'
5import { logger } from '../../../helpers/logger'
80fdaf06 6import { sequelizeTypescript } from '../../../initializers/database'
3fd3ab2d 7import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
7d9ba5c0
C
8import { ActorModel } from '../../../models/actor/actor'
9import { ActorFollowModel } from '../../../models/actor/actor-follow'
c48e82b5 10import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
c15aff66 11import { VideoShareModel } from '../../../models/video/video-share'
26d6bf65
C
12import { APProcessorOptions } from '../../../types/activitypub-processor.model'
13import { MActorSignature } from '../../../types/models'
57e4e1c1
C
14import { forwardVideoRelatedActivity } from '../send/shared/send-utils'
15import { federateVideoIfNeeded, getOrCreateAPVideo } from '../videos'
54141398 16
1198edf4
C
17async function processUndoActivity (options: APProcessorOptions<ActivityUndo>) {
18 const { activity, byActor } = options
54141398
C
19 const activityToUndo = activity.object
20
0032ebe9 21 if (activityToUndo.type === 'Like') {
12ba460e 22 return retryTransactionWrapper(processUndoLike, byActor, activity)
c48e82b5
C
23 }
24
25 if (activityToUndo.type === 'Create') {
c15aff66 26 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
453e83ea 56async function processUndoLike (byActor: MActorSignature, activity: ActivityUndo) {
63c93323
C
57 const likeActivity = activity.object as ActivityLike
58
57e4e1c1
C
59 const { video: onlyVideo } = await getOrCreateAPVideo({ videoObject: likeActivity.object })
60 // We don't care about likes of remote videos
61 if (!onlyVideo.isOwned()) return
2ccaeeb3 62
3fd3ab2d 63 return sequelizeTypescript.transaction(async t => {
12ba460e 64 if (!byActor.Account) throw new Error('Unknown account ' + byActor.url)
0032ebe9 65
4fae2b1f 66 const video = await VideoModel.loadFull(onlyVideo.id, t)
29d4e137 67 const rate = await AccountVideoRateModel.loadByAccountAndVideoOrUrl(byActor.Account.id, video.id, likeActivity.id, t)
c5cadb28
C
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 }
0032ebe9
C
72
73 await rate.destroy({ transaction: t })
63c93323 74 await video.decrement('likes', { transaction: t })
0032ebe9 75
57e4e1c1
C
76 video.likes--
77 await federateVideoIfNeeded(video, false, t)
0032ebe9
C
78 })
79}
80
453e83ea 81async function processUndoDislike (byActor: MActorSignature, activity: ActivityUndo) {
848f499d
C
82 const dislike = activity.object.type === 'Dislike'
83 ? activity.object
84 : activity.object.object as DislikeObject
63c93323 85
57e4e1c1
C
86 const { video: onlyVideo } = await getOrCreateAPVideo({ videoObject: dislike.object })
87 // We don't care about likes of remote videos
88 if (!onlyVideo.isOwned()) return
2ccaeeb3 89
3fd3ab2d 90 return sequelizeTypescript.transaction(async t => {
12ba460e 91 if (!byActor.Account) throw new Error('Unknown account ' + byActor.url)
0032ebe9 92
4fae2b1f 93 const video = await VideoModel.loadFull(onlyVideo.id, t)
29d4e137 94 const rate = await AccountVideoRateModel.loadByAccountAndVideoOrUrl(byActor.Account.id, video.id, dislike.id, t)
c5cadb28
C
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 }
0032ebe9
C
99
100 await rate.destroy({ transaction: t })
63c93323 101 await video.decrement('dislikes', { transaction: t })
57e4e1c1 102 video.dislikes--
0032ebe9 103
57e4e1c1 104 await federateVideoIfNeeded(video, false, t)
0032ebe9
C
105 })
106}
107
57e4e1c1
C
108// ---------------------------------------------------------------------------
109
453e83ea 110async function processUndoCacheFile (byActor: MActorSignature, activity: ActivityUndo) {
c48e82b5
C
111 const cacheFileObject = activity.object.object as CacheFileObject
112
304a84d5 113 const { video } = await getOrCreateAPVideo({ videoObject: cacheFileObject.object })
c48e82b5
C
114
115 return sequelizeTypescript.transaction(async t => {
eae0365b 116 const cacheFile = await VideoRedundancyModel.loadByUrl(cacheFileObject.id, t)
9cc8d43e
C
117 if (!cacheFile) {
118 logger.debug('Cannot undo unknown video cache %s.', cacheFileObject.id)
119 return
120 }
c48e82b5 121
12ba460e
C
122 if (cacheFile.actorId !== byActor.id) throw new Error('Cannot delete redundancy ' + cacheFile.url + ' of another actor.')
123
eae0365b 124 await cacheFile.destroy({ transaction: t })
c48e82b5
C
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
453e83ea 135function processUndoAnnounce (byActor: MActorSignature, announceActivity: ActivityAnnounce) {
0f320037
C
136 return sequelizeTypescript.transaction(async t => {
137 const share = await VideoShareModel.loadByUrl(announceActivity.id, t)
c5cadb28
C
138 if (!share) {
139 logger.warn('Unknown video share %d', announceActivity.id)
140 return
141 }
5cf84858
C
142
143 if (share.actorId !== byActor.id) throw new Error(`${share.url} is not shared by ${byActor.url}.`)
0f320037
C
144
145 await share.destroy({ transaction: t })
146
9588d4f4
C
147 if (share.Video.isOwned()) {
148 // Don't resend the activity to the sender
5cf84858 149 const exceptions = [ byActor ]
9588d4f4
C
150
151 await forwardVideoRelatedActivity(announceActivity, t, exceptions, share.Video)
152 }
0f320037
C
153 })
154}
57e4e1c1
C
155
156// ---------------------------------------------------------------------------
157
158function 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
c5cadb28
C
163 if (!actorFollow) {
164 logger.warn('Unknown actor follow %d -> %d.', follower.id, following.id)
165 return
166 }
57e4e1c1
C
167
168 await actorFollow.destroy({ transaction: t })
169
170 return undefined
171 })
172}