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