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