]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/process-delete.ts
Add ability to delete comments
[github/Chocobozzz/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
12 async function processDeleteActivity (activity: ActivityDelete) {
13 const actor = await getOrCreateActorAndServerAndModel(activity.actor)
14
15 if (actor.url === activity.id) {
16 if (actor.type === 'Person') {
17 if (!actor.Account) throw new Error('Actor ' + actor.url + ' is a person but we cannot find it in database.')
18
19 return processDeleteAccount(actor.Account)
20 } else if (actor.type === 'Group') {
21 if (!actor.VideoChannel) throw new Error('Actor ' + actor.url + ' is a group but we cannot find it in database.')
22
23 return processDeleteVideoChannel(actor.VideoChannel)
24 }
25 }
26
27 {
28 const videoCommentInstance = await VideoCommentModel.loadByUrlAndPopulateAccount(activity.id)
29 if (videoCommentInstance) {
30 return processDeleteVideoComment(actor, videoCommentInstance)
31 }
32 }
33
34 {
35 const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(activity.id)
36 if (videoInstance) {
37 return processDeleteVideo(actor, videoInstance)
38 }
39 }
40
41 return
42 }
43
44 // ---------------------------------------------------------------------------
45
46 export {
47 processDeleteActivity
48 }
49
50 // ---------------------------------------------------------------------------
51
52 async function processDeleteVideo (actor: ActorModel, videoToDelete: VideoModel) {
53 const options = {
54 arguments: [ actor, videoToDelete ],
55 errorMessage: 'Cannot remove the remote video with many retries.'
56 }
57
58 await retryTransactionWrapper(deleteRemoteVideo, options)
59 }
60
61 async function deleteRemoteVideo (actor: ActorModel, videoToDelete: VideoModel) {
62 logger.debug('Removing remote video "%s".', videoToDelete.uuid)
63
64 await sequelizeTypescript.transaction(async t => {
65 if (videoToDelete.VideoChannel.Account.Actor.id !== actor.id) {
66 throw new Error('Account ' + actor.url + ' does not own video channel ' + videoToDelete.VideoChannel.Actor.url)
67 }
68
69 await videoToDelete.destroy({ transaction: t })
70 })
71
72 logger.info('Remote video with uuid %s removed.', videoToDelete.uuid)
73 }
74
75 async function processDeleteAccount (accountToRemove: AccountModel) {
76 const options = {
77 arguments: [ accountToRemove ],
78 errorMessage: 'Cannot remove the remote account with many retries.'
79 }
80
81 await retryTransactionWrapper(deleteRemoteAccount, options)
82 }
83
84 async function deleteRemoteAccount (accountToRemove: AccountModel) {
85 logger.debug('Removing remote account "%s".', accountToRemove.Actor.uuid)
86
87 await sequelizeTypescript.transaction(async t => {
88 await accountToRemove.destroy({ transaction: t })
89 })
90
91 logger.info('Remote account with uuid %s removed.', accountToRemove.Actor.uuid)
92 }
93
94 async function processDeleteVideoChannel (videoChannelToRemove: VideoChannelModel) {
95 const options = {
96 arguments: [ videoChannelToRemove ],
97 errorMessage: 'Cannot remove the remote video channel with many retries.'
98 }
99
100 await retryTransactionWrapper(deleteRemoteVideoChannel, options)
101 }
102
103 async function deleteRemoteVideoChannel (videoChannelToRemove: VideoChannelModel) {
104 logger.debug('Removing remote video channel "%s".', videoChannelToRemove.Actor.uuid)
105
106 await sequelizeTypescript.transaction(async t => {
107 await videoChannelToRemove.destroy({ transaction: t })
108 })
109
110 logger.info('Remote video channel with uuid %s removed.', videoChannelToRemove.Actor.uuid)
111 }
112
113 async function processDeleteVideoComment (actor: ActorModel, videoComment: VideoCommentModel) {
114 const options = {
115 arguments: [ actor, videoComment ],
116 errorMessage: 'Cannot remove the remote video comment with many retries.'
117 }
118
119 await retryTransactionWrapper(deleteRemoteVideoComment, options)
120 }
121
122 function deleteRemoteVideoComment (actor: ActorModel, videoComment: VideoCommentModel) {
123 logger.debug('Removing remote video comment "%s".', videoComment.url)
124
125 return sequelizeTypescript.transaction(async t => {
126 await videoComment.destroy({ transaction: t })
127
128 logger.info('Remote video comment %s removed.', videoComment.url)
129 })
130 }