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