]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-delete.ts
Stronger actor association typing in AP functions
[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'
df0b219d 11import { VideoPlaylistModel } from '../../../models/video/video-playlist'
1198edf4 12import { APProcessorOptions } from '../../../typings/activitypub-processor.model'
5224c394 13import { SignatureActorModel } from '../../../typings/models'
1198edf4
C
14
15async function processDeleteActivity (options: APProcessorOptions<ActivityDelete>) {
16 const { activity, byActor } = options
7a7724e6 17
2890b615 18 const objectUrl = typeof activity.object === 'string' ? activity.object : activity.object.id
7a7724e6 19
f05a1c30 20 if (activity.actor === objectUrl) {
e587e0ec
C
21 // We need more attributes (all the account and channel)
22 const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
f05a1c30 23
e587e0ec
C
24 if (byActorFull.type === 'Person') {
25 if (!byActorFull.Account) throw new Error('Actor ' + byActorFull.url + ' is a person but we cannot find it in database.')
7a7724e6 26
e587e0ec
C
27 byActorFull.Account.Actor = await byActorFull.Account.$get('Actor') as ActorModel
28 return retryTransactionWrapper(processDeleteAccount, byActorFull.Account)
29 } else if (byActorFull.type === 'Group') {
30 if (!byActorFull.VideoChannel) throw new Error('Actor ' + byActorFull.url + ' is a group but we cannot find it in database.')
50d6de9c 31
e587e0ec
C
32 byActorFull.VideoChannel.Actor = await byActorFull.VideoChannel.$get('Actor') as ActorModel
33 return retryTransactionWrapper(processDeleteVideoChannel, byActorFull.VideoChannel)
7a7724e6
C
34 }
35 }
36
37 {
511765c9 38 const videoCommentInstance = await VideoCommentModel.loadByUrlAndPopulateAccountAndVideo(objectUrl)
4cb6d457 39 if (videoCommentInstance) {
e587e0ec 40 return retryTransactionWrapper(processDeleteVideoComment, byActor, videoCommentInstance, activity)
4cb6d457
C
41 }
42 }
43
44 {
2890b615 45 const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(objectUrl)
4cb6d457 46 if (videoInstance) {
a2377d15
C
47 if (videoInstance.isOwned()) throw new Error(`Remote instance cannot delete owned video ${videoInstance.url}.`)
48
e587e0ec 49 return retryTransactionWrapper(processDeleteVideo, byActor, videoInstance)
7a7724e6
C
50 }
51 }
52
df0b219d
C
53 {
54 const videoPlaylist = await VideoPlaylistModel.loadByUrlAndPopulateAccount(objectUrl)
55 if (videoPlaylist) {
56 if (videoPlaylist.isOwned()) throw new Error(`Remote instance cannot delete owned playlist ${videoPlaylist.url}.`)
57
58 return retryTransactionWrapper(processDeleteVideoPlaylist, byActor, videoPlaylist)
59 }
60 }
61
bcec136e 62 return undefined
7a7724e6
C
63}
64
65// ---------------------------------------------------------------------------
66
67export {
68 processDeleteActivity
69}
70
71// ---------------------------------------------------------------------------
72
50d6de9c 73async function processDeleteVideo (actor: ActorModel, videoToDelete: VideoModel) {
7a7724e6
C
74 logger.debug('Removing remote video "%s".', videoToDelete.uuid)
75
3fd3ab2d 76 await sequelizeTypescript.transaction(async t => {
50d6de9c
C
77 if (videoToDelete.VideoChannel.Account.Actor.id !== actor.id) {
78 throw new Error('Account ' + actor.url + ' does not own video channel ' + videoToDelete.VideoChannel.Actor.url)
7a7724e6
C
79 }
80
81 await videoToDelete.destroy({ transaction: t })
82 })
83
84 logger.info('Remote video with uuid %s removed.', videoToDelete.uuid)
85}
86
df0b219d
C
87async function processDeleteVideoPlaylist (actor: ActorModel, playlistToDelete: VideoPlaylistModel) {
88 logger.debug('Removing remote video playlist "%s".', playlistToDelete.uuid)
89
90 await sequelizeTypescript.transaction(async t => {
91 if (playlistToDelete.OwnerAccount.Actor.id !== actor.id) {
92 throw new Error('Account ' + actor.url + ' does not own video playlist ' + playlistToDelete.url)
93 }
94
95 await playlistToDelete.destroy({ transaction: t })
96 })
97
98 logger.info('Remote video playlist with uuid %s removed.', playlistToDelete.uuid)
99}
100
50d6de9c 101async function processDeleteAccount (accountToRemove: AccountModel) {
57cfff78 102 logger.debug('Removing remote account "%s".', accountToRemove.Actor.url)
7a7724e6 103
3fd3ab2d 104 await sequelizeTypescript.transaction(async t => {
50d6de9c 105 await accountToRemove.destroy({ transaction: t })
7a7724e6
C
106 })
107
57cfff78 108 logger.info('Remote account %s removed.', accountToRemove.Actor.url)
7a7724e6
C
109}
110
50d6de9c 111async function processDeleteVideoChannel (videoChannelToRemove: VideoChannelModel) {
57cfff78 112 logger.debug('Removing remote video channel "%s".', videoChannelToRemove.Actor.url)
7a7724e6 113
3fd3ab2d 114 await sequelizeTypescript.transaction(async t => {
50d6de9c 115 await videoChannelToRemove.destroy({ transaction: t })
7a7724e6
C
116 })
117
57cfff78 118 logger.info('Remote video channel %s removed.', videoChannelToRemove.Actor.url)
7a7724e6 119}
4cb6d457 120
5224c394 121function processDeleteVideoComment (byActor: SignatureActorModel, videoComment: VideoCommentModel, activity: ActivityDelete) {
4cb6d457
C
122 logger.debug('Removing remote video comment "%s".', videoComment.url)
123
124 return sequelizeTypescript.transaction(async t => {
511765c9
C
125 if (byActor.Account.id !== videoComment.Account.id && byActor.Account.id !== videoComment.Video.VideoChannel.accountId) {
126 throw new Error(`Account ${byActor.url} does not own video comment ${videoComment.url} or video ${videoComment.Video.url}`)
12ba460e
C
127 }
128
4cb6d457
C
129 await videoComment.destroy({ transaction: t })
130
73c08093
C
131 if (videoComment.Video.isOwned()) {
132 // Don't resend the activity to the sender
133 const exceptions = [ byActor ]
659edaa6 134 await forwardVideoRelatedActivity(activity, t, exceptions, videoComment.Video)
73c08093
C
135 }
136
4cb6d457
C
137 logger.info('Remote video comment %s removed.', videoComment.url)
138 })
139}