]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-delete.ts
Merge branch 'move-utils-to-shared' of https://github.com/buoyantair/PeerTube into...
[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'
659edaa6 10import { forwardVideoRelatedActivity } from '../send/utils'
7a7724e6 11
e587e0ec 12async function processDeleteActivity (activity: ActivityDelete, byActor: ActorModel) {
2890b615 13 const objectUrl = typeof activity.object === 'string' ? activity.object : activity.object.id
7a7724e6 14
f05a1c30 15 if (activity.actor === objectUrl) {
e587e0ec
C
16 // We need more attributes (all the account and channel)
17 const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
f05a1c30 18
e587e0ec
C
19 if (byActorFull.type === 'Person') {
20 if (!byActorFull.Account) throw new Error('Actor ' + byActorFull.url + ' is a person but we cannot find it in database.')
7a7724e6 21
e587e0ec
C
22 byActorFull.Account.Actor = await byActorFull.Account.$get('Actor') as ActorModel
23 return retryTransactionWrapper(processDeleteAccount, byActorFull.Account)
24 } else if (byActorFull.type === 'Group') {
25 if (!byActorFull.VideoChannel) throw new Error('Actor ' + byActorFull.url + ' is a group but we cannot find it in database.')
50d6de9c 26
e587e0ec
C
27 byActorFull.VideoChannel.Actor = await byActorFull.VideoChannel.$get('Actor') as ActorModel
28 return retryTransactionWrapper(processDeleteVideoChannel, byActorFull.VideoChannel)
7a7724e6
C
29 }
30 }
31
32 {
2890b615 33 const videoCommentInstance = await VideoCommentModel.loadByUrlAndPopulateAccount(objectUrl)
4cb6d457 34 if (videoCommentInstance) {
e587e0ec 35 return retryTransactionWrapper(processDeleteVideoComment, byActor, videoCommentInstance, activity)
4cb6d457
C
36 }
37 }
38
39 {
2890b615 40 const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(objectUrl)
4cb6d457 41 if (videoInstance) {
a2377d15
C
42 if (videoInstance.isOwned()) throw new Error(`Remote instance cannot delete owned video ${videoInstance.url}.`)
43
e587e0ec 44 return retryTransactionWrapper(processDeleteVideo, byActor, videoInstance)
7a7724e6
C
45 }
46 }
47
bcec136e 48 return undefined
7a7724e6
C
49}
50
51// ---------------------------------------------------------------------------
52
53export {
54 processDeleteActivity
55}
56
57// ---------------------------------------------------------------------------
58
50d6de9c 59async function processDeleteVideo (actor: ActorModel, videoToDelete: VideoModel) {
7a7724e6
C
60 logger.debug('Removing remote video "%s".', videoToDelete.uuid)
61
3fd3ab2d 62 await sequelizeTypescript.transaction(async t => {
50d6de9c
C
63 if (videoToDelete.VideoChannel.Account.Actor.id !== actor.id) {
64 throw new Error('Account ' + actor.url + ' does not own video channel ' + videoToDelete.VideoChannel.Actor.url)
7a7724e6
C
65 }
66
67 await videoToDelete.destroy({ transaction: t })
68 })
69
70 logger.info('Remote video with uuid %s removed.', videoToDelete.uuid)
71}
72
50d6de9c 73async function processDeleteAccount (accountToRemove: AccountModel) {
50d6de9c 74 logger.debug('Removing remote account "%s".', accountToRemove.Actor.uuid)
7a7724e6 75
3fd3ab2d 76 await sequelizeTypescript.transaction(async t => {
50d6de9c 77 await accountToRemove.destroy({ transaction: t })
7a7724e6
C
78 })
79
50d6de9c 80 logger.info('Remote account with uuid %s removed.', accountToRemove.Actor.uuid)
7a7724e6
C
81}
82
50d6de9c 83async function processDeleteVideoChannel (videoChannelToRemove: VideoChannelModel) {
50d6de9c 84 logger.debug('Removing remote video channel "%s".', videoChannelToRemove.Actor.uuid)
7a7724e6 85
3fd3ab2d 86 await sequelizeTypescript.transaction(async t => {
50d6de9c 87 await videoChannelToRemove.destroy({ transaction: t })
7a7724e6
C
88 })
89
50d6de9c 90 logger.info('Remote video channel with uuid %s removed.', videoChannelToRemove.Actor.uuid)
7a7724e6 91}
4cb6d457 92
90d4bb81 93function processDeleteVideoComment (byActor: ActorModel, videoComment: VideoCommentModel, activity: ActivityDelete) {
4cb6d457
C
94 logger.debug('Removing remote video comment "%s".', videoComment.url)
95
96 return sequelizeTypescript.transaction(async t => {
12ba460e
C
97 if (videoComment.Account.id !== byActor.Account.id) {
98 throw new Error('Account ' + byActor.url + ' does not own video comment ' + videoComment.url)
99 }
100
4cb6d457
C
101 await videoComment.destroy({ transaction: t })
102
73c08093
C
103 if (videoComment.Video.isOwned()) {
104 // Don't resend the activity to the sender
105 const exceptions = [ byActor ]
659edaa6 106 await forwardVideoRelatedActivity(activity, t, exceptions, videoComment.Video)
73c08093
C
107 }
108
4cb6d457
C
109 logger.info('Remote video comment %s removed.', videoComment.url)
110 })
111}