]>
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 | import { forwardActivity } from '../send/utils' | |
12 | ||
13 | async function processDeleteActivity (activity: ActivityDelete) { | |
14 | const objectUrl = typeof activity.object === 'string' ? activity.object : activity.object.id | |
15 | ||
16 | if (activity.actor === objectUrl) { | |
17 | let actor = await ActorModel.loadByUrl(activity.actor) | |
18 | if (!actor) return undefined | |
19 | ||
20 | if (actor.type === 'Person') { | |
21 | if (!actor.Account) throw new Error('Actor ' + actor.url + ' is a person but we cannot find it in database.') | |
22 | ||
23 | actor.Account.Actor = await actor.Account.$get('Actor') as ActorModel | |
24 | return retryTransactionWrapper(processDeleteAccount, actor.Account) | |
25 | } else if (actor.type === 'Group') { | |
26 | if (!actor.VideoChannel) throw new Error('Actor ' + actor.url + ' is a group but we cannot find it in database.') | |
27 | ||
28 | actor.VideoChannel.Actor = await actor.VideoChannel.$get('Actor') as ActorModel | |
29 | return retryTransactionWrapper(processDeleteVideoChannel, actor.VideoChannel) | |
30 | } | |
31 | } | |
32 | ||
33 | const actor = await getOrCreateActorAndServerAndModel(activity.actor) | |
34 | { | |
35 | const videoCommentInstance = await VideoCommentModel.loadByUrlAndPopulateAccount(objectUrl) | |
36 | if (videoCommentInstance) { | |
37 | return retryTransactionWrapper(processDeleteVideoComment, actor, videoCommentInstance, activity) | |
38 | } | |
39 | } | |
40 | ||
41 | { | |
42 | const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(objectUrl) | |
43 | if (videoInstance) { | |
44 | return retryTransactionWrapper(processDeleteVideo, actor, videoInstance) | |
45 | } | |
46 | } | |
47 | ||
48 | return undefined | |
49 | } | |
50 | ||
51 | // --------------------------------------------------------------------------- | |
52 | ||
53 | export { | |
54 | processDeleteActivity | |
55 | } | |
56 | ||
57 | // --------------------------------------------------------------------------- | |
58 | ||
59 | async function processDeleteVideo (actor: ActorModel, videoToDelete: VideoModel) { | |
60 | logger.debug('Removing remote video "%s".', videoToDelete.uuid) | |
61 | ||
62 | await sequelizeTypescript.transaction(async t => { | |
63 | if (videoToDelete.VideoChannel.Account.Actor.id !== actor.id) { | |
64 | throw new Error('Account ' + actor.url + ' does not own video channel ' + videoToDelete.VideoChannel.Actor.url) | |
65 | } | |
66 | ||
67 | await videoToDelete.destroy({ transaction: t }) | |
68 | }) | |
69 | ||
70 | logger.info('Remote video with uuid %s removed.', videoToDelete.uuid) | |
71 | } | |
72 | ||
73 | async function processDeleteAccount (accountToRemove: AccountModel) { | |
74 | logger.debug('Removing remote account "%s".', accountToRemove.Actor.uuid) | |
75 | ||
76 | await sequelizeTypescript.transaction(async t => { | |
77 | await accountToRemove.destroy({ transaction: t }) | |
78 | }) | |
79 | ||
80 | logger.info('Remote account with uuid %s removed.', accountToRemove.Actor.uuid) | |
81 | } | |
82 | ||
83 | async function processDeleteVideoChannel (videoChannelToRemove: VideoChannelModel) { | |
84 | logger.debug('Removing remote video channel "%s".', videoChannelToRemove.Actor.uuid) | |
85 | ||
86 | await sequelizeTypescript.transaction(async t => { | |
87 | await videoChannelToRemove.destroy({ transaction: t }) | |
88 | }) | |
89 | ||
90 | logger.info('Remote video channel with uuid %s removed.', videoChannelToRemove.Actor.uuid) | |
91 | } | |
92 | ||
93 | function processDeleteVideoComment (byActor: ActorModel, videoComment: VideoCommentModel, activity: ActivityDelete) { | |
94 | logger.debug('Removing remote video comment "%s".', videoComment.url) | |
95 | ||
96 | return sequelizeTypescript.transaction(async t => { | |
97 | await videoComment.destroy({ transaction: t }) | |
98 | ||
99 | if (videoComment.Video.isOwned()) { | |
100 | // Don't resend the activity to the sender | |
101 | const exceptions = [ byActor ] | |
102 | await forwardActivity(activity, t, exceptions) | |
103 | } | |
104 | ||
105 | logger.info('Remote video comment %s removed.', videoComment.url) | |
106 | }) | |
107 | } |