]>
Commit | Line | Data |
---|---|---|
3fd3ab2d C |
1 | import { ActivityDelete } from '../../../../shared/models/activitypub' |
2 | import { logger, retryTransactionWrapper } from '../../../helpers' | |
3 | import { sequelizeTypescript } from '../../../initializers' | |
4 | import { AccountModel } from '../../../models/account/account' | |
5 | import { VideoModel } from '../../../models/video/video' | |
6 | import { VideoChannelModel } from '../../../models/video/video-channel' | |
0f91ae62 | 7 | import { getOrCreateAccountAndServer } from '../account' |
7a7724e6 C |
8 | |
9 | async function processDeleteActivity (activity: ActivityDelete) { | |
0f91ae62 | 10 | const account = await getOrCreateAccountAndServer(activity.actor) |
7a7724e6 C |
11 | |
12 | if (account.url === activity.id) { | |
13 | return processDeleteAccount(account) | |
14 | } | |
15 | ||
16 | { | |
3fd3ab2d | 17 | let videoObject = await VideoModel.loadByUrlAndPopulateAccount(activity.id) |
7a7724e6 C |
18 | if (videoObject !== undefined) { |
19 | return processDeleteVideo(account, videoObject) | |
20 | } | |
21 | } | |
22 | ||
23 | { | |
3fd3ab2d | 24 | let videoChannelObject = await VideoChannelModel.loadByUrl(activity.id) |
7a7724e6 C |
25 | if (videoChannelObject !== undefined) { |
26 | return processDeleteVideoChannel(account, videoChannelObject) | |
27 | } | |
28 | } | |
29 | ||
79d5caf9 | 30 | return |
7a7724e6 C |
31 | } |
32 | ||
33 | // --------------------------------------------------------------------------- | |
34 | ||
35 | export { | |
36 | processDeleteActivity | |
37 | } | |
38 | ||
39 | // --------------------------------------------------------------------------- | |
40 | ||
3fd3ab2d | 41 | async function processDeleteVideo (account: AccountModel, videoToDelete: VideoModel) { |
7a7724e6 C |
42 | const options = { |
43 | arguments: [ account, videoToDelete ], | |
44 | errorMessage: 'Cannot remove the remote video with many retries.' | |
45 | } | |
46 | ||
47 | await retryTransactionWrapper(deleteRemoteVideo, options) | |
48 | } | |
49 | ||
3fd3ab2d | 50 | async function deleteRemoteVideo (account: AccountModel, videoToDelete: VideoModel) { |
7a7724e6 C |
51 | logger.debug('Removing remote video "%s".', videoToDelete.uuid) |
52 | ||
3fd3ab2d | 53 | await sequelizeTypescript.transaction(async t => { |
7a7724e6 C |
54 | if (videoToDelete.VideoChannel.Account.id !== account.id) { |
55 | throw new Error('Account ' + account.url + ' does not own video channel ' + videoToDelete.VideoChannel.url) | |
56 | } | |
57 | ||
58 | await videoToDelete.destroy({ transaction: t }) | |
59 | }) | |
60 | ||
61 | logger.info('Remote video with uuid %s removed.', videoToDelete.uuid) | |
62 | } | |
63 | ||
3fd3ab2d | 64 | async function processDeleteVideoChannel (account: AccountModel, videoChannelToRemove: VideoChannelModel) { |
7a7724e6 C |
65 | const options = { |
66 | arguments: [ account, videoChannelToRemove ], | |
67 | errorMessage: 'Cannot remove the remote video channel with many retries.' | |
68 | } | |
69 | ||
70 | await retryTransactionWrapper(deleteRemoteVideoChannel, options) | |
71 | } | |
72 | ||
3fd3ab2d | 73 | async function deleteRemoteVideoChannel (account: AccountModel, videoChannelToRemove: VideoChannelModel) { |
7a7724e6 C |
74 | logger.debug('Removing remote video channel "%s".', videoChannelToRemove.uuid) |
75 | ||
3fd3ab2d | 76 | await sequelizeTypescript.transaction(async t => { |
7a7724e6 C |
77 | if (videoChannelToRemove.Account.id !== account.id) { |
78 | throw new Error('Account ' + account.url + ' does not own video channel ' + videoChannelToRemove.url) | |
79 | } | |
80 | ||
81 | await videoChannelToRemove.destroy({ transaction: t }) | |
82 | }) | |
83 | ||
84 | logger.info('Remote video channel with uuid %s removed.', videoChannelToRemove.uuid) | |
85 | } | |
86 | ||
3fd3ab2d | 87 | async function processDeleteAccount (accountToRemove: AccountModel) { |
7a7724e6 C |
88 | const options = { |
89 | arguments: [ accountToRemove ], | |
90 | errorMessage: 'Cannot remove the remote account with many retries.' | |
91 | } | |
92 | ||
93 | await retryTransactionWrapper(deleteRemoteAccount, options) | |
94 | } | |
95 | ||
3fd3ab2d | 96 | async function deleteRemoteAccount (accountToRemove: AccountModel) { |
7a7724e6 C |
97 | logger.debug('Removing remote account "%s".', accountToRemove.uuid) |
98 | ||
3fd3ab2d | 99 | await sequelizeTypescript.transaction(async t => { |
7a7724e6 C |
100 | await accountToRemove.destroy({ transaction: t }) |
101 | }) | |
102 | ||
103 | logger.info('Remote account with uuid %s removed.', accountToRemove.uuid) | |
104 | } |