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