]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/process-delete.ts
Merge branch 'master' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-delete.ts
1 import { ActivityDelete } from '../../../../shared/models/activitypub'
2 import { retryTransactionWrapper } from '../../../helpers/database-utils'
3 import { logger } from '../../../helpers/logger'
4 import { sequelizeTypescript } from '../../../initializers'
5 import { AccountModel } from '../../../models/account/account'
6 import { ActorModel } from '../../../models/activitypub/actor'
7 import { VideoModel } from '../../../models/video/video'
8 import { VideoChannelModel } from '../../../models/video/video-channel'
9 import { VideoCommentModel } from '../../../models/video/video-comment'
10 import { forwardVideoRelatedActivity } from '../send/utils'
11 import { VideoPlaylistModel } from '../../../models/video/video-playlist'
12
13 async function processDeleteActivity (activity: ActivityDelete, byActor: ActorModel) {
14 const objectUrl = typeof activity.object === 'string' ? activity.object : activity.object.id
15
16 if (activity.actor === objectUrl) {
17 // We need more attributes (all the account and channel)
18 const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
19
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.')
22
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.')
27
28 byActorFull.VideoChannel.Actor = await byActorFull.VideoChannel.$get('Actor') as ActorModel
29 return retryTransactionWrapper(processDeleteVideoChannel, byActorFull.VideoChannel)
30 }
31 }
32
33 {
34 const videoCommentInstance = await VideoCommentModel.loadByUrlAndPopulateAccount(objectUrl)
35 if (videoCommentInstance) {
36 return retryTransactionWrapper(processDeleteVideoComment, byActor, videoCommentInstance, activity)
37 }
38 }
39
40 {
41 const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(objectUrl)
42 if (videoInstance) {
43 if (videoInstance.isOwned()) throw new Error(`Remote instance cannot delete owned video ${videoInstance.url}.`)
44
45 return retryTransactionWrapper(processDeleteVideo, byActor, videoInstance)
46 }
47 }
48
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
58 return undefined
59 }
60
61 // ---------------------------------------------------------------------------
62
63 export {
64 processDeleteActivity
65 }
66
67 // ---------------------------------------------------------------------------
68
69 async function processDeleteVideo (actor: ActorModel, videoToDelete: VideoModel) {
70 logger.debug('Removing remote video "%s".', videoToDelete.uuid)
71
72 await sequelizeTypescript.transaction(async t => {
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)
75 }
76
77 await videoToDelete.destroy({ transaction: t })
78 })
79
80 logger.info('Remote video with uuid %s removed.', videoToDelete.uuid)
81 }
82
83 async 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
97 async function processDeleteAccount (accountToRemove: AccountModel) {
98 logger.debug('Removing remote account "%s".', accountToRemove.Actor.uuid)
99
100 await sequelizeTypescript.transaction(async t => {
101 await accountToRemove.destroy({ transaction: t })
102 })
103
104 logger.info('Remote account with uuid %s removed.', accountToRemove.Actor.uuid)
105 }
106
107 async function processDeleteVideoChannel (videoChannelToRemove: VideoChannelModel) {
108 logger.debug('Removing remote video channel "%s".', videoChannelToRemove.Actor.uuid)
109
110 await sequelizeTypescript.transaction(async t => {
111 await videoChannelToRemove.destroy({ transaction: t })
112 })
113
114 logger.info('Remote video channel with uuid %s removed.', videoChannelToRemove.Actor.uuid)
115 }
116
117 function processDeleteVideoComment (byActor: ActorModel, videoComment: VideoCommentModel, activity: ActivityDelete) {
118 logger.debug('Removing remote video comment "%s".', videoComment.url)
119
120 return sequelizeTypescript.transaction(async t => {
121 if (videoComment.Account.id !== byActor.Account.id) {
122 throw new Error('Account ' + byActor.url + ' does not own video comment ' + videoComment.url)
123 }
124
125 await videoComment.destroy({ transaction: t })
126
127 if (videoComment.Video.isOwned()) {
128 // Don't resend the activity to the sender
129 const exceptions = [ byActor ]
130 await forwardVideoRelatedActivity(activity, t, exceptions, videoComment.Video)
131 }
132
133 logger.info('Remote video comment %s removed.', videoComment.url)
134 })
135 }