Split files in activitypub server
[oweals/peertube.git] / server / lib / activitypub / process / process-delete.ts
1 import { ActivityDelete } from '../../../../shared/models/activitypub'
2 import { retryTransactionWrapper } from '../../../helpers/database-utils'
3 import { logger } from '../../../helpers/logger'
4 import { sequelizeTypescript } from '../../../initializers'
5 import { AccountModel } from '../../../models/account/account'
6 import { ActorModel } from '../../../models/activitypub/actor'
7 import { VideoModel } from '../../../models/video/video'
8 import { VideoChannelModel } from '../../../models/video/video-channel'
9 import { VideoCommentModel } from '../../../models/video/video-comment'
10 import { getOrCreateActorAndServerAndModel } from '../actor'
11 import { forwardActivity } from '../send/utils'
12
13 async function processDeleteActivity (activity: ActivityDelete) {
14   const objectUrl = typeof activity.object === 'string' ? activity.object : activity.object.id
15
16   if (activity.actor === objectUrl) {
17     let actor = await ActorModel.loadByUrl(activity.actor)
18     if (!actor) return
19
20     if (actor.type === 'Person') {
21       if (!actor.Account) throw new Error('Actor ' + actor.url + ' is a person but we cannot find it in database.')
22
23       actor.Account.Actor = await actor.Account.$get('Actor') as ActorModel
24       return processDeleteAccount(actor.Account)
25     } else if (actor.type === 'Group') {
26       if (!actor.VideoChannel) throw new Error('Actor ' + actor.url + ' is a group but we cannot find it in database.')
27
28       actor.VideoChannel.Actor = await actor.VideoChannel.$get('Actor') as ActorModel
29       return processDeleteVideoChannel(actor.VideoChannel)
30     }
31   }
32
33   const actor = await getOrCreateActorAndServerAndModel(activity.actor)
34   {
35     const videoCommentInstance = await VideoCommentModel.loadByUrlAndPopulateAccount(objectUrl)
36     if (videoCommentInstance) {
37       return processDeleteVideoComment(actor, videoCommentInstance, activity)
38     }
39   }
40
41   {
42     const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(objectUrl)
43     if (videoInstance) {
44       return processDeleteVideo(actor, videoInstance)
45     }
46   }
47
48   return
49 }
50
51 // ---------------------------------------------------------------------------
52
53 export {
54   processDeleteActivity
55 }
56
57 // ---------------------------------------------------------------------------
58
59 async function processDeleteVideo (actor: ActorModel, videoToDelete: VideoModel) {
60   const options = {
61     arguments: [ actor, videoToDelete ],
62     errorMessage: 'Cannot remove the remote video with many retries.'
63   }
64
65   await retryTransactionWrapper(deleteRemoteVideo, options)
66 }
67
68 async function deleteRemoteVideo (actor: ActorModel, videoToDelete: VideoModel) {
69   logger.debug('Removing remote video "%s".', videoToDelete.uuid)
70
71   await sequelizeTypescript.transaction(async t => {
72     if (videoToDelete.VideoChannel.Account.Actor.id !== actor.id) {
73       throw new Error('Account ' + actor.url + ' does not own video channel ' + videoToDelete.VideoChannel.Actor.url)
74     }
75
76     await videoToDelete.destroy({ transaction: t })
77   })
78
79   logger.info('Remote video with uuid %s removed.', videoToDelete.uuid)
80 }
81
82 async function processDeleteAccount (accountToRemove: AccountModel) {
83   const options = {
84     arguments: [ accountToRemove ],
85     errorMessage: 'Cannot remove the remote account with many retries.'
86   }
87
88   await retryTransactionWrapper(deleteRemoteAccount, options)
89 }
90
91 async function deleteRemoteAccount (accountToRemove: AccountModel) {
92   logger.debug('Removing remote account "%s".', accountToRemove.Actor.uuid)
93
94   await sequelizeTypescript.transaction(async t => {
95     await accountToRemove.destroy({ transaction: t })
96   })
97
98   logger.info('Remote account with uuid %s removed.', accountToRemove.Actor.uuid)
99 }
100
101 async function processDeleteVideoChannel (videoChannelToRemove: VideoChannelModel) {
102   const options = {
103     arguments: [ videoChannelToRemove ],
104     errorMessage: 'Cannot remove the remote video channel with many retries.'
105   }
106
107   await retryTransactionWrapper(deleteRemoteVideoChannel, options)
108 }
109
110 async function deleteRemoteVideoChannel (videoChannelToRemove: VideoChannelModel) {
111   logger.debug('Removing remote video channel "%s".', videoChannelToRemove.Actor.uuid)
112
113   await sequelizeTypescript.transaction(async t => {
114     await videoChannelToRemove.destroy({ transaction: t })
115   })
116
117   logger.info('Remote video channel with uuid %s removed.', videoChannelToRemove.Actor.uuid)
118 }
119
120 async function processDeleteVideoComment (byActor: ActorModel, videoComment: VideoCommentModel, activity: ActivityDelete) {
121   const options = {
122     arguments: [ byActor, videoComment, activity ],
123     errorMessage: 'Cannot remove the remote video comment with many retries.'
124   }
125
126   await retryTransactionWrapper(deleteRemoteVideoComment, options)
127 }
128
129 function deleteRemoteVideoComment (byActor: ActorModel, videoComment: VideoCommentModel, activity: ActivityDelete) {
130   logger.debug('Removing remote video comment "%s".', videoComment.url)
131
132   return sequelizeTypescript.transaction(async t => {
133     await videoComment.destroy({ transaction: t })
134
135     if (videoComment.Video.isOwned()) {
136       // Don't resend the activity to the sender
137       const exceptions = [ byActor ]
138       await forwardActivity(activity, t, exceptions)
139     }
140
141     logger.info('Remote video comment %s removed.', videoComment.url)
142   })
143 }