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