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