]>
Commit | Line | Data |
---|---|---|
89cd1275 | 1 | async function register ({ registerHook, registerSetting, settingsManager, storageManager, peertubeHelpers }) { |
c3441b03 C |
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 | } | |
89cd1275 | 51 | } |
9b474844 C |
52 | |
53 | registerHook({ | |
89cd1275 C |
54 | target: 'filter:api.videos.list.params', |
55 | handler: obj => addToCount(obj) | |
9b474844 C |
56 | }) |
57 | ||
89cd1275 C |
58 | registerHook({ |
59 | target: 'filter:api.videos.list.result', | |
6691c522 | 60 | handler: obj => addToTotal(obj) |
9b474844 C |
61 | }) |
62 | ||
c5ca7e1e | 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 | ||
38267c0c C |
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 | ||
a4d2ca07 C |
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 | ||
c0687c91 W |
103 | registerHook({ |
104 | target: 'filter:api.user.me.subscription-videos.list.params', | |
692ae8c3 | 105 | handler: obj => addToCount(obj) |
c0687c91 W |
106 | }) |
107 | ||
108 | registerHook({ | |
109 | target: 'filter:api.user.me.subscription-videos.list.result', | |
110 | handler: obj => addToTotal(obj, 4) | |
111 | }) | |
112 | ||
89cd1275 C |
113 | registerHook({ |
114 | target: 'filter:api.video.get.result', | |
115 | handler: video => { | |
116 | video.name += ' <3' | |
9b474844 | 117 | |
89cd1275 C |
118 | return video |
119 | } | |
120 | }) | |
121 | ||
0260dc8a C |
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 | ||
3cabf353 C |
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 } | |
2158ac90 | 150 | |
3cabf353 C |
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 | } | |
2158ac90 RK |
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' } | |
89cd1275 C |
197 | |
198 | return { accepted: true } | |
199 | } | |
200 | }) | |
6691c522 | 201 | |
b2a70e3c C |
202 | // --------------------------------------------------------------------------- |
203 | ||
6691c522 C |
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 | ||
b2a70e3c C |
214 | registerHook({ |
215 | target: 'filter:activity-pub.remote-video-comment.create.accept.result', | |
216 | handler: ({ accepted }, { comment }) => checkCommentBadWord(accepted, comment) | |
217 | }) | |
218 | ||
219 | // --------------------------------------------------------------------------- | |
220 | ||
58e735dd C |
221 | registerHook({ |
222 | target: 'filter:activity-pub.activity.context.build.result', | |
2b02a9b9 | 223 | handler: context => context.concat([ { recordedAt: 'https://schema.org/recordedAt' } ]) |
58e735dd C |
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 | ||
6691c522 C |
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 | }) | |
4ce7eb71 | 261 | |
b379759f C |
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 | |
4ce7eb71 | 271 | } |
b379759f | 272 | }) |
4ce7eb71 | 273 | |
b379759f C |
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 | } | |
4bc45da3 C |
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', | |
868314e8 C |
299 | handler: async (result, params) => { |
300 | const loggedInUser = await peertubeHelpers.user.getAuthUser(params.res) | |
301 | if (loggedInUser) return { allowed: true } | |
302 | ||
4bc45da3 C |
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 | }) | |
eebd9838 | 314 | |
d91ce83d C |
315 | // --------------------------------------------------------------------------- |
316 | ||
eebd9838 C |
317 | registerHook({ |
318 | target: 'filter:html.embed.video.allowed.result', | |
319 | handler: (result, params) => { | |
320 | return { | |
321 | allowed: false, | |
322 | html: 'Lu Bu' | |
323 | } | |
324 | } | |
325 | }) | |
326 | ||
327 | registerHook({ | |
328 | target: 'filter:html.embed.video-playlist.allowed.result', | |
329 | handler: (result, params) => { | |
330 | return { | |
331 | allowed: false, | |
332 | html: 'Diao Chan' | |
333 | } | |
334 | } | |
335 | }) | |
74a4d531 | 336 | |
d91ce83d C |
337 | // --------------------------------------------------------------------------- |
338 | ||
339 | registerHook({ | |
340 | target: 'filter:html.client.json-ld.result', | |
341 | handler: (jsonld, context) => { | |
342 | if (!context || !context.video) return jsonld | |
343 | ||
344 | return Object.assign(jsonld, { recordedAt: 'http://example.com/recordedAt' }) | |
345 | } | |
346 | }) | |
347 | ||
348 | // --------------------------------------------------------------------------- | |
349 | ||
65058050 C |
350 | registerHook({ |
351 | target: 'filter:api.server.stats.get.result', | |
352 | handler: (result) => { | |
353 | return { ...result, customStats: 14 } | |
354 | } | |
355 | }) | |
356 | ||
22df69fd C |
357 | registerHook({ |
358 | target: 'filter:job-queue.process.params', | |
359 | handler: (object, context) => { | |
22df69fd C |
360 | if (context.type !== 'video-studio-edition') return object |
361 | ||
362 | object.data.tasks = [ | |
363 | { | |
364 | name: 'cut', | |
365 | options: { | |
366 | start: 0, | |
367 | end: 1 | |
368 | } | |
369 | } | |
370 | ] | |
371 | ||
372 | return object | |
373 | } | |
374 | }) | |
375 | ||
ebb9e53a | 376 | registerHook({ |
64fd6158 | 377 | target: 'filter:transcoding.auto.resolutions-to-transcode.result', |
ebb9e53a C |
378 | handler: (object, context) => { |
379 | if (context.video.name.includes('transcode-filter')) { | |
380 | object = [ 100 ] | |
381 | } | |
382 | ||
383 | return object | |
384 | } | |
385 | }) | |
386 | ||
d17d7430 C |
387 | // Upload/import/live attributes |
388 | for (const target of [ | |
389 | 'filter:api.video.upload.video-attribute.result', | |
390 | 'filter:api.video.import-url.video-attribute.result', | |
391 | 'filter:api.video.import-torrent.video-attribute.result', | |
392 | 'filter:api.video.live.video-attribute.result' | |
393 | ]) { | |
394 | registerHook({ | |
395 | target, | |
396 | handler: (result) => { | |
397 | return { ...result, description: result.description + ' - ' + target } | |
398 | } | |
399 | }) | |
400 | } | |
401 | ||
74a4d531 | 402 | { |
d1aed103 | 403 | const filterHooks = [ |
74a4d531 C |
404 | 'filter:api.search.videos.local.list.params', |
405 | 'filter:api.search.videos.local.list.result', | |
406 | 'filter:api.search.videos.index.list.params', | |
407 | 'filter:api.search.videos.index.list.result', | |
408 | 'filter:api.search.video-channels.local.list.params', | |
409 | 'filter:api.search.video-channels.local.list.result', | |
410 | 'filter:api.search.video-channels.index.list.params', | |
411 | 'filter:api.search.video-channels.index.list.result', | |
37a44fc9 C |
412 | 'filter:api.search.video-playlists.local.list.params', |
413 | 'filter:api.search.video-playlists.local.list.result', | |
414 | 'filter:api.search.video-playlists.index.list.params', | |
d1aed103 C |
415 | 'filter:api.search.video-playlists.index.list.result', |
416 | ||
417 | 'filter:api.overviews.videos.list.params', | |
22df69fd C |
418 | 'filter:api.overviews.videos.list.result', |
419 | ||
420 | 'filter:job-queue.process.params', | |
421 | 'filter:job-queue.process.result' | |
74a4d531 C |
422 | ] |
423 | ||
d1aed103 | 424 | for (const h of filterHooks) { |
74a4d531 C |
425 | registerHook({ |
426 | target: h, | |
427 | handler: (obj) => { | |
428 | peertubeHelpers.logger.debug('Run hook %s.', h) | |
429 | ||
430 | return obj | |
431 | } | |
432 | }) | |
433 | } | |
434 | } | |
9b474844 C |
435 | } |
436 | ||
437 | async function unregister () { | |
438 | return | |
439 | } | |
440 | ||
441 | module.exports = { | |
442 | register, | |
443 | unregister | |
444 | } | |
445 | ||
446 | // ############################################################################ | |
447 | ||
38267c0c C |
448 | function addToCount (obj, amount = 1) { |
449 | return Object.assign({}, obj, { count: obj.count + amount }) | |
9b474844 | 450 | } |
6691c522 | 451 | |
38267c0c | 452 | function addToTotal (result, amount = 1) { |
6691c522 C |
453 | return { |
454 | data: result.data, | |
38267c0c | 455 | total: result.total + amount |
6691c522 C |
456 | } |
457 | } | |
458 | ||
459 | function checkCommentBadWord (accepted, commentBody) { | |
460 | if (!accepted) return { accepted: false } | |
461 | if (commentBody.text.indexOf('bad word') !== -1) return { accepted: false, errorMessage: 'bad word '} | |
462 | ||
463 | return { accepted: true } | |
464 | } |