]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/process-delete.ts
Lazy load avatars
[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 import { APProcessorOptions } from '../../../typings/activitypub-processor.model'
13 import { SignatureActorModel } from '../../../typings/models'
14
15 async function processDeleteActivity (options: APProcessorOptions<ActivityDelete>) {
16 const { activity, byActor } = options
17
18 const objectUrl = typeof activity.object === 'string' ? activity.object : activity.object.id
19
20 if (activity.actor === objectUrl) {
21 // We need more attributes (all the account and channel)
22 const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
23
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.')
26
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.')
31
32 byActorFull.VideoChannel.Actor = await byActorFull.VideoChannel.$get('Actor') as ActorModel
33 return retryTransactionWrapper(processDeleteVideoChannel, byActorFull.VideoChannel)
34 }
35 }
36
37 {
38 const videoCommentInstance = await VideoCommentModel.loadByUrlAndPopulateAccountAndVideo(objectUrl)
39 if (videoCommentInstance) {
40 return retryTransactionWrapper(processDeleteVideoComment, byActor, videoCommentInstance, activity)
41 }
42 }
43
44 {
45 const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(objectUrl)
46 if (videoInstance) {
47 if (videoInstance.isOwned()) throw new Error(`Remote instance cannot delete owned video ${videoInstance.url}.`)
48
49 return retryTransactionWrapper(processDeleteVideo, byActor, videoInstance)
50 }
51 }
52
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
62 return undefined
63 }
64
65 // ---------------------------------------------------------------------------
66
67 export {
68 processDeleteActivity
69 }
70
71 // ---------------------------------------------------------------------------
72
73 async function processDeleteVideo (actor: ActorModel, videoToDelete: VideoModel) {
74 logger.debug('Removing remote video "%s".', videoToDelete.uuid)
75
76 await sequelizeTypescript.transaction(async t => {
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)
79 }
80
81 await videoToDelete.destroy({ transaction: t })
82 })
83
84 logger.info('Remote video with uuid %s removed.', videoToDelete.uuid)
85 }
86
87 async 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
101 async function processDeleteAccount (accountToRemove: AccountModel) {
102 logger.debug('Removing remote account "%s".', accountToRemove.Actor.url)
103
104 await sequelizeTypescript.transaction(async t => {
105 await accountToRemove.destroy({ transaction: t })
106 })
107
108 logger.info('Remote account %s removed.', accountToRemove.Actor.url)
109 }
110
111 async function processDeleteVideoChannel (videoChannelToRemove: VideoChannelModel) {
112 logger.debug('Removing remote video channel "%s".', videoChannelToRemove.Actor.url)
113
114 await sequelizeTypescript.transaction(async t => {
115 await videoChannelToRemove.destroy({ transaction: t })
116 })
117
118 logger.info('Remote video channel %s removed.', videoChannelToRemove.Actor.url)
119 }
120
121 function processDeleteVideoComment (byActor: SignatureActorModel, videoComment: VideoCommentModel, activity: ActivityDelete) {
122 logger.debug('Removing remote video comment "%s".', videoComment.url)
123
124 return sequelizeTypescript.transaction(async t => {
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}`)
127 }
128
129 await videoComment.destroy({ transaction: t })
130
131 if (videoComment.Video.isOwned()) {
132 // Don't resend the activity to the sender
133 const exceptions = [ byActor ]
134 await forwardVideoRelatedActivity(activity, t, exceptions, videoComment.Video)
135 }
136
137 logger.info('Remote video comment %s removed.', videoComment.url)
138 })
139 }