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