]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-undo.ts
Fix videos list user NSFW policy
[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
29 if (activityToUndo.type === 'Follow') {
e587e0ec 30 return retryTransactionWrapper(processUndoFollow, byActor, activityToUndo)
c48e82b5
C
31 }
32
33 if (activityToUndo.type === 'Announce') {
e587e0ec 34 return retryTransactionWrapper(processUndoAnnounce, byActor, activityToUndo)
54141398
C
35 }
36
37 logger.warn('Unknown activity object type %s -> %s when undo activity.', activityToUndo.type, { activity: activity.id })
38
39 return undefined
40}
41
42// ---------------------------------------------------------------------------
43
44export {
45 processUndoActivity
46}
47
48// ---------------------------------------------------------------------------
0032ebe9 49
12ba460e 50async function processUndoLike (byActor: ActorModel, activity: ActivityUndo) {
63c93323
C
51 const likeActivity = activity.object as ActivityLike
52
4157cdb1 53 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: likeActivity.object })
2ccaeeb3 54
3fd3ab2d 55 return sequelizeTypescript.transaction(async t => {
12ba460e 56 if (!byActor.Account) throw new Error('Unknown account ' + byActor.url)
0032ebe9 57
12ba460e
C
58 const rate = await AccountVideoRateModel.load(byActor.Account.id, video.id, t)
59 if (!rate) throw new Error(`Unknown rate by account ${byActor.Account.id} for video ${video.id}.`)
0032ebe9
C
60
61 await rate.destroy({ transaction: t })
63c93323 62 await video.decrement('likes', { transaction: t })
0032ebe9 63
63c93323
C
64 if (video.isOwned()) {
65 // Don't resend the activity to the sender
12ba460e 66 const exceptions = [ byActor ]
9588d4f4
C
67
68 await forwardVideoRelatedActivity(activity, t, exceptions, video)
63c93323 69 }
0032ebe9
C
70 })
71}
72
12ba460e 73async function processUndoDislike (byActor: ActorModel, activity: ActivityUndo) {
63c93323
C
74 const dislike = activity.object.object as DislikeObject
75
4157cdb1 76 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: dislike.object })
2ccaeeb3 77
3fd3ab2d 78 return sequelizeTypescript.transaction(async t => {
12ba460e 79 if (!byActor.Account) throw new Error('Unknown account ' + byActor.url)
0032ebe9 80
12ba460e
C
81 const rate = await AccountVideoRateModel.load(byActor.Account.id, video.id, t)
82 if (!rate) throw new Error(`Unknown rate by account ${byActor.Account.id} for video ${video.id}.`)
0032ebe9
C
83
84 await rate.destroy({ transaction: t })
63c93323 85 await video.decrement('dislikes', { transaction: t })
0032ebe9 86
63c93323
C
87 if (video.isOwned()) {
88 // Don't resend the activity to the sender
12ba460e 89 const exceptions = [ byActor ]
9588d4f4
C
90
91 await forwardVideoRelatedActivity(activity, t, exceptions, video)
63c93323 92 }
0032ebe9
C
93 })
94}
95
e587e0ec 96async function processUndoCacheFile (byActor: ActorModel, activity: ActivityUndo) {
c48e82b5
C
97 const cacheFileObject = activity.object.object as CacheFileObject
98
4157cdb1 99 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.object })
c48e82b5
C
100
101 return sequelizeTypescript.transaction(async t => {
c48e82b5
C
102 const cacheFile = await VideoRedundancyModel.loadByUrl(cacheFileObject.id)
103 if (!cacheFile) throw new Error('Unknown video cache ' + cacheFile.url)
104
12ba460e
C
105 if (cacheFile.actorId !== byActor.id) throw new Error('Cannot delete redundancy ' + cacheFile.url + ' of another actor.')
106
c48e82b5
C
107 await cacheFile.destroy()
108
109 if (video.isOwned()) {
110 // Don't resend the activity to the sender
111 const exceptions = [ byActor ]
112
113 await forwardVideoRelatedActivity(activity, t, exceptions, video)
114 }
115 })
116}
117
e587e0ec 118function processUndoFollow (follower: ActorModel, followActivity: ActivityFollow) {
3fd3ab2d 119 return sequelizeTypescript.transaction(async t => {
e587e0ec 120 const following = await ActorModel.loadByUrlAndPopulateAccountAndChannel(followActivity.object, t)
50d6de9c 121 const actorFollow = await ActorFollowModel.loadByActorAndTarget(follower.id, following.id, t)
0032ebe9 122
50d6de9c 123 if (!actorFollow) throw new Error(`'Unknown actor follow ${follower.id} -> ${following.id}.`)
0032ebe9 124
50d6de9c 125 await actorFollow.destroy({ transaction: t })
0032ebe9
C
126
127 return undefined
128 })
129}
0f320037 130
e587e0ec 131function processUndoAnnounce (byActor: ActorModel, announceActivity: ActivityAnnounce) {
0f320037
C
132 return sequelizeTypescript.transaction(async t => {
133 const share = await VideoShareModel.loadByUrl(announceActivity.id, t)
5cf84858
C
134 if (!share) throw new Error(`Unknown video share ${announceActivity.id}.`)
135
136 if (share.actorId !== byActor.id) throw new Error(`${share.url} is not shared by ${byActor.url}.`)
0f320037
C
137
138 await share.destroy({ transaction: t })
139
9588d4f4
C
140 if (share.Video.isOwned()) {
141 // Don't resend the activity to the sender
5cf84858 142 const exceptions = [ byActor ]
9588d4f4
C
143
144 await forwardVideoRelatedActivity(announceActivity, t, exceptions, share.Video)
145 }
0f320037
C
146 })
147}