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