]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/fixtures/peertube-plugin-test/main.js
5b4d34f1547629d00231c2e3a172cf5a94ab0e23
[github/Chocobozzz/PeerTube.git] / server / tests / fixtures / peertube-plugin-test / main.js
1 async function register ({ registerHook, registerSetting, settingsManager, storageManager, peertubeHelpers }) {
2 const actionHooks = [
3 'action:application.listening',
4 'action:notifier.notification.created',
5
6 'action:api.video.updated',
7 'action:api.video.deleted',
8 'action:api.video.uploaded',
9 'action:api.video.viewed',
10
11 'action:api.video-channel.created',
12 'action:api.video-channel.updated',
13 'action:api.video-channel.deleted',
14
15 'action:api.live-video.created',
16
17 'action:api.video-thread.created',
18 'action:api.video-comment-reply.created',
19 'action:api.video-comment.deleted',
20
21 'action:api.video-caption.created',
22 'action:api.video-caption.deleted',
23
24 'action:api.user.blocked',
25 'action:api.user.unblocked',
26 'action:api.user.registered',
27 'action:api.user.created',
28 'action:api.user.deleted',
29 'action:api.user.updated',
30 'action:api.user.oauth2-got-token',
31
32 'action:api.video-playlist-element.created'
33 ]
34
35 for (const h of actionHooks) {
36 registerHook({
37 target: h,
38 handler: () => peertubeHelpers.logger.debug('Run hook %s.', h)
39 })
40 }
41
42 registerHook({
43 target: 'filter:api.videos.list.params',
44 handler: obj => addToCount(obj)
45 })
46
47 registerHook({
48 target: 'filter:api.videos.list.result',
49 handler: obj => addToTotal(obj)
50 })
51
52 registerHook({
53 target: 'filter:api.video-playlist.videos.list.params',
54 handler: obj => addToCount(obj)
55 })
56
57 registerHook({
58 target: 'filter:api.video-playlist.videos.list.result',
59 handler: obj => addToTotal(obj)
60 })
61
62 registerHook({
63 target: 'filter:api.accounts.videos.list.params',
64 handler: obj => addToCount(obj)
65 })
66
67 registerHook({
68 target: 'filter:api.accounts.videos.list.result',
69 handler: obj => addToTotal(obj, 2)
70 })
71
72 registerHook({
73 target: 'filter:api.video-channels.videos.list.params',
74 handler: obj => addToCount(obj, 3)
75 })
76
77 registerHook({
78 target: 'filter:api.video-channels.videos.list.result',
79 handler: obj => addToTotal(obj, 3)
80 })
81
82 registerHook({
83 target: 'filter:api.user.me.videos.list.params',
84 handler: obj => addToCount(obj, 4)
85 })
86
87 registerHook({
88 target: 'filter:api.user.me.videos.list.result',
89 handler: obj => addToTotal(obj, 4)
90 })
91
92 registerHook({
93 target: 'filter:api.video.get.result',
94 handler: video => {
95 video.name += ' <3'
96
97 return video
98 }
99 })
100
101 // ---------------------------------------------------------------------------
102
103 registerHook({
104 target: 'filter:api.video-channels.list.params',
105 handler: obj => addToCount(obj, 1)
106 })
107
108 registerHook({
109 target: 'filter:api.video-channels.list.result',
110 handler: obj => addToTotal(obj, 1)
111 })
112
113 registerHook({
114 target: 'filter:api.video-channel.get.result',
115 handler: channel => {
116 channel.name += ' <3'
117
118 return channel
119 }
120 })
121
122 // ---------------------------------------------------------------------------
123
124 for (const hook of [ 'filter:api.video.upload.accept.result', 'filter:api.live-video.create.accept.result' ]) {
125 registerHook({
126 target: hook,
127 handler: ({ accepted }, { videoBody, liveVideoBody }) => {
128 if (!accepted) return { accepted: false }
129
130 const name = videoBody
131 ? videoBody.name
132 : liveVideoBody.name
133
134 if (name.indexOf('bad word') !== -1) return { accepted: false, errorMessage: 'bad word' }
135
136 return { accepted: true }
137 }
138 })
139 }
140
141 registerHook({
142 target: 'filter:api.video.pre-import-url.accept.result',
143 handler: ({ accepted }, { videoImportBody }) => {
144 if (!accepted) return { accepted: false }
145 if (videoImportBody.targetUrl.includes('bad')) return { accepted: false, errorMessage: 'bad target url' }
146
147 return { accepted: true }
148 }
149 })
150
151 registerHook({
152 target: 'filter:api.video.pre-import-torrent.accept.result',
153 handler: ({ accepted }, { videoImportBody }) => {
154 if (!accepted) return { accepted: false }
155 if (videoImportBody.name.includes('bad torrent')) return { accepted: false, errorMessage: 'bad torrent' }
156
157 return { accepted: true }
158 }
159 })
160
161 registerHook({
162 target: 'filter:api.video.post-import-url.accept.result',
163 handler: ({ accepted }, { video }) => {
164 if (!accepted) return { accepted: false }
165 if (video.name.includes('bad word')) return { accepted: false, errorMessage: 'bad word' }
166
167 return { accepted: true }
168 }
169 })
170
171 registerHook({
172 target: 'filter:api.video.post-import-torrent.accept.result',
173 handler: ({ accepted }, { video }) => {
174 if (!accepted) return { accepted: false }
175 if (video.name.includes('bad word')) return { accepted: false, errorMessage: 'bad word' }
176
177 return { accepted: true }
178 }
179 })
180
181 // ---------------------------------------------------------------------------
182
183 registerHook({
184 target: 'filter:api.video-thread.create.accept.result',
185 handler: ({ accepted }, { commentBody }) => checkCommentBadWord(accepted, commentBody)
186 })
187
188 registerHook({
189 target: 'filter:api.video-comment-reply.create.accept.result',
190 handler: ({ accepted }, { commentBody }) => checkCommentBadWord(accepted, commentBody)
191 })
192
193 registerHook({
194 target: 'filter:activity-pub.remote-video-comment.create.accept.result',
195 handler: ({ accepted }, { comment }) => checkCommentBadWord(accepted, comment)
196 })
197
198 // ---------------------------------------------------------------------------
199
200 registerHook({
201 target: 'filter:api.video-threads.list.params',
202 handler: obj => addToCount(obj)
203 })
204
205 registerHook({
206 target: 'filter:api.video-threads.list.result',
207 handler: obj => addToTotal(obj)
208 })
209
210 registerHook({
211 target: 'filter:api.video-thread-comments.list.result',
212 handler: obj => {
213 obj.data.forEach(c => c.text += ' <3')
214
215 return obj
216 }
217 })
218
219 registerHook({
220 target: 'filter:video.auto-blacklist.result',
221 handler: (blacklisted, { video }) => {
222 if (blacklisted) return true
223 if (video.name.includes('please blacklist me')) return true
224
225 return false
226 }
227 })
228
229 {
230 registerHook({
231 target: 'filter:api.user.signup.allowed.result',
232 handler: (result, params) => {
233 if (params && params.body && params.body.email && params.body.email.includes('jma 1')) {
234 return { allowed: false, errorMessage: 'No jma 1' }
235 }
236
237 return result
238 }
239 })
240
241 registerHook({
242 target: 'filter:api.user.request-signup.allowed.result',
243 handler: (result, params) => {
244 if (params && params.body && params.body.email && params.body.email.includes('jma 2')) {
245 return { allowed: false, errorMessage: 'No jma 2' }
246 }
247
248 return result
249 }
250 })
251 }
252
253 registerHook({
254 target: 'filter:api.download.torrent.allowed.result',
255 handler: (result, params) => {
256 if (params && params.downloadName.includes('bad torrent')) {
257 return { allowed: false, errorMessage: 'Liu Bei' }
258 }
259
260 return result
261 }
262 })
263
264 registerHook({
265 target: 'filter:api.download.video.allowed.result',
266 handler: async (result, params) => {
267 const loggedInUser = await peertubeHelpers.user.getAuthUser(params.res)
268 if (loggedInUser) return { allowed: true }
269
270 if (params && !params.streamingPlaylist && params.video.name.includes('bad file')) {
271 return { allowed: false, errorMessage: 'Cao Cao' }
272 }
273
274 if (params && params.streamingPlaylist && params.video.name.includes('bad playlist file')) {
275 return { allowed: false, errorMessage: 'Sun Jian' }
276 }
277
278 return result
279 }
280 })
281
282 registerHook({
283 target: 'filter:html.embed.video.allowed.result',
284 handler: (result, params) => {
285 return {
286 allowed: false,
287 html: 'Lu Bu'
288 }
289 }
290 })
291
292 registerHook({
293 target: 'filter:html.embed.video-playlist.allowed.result',
294 handler: (result, params) => {
295 return {
296 allowed: false,
297 html: 'Diao Chan'
298 }
299 }
300 })
301
302 registerHook({
303 target: 'filter:api.server.stats.get.result',
304 handler: (result) => {
305 return { ...result, customStats: 14 }
306 }
307 })
308
309 registerHook({
310 target: 'filter:job-queue.process.params',
311 handler: (object, context) => {
312 if (context.type !== 'video-studio-edition') return object
313
314 object.data.tasks = [
315 {
316 name: 'cut',
317 options: {
318 start: 0,
319 end: 1
320 }
321 }
322 ]
323
324 return object
325 }
326 })
327
328 registerHook({
329 target: 'filter:transcoding.auto.resolutions-to-transcode.result',
330 handler: (object, context) => {
331 if (context.video.name.includes('transcode-filter')) {
332 object = [ 100 ]
333 }
334
335 return object
336 }
337 })
338
339 // Upload/import/live attributes
340 for (const target of [
341 'filter:api.video.upload.video-attribute.result',
342 'filter:api.video.import-url.video-attribute.result',
343 'filter:api.video.import-torrent.video-attribute.result',
344 'filter:api.video.live.video-attribute.result'
345 ]) {
346 registerHook({
347 target,
348 handler: (result) => {
349 return { ...result, description: result.description + ' - ' + target }
350 }
351 })
352 }
353
354 {
355 const filterHooks = [
356 'filter:api.search.videos.local.list.params',
357 'filter:api.search.videos.local.list.result',
358 'filter:api.search.videos.index.list.params',
359 'filter:api.search.videos.index.list.result',
360 'filter:api.search.video-channels.local.list.params',
361 'filter:api.search.video-channels.local.list.result',
362 'filter:api.search.video-channels.index.list.params',
363 'filter:api.search.video-channels.index.list.result',
364 'filter:api.search.video-playlists.local.list.params',
365 'filter:api.search.video-playlists.local.list.result',
366 'filter:api.search.video-playlists.index.list.params',
367 'filter:api.search.video-playlists.index.list.result',
368
369 'filter:api.overviews.videos.list.params',
370 'filter:api.overviews.videos.list.result',
371
372 'filter:job-queue.process.params',
373 'filter:job-queue.process.result'
374 ]
375
376 for (const h of filterHooks) {
377 registerHook({
378 target: h,
379 handler: (obj) => {
380 peertubeHelpers.logger.debug('Run hook %s.', h)
381
382 return obj
383 }
384 })
385 }
386 }
387 }
388
389 async function unregister () {
390 return
391 }
392
393 module.exports = {
394 register,
395 unregister
396 }
397
398 // ############################################################################
399
400 function addToCount (obj, amount = 1) {
401 return Object.assign({}, obj, { count: obj.count + amount })
402 }
403
404 function addToTotal (result, amount = 1) {
405 return {
406 data: result.data,
407 total: result.total + amount
408 }
409 }
410
411 function checkCommentBadWord (accepted, commentBody) {
412 if (!accepted) return { accepted: false }
413 if (commentBody.text.indexOf('bad word') !== -1) return { accepted: false, errorMessage: 'bad word '}
414
415 return { accepted: true }
416 }