]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/fixtures/peertube-plugin-test/main.js
Fix resolution to transcode hook name
[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 registerHook({
182 target: 'filter:api.video-thread.create.accept.result',
183 handler: ({ accepted }, { commentBody }) => checkCommentBadWord(accepted, commentBody)
184 })
185
186 registerHook({
187 target: 'filter:api.video-comment-reply.create.accept.result',
188 handler: ({ accepted }, { commentBody }) => checkCommentBadWord(accepted, commentBody)
189 })
190
191 registerHook({
192 target: 'filter:api.video-threads.list.params',
193 handler: obj => addToCount(obj)
194 })
195
196 registerHook({
197 target: 'filter:api.video-threads.list.result',
198 handler: obj => addToTotal(obj)
199 })
200
201 registerHook({
202 target: 'filter:api.video-thread-comments.list.result',
203 handler: obj => {
204 obj.data.forEach(c => c.text += ' <3')
205
206 return obj
207 }
208 })
209
210 registerHook({
211 target: 'filter:video.auto-blacklist.result',
212 handler: (blacklisted, { video }) => {
213 if (blacklisted) return true
214 if (video.name.includes('please blacklist me')) return true
215
216 return false
217 }
218 })
219
220 registerHook({
221 target: 'filter:api.user.signup.allowed.result',
222 handler: (result, params) => {
223 if (params && params.body && params.body.email && params.body.email.includes('jma')) {
224 return { allowed: false, errorMessage: 'No jma' }
225 }
226
227 return result
228 }
229 })
230
231 registerHook({
232 target: 'filter:api.download.torrent.allowed.result',
233 handler: (result, params) => {
234 if (params && params.downloadName.includes('bad torrent')) {
235 return { allowed: false, errorMessage: 'Liu Bei' }
236 }
237
238 return result
239 }
240 })
241
242 registerHook({
243 target: 'filter:api.download.video.allowed.result',
244 handler: (result, params) => {
245 if (params && !params.streamingPlaylist && params.video.name.includes('bad file')) {
246 return { allowed: false, errorMessage: 'Cao Cao' }
247 }
248
249 if (params && params.streamingPlaylist && params.video.name.includes('bad playlist file')) {
250 return { allowed: false, errorMessage: 'Sun Jian' }
251 }
252
253 return result
254 }
255 })
256
257 registerHook({
258 target: 'filter:html.embed.video.allowed.result',
259 handler: (result, params) => {
260 return {
261 allowed: false,
262 html: 'Lu Bu'
263 }
264 }
265 })
266
267 registerHook({
268 target: 'filter:html.embed.video-playlist.allowed.result',
269 handler: (result, params) => {
270 return {
271 allowed: false,
272 html: 'Diao Chan'
273 }
274 }
275 })
276
277 registerHook({
278 target: 'filter:api.server.stats.get.result',
279 handler: (result) => {
280 return { ...result, customStats: 14 }
281 }
282 })
283
284 registerHook({
285 target: 'filter:job-queue.process.params',
286 handler: (object, context) => {
287 if (context.type !== 'video-studio-edition') return object
288
289 object.data.tasks = [
290 {
291 name: 'cut',
292 options: {
293 start: 0,
294 end: 1
295 }
296 }
297 ]
298
299 return object
300 }
301 })
302
303 registerHook({
304 target: 'filter:transcoding.auto.resolutions-to-transcode.result',
305 handler: (object, context) => {
306 if (context.video.name.includes('transcode-filter')) {
307 object = [ 100 ]
308 }
309
310 return object
311 }
312 })
313
314 // Upload/import/live attributes
315 for (const target of [
316 'filter:api.video.upload.video-attribute.result',
317 'filter:api.video.import-url.video-attribute.result',
318 'filter:api.video.import-torrent.video-attribute.result',
319 'filter:api.video.live.video-attribute.result'
320 ]) {
321 registerHook({
322 target,
323 handler: (result) => {
324 return { ...result, description: result.description + ' - ' + target }
325 }
326 })
327 }
328
329 {
330 const filterHooks = [
331 'filter:api.search.videos.local.list.params',
332 'filter:api.search.videos.local.list.result',
333 'filter:api.search.videos.index.list.params',
334 'filter:api.search.videos.index.list.result',
335 'filter:api.search.video-channels.local.list.params',
336 'filter:api.search.video-channels.local.list.result',
337 'filter:api.search.video-channels.index.list.params',
338 'filter:api.search.video-channels.index.list.result',
339 'filter:api.search.video-playlists.local.list.params',
340 'filter:api.search.video-playlists.local.list.result',
341 'filter:api.search.video-playlists.index.list.params',
342 'filter:api.search.video-playlists.index.list.result',
343
344 'filter:api.overviews.videos.list.params',
345 'filter:api.overviews.videos.list.result',
346
347 'filter:job-queue.process.params',
348 'filter:job-queue.process.result'
349 ]
350
351 for (const h of filterHooks) {
352 registerHook({
353 target: h,
354 handler: (obj) => {
355 peertubeHelpers.logger.debug('Run hook %s.', h)
356
357 return obj
358 }
359 })
360 }
361 }
362 }
363
364 async function unregister () {
365 return
366 }
367
368 module.exports = {
369 register,
370 unregister
371 }
372
373 // ############################################################################
374
375 function addToCount (obj, amount = 1) {
376 return Object.assign({}, obj, { count: obj.count + amount })
377 }
378
379 function addToTotal (result, amount = 1) {
380 return {
381 data: result.data,
382 total: result.total + amount
383 }
384 }
385
386 function checkCommentBadWord (accepted, commentBody) {
387 if (!accepted) return { accepted: false }
388 if (commentBody.text.indexOf('bad word') !== -1) return { accepted: false, errorMessage: 'bad word '}
389
390 return { accepted: true }
391 }