diff options
author | Chocobozzz <me@florianbigard.com> | 2023-07-31 14:34:36 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2023-08-11 15:02:33 +0200 |
commit | 3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch) | |
tree | e4510b39bdac9c318fdb4b47018d08f15368b8f0 /server/lib/activitypub/process/process-delete.ts | |
parent | 04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff) | |
download | PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.gz PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.zst PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.zip |
Migrate server to ESM
Sorry for the very big commit that may lead to git log issues and merge
conflicts, but it's a major step forward:
* Server can be faster at startup because imports() are async and we can
easily lazy import big modules
* Angular doesn't seem to support ES import (with .js extension), so we
had to correctly organize peertube into a monorepo:
* Use yarn workspace feature
* Use typescript reference projects for dependencies
* Shared projects have been moved into "packages", each one is now a
node module (with a dedicated package.json/tsconfig.json)
* server/tools have been moved into apps/ and is now a dedicated app
bundled and published on NPM so users don't have to build peertube
cli tools manually
* server/tests have been moved into packages/ so we don't compile
them every time we want to run the server
* Use isolatedModule option:
* Had to move from const enum to const
(https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums)
* Had to explictely specify "type" imports when used in decorators
* Prefer tsx (that uses esbuild under the hood) instead of ts-node to
load typescript files (tests with mocha or scripts):
* To reduce test complexity as esbuild doesn't support decorator
metadata, we only test server files that do not import server
models
* We still build tests files into js files for a faster CI
* Remove unmaintained peertube CLI import script
* Removed some barrels to speed up execution (less imports)
Diffstat (limited to 'server/lib/activitypub/process/process-delete.ts')
-rw-r--r-- | server/lib/activitypub/process/process-delete.ts | 153 |
1 files changed, 0 insertions, 153 deletions
diff --git a/server/lib/activitypub/process/process-delete.ts b/server/lib/activitypub/process/process-delete.ts deleted file mode 100644 index ac0e7e235..000000000 --- a/server/lib/activitypub/process/process-delete.ts +++ /dev/null | |||
@@ -1,153 +0,0 @@ | |||
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/database' | ||
5 | import { ActorModel } from '../../../models/actor/actor' | ||
6 | import { VideoModel } from '../../../models/video/video' | ||
7 | import { VideoCommentModel } from '../../../models/video/video-comment' | ||
8 | import { VideoPlaylistModel } from '../../../models/video/video-playlist' | ||
9 | import { APProcessorOptions } from '../../../types/activitypub-processor.model' | ||
10 | import { | ||
11 | MAccountActor, | ||
12 | MActor, | ||
13 | MActorFull, | ||
14 | MActorSignature, | ||
15 | MChannelAccountActor, | ||
16 | MChannelActor, | ||
17 | MCommentOwnerVideo | ||
18 | } from '../../../types/models' | ||
19 | import { forwardVideoRelatedActivity } from '../send/shared/send-utils' | ||
20 | |||
21 | async function processDeleteActivity (options: APProcessorOptions<ActivityDelete>) { | ||
22 | const { activity, byActor } = options | ||
23 | |||
24 | const objectUrl = typeof activity.object === 'string' ? activity.object : activity.object.id | ||
25 | |||
26 | if (activity.actor === objectUrl) { | ||
27 | // We need more attributes (all the account and channel) | ||
28 | const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url) | ||
29 | |||
30 | if (byActorFull.type === 'Person') { | ||
31 | if (!byActorFull.Account) throw new Error('Actor ' + byActorFull.url + ' is a person but we cannot find it in database.') | ||
32 | |||
33 | const accountToDelete = byActorFull.Account as MAccountActor | ||
34 | accountToDelete.Actor = byActorFull | ||
35 | |||
36 | return retryTransactionWrapper(processDeleteAccount, accountToDelete) | ||
37 | } else if (byActorFull.type === 'Group') { | ||
38 | if (!byActorFull.VideoChannel) throw new Error('Actor ' + byActorFull.url + ' is a group but we cannot find it in database.') | ||
39 | |||
40 | const channelToDelete = byActorFull.VideoChannel as MChannelAccountActor & { Actor: MActorFull } | ||
41 | channelToDelete.Actor = byActorFull | ||
42 | return retryTransactionWrapper(processDeleteVideoChannel, channelToDelete) | ||
43 | } | ||
44 | } | ||
45 | |||
46 | { | ||
47 | const videoCommentInstance = await VideoCommentModel.loadByUrlAndPopulateAccountAndVideo(objectUrl) | ||
48 | if (videoCommentInstance) { | ||
49 | return retryTransactionWrapper(processDeleteVideoComment, byActor, videoCommentInstance, activity) | ||
50 | } | ||
51 | } | ||
52 | |||
53 | { | ||
54 | const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(objectUrl) | ||
55 | if (videoInstance) { | ||
56 | if (videoInstance.isOwned()) throw new Error(`Remote instance cannot delete owned video ${videoInstance.url}.`) | ||
57 | |||
58 | return retryTransactionWrapper(processDeleteVideo, byActor, videoInstance) | ||
59 | } | ||
60 | } | ||
61 | |||
62 | { | ||
63 | const videoPlaylist = await VideoPlaylistModel.loadByUrlAndPopulateAccount(objectUrl) | ||
64 | if (videoPlaylist) { | ||
65 | if (videoPlaylist.isOwned()) throw new Error(`Remote instance cannot delete owned playlist ${videoPlaylist.url}.`) | ||
66 | |||
67 | return retryTransactionWrapper(processDeleteVideoPlaylist, byActor, videoPlaylist) | ||
68 | } | ||
69 | } | ||
70 | |||
71 | return undefined | ||
72 | } | ||
73 | |||
74 | // --------------------------------------------------------------------------- | ||
75 | |||
76 | export { | ||
77 | processDeleteActivity | ||
78 | } | ||
79 | |||
80 | // --------------------------------------------------------------------------- | ||
81 | |||
82 | async function processDeleteVideo (actor: MActor, videoToDelete: VideoModel) { | ||
83 | logger.debug('Removing remote video "%s".', videoToDelete.uuid) | ||
84 | |||
85 | await sequelizeTypescript.transaction(async t => { | ||
86 | if (videoToDelete.VideoChannel.Account.Actor.id !== actor.id) { | ||
87 | throw new Error('Account ' + actor.url + ' does not own video channel ' + videoToDelete.VideoChannel.Actor.url) | ||
88 | } | ||
89 | |||
90 | await videoToDelete.destroy({ transaction: t }) | ||
91 | }) | ||
92 | |||
93 | logger.info('Remote video with uuid %s removed.', videoToDelete.uuid) | ||
94 | } | ||
95 | |||
96 | async function processDeleteVideoPlaylist (actor: MActor, playlistToDelete: VideoPlaylistModel) { | ||
97 | logger.debug('Removing remote video playlist "%s".', playlistToDelete.uuid) | ||
98 | |||
99 | await sequelizeTypescript.transaction(async t => { | ||
100 | if (playlistToDelete.OwnerAccount.Actor.id !== actor.id) { | ||
101 | throw new Error('Account ' + actor.url + ' does not own video playlist ' + playlistToDelete.url) | ||
102 | } | ||
103 | |||
104 | await playlistToDelete.destroy({ transaction: t }) | ||
105 | }) | ||
106 | |||
107 | logger.info('Remote video playlist with uuid %s removed.', playlistToDelete.uuid) | ||
108 | } | ||
109 | |||
110 | async function processDeleteAccount (accountToRemove: MAccountActor) { | ||
111 | logger.debug('Removing remote account "%s".', accountToRemove.Actor.url) | ||
112 | |||
113 | await sequelizeTypescript.transaction(async t => { | ||
114 | await accountToRemove.destroy({ transaction: t }) | ||
115 | }) | ||
116 | |||
117 | logger.info('Remote account %s removed.', accountToRemove.Actor.url) | ||
118 | } | ||
119 | |||
120 | async function processDeleteVideoChannel (videoChannelToRemove: MChannelActor) { | ||
121 | logger.debug('Removing remote video channel "%s".', videoChannelToRemove.Actor.url) | ||
122 | |||
123 | await sequelizeTypescript.transaction(async t => { | ||
124 | await videoChannelToRemove.destroy({ transaction: t }) | ||
125 | }) | ||
126 | |||
127 | logger.info('Remote video channel %s removed.', videoChannelToRemove.Actor.url) | ||
128 | } | ||
129 | |||
130 | function processDeleteVideoComment (byActor: MActorSignature, videoComment: MCommentOwnerVideo, activity: ActivityDelete) { | ||
131 | // Already deleted | ||
132 | if (videoComment.isDeleted()) return Promise.resolve() | ||
133 | |||
134 | logger.debug('Removing remote video comment "%s".', videoComment.url) | ||
135 | |||
136 | return sequelizeTypescript.transaction(async t => { | ||
137 | if (byActor.Account.id !== videoComment.Account.id && byActor.Account.id !== videoComment.Video.VideoChannel.accountId) { | ||
138 | throw new Error(`Account ${byActor.url} does not own video comment ${videoComment.url} or video ${videoComment.Video.url}`) | ||
139 | } | ||
140 | |||
141 | videoComment.markAsDeleted() | ||
142 | |||
143 | await videoComment.save({ transaction: t }) | ||
144 | |||
145 | if (videoComment.Video.isOwned()) { | ||
146 | // Don't resend the activity to the sender | ||
147 | const exceptions = [ byActor ] | ||
148 | await forwardVideoRelatedActivity(activity, t, exceptions, videoComment.Video) | ||
149 | } | ||
150 | |||
151 | logger.info('Remote video comment %s removed.', videoComment.url) | ||
152 | }) | ||
153 | } | ||