]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-delete.ts
Refractor activities sending
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-delete.ts
CommitLineData
3fd3ab2d 1import { ActivityDelete } from '../../../../shared/models/activitypub'
da854ddd
C
2import { retryTransactionWrapper } from '../../../helpers/database-utils'
3import { logger } from '../../../helpers/logger'
3fd3ab2d
C
4import { sequelizeTypescript } from '../../../initializers'
5import { AccountModel } from '../../../models/account/account'
50d6de9c 6import { ActorModel } from '../../../models/activitypub/actor'
3fd3ab2d
C
7import { VideoModel } from '../../../models/video/video'
8import { VideoChannelModel } from '../../../models/video/video-channel'
4cb6d457 9import { VideoCommentModel } from '../../../models/video/video-comment'
50d6de9c 10import { getOrCreateActorAndServerAndModel } from '../actor'
e251f170 11import { forwardActivity } from '../send/utils'
7a7724e6
C
12
13async function processDeleteActivity (activity: ActivityDelete) {
2890b615 14 const objectUrl = typeof activity.object === 'string' ? activity.object : activity.object.id
7a7724e6 15
f05a1c30
C
16 if (activity.actor === objectUrl) {
17 let actor = await ActorModel.loadByUrl(activity.actor)
bcec136e 18 if (!actor) return undefined
f05a1c30 19
50d6de9c
C
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.')
7a7724e6 22
f05a1c30 23 actor.Account.Actor = await actor.Account.$get('Actor') as ActorModel
90d4bb81 24 return retryTransactionWrapper(processDeleteAccount, actor.Account)
50d6de9c
C
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
f05a1c30 28 actor.VideoChannel.Actor = await actor.VideoChannel.$get('Actor') as ActorModel
90d4bb81 29 return retryTransactionWrapper(processDeleteVideoChannel, actor.VideoChannel)
7a7724e6
C
30 }
31 }
32
f05a1c30 33 const actor = await getOrCreateActorAndServerAndModel(activity.actor)
7a7724e6 34 {
2890b615 35 const videoCommentInstance = await VideoCommentModel.loadByUrlAndPopulateAccount(objectUrl)
4cb6d457 36 if (videoCommentInstance) {
90d4bb81 37 return retryTransactionWrapper(processDeleteVideoComment, actor, videoCommentInstance, activity)
4cb6d457
C
38 }
39 }
40
41 {
2890b615 42 const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(objectUrl)
4cb6d457 43 if (videoInstance) {
a2377d15
C
44 if (videoInstance.isOwned()) throw new Error(`Remote instance cannot delete owned video ${videoInstance.url}.`)
45
90d4bb81 46 return retryTransactionWrapper(processDeleteVideo, actor, videoInstance)
7a7724e6
C
47 }
48 }
49
bcec136e 50 return undefined
7a7724e6
C
51}
52
53// ---------------------------------------------------------------------------
54
55export {
56 processDeleteActivity
57}
58
59// ---------------------------------------------------------------------------
60
50d6de9c 61async function processDeleteVideo (actor: ActorModel, videoToDelete: VideoModel) {
7a7724e6
C
62 logger.debug('Removing remote video "%s".', videoToDelete.uuid)
63
3fd3ab2d 64 await sequelizeTypescript.transaction(async t => {
50d6de9c
C
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)
7a7724e6
C
67 }
68
69 await videoToDelete.destroy({ transaction: t })
70 })
71
72 logger.info('Remote video with uuid %s removed.', videoToDelete.uuid)
73}
74
50d6de9c 75async function processDeleteAccount (accountToRemove: AccountModel) {
50d6de9c 76 logger.debug('Removing remote account "%s".', accountToRemove.Actor.uuid)
7a7724e6 77
3fd3ab2d 78 await sequelizeTypescript.transaction(async t => {
50d6de9c 79 await accountToRemove.destroy({ transaction: t })
7a7724e6
C
80 })
81
50d6de9c 82 logger.info('Remote account with uuid %s removed.', accountToRemove.Actor.uuid)
7a7724e6
C
83}
84
50d6de9c 85async function processDeleteVideoChannel (videoChannelToRemove: VideoChannelModel) {
50d6de9c 86 logger.debug('Removing remote video channel "%s".', videoChannelToRemove.Actor.uuid)
7a7724e6 87
3fd3ab2d 88 await sequelizeTypescript.transaction(async t => {
50d6de9c 89 await videoChannelToRemove.destroy({ transaction: t })
7a7724e6
C
90 })
91
50d6de9c 92 logger.info('Remote video channel with uuid %s removed.', videoChannelToRemove.Actor.uuid)
7a7724e6 93}
4cb6d457 94
90d4bb81 95function processDeleteVideoComment (byActor: ActorModel, videoComment: VideoCommentModel, activity: ActivityDelete) {
4cb6d457
C
96 logger.debug('Removing remote video comment "%s".', videoComment.url)
97
98 return sequelizeTypescript.transaction(async t => {
99 await videoComment.destroy({ transaction: t })
100
73c08093
C
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
4cb6d457
C
107 logger.info('Remote video comment %s removed.', videoComment.url)
108 })
109}