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