]>
Commit | Line | Data |
---|---|---|
3fd3ab2d | 1 | import { ActivityDelete } from '../../../../shared/models/activitypub' |
da854ddd C |
2 | import { retryTransactionWrapper } from '../../../helpers/database-utils' |
3 | import { logger } from '../../../helpers/logger' | |
3fd3ab2d | 4 | import { sequelizeTypescript } from '../../../initializers' |
50d6de9c | 5 | import { ActorModel } from '../../../models/activitypub/actor' |
3fd3ab2d | 6 | import { VideoModel } from '../../../models/video/video' |
4cb6d457 | 7 | import { VideoCommentModel } from '../../../models/video/video-comment' |
659edaa6 | 8 | import { forwardVideoRelatedActivity } from '../send/utils' |
df0b219d | 9 | import { VideoPlaylistModel } from '../../../models/video/video-playlist' |
1198edf4 | 10 | import { APProcessorOptions } from '../../../typings/activitypub-processor.model' |
453e83ea | 11 | import { MAccountActor, MActor, MActorSignature, MChannelActor, MChannelActorAccountActor } from '../../../typings/models' |
1198edf4 C |
12 | |
13 | async function processDeleteActivity (options: APProcessorOptions<ActivityDelete>) { | |
14 | const { activity, byActor } = options | |
7a7724e6 | 15 | |
2890b615 | 16 | const objectUrl = typeof activity.object === 'string' ? activity.object : activity.object.id |
7a7724e6 | 17 | |
f05a1c30 | 18 | if (activity.actor === objectUrl) { |
e587e0ec C |
19 | // We need more attributes (all the account and channel) |
20 | const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url) | |
f05a1c30 | 21 | |
e587e0ec C |
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.') | |
7a7724e6 | 24 | |
453e83ea C |
25 | const accountToDelete = byActorFull.Account as MAccountActor |
26 | accountToDelete.Actor = byActorFull | |
27 | ||
28 | return retryTransactionWrapper(processDeleteAccount, accountToDelete) | |
e587e0ec C |
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.') | |
50d6de9c | 31 | |
453e83ea C |
32 | const channelToDelete = byActorFull.VideoChannel as MChannelActorAccountActor |
33 | channelToDelete.Actor = byActorFull | |
34 | ||
35 | return retryTransactionWrapper(processDeleteVideoChannel, channelToDelete) | |
7a7724e6 C |
36 | } |
37 | } | |
38 | ||
39 | { | |
511765c9 | 40 | const videoCommentInstance = await VideoCommentModel.loadByUrlAndPopulateAccountAndVideo(objectUrl) |
4cb6d457 | 41 | if (videoCommentInstance) { |
e587e0ec | 42 | return retryTransactionWrapper(processDeleteVideoComment, byActor, videoCommentInstance, activity) |
4cb6d457 C |
43 | } |
44 | } | |
45 | ||
46 | { | |
2890b615 | 47 | const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(objectUrl) |
4cb6d457 | 48 | if (videoInstance) { |
a2377d15 C |
49 | if (videoInstance.isOwned()) throw new Error(`Remote instance cannot delete owned video ${videoInstance.url}.`) |
50 | ||
e587e0ec | 51 | return retryTransactionWrapper(processDeleteVideo, byActor, videoInstance) |
7a7724e6 C |
52 | } |
53 | } | |
54 | ||
df0b219d C |
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 | ||
bcec136e | 64 | return undefined |
7a7724e6 C |
65 | } |
66 | ||
67 | // --------------------------------------------------------------------------- | |
68 | ||
69 | export { | |
70 | processDeleteActivity | |
71 | } | |
72 | ||
73 | // --------------------------------------------------------------------------- | |
74 | ||
453e83ea | 75 | async function processDeleteVideo (actor: MActor, videoToDelete: VideoModel) { |
7a7724e6 C |
76 | logger.debug('Removing remote video "%s".', videoToDelete.uuid) |
77 | ||
3fd3ab2d | 78 | await sequelizeTypescript.transaction(async t => { |
50d6de9c C |
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) | |
7a7724e6 C |
81 | } |
82 | ||
83 | await videoToDelete.destroy({ transaction: t }) | |
84 | }) | |
85 | ||
86 | logger.info('Remote video with uuid %s removed.', videoToDelete.uuid) | |
87 | } | |
88 | ||
453e83ea | 89 | async function processDeleteVideoPlaylist (actor: MActor, playlistToDelete: VideoPlaylistModel) { |
df0b219d C |
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 | ||
453e83ea | 103 | async function processDeleteAccount (accountToRemove: MAccountActor) { |
57cfff78 | 104 | logger.debug('Removing remote account "%s".', accountToRemove.Actor.url) |
7a7724e6 | 105 | |
3fd3ab2d | 106 | await sequelizeTypescript.transaction(async t => { |
50d6de9c | 107 | await accountToRemove.destroy({ transaction: t }) |
7a7724e6 C |
108 | }) |
109 | ||
57cfff78 | 110 | logger.info('Remote account %s removed.', accountToRemove.Actor.url) |
7a7724e6 C |
111 | } |
112 | ||
453e83ea | 113 | async function processDeleteVideoChannel (videoChannelToRemove: MChannelActor) { |
57cfff78 | 114 | logger.debug('Removing remote video channel "%s".', videoChannelToRemove.Actor.url) |
7a7724e6 | 115 | |
3fd3ab2d | 116 | await sequelizeTypescript.transaction(async t => { |
50d6de9c | 117 | await videoChannelToRemove.destroy({ transaction: t }) |
7a7724e6 C |
118 | }) |
119 | ||
57cfff78 | 120 | logger.info('Remote video channel %s removed.', videoChannelToRemove.Actor.url) |
7a7724e6 | 121 | } |
4cb6d457 | 122 | |
453e83ea | 123 | function processDeleteVideoComment (byActor: MActorSignature, videoComment: VideoCommentModel, activity: ActivityDelete) { |
4cb6d457 C |
124 | logger.debug('Removing remote video comment "%s".', videoComment.url) |
125 | ||
126 | return sequelizeTypescript.transaction(async t => { | |
511765c9 C |
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}`) | |
12ba460e C |
129 | } |
130 | ||
4cb6d457 C |
131 | await videoComment.destroy({ transaction: t }) |
132 | ||
73c08093 C |
133 | if (videoComment.Video.isOwned()) { |
134 | // Don't resend the activity to the sender | |
135 | const exceptions = [ byActor ] | |
659edaa6 | 136 | await forwardVideoRelatedActivity(activity, t, exceptions, videoComment.Video) |
73c08093 C |
137 | } |
138 | ||
4cb6d457 C |
139 | logger.info('Remote video comment %s removed.', videoComment.url) |
140 | }) | |
141 | } |