aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/fixtures/peertube-plugin-test-four/main.js
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2023-07-31 14:34:36 +0200
committerChocobozzz <me@florianbigard.com>2023-08-11 15:02:33 +0200
commit3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch)
treee4510b39bdac9c318fdb4b47018d08f15368b8f0 /server/tests/fixtures/peertube-plugin-test-four/main.js
parent04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff)
downloadPeerTube-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/tests/fixtures/peertube-plugin-test-four/main.js')
-rw-r--r--server/tests/fixtures/peertube-plugin-test-four/main.js201
1 files changed, 0 insertions, 201 deletions
diff --git a/server/tests/fixtures/peertube-plugin-test-four/main.js b/server/tests/fixtures/peertube-plugin-test-four/main.js
deleted file mode 100644
index b10177b45..000000000
--- a/server/tests/fixtures/peertube-plugin-test-four/main.js
+++ /dev/null
@@ -1,201 +0,0 @@
1async function register ({
2 peertubeHelpers,
3 registerHook,
4 getRouter
5}) {
6 const logger = peertubeHelpers.logger
7
8 logger.info('Hello world from plugin four')
9
10 {
11 const username = 'root'
12 const results = await peertubeHelpers.database.query(
13 'SELECT "email" from "user" WHERE "username" = $username',
14 {
15 type: 'SELECT',
16 bind: { username }
17 }
18 )
19
20 logger.info('root email is ' + results[0]['email'])
21 }
22
23 {
24 registerHook({
25 target: 'action:api.video.viewed',
26 handler: async ({ video }) => {
27 const videoFromDB1 = await peertubeHelpers.videos.loadByUrl(video.url)
28 const videoFromDB2 = await peertubeHelpers.videos.loadByIdOrUUID(video.id)
29 const videoFromDB3 = await peertubeHelpers.videos.loadByIdOrUUID(video.uuid)
30
31 if (videoFromDB1.uuid !== videoFromDB2.uuid || videoFromDB2.uuid !== videoFromDB3.uuid) return
32
33 logger.info('video from DB uuid is %s.', videoFromDB1.uuid)
34
35 await peertubeHelpers.videos.removeVideo(video.id)
36
37 logger.info('Video deleted by plugin four.')
38 }
39 })
40 }
41
42 {
43 const serverActor = await peertubeHelpers.server.getServerActor()
44 logger.info('server actor name is %s', serverActor.preferredUsername)
45 }
46
47 {
48 logger.info('server url is %s', peertubeHelpers.config.getWebserverUrl())
49 }
50
51 {
52 const actions = {
53 blockServer,
54 unblockServer,
55 blockAccount,
56 unblockAccount,
57 blacklist,
58 unblacklist
59 }
60
61 const router = getRouter()
62 router.post('/commander', async (req, res) => {
63 try {
64 await actions[req.body.command](peertubeHelpers, req.body)
65
66 res.sendStatus(204)
67 } catch (err) {
68 logger.error('Error in commander.', { err })
69 res.sendStatus(500)
70 }
71 })
72
73 router.get('/server-config', async (req, res) => {
74 const serverConfig = await peertubeHelpers.config.getServerConfig()
75
76 return res.json({ serverConfig })
77 })
78
79 router.get('/server-listening-config', async (req, res) => {
80 const config = await peertubeHelpers.config.getServerListeningConfig()
81
82 return res.json({ config })
83 })
84
85 router.get('/static-route', async (req, res) => {
86 const staticRoute = peertubeHelpers.plugin.getBaseStaticRoute()
87
88 return res.json({ staticRoute })
89 })
90
91 router.get('/router-route', async (req, res) => {
92 const routerRoute = peertubeHelpers.plugin.getBaseRouterRoute()
93
94 return res.json({ routerRoute })
95 })
96
97 router.get('/user/:id', async (req, res) => {
98 const user = await peertubeHelpers.user.loadById(req.params.id)
99 if (!user) return res.status(404).end()
100
101 return res.json({
102 username: user.username
103 })
104 })
105
106 router.get('/user', async (req, res) => {
107 const user = await peertubeHelpers.user.getAuthUser(res)
108 if (!user) return res.sendStatus(404)
109
110 const isAdmin = user.role === 0
111 const isModerator = user.role === 1
112 const isUser = user.role === 2
113
114 return res.json({
115 id: user.id,
116 username: user.username,
117 displayName: user.Account.name,
118 isAdmin,
119 isModerator,
120 isUser
121 })
122 })
123
124 router.get('/video-files/:id', async (req, res) => {
125 const details = await peertubeHelpers.videos.getFiles(req.params.id)
126 if (!details) return res.sendStatus(404)
127
128 return res.json(details)
129 })
130
131 router.get('/ffprobe', async (req, res) => {
132 const result = await peertubeHelpers.videos.ffprobe(req.query.path)
133 if (!result) return res.sendStatus(404)
134
135 return res.json(result)
136 })
137
138 router.post('/send-notification', async (req, res) => {
139 peertubeHelpers.socket.sendNotification(req.body.userId, {
140 type: 1,
141 userId: req.body.userId
142 })
143
144 return res.sendStatus(201)
145 })
146
147 router.post('/send-video-live-new-state/:uuid', async (req, res) => {
148 const video = await peertubeHelpers.videos.loadByIdOrUUID(req.params.uuid)
149 peertubeHelpers.socket.sendVideoLiveNewState(video)
150
151 return res.sendStatus(201)
152 })
153 }
154
155}
156
157async function unregister () {
158 return
159}
160
161module.exports = {
162 register,
163 unregister
164}
165
166// ###########################################################################
167
168async function blockServer (peertubeHelpers, body) {
169 const serverActor = await peertubeHelpers.server.getServerActor()
170
171 await peertubeHelpers.moderation.blockServer({ byAccountId: serverActor.Account.id, hostToBlock: body.hostToBlock })
172}
173
174async function unblockServer (peertubeHelpers, body) {
175 const serverActor = await peertubeHelpers.server.getServerActor()
176
177 await peertubeHelpers.moderation.unblockServer({ byAccountId: serverActor.Account.id, hostToUnblock: body.hostToUnblock })
178}
179
180async function blockAccount (peertubeHelpers, body) {
181 const serverActor = await peertubeHelpers.server.getServerActor()
182
183 await peertubeHelpers.moderation.blockAccount({ byAccountId: serverActor.Account.id, handleToBlock: body.handleToBlock })
184}
185
186async function unblockAccount (peertubeHelpers, body) {
187 const serverActor = await peertubeHelpers.server.getServerActor()
188
189 await peertubeHelpers.moderation.unblockAccount({ byAccountId: serverActor.Account.id, handleToUnblock: body.handleToUnblock })
190}
191
192async function blacklist (peertubeHelpers, body) {
193 await peertubeHelpers.moderation.blacklistVideo({
194 videoIdOrUUID: body.videoUUID,
195 createOptions: body
196 })
197}
198
199async function unblacklist (peertubeHelpers, body) {
200 await peertubeHelpers.moderation.unblacklistVideo({ videoIdOrUUID: body.videoUUID })
201}