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