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