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