]>
Commit | Line | Data |
---|---|---|
89cd1275 C |
1 | async function register ({ registerHook, registerSetting, settingsManager, storageManager, peertubeHelpers }) { |
2 | const actionHooks = [ | |
3 | 'action:application.listening', | |
4 | ||
5 | 'action:api.video.updated', | |
6 | 'action:api.video.deleted', | |
7 | 'action:api.video.uploaded', | |
8 | 'action:api.video.viewed', | |
9 | ||
3cabf353 C |
10 | 'action:api.live-video.created', |
11 | ||
89cd1275 C |
12 | 'action:api.video-thread.created', |
13 | 'action:api.video-comment-reply.created', | |
6f3fe96f C |
14 | 'action:api.video-comment.deleted', |
15 | ||
5e3d29ab | 16 | 'action:api.video-caption.created', |
17 | 'action:api.video-caption.deleted', | |
18 | ||
6f3fe96f C |
19 | 'action:api.user.blocked', |
20 | 'action:api.user.unblocked', | |
21 | 'action:api.user.registered', | |
22 | 'action:api.user.created', | |
23 | 'action:api.user.deleted', | |
24 | 'action:api.user.updated', | |
e2e0b645 | 25 | 'action:api.user.oauth2-got-token', |
26 | ||
27 | 'action:api.video-playlist-element.created' | |
89cd1275 C |
28 | ] |
29 | ||
30 | for (const h of actionHooks) { | |
31 | registerHook({ | |
32 | target: h, | |
33 | handler: () => peertubeHelpers.logger.debug('Run hook %s.', h) | |
34 | }) | |
35 | } | |
9b474844 C |
36 | |
37 | registerHook({ | |
89cd1275 C |
38 | target: 'filter:api.videos.list.params', |
39 | handler: obj => addToCount(obj) | |
9b474844 C |
40 | }) |
41 | ||
89cd1275 C |
42 | registerHook({ |
43 | target: 'filter:api.videos.list.result', | |
6691c522 | 44 | handler: obj => addToTotal(obj) |
9b474844 C |
45 | }) |
46 | ||
c5ca7e1e | 47 | registerHook({ |
48 | target: 'filter:api.video-playlist.videos.list.params', | |
49 | handler: obj => addToCount(obj) | |
50 | }) | |
51 | ||
52 | registerHook({ | |
53 | target: 'filter:api.video-playlist.videos.list.result', | |
54 | handler: obj => addToTotal(obj) | |
55 | }) | |
56 | ||
38267c0c C |
57 | registerHook({ |
58 | target: 'filter:api.accounts.videos.list.params', | |
59 | handler: obj => addToCount(obj) | |
60 | }) | |
61 | ||
62 | registerHook({ | |
63 | target: 'filter:api.accounts.videos.list.result', | |
64 | handler: obj => addToTotal(obj, 2) | |
65 | }) | |
66 | ||
67 | registerHook({ | |
68 | target: 'filter:api.video-channels.videos.list.params', | |
69 | handler: obj => addToCount(obj, 3) | |
70 | }) | |
71 | ||
72 | registerHook({ | |
73 | target: 'filter:api.video-channels.videos.list.result', | |
74 | handler: obj => addToTotal(obj, 3) | |
75 | }) | |
76 | ||
a4d2ca07 C |
77 | registerHook({ |
78 | target: 'filter:api.user.me.videos.list.params', | |
79 | handler: obj => addToCount(obj, 4) | |
80 | }) | |
81 | ||
82 | registerHook({ | |
83 | target: 'filter:api.user.me.videos.list.result', | |
84 | handler: obj => addToTotal(obj, 4) | |
85 | }) | |
86 | ||
89cd1275 C |
87 | registerHook({ |
88 | target: 'filter:api.video.get.result', | |
89 | handler: video => { | |
90 | video.name += ' <3' | |
9b474844 | 91 | |
89cd1275 C |
92 | return video |
93 | } | |
94 | }) | |
95 | ||
3cabf353 C |
96 | for (const hook of [ 'filter:api.video.upload.accept.result', 'filter:api.live-video.create.accept.result' ]) { |
97 | registerHook({ | |
98 | target: hook, | |
99 | handler: ({ accepted }, { videoBody, liveVideoBody }) => { | |
100 | if (!accepted) return { accepted: false } | |
2158ac90 | 101 | |
3cabf353 C |
102 | const name = videoBody |
103 | ? videoBody.name | |
104 | : liveVideoBody.name | |
105 | ||
106 | if (name.indexOf('bad word') !== -1) return { accepted: false, errorMessage: 'bad word' } | |
107 | ||
108 | return { accepted: true } | |
109 | } | |
110 | }) | |
111 | } | |
2158ac90 RK |
112 | |
113 | registerHook({ | |
114 | target: 'filter:api.video.pre-import-url.accept.result', | |
115 | handler: ({ accepted }, { videoImportBody }) => { | |
116 | if (!accepted) return { accepted: false } | |
117 | if (videoImportBody.targetUrl.includes('bad')) return { accepted: false, errorMessage: 'bad target url' } | |
118 | ||
119 | return { accepted: true } | |
120 | } | |
121 | }) | |
122 | ||
123 | registerHook({ | |
124 | target: 'filter:api.video.pre-import-torrent.accept.result', | |
125 | handler: ({ accepted }, { videoImportBody }) => { | |
126 | if (!accepted) return { accepted: false } | |
127 | if (videoImportBody.name.includes('bad torrent')) return { accepted: false, errorMessage: 'bad torrent' } | |
128 | ||
129 | return { accepted: true } | |
130 | } | |
131 | }) | |
132 | ||
133 | registerHook({ | |
134 | target: 'filter:api.video.post-import-url.accept.result', | |
135 | handler: ({ accepted }, { video }) => { | |
136 | if (!accepted) return { accepted: false } | |
137 | if (video.name.includes('bad word')) return { accepted: false, errorMessage: 'bad word' } | |
138 | ||
139 | return { accepted: true } | |
140 | } | |
141 | }) | |
142 | ||
143 | registerHook({ | |
144 | target: 'filter:api.video.post-import-torrent.accept.result', | |
145 | handler: ({ accepted }, { video }) => { | |
146 | if (!accepted) return { accepted: false } | |
147 | if (video.name.includes('bad word')) return { accepted: false, errorMessage: 'bad word' } | |
89cd1275 C |
148 | |
149 | return { accepted: true } | |
150 | } | |
151 | }) | |
6691c522 C |
152 | |
153 | registerHook({ | |
154 | target: 'filter:api.video-thread.create.accept.result', | |
155 | handler: ({ accepted }, { commentBody }) => checkCommentBadWord(accepted, commentBody) | |
156 | }) | |
157 | ||
158 | registerHook({ | |
159 | target: 'filter:api.video-comment-reply.create.accept.result', | |
160 | handler: ({ accepted }, { commentBody }) => checkCommentBadWord(accepted, commentBody) | |
161 | }) | |
162 | ||
163 | registerHook({ | |
164 | target: 'filter:api.video-threads.list.params', | |
165 | handler: obj => addToCount(obj) | |
166 | }) | |
167 | ||
168 | registerHook({ | |
169 | target: 'filter:api.video-threads.list.result', | |
170 | handler: obj => addToTotal(obj) | |
171 | }) | |
172 | ||
173 | registerHook({ | |
174 | target: 'filter:api.video-thread-comments.list.result', | |
175 | handler: obj => { | |
176 | obj.data.forEach(c => c.text += ' <3') | |
177 | ||
178 | return obj | |
179 | } | |
180 | }) | |
181 | ||
182 | registerHook({ | |
183 | target: 'filter:video.auto-blacklist.result', | |
184 | handler: (blacklisted, { video }) => { | |
185 | if (blacklisted) return true | |
186 | if (video.name.includes('please blacklist me')) return true | |
187 | ||
188 | return false | |
189 | } | |
190 | }) | |
4ce7eb71 C |
191 | |
192 | registerHook({ | |
193 | target: 'filter:api.user.signup.allowed.result', | |
194 | handler: (result, params) => { | |
da5f4648 | 195 | if (params && params.body && params.body.email && params.body.email.includes('jma')) { |
4ce7eb71 C |
196 | return { allowed: false, errorMessage: 'No jma' } |
197 | } | |
198 | ||
199 | return result | |
200 | } | |
201 | }) | |
4bc45da3 C |
202 | |
203 | registerHook({ | |
204 | target: 'filter:api.download.torrent.allowed.result', | |
205 | handler: (result, params) => { | |
206 | if (params && params.downloadName.includes('bad torrent')) { | |
207 | return { allowed: false, errorMessage: 'Liu Bei' } | |
208 | } | |
209 | ||
210 | return result | |
211 | } | |
212 | }) | |
213 | ||
214 | registerHook({ | |
215 | target: 'filter:api.download.video.allowed.result', | |
216 | handler: (result, params) => { | |
217 | if (params && !params.streamingPlaylist && params.video.name.includes('bad file')) { | |
218 | return { allowed: false, errorMessage: 'Cao Cao' } | |
219 | } | |
220 | ||
221 | if (params && params.streamingPlaylist && params.video.name.includes('bad playlist file')) { | |
222 | return { allowed: false, errorMessage: 'Sun Jian' } | |
223 | } | |
224 | ||
225 | return result | |
226 | } | |
227 | }) | |
eebd9838 C |
228 | |
229 | registerHook({ | |
230 | target: 'filter:html.embed.video.allowed.result', | |
231 | handler: (result, params) => { | |
232 | return { | |
233 | allowed: false, | |
234 | html: 'Lu Bu' | |
235 | } | |
236 | } | |
237 | }) | |
238 | ||
239 | registerHook({ | |
240 | target: 'filter:html.embed.video-playlist.allowed.result', | |
241 | handler: (result, params) => { | |
242 | return { | |
243 | allowed: false, | |
244 | html: 'Diao Chan' | |
245 | } | |
246 | } | |
247 | }) | |
74a4d531 | 248 | |
65058050 C |
249 | registerHook({ |
250 | target: 'filter:api.server.stats.get.result', | |
251 | handler: (result) => { | |
252 | return { ...result, customStats: 14 } | |
253 | } | |
254 | }) | |
255 | ||
22df69fd C |
256 | registerHook({ |
257 | target: 'filter:job-queue.process.params', | |
258 | handler: (object, context) => { | |
22df69fd C |
259 | if (context.type !== 'video-studio-edition') return object |
260 | ||
261 | object.data.tasks = [ | |
262 | { | |
263 | name: 'cut', | |
264 | options: { | |
265 | start: 0, | |
266 | end: 1 | |
267 | } | |
268 | } | |
269 | ] | |
270 | ||
271 | return object | |
272 | } | |
273 | }) | |
274 | ||
ebb9e53a C |
275 | registerHook({ |
276 | target: 'filter:transcoding.auto.lower-resolutions-to-transcode.result', | |
277 | handler: (object, context) => { | |
278 | if (context.video.name.includes('transcode-filter')) { | |
279 | object = [ 100 ] | |
280 | } | |
281 | ||
282 | return object | |
283 | } | |
284 | }) | |
285 | ||
d17d7430 C |
286 | // Upload/import/live attributes |
287 | for (const target of [ | |
288 | 'filter:api.video.upload.video-attribute.result', | |
289 | 'filter:api.video.import-url.video-attribute.result', | |
290 | 'filter:api.video.import-torrent.video-attribute.result', | |
291 | 'filter:api.video.live.video-attribute.result' | |
292 | ]) { | |
293 | registerHook({ | |
294 | target, | |
295 | handler: (result) => { | |
296 | return { ...result, description: result.description + ' - ' + target } | |
297 | } | |
298 | }) | |
299 | } | |
300 | ||
74a4d531 | 301 | { |
d1aed103 | 302 | const filterHooks = [ |
74a4d531 C |
303 | 'filter:api.search.videos.local.list.params', |
304 | 'filter:api.search.videos.local.list.result', | |
305 | 'filter:api.search.videos.index.list.params', | |
306 | 'filter:api.search.videos.index.list.result', | |
307 | 'filter:api.search.video-channels.local.list.params', | |
308 | 'filter:api.search.video-channels.local.list.result', | |
309 | 'filter:api.search.video-channels.index.list.params', | |
310 | 'filter:api.search.video-channels.index.list.result', | |
37a44fc9 C |
311 | 'filter:api.search.video-playlists.local.list.params', |
312 | 'filter:api.search.video-playlists.local.list.result', | |
313 | 'filter:api.search.video-playlists.index.list.params', | |
d1aed103 C |
314 | 'filter:api.search.video-playlists.index.list.result', |
315 | ||
316 | 'filter:api.overviews.videos.list.params', | |
22df69fd C |
317 | 'filter:api.overviews.videos.list.result', |
318 | ||
319 | 'filter:job-queue.process.params', | |
320 | 'filter:job-queue.process.result' | |
74a4d531 C |
321 | ] |
322 | ||
d1aed103 | 323 | for (const h of filterHooks) { |
74a4d531 C |
324 | registerHook({ |
325 | target: h, | |
326 | handler: (obj) => { | |
327 | peertubeHelpers.logger.debug('Run hook %s.', h) | |
328 | ||
329 | return obj | |
330 | } | |
331 | }) | |
332 | } | |
333 | } | |
9b474844 C |
334 | } |
335 | ||
336 | async function unregister () { | |
337 | return | |
338 | } | |
339 | ||
340 | module.exports = { | |
341 | register, | |
342 | unregister | |
343 | } | |
344 | ||
345 | // ############################################################################ | |
346 | ||
38267c0c C |
347 | function addToCount (obj, amount = 1) { |
348 | return Object.assign({}, obj, { count: obj.count + amount }) | |
9b474844 | 349 | } |
6691c522 | 350 | |
38267c0c | 351 | function addToTotal (result, amount = 1) { |
6691c522 C |
352 | return { |
353 | data: result.data, | |
38267c0c | 354 | total: result.total + amount |
6691c522 C |
355 | } |
356 | } | |
357 | ||
358 | function checkCommentBadWord (accepted, commentBody) { | |
359 | if (!accepted) return { accepted: false } | |
360 | if (commentBody.text.indexOf('bad word') !== -1) return { accepted: false, errorMessage: 'bad word '} | |
361 | ||
362 | return { accepted: true } | |
363 | } |