]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/fixtures/peertube-plugin-test/main.js
322c0610c934ccab9e62f8cb96f13c7707367056
[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.video.get.result',
44 handler: video => {
45 video.name += ' <3'
46
47 return video
48 }
49 })
50
51 for (const hook of [ 'filter:api.video.upload.accept.result', 'filter:api.live-video.create.accept.result' ]) {
52 registerHook({
53 target: hook,
54 handler: ({ accepted }, { videoBody, liveVideoBody }) => {
55 if (!accepted) return { accepted: false }
56
57 const name = videoBody
58 ? videoBody.name
59 : liveVideoBody.name
60
61 if (name.indexOf('bad word') !== -1) return { accepted: false, errorMessage: 'bad word' }
62
63 return { accepted: true }
64 }
65 })
66 }
67
68 registerHook({
69 target: 'filter:api.video.pre-import-url.accept.result',
70 handler: ({ accepted }, { videoImportBody }) => {
71 if (!accepted) return { accepted: false }
72 if (videoImportBody.targetUrl.includes('bad')) return { accepted: false, errorMessage: 'bad target url' }
73
74 return { accepted: true }
75 }
76 })
77
78 registerHook({
79 target: 'filter:api.video.pre-import-torrent.accept.result',
80 handler: ({ accepted }, { videoImportBody }) => {
81 if (!accepted) return { accepted: false }
82 if (videoImportBody.name.includes('bad torrent')) return { accepted: false, errorMessage: 'bad torrent' }
83
84 return { accepted: true }
85 }
86 })
87
88 registerHook({
89 target: 'filter:api.video.post-import-url.accept.result',
90 handler: ({ accepted }, { video }) => {
91 if (!accepted) return { accepted: false }
92 if (video.name.includes('bad word')) return { accepted: false, errorMessage: 'bad word' }
93
94 return { accepted: true }
95 }
96 })
97
98 registerHook({
99 target: 'filter:api.video.post-import-torrent.accept.result',
100 handler: ({ accepted }, { video }) => {
101 if (!accepted) return { accepted: false }
102 if (video.name.includes('bad word')) return { accepted: false, errorMessage: 'bad word' }
103
104 return { accepted: true }
105 }
106 })
107
108 registerHook({
109 target: 'filter:api.video-thread.create.accept.result',
110 handler: ({ accepted }, { commentBody }) => checkCommentBadWord(accepted, commentBody)
111 })
112
113 registerHook({
114 target: 'filter:api.video-comment-reply.create.accept.result',
115 handler: ({ accepted }, { commentBody }) => checkCommentBadWord(accepted, commentBody)
116 })
117
118 registerHook({
119 target: 'filter:api.video-threads.list.params',
120 handler: obj => addToCount(obj)
121 })
122
123 registerHook({
124 target: 'filter:api.video-threads.list.result',
125 handler: obj => addToTotal(obj)
126 })
127
128 registerHook({
129 target: 'filter:api.video-thread-comments.list.result',
130 handler: obj => {
131 obj.data.forEach(c => c.text += ' <3')
132
133 return obj
134 }
135 })
136
137 registerHook({
138 target: 'filter:video.auto-blacklist.result',
139 handler: (blacklisted, { video }) => {
140 if (blacklisted) return true
141 if (video.name.includes('please blacklist me')) return true
142
143 return false
144 }
145 })
146
147 registerHook({
148 target: 'filter:api.user.signup.allowed.result',
149 handler: (result, params) => {
150 if (params && params.body.email.includes('jma')) {
151 return { allowed: false, errorMessage: 'No jma' }
152 }
153
154 return result
155 }
156 })
157 }
158
159 async function unregister () {
160 return
161 }
162
163 module.exports = {
164 register,
165 unregister
166 }
167
168 // ############################################################################
169
170 function addToCount (obj) {
171 return Object.assign({}, obj, { count: obj.count + 1 })
172 }
173
174 function addToTotal (result) {
175 return {
176 data: result.data,
177 total: result.total + 1
178 }
179 }
180
181 function checkCommentBadWord (accepted, commentBody) {
182 if (!accepted) return { accepted: false }
183 if (commentBody.text.indexOf('bad word') !== -1) return { accepted: false, errorMessage: 'bad word '}
184
185 return { accepted: true }
186 }