]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/fixtures/peertube-plugin-test/main.js
Add video caption created and deleted 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
5 'action:api.video.updated',
6 'action:api.video.deleted',
7 'action:api.video.uploaded',
8 'action:api.video.viewed',
9
10 'action:api.live-video.created',
11
12 'action:api.video-thread.created',
13 'action:api.video-comment-reply.created',
14 'action:api.video-comment.deleted',
15
16 'action:api.video-caption.created',
17 'action:api.video-caption.deleted',
18
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',
25 'action:api.user.oauth2-got-token',
26
27 'action:api.video-playlist-element.created'
28 ]
29
30 for (const h of actionHooks) {
31 registerHook({
32 target: h,
33 handler: () => peertubeHelpers.logger.debug('Run hook %s.', h)
34 })
35 }
36
37 registerHook({
38 target: 'filter:api.videos.list.params',
39 handler: obj => addToCount(obj)
40 })
41
42 registerHook({
43 target: 'filter:api.videos.list.result',
44 handler: obj => addToTotal(obj)
45 })
46
47 registerHook({
48 target: 'filter:api.accounts.videos.list.params',
49 handler: obj => addToCount(obj)
50 })
51
52 registerHook({
53 target: 'filter:api.accounts.videos.list.result',
54 handler: obj => addToTotal(obj, 2)
55 })
56
57 registerHook({
58 target: 'filter:api.video-channels.videos.list.params',
59 handler: obj => addToCount(obj, 3)
60 })
61
62 registerHook({
63 target: 'filter:api.video-channels.videos.list.result',
64 handler: obj => addToTotal(obj, 3)
65 })
66
67 registerHook({
68 target: 'filter:api.user.me.videos.list.params',
69 handler: obj => addToCount(obj, 4)
70 })
71
72 registerHook({
73 target: 'filter:api.user.me.videos.list.result',
74 handler: obj => addToTotal(obj, 4)
75 })
76
77 registerHook({
78 target: 'filter:api.video.get.result',
79 handler: video => {
80 video.name += ' <3'
81
82 return video
83 }
84 })
85
86 for (const hook of [ 'filter:api.video.upload.accept.result', 'filter:api.live-video.create.accept.result' ]) {
87 registerHook({
88 target: hook,
89 handler: ({ accepted }, { videoBody, liveVideoBody }) => {
90 if (!accepted) return { accepted: false }
91
92 const name = videoBody
93 ? videoBody.name
94 : liveVideoBody.name
95
96 if (name.indexOf('bad word') !== -1) return { accepted: false, errorMessage: 'bad word' }
97
98 return { accepted: true }
99 }
100 })
101 }
102
103 registerHook({
104 target: 'filter:api.video.pre-import-url.accept.result',
105 handler: ({ accepted }, { videoImportBody }) => {
106 if (!accepted) return { accepted: false }
107 if (videoImportBody.targetUrl.includes('bad')) return { accepted: false, errorMessage: 'bad target url' }
108
109 return { accepted: true }
110 }
111 })
112
113 registerHook({
114 target: 'filter:api.video.pre-import-torrent.accept.result',
115 handler: ({ accepted }, { videoImportBody }) => {
116 if (!accepted) return { accepted: false }
117 if (videoImportBody.name.includes('bad torrent')) return { accepted: false, errorMessage: 'bad torrent' }
118
119 return { accepted: true }
120 }
121 })
122
123 registerHook({
124 target: 'filter:api.video.post-import-url.accept.result',
125 handler: ({ accepted }, { video }) => {
126 if (!accepted) return { accepted: false }
127 if (video.name.includes('bad word')) return { accepted: false, errorMessage: 'bad word' }
128
129 return { accepted: true }
130 }
131 })
132
133 registerHook({
134 target: 'filter:api.video.post-import-torrent.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-thread.create.accept.result',
145 handler: ({ accepted }, { commentBody }) => checkCommentBadWord(accepted, commentBody)
146 })
147
148 registerHook({
149 target: 'filter:api.video-comment-reply.create.accept.result',
150 handler: ({ accepted }, { commentBody }) => checkCommentBadWord(accepted, commentBody)
151 })
152
153 registerHook({
154 target: 'filter:api.video-threads.list.params',
155 handler: obj => addToCount(obj)
156 })
157
158 registerHook({
159 target: 'filter:api.video-threads.list.result',
160 handler: obj => addToTotal(obj)
161 })
162
163 registerHook({
164 target: 'filter:api.video-thread-comments.list.result',
165 handler: obj => {
166 obj.data.forEach(c => c.text += ' <3')
167
168 return obj
169 }
170 })
171
172 registerHook({
173 target: 'filter:video.auto-blacklist.result',
174 handler: (blacklisted, { video }) => {
175 if (blacklisted) return true
176 if (video.name.includes('please blacklist me')) return true
177
178 return false
179 }
180 })
181
182 registerHook({
183 target: 'filter:api.user.signup.allowed.result',
184 handler: (result, params) => {
185 if (params && params.body.email.includes('jma')) {
186 return { allowed: false, errorMessage: 'No jma' }
187 }
188
189 return result
190 }
191 })
192
193 registerHook({
194 target: 'filter:api.download.torrent.allowed.result',
195 handler: (result, params) => {
196 if (params && params.downloadName.includes('bad torrent')) {
197 return { allowed: false, errorMessage: 'Liu Bei' }
198 }
199
200 return result
201 }
202 })
203
204 registerHook({
205 target: 'filter:api.download.video.allowed.result',
206 handler: (result, params) => {
207 if (params && !params.streamingPlaylist && params.video.name.includes('bad file')) {
208 return { allowed: false, errorMessage: 'Cao Cao' }
209 }
210
211 if (params && params.streamingPlaylist && params.video.name.includes('bad playlist file')) {
212 return { allowed: false, errorMessage: 'Sun Jian' }
213 }
214
215 return result
216 }
217 })
218
219 registerHook({
220 target: 'filter:html.embed.video.allowed.result',
221 handler: (result, params) => {
222 return {
223 allowed: false,
224 html: 'Lu Bu'
225 }
226 }
227 })
228
229 registerHook({
230 target: 'filter:html.embed.video-playlist.allowed.result',
231 handler: (result, params) => {
232 return {
233 allowed: false,
234 html: 'Diao Chan'
235 }
236 }
237 })
238
239 registerHook({
240 target: 'filter:api.server.stats.get.result',
241 handler: (result) => {
242 return { ...result, customStats: 14 }
243 }
244 })
245
246 // Upload/import/live attributes
247 for (const target of [
248 'filter:api.video.upload.video-attribute.result',
249 'filter:api.video.import-url.video-attribute.result',
250 'filter:api.video.import-torrent.video-attribute.result',
251 'filter:api.video.live.video-attribute.result'
252 ]) {
253 registerHook({
254 target,
255 handler: (result) => {
256 return { ...result, description: result.description + ' - ' + target }
257 }
258 })
259 }
260
261 {
262 const filterHooks = [
263 'filter:api.search.videos.local.list.params',
264 'filter:api.search.videos.local.list.result',
265 'filter:api.search.videos.index.list.params',
266 'filter:api.search.videos.index.list.result',
267 'filter:api.search.video-channels.local.list.params',
268 'filter:api.search.video-channels.local.list.result',
269 'filter:api.search.video-channels.index.list.params',
270 'filter:api.search.video-channels.index.list.result',
271 'filter:api.search.video-playlists.local.list.params',
272 'filter:api.search.video-playlists.local.list.result',
273 'filter:api.search.video-playlists.index.list.params',
274 'filter:api.search.video-playlists.index.list.result',
275
276 'filter:api.overviews.videos.list.params',
277 'filter:api.overviews.videos.list.result'
278 ]
279
280 for (const h of filterHooks) {
281 registerHook({
282 target: h,
283 handler: (obj) => {
284 peertubeHelpers.logger.debug('Run hook %s.', h)
285
286 return obj
287 }
288 })
289 }
290 }
291 }
292
293 async function unregister () {
294 return
295 }
296
297 module.exports = {
298 register,
299 unregister
300 }
301
302 // ############################################################################
303
304 function addToCount (obj, amount = 1) {
305 return Object.assign({}, obj, { count: obj.count + amount })
306 }
307
308 function addToTotal (result, amount = 1) {
309 return {
310 data: result.data,
311 total: result.total + amount
312 }
313 }
314
315 function checkCommentBadWord (accepted, commentBody) {
316 if (!accepted) return { accepted: false }
317 if (commentBody.text.indexOf('bad word') !== -1) return { accepted: false, errorMessage: 'bad word '}
318
319 return { accepted: true }
320 }