]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/process-delete.ts
4c034a81c82e2e145acdfb4a90af493e0183e190
[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 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 undefined
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 retryTransactionWrapper(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 retryTransactionWrapper(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 retryTransactionWrapper(processDeleteVideoComment, actor, videoCommentInstance, activity)
38 }
39 }
40
41 {
42 const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(objectUrl)
43 if (videoInstance) {
44 if (videoInstance.isOwned()) throw new Error(`Remote instance cannot delete owned video ${videoInstance.url}.`)
45
46 return retryTransactionWrapper(processDeleteVideo, actor, videoInstance)
47 }
48 }
49
50 return undefined
51 }
52
53 // ---------------------------------------------------------------------------
54
55 export {
56 processDeleteActivity
57 }
58
59 // ---------------------------------------------------------------------------
60
61 async function processDeleteVideo (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 logger.debug('Removing remote account "%s".', accountToRemove.Actor.uuid)
77
78 await sequelizeTypescript.transaction(async t => {
79 await accountToRemove.destroy({ transaction: t })
80 })
81
82 logger.info('Remote account with uuid %s removed.', accountToRemove.Actor.uuid)
83 }
84
85 async function processDeleteVideoChannel (videoChannelToRemove: VideoChannelModel) {
86 logger.debug('Removing remote video channel "%s".', videoChannelToRemove.Actor.uuid)
87
88 await sequelizeTypescript.transaction(async t => {
89 await videoChannelToRemove.destroy({ transaction: t })
90 })
91
92 logger.info('Remote video channel with uuid %s removed.', videoChannelToRemove.Actor.uuid)
93 }
94
95 function processDeleteVideoComment (byActor: ActorModel, videoComment: VideoCommentModel, activity: ActivityDelete) {
96 logger.debug('Removing remote video comment "%s".', videoComment.url)
97
98 return sequelizeTypescript.transaction(async t => {
99 await videoComment.destroy({ transaction: t })
100
101 if (videoComment.Video.isOwned()) {
102 // Don't resend the activity to the sender
103 const exceptions = [ byActor ]
104 await forwardActivity(activity, t, exceptions)
105 }
106
107 logger.info('Remote video comment %s removed.', videoComment.url)
108 })
109 }