]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/process-undo.ts
Improve redundancy: add 'min_lifetime' configuration
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-undo.ts
1 import { ActivityAnnounce, ActivityFollow, ActivityLike, ActivityUndo, CacheFileObject } from '../../../../shared/models/activitypub'
2 import { DislikeObject } from '../../../../shared/models/activitypub/objects'
3 import { retryTransactionWrapper } from '../../../helpers/database-utils'
4 import { logger } from '../../../helpers/logger'
5 import { sequelizeTypescript } from '../../../initializers'
6 import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
7 import { ActorModel } from '../../../models/activitypub/actor'
8 import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
9 import { forwardVideoRelatedActivity } from '../send/utils'
10 import { getOrCreateVideoAndAccountAndChannel } from '../videos'
11 import { VideoShareModel } from '../../../models/video/video-share'
12 import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
13
14 async function processUndoActivity (activity: ActivityUndo, byActor: ActorModel) {
15 const activityToUndo = activity.object
16
17 if (activityToUndo.type === 'Like') {
18 return retryTransactionWrapper(processUndoLike, byActor, activity)
19 }
20
21 if (activityToUndo.type === 'Create') {
22 if (activityToUndo.object.type === 'Dislike') {
23 return retryTransactionWrapper(processUndoDislike, byActor, activity)
24 } else if (activityToUndo.object.type === 'CacheFile') {
25 return retryTransactionWrapper(processUndoCacheFile, byActor, activity)
26 }
27 }
28
29 if (activityToUndo.type === 'Follow') {
30 return retryTransactionWrapper(processUndoFollow, byActor, activityToUndo)
31 }
32
33 if (activityToUndo.type === 'Announce') {
34 return retryTransactionWrapper(processUndoAnnounce, byActor, activityToUndo)
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
44 export {
45 processUndoActivity
46 }
47
48 // ---------------------------------------------------------------------------
49
50 async function processUndoLike (byActor: ActorModel, activity: ActivityUndo) {
51 const likeActivity = activity.object as ActivityLike
52
53 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: likeActivity.object })
54
55 return sequelizeTypescript.transaction(async t => {
56 if (!byActor.Account) throw new Error('Unknown account ' + byActor.url)
57
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}.`)
60
61 await rate.destroy({ transaction: t })
62 await video.decrement('likes', { transaction: t })
63
64 if (video.isOwned()) {
65 // Don't resend the activity to the sender
66 const exceptions = [ byActor ]
67
68 await forwardVideoRelatedActivity(activity, t, exceptions, video)
69 }
70 })
71 }
72
73 async function processUndoDislike (byActor: ActorModel, activity: ActivityUndo) {
74 const dislike = activity.object.object as DislikeObject
75
76 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: dislike.object })
77
78 return sequelizeTypescript.transaction(async t => {
79 if (!byActor.Account) throw new Error('Unknown account ' + byActor.url)
80
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}.`)
83
84 await rate.destroy({ transaction: t })
85 await video.decrement('dislikes', { transaction: t })
86
87 if (video.isOwned()) {
88 // Don't resend the activity to the sender
89 const exceptions = [ byActor ]
90
91 await forwardVideoRelatedActivity(activity, t, exceptions, video)
92 }
93 })
94 }
95
96 async function processUndoCacheFile (byActor: ActorModel, activity: ActivityUndo) {
97 const cacheFileObject = activity.object.object as CacheFileObject
98
99 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.object })
100
101 return sequelizeTypescript.transaction(async t => {
102 const cacheFile = await VideoRedundancyModel.loadByUrl(cacheFileObject.id)
103 if (!cacheFile) throw new Error('Unknown video cache ' + cacheFileObject.id)
104
105 if (cacheFile.actorId !== byActor.id) throw new Error('Cannot delete redundancy ' + cacheFile.url + ' of another actor.')
106
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
118 function processUndoFollow (follower: ActorModel, followActivity: ActivityFollow) {
119 return sequelizeTypescript.transaction(async t => {
120 const following = await ActorModel.loadByUrlAndPopulateAccountAndChannel(followActivity.object, t)
121 const actorFollow = await ActorFollowModel.loadByActorAndTarget(follower.id, following.id, t)
122
123 if (!actorFollow) throw new Error(`'Unknown actor follow ${follower.id} -> ${following.id}.`)
124
125 await actorFollow.destroy({ transaction: t })
126
127 return undefined
128 })
129 }
130
131 function processUndoAnnounce (byActor: ActorModel, announceActivity: ActivityAnnounce) {
132 return sequelizeTypescript.transaction(async t => {
133 const share = await VideoShareModel.loadByUrl(announceActivity.id, t)
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}.`)
137
138 await share.destroy({ transaction: t })
139
140 if (share.Video.isOwned()) {
141 // Don't resend the activity to the sender
142 const exceptions = [ byActor ]
143
144 await forwardVideoRelatedActivity(announceActivity, t, exceptions, share.Video)
145 }
146 })
147 }