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