]>
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 C |
4 | import { sequelizeTypescript } from '../../../initializers' |
5 | import { AccountModel } from '../../../models/account/account' | |
50d6de9c | 6 | import { ActorModel } from '../../../models/activitypub/actor' |
3fd3ab2d C |
7 | import { VideoModel } from '../../../models/video/video' |
8 | import { VideoChannelModel } from '../../../models/video/video-channel' | |
4cb6d457 | 9 | import { VideoCommentModel } from '../../../models/video/video-comment' |
50d6de9c | 10 | import { getOrCreateActorAndServerAndModel } from '../actor' |
7a7724e6 C |
11 | |
12 | async function processDeleteActivity (activity: ActivityDelete) { | |
2890b615 | 13 | const objectUrl = typeof activity.object === 'string' ? activity.object : activity.object.id |
7a7724e6 | 14 | |
f05a1c30 C |
15 | if (activity.actor === objectUrl) { |
16 | let actor = await ActorModel.loadByUrl(activity.actor) | |
17 | if (!actor) return | |
18 | ||
50d6de9c C |
19 | if (actor.type === 'Person') { |
20 | if (!actor.Account) throw new Error('Actor ' + actor.url + ' is a person but we cannot find it in database.') | |
7a7724e6 | 21 | |
f05a1c30 | 22 | actor.Account.Actor = await actor.Account.$get('Actor') as ActorModel |
50d6de9c C |
23 | return processDeleteAccount(actor.Account) |
24 | } else if (actor.type === 'Group') { | |
25 | if (!actor.VideoChannel) throw new Error('Actor ' + actor.url + ' is a group but we cannot find it in database.') | |
26 | ||
f05a1c30 | 27 | actor.VideoChannel.Actor = await actor.VideoChannel.$get('Actor') as ActorModel |
50d6de9c | 28 | return processDeleteVideoChannel(actor.VideoChannel) |
7a7724e6 C |
29 | } |
30 | } | |
31 | ||
f05a1c30 | 32 | const actor = await getOrCreateActorAndServerAndModel(activity.actor) |
7a7724e6 | 33 | { |
2890b615 | 34 | const videoCommentInstance = await VideoCommentModel.loadByUrlAndPopulateAccount(objectUrl) |
4cb6d457 C |
35 | if (videoCommentInstance) { |
36 | return processDeleteVideoComment(actor, videoCommentInstance) | |
37 | } | |
38 | } | |
39 | ||
40 | { | |
2890b615 | 41 | const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(objectUrl) |
4cb6d457 C |
42 | if (videoInstance) { |
43 | return processDeleteVideo(actor, videoInstance) | |
7a7724e6 C |
44 | } |
45 | } | |
46 | ||
79d5caf9 | 47 | return |
7a7724e6 C |
48 | } |
49 | ||
50 | // --------------------------------------------------------------------------- | |
51 | ||
52 | export { | |
53 | processDeleteActivity | |
54 | } | |
55 | ||
56 | // --------------------------------------------------------------------------- | |
57 | ||
50d6de9c | 58 | async function processDeleteVideo (actor: ActorModel, videoToDelete: VideoModel) { |
7a7724e6 | 59 | const options = { |
50d6de9c | 60 | arguments: [ actor, videoToDelete ], |
7a7724e6 C |
61 | errorMessage: 'Cannot remove the remote video with many retries.' |
62 | } | |
63 | ||
64 | await retryTransactionWrapper(deleteRemoteVideo, options) | |
65 | } | |
66 | ||
50d6de9c | 67 | async function deleteRemoteVideo (actor: ActorModel, videoToDelete: VideoModel) { |
7a7724e6 C |
68 | logger.debug('Removing remote video "%s".', videoToDelete.uuid) |
69 | ||
3fd3ab2d | 70 | await sequelizeTypescript.transaction(async t => { |
50d6de9c C |
71 | if (videoToDelete.VideoChannel.Account.Actor.id !== actor.id) { |
72 | throw new Error('Account ' + actor.url + ' does not own video channel ' + videoToDelete.VideoChannel.Actor.url) | |
7a7724e6 C |
73 | } |
74 | ||
75 | await videoToDelete.destroy({ transaction: t }) | |
76 | }) | |
77 | ||
78 | logger.info('Remote video with uuid %s removed.', videoToDelete.uuid) | |
79 | } | |
80 | ||
50d6de9c | 81 | async function processDeleteAccount (accountToRemove: AccountModel) { |
7a7724e6 | 82 | const options = { |
50d6de9c C |
83 | arguments: [ accountToRemove ], |
84 | errorMessage: 'Cannot remove the remote account with many retries.' | |
7a7724e6 C |
85 | } |
86 | ||
50d6de9c | 87 | await retryTransactionWrapper(deleteRemoteAccount, options) |
7a7724e6 C |
88 | } |
89 | ||
50d6de9c C |
90 | async function deleteRemoteAccount (accountToRemove: AccountModel) { |
91 | logger.debug('Removing remote account "%s".', accountToRemove.Actor.uuid) | |
7a7724e6 | 92 | |
3fd3ab2d | 93 | await sequelizeTypescript.transaction(async t => { |
50d6de9c | 94 | await accountToRemove.destroy({ transaction: t }) |
7a7724e6 C |
95 | }) |
96 | ||
50d6de9c | 97 | logger.info('Remote account with uuid %s removed.', accountToRemove.Actor.uuid) |
7a7724e6 C |
98 | } |
99 | ||
50d6de9c | 100 | async function processDeleteVideoChannel (videoChannelToRemove: VideoChannelModel) { |
7a7724e6 | 101 | const options = { |
50d6de9c C |
102 | arguments: [ videoChannelToRemove ], |
103 | errorMessage: 'Cannot remove the remote video channel with many retries.' | |
7a7724e6 C |
104 | } |
105 | ||
50d6de9c | 106 | await retryTransactionWrapper(deleteRemoteVideoChannel, options) |
7a7724e6 C |
107 | } |
108 | ||
50d6de9c C |
109 | async function deleteRemoteVideoChannel (videoChannelToRemove: VideoChannelModel) { |
110 | logger.debug('Removing remote video channel "%s".', videoChannelToRemove.Actor.uuid) | |
7a7724e6 | 111 | |
3fd3ab2d | 112 | await sequelizeTypescript.transaction(async t => { |
50d6de9c | 113 | await videoChannelToRemove.destroy({ transaction: t }) |
7a7724e6 C |
114 | }) |
115 | ||
50d6de9c | 116 | logger.info('Remote video channel with uuid %s removed.', videoChannelToRemove.Actor.uuid) |
7a7724e6 | 117 | } |
4cb6d457 C |
118 | |
119 | async function processDeleteVideoComment (actor: ActorModel, videoComment: VideoCommentModel) { | |
120 | const options = { | |
121 | arguments: [ actor, videoComment ], | |
122 | errorMessage: 'Cannot remove the remote video comment with many retries.' | |
123 | } | |
124 | ||
125 | await retryTransactionWrapper(deleteRemoteVideoComment, options) | |
126 | } | |
127 | ||
128 | function deleteRemoteVideoComment (actor: ActorModel, videoComment: VideoCommentModel) { | |
129 | logger.debug('Removing remote video comment "%s".', videoComment.url) | |
130 | ||
131 | return sequelizeTypescript.transaction(async t => { | |
132 | await videoComment.destroy({ transaction: t }) | |
133 | ||
134 | logger.info('Remote video comment %s removed.', videoComment.url) | |
135 | }) | |
136 | } |