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