]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/fixtures/peertube-plugin-test/main.js
Use dedicated hooks for account/channel videos
[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',
22 'action:api.user.oauth2-got-token'
89cd1275
C
23 ]
24
25 for (const h of actionHooks) {
26 registerHook({
27 target: h,
28 handler: () => peertubeHelpers.logger.debug('Run hook %s.', h)
29 })
30 }
9b474844
C
31
32 registerHook({
89cd1275
C
33 target: 'filter:api.videos.list.params',
34 handler: obj => addToCount(obj)
9b474844
C
35 })
36
89cd1275
C
37 registerHook({
38 target: 'filter:api.videos.list.result',
6691c522 39 handler: obj => addToTotal(obj)
9b474844
C
40 })
41
38267c0c
C
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
89cd1275
C
62 registerHook({
63 target: 'filter:api.video.get.result',
64 handler: video => {
65 video.name += ' <3'
9b474844 66
89cd1275
C
67 return video
68 }
69 })
70
3cabf353
C
71 for (const hook of [ 'filter:api.video.upload.accept.result', 'filter:api.live-video.create.accept.result' ]) {
72 registerHook({
73 target: hook,
74 handler: ({ accepted }, { videoBody, liveVideoBody }) => {
75 if (!accepted) return { accepted: false }
2158ac90 76
3cabf353
C
77 const name = videoBody
78 ? videoBody.name
79 : liveVideoBody.name
80
81 if (name.indexOf('bad word') !== -1) return { accepted: false, errorMessage: 'bad word' }
82
83 return { accepted: true }
84 }
85 })
86 }
2158ac90
RK
87
88 registerHook({
89 target: 'filter:api.video.pre-import-url.accept.result',
90 handler: ({ accepted }, { videoImportBody }) => {
91 if (!accepted) return { accepted: false }
92 if (videoImportBody.targetUrl.includes('bad')) return { accepted: false, errorMessage: 'bad target url' }
93
94 return { accepted: true }
95 }
96 })
97
98 registerHook({
99 target: 'filter:api.video.pre-import-torrent.accept.result',
100 handler: ({ accepted }, { videoImportBody }) => {
101 if (!accepted) return { accepted: false }
102 if (videoImportBody.name.includes('bad torrent')) return { accepted: false, errorMessage: 'bad torrent' }
103
104 return { accepted: true }
105 }
106 })
107
108 registerHook({
109 target: 'filter:api.video.post-import-url.accept.result',
110 handler: ({ accepted }, { video }) => {
111 if (!accepted) return { accepted: false }
112 if (video.name.includes('bad word')) return { accepted: false, errorMessage: 'bad word' }
113
114 return { accepted: true }
115 }
116 })
117
118 registerHook({
119 target: 'filter:api.video.post-import-torrent.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' }
89cd1275
C
123
124 return { accepted: true }
125 }
126 })
6691c522
C
127
128 registerHook({
129 target: 'filter:api.video-thread.create.accept.result',
130 handler: ({ accepted }, { commentBody }) => checkCommentBadWord(accepted, commentBody)
131 })
132
133 registerHook({
134 target: 'filter:api.video-comment-reply.create.accept.result',
135 handler: ({ accepted }, { commentBody }) => checkCommentBadWord(accepted, commentBody)
136 })
137
138 registerHook({
139 target: 'filter:api.video-threads.list.params',
140 handler: obj => addToCount(obj)
141 })
142
143 registerHook({
144 target: 'filter:api.video-threads.list.result',
145 handler: obj => addToTotal(obj)
146 })
147
148 registerHook({
149 target: 'filter:api.video-thread-comments.list.result',
150 handler: obj => {
151 obj.data.forEach(c => c.text += ' <3')
152
153 return obj
154 }
155 })
156
157 registerHook({
158 target: 'filter:video.auto-blacklist.result',
159 handler: (blacklisted, { video }) => {
160 if (blacklisted) return true
161 if (video.name.includes('please blacklist me')) return true
162
163 return false
164 }
165 })
4ce7eb71
C
166
167 registerHook({
168 target: 'filter:api.user.signup.allowed.result',
169 handler: (result, params) => {
170 if (params && params.body.email.includes('jma')) {
171 return { allowed: false, errorMessage: 'No jma' }
172 }
173
174 return result
175 }
176 })
9b474844
C
177}
178
179async function unregister () {
180 return
181}
182
183module.exports = {
184 register,
185 unregister
186}
187
188// ############################################################################
189
38267c0c
C
190function addToCount (obj, amount = 1) {
191 return Object.assign({}, obj, { count: obj.count + amount })
9b474844 192}
6691c522 193
38267c0c 194function addToTotal (result, amount = 1) {
6691c522
C
195 return {
196 data: result.data,
38267c0c 197 total: result.total + amount
6691c522
C
198 }
199}
200
201function checkCommentBadWord (accepted, commentBody) {
202 if (!accepted) return { accepted: false }
203 if (commentBody.text.indexOf('bad word') !== -1) return { accepted: false, errorMessage: 'bad word '}
204
205 return { accepted: true }
206}