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/video-blacklist.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/video-blacklist.ts')
-rw-r--r-- | server/lib/video-blacklist.ts | 145 |
1 files changed, 0 insertions, 145 deletions
diff --git a/server/lib/video-blacklist.ts b/server/lib/video-blacklist.ts deleted file mode 100644 index d5664a1b9..000000000 --- a/server/lib/video-blacklist.ts +++ /dev/null | |||
@@ -1,145 +0,0 @@ | |||
1 | import { Transaction } from 'sequelize' | ||
2 | import { afterCommitIfTransaction } from '@server/helpers/database-utils' | ||
3 | import { sequelizeTypescript } from '@server/initializers/database' | ||
4 | import { | ||
5 | MUser, | ||
6 | MVideoAccountLight, | ||
7 | MVideoBlacklist, | ||
8 | MVideoBlacklistVideo, | ||
9 | MVideoFullLight, | ||
10 | MVideoWithBlacklistLight | ||
11 | } from '@server/types/models' | ||
12 | import { LiveVideoError, UserRight, VideoBlacklistCreate, VideoBlacklistType } from '../../shared/models' | ||
13 | import { UserAdminFlag } from '../../shared/models/users/user-flag.model' | ||
14 | import { logger, loggerTagsFactory } from '../helpers/logger' | ||
15 | import { CONFIG } from '../initializers/config' | ||
16 | import { VideoBlacklistModel } from '../models/video/video-blacklist' | ||
17 | import { sendDeleteVideo } from './activitypub/send' | ||
18 | import { federateVideoIfNeeded } from './activitypub/videos' | ||
19 | import { LiveManager } from './live/live-manager' | ||
20 | import { Notifier } from './notifier' | ||
21 | import { Hooks } from './plugins/hooks' | ||
22 | |||
23 | const lTags = loggerTagsFactory('blacklist') | ||
24 | |||
25 | async function autoBlacklistVideoIfNeeded (parameters: { | ||
26 | video: MVideoWithBlacklistLight | ||
27 | user?: MUser | ||
28 | isRemote: boolean | ||
29 | isNew: boolean | ||
30 | isNewFile: boolean | ||
31 | notify?: boolean | ||
32 | transaction?: Transaction | ||
33 | }) { | ||
34 | const { video, user, isRemote, isNew, isNewFile, notify = true, transaction } = parameters | ||
35 | const doAutoBlacklist = await Hooks.wrapFun( | ||
36 | autoBlacklistNeeded, | ||
37 | { video, user, isRemote, isNew, isNewFile }, | ||
38 | 'filter:video.auto-blacklist.result' | ||
39 | ) | ||
40 | |||
41 | if (!doAutoBlacklist) return false | ||
42 | |||
43 | const videoBlacklistToCreate = { | ||
44 | videoId: video.id, | ||
45 | unfederated: true, | ||
46 | reason: 'Auto-blacklisted. Moderator review required.', | ||
47 | type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED | ||
48 | } | ||
49 | const [ videoBlacklist ] = await VideoBlacklistModel.findOrCreate<MVideoBlacklistVideo>({ | ||
50 | where: { | ||
51 | videoId: video.id | ||
52 | }, | ||
53 | defaults: videoBlacklistToCreate, | ||
54 | transaction | ||
55 | }) | ||
56 | video.VideoBlacklist = videoBlacklist | ||
57 | |||
58 | videoBlacklist.Video = video | ||
59 | |||
60 | if (notify) { | ||
61 | afterCommitIfTransaction(transaction, () => { | ||
62 | Notifier.Instance.notifyOnVideoAutoBlacklist(videoBlacklist) | ||
63 | }) | ||
64 | } | ||
65 | |||
66 | logger.info('Video %s auto-blacklisted.', video.uuid, lTags(video.uuid)) | ||
67 | |||
68 | return true | ||
69 | } | ||
70 | |||
71 | async function blacklistVideo (videoInstance: MVideoAccountLight, options: VideoBlacklistCreate) { | ||
72 | const blacklist: MVideoBlacklistVideo = await VideoBlacklistModel.create({ | ||
73 | videoId: videoInstance.id, | ||
74 | unfederated: options.unfederate === true, | ||
75 | reason: options.reason, | ||
76 | type: VideoBlacklistType.MANUAL | ||
77 | }) | ||
78 | blacklist.Video = videoInstance | ||
79 | |||
80 | if (options.unfederate === true) { | ||
81 | await sendDeleteVideo(videoInstance, undefined) | ||
82 | } | ||
83 | |||
84 | if (videoInstance.isLive) { | ||
85 | LiveManager.Instance.stopSessionOf(videoInstance.uuid, LiveVideoError.BLACKLISTED) | ||
86 | } | ||
87 | |||
88 | Notifier.Instance.notifyOnVideoBlacklist(blacklist) | ||
89 | } | ||
90 | |||
91 | async function unblacklistVideo (videoBlacklist: MVideoBlacklist, video: MVideoFullLight) { | ||
92 | const videoBlacklistType = await sequelizeTypescript.transaction(async t => { | ||
93 | const unfederated = videoBlacklist.unfederated | ||
94 | const videoBlacklistType = videoBlacklist.type | ||
95 | |||
96 | await videoBlacklist.destroy({ transaction: t }) | ||
97 | video.VideoBlacklist = undefined | ||
98 | |||
99 | // Re federate the video | ||
100 | if (unfederated === true) { | ||
101 | await federateVideoIfNeeded(video, true, t) | ||
102 | } | ||
103 | |||
104 | return videoBlacklistType | ||
105 | }) | ||
106 | |||
107 | Notifier.Instance.notifyOnVideoUnblacklist(video) | ||
108 | |||
109 | if (videoBlacklistType === VideoBlacklistType.AUTO_BEFORE_PUBLISHED) { | ||
110 | Notifier.Instance.notifyOnVideoPublishedAfterRemovedFromAutoBlacklist(video) | ||
111 | |||
112 | // Delete on object so new video notifications will send | ||
113 | delete video.VideoBlacklist | ||
114 | Notifier.Instance.notifyOnNewVideoIfNeeded(video) | ||
115 | } | ||
116 | } | ||
117 | |||
118 | // --------------------------------------------------------------------------- | ||
119 | |||
120 | export { | ||
121 | autoBlacklistVideoIfNeeded, | ||
122 | blacklistVideo, | ||
123 | unblacklistVideo | ||
124 | } | ||
125 | |||
126 | // --------------------------------------------------------------------------- | ||
127 | |||
128 | function autoBlacklistNeeded (parameters: { | ||
129 | video: MVideoWithBlacklistLight | ||
130 | isRemote: boolean | ||
131 | isNew: boolean | ||
132 | isNewFile: boolean | ||
133 | user?: MUser | ||
134 | }) { | ||
135 | const { user, video, isRemote, isNew, isNewFile } = parameters | ||
136 | |||
137 | // Already blacklisted | ||
138 | if (video.VideoBlacklist) return false | ||
139 | if (!CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED || !user) return false | ||
140 | if (isRemote || (isNew === false && isNewFile === false)) return false | ||
141 | |||
142 | if (user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) || user.hasAdminFlag(UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST)) return false | ||
143 | |||
144 | return true | ||
145 | } | ||