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