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