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