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