]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/fixtures/peertube-plugin-test/main.js
Fix 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
10 'action:api.video-thread.created',
11 'action:api.video-comment-reply.created',
6f3fe96f
C
12 'action:api.video-comment.deleted',
13
14 'action:api.user.blocked',
15 'action:api.user.unblocked',
16 'action:api.user.registered',
17 'action:api.user.created',
18 'action:api.user.deleted',
19 'action:api.user.updated',
20 'action:api.user.oauth2-got-token'
89cd1275
C
21 ]
22
23 for (const h of actionHooks) {
24 registerHook({
25 target: h,
26 handler: () => peertubeHelpers.logger.debug('Run hook %s.', h)
27 })
28 }
9b474844
C
29
30 registerHook({
89cd1275
C
31 target: 'filter:api.videos.list.params',
32 handler: obj => addToCount(obj)
9b474844
C
33 })
34
89cd1275
C
35 registerHook({
36 target: 'filter:api.videos.list.result',
6691c522 37 handler: obj => addToTotal(obj)
9b474844
C
38 })
39
89cd1275
C
40 registerHook({
41 target: 'filter:api.video.get.result',
42 handler: video => {
43 video.name += ' <3'
9b474844 44
89cd1275
C
45 return video
46 }
47 })
48
49 registerHook({
50 target: 'filter:api.video.upload.accept.result',
51 handler: ({ accepted }, { videoBody }) => {
6691c522 52 if (!accepted) return { accepted: false }
2158ac90
RK
53 if (videoBody.name.indexOf('bad word') !== -1) return { accepted: false, errorMessage: 'bad word' }
54
55 return { accepted: true }
56 }
57 })
58
59 registerHook({
60 target: 'filter:api.video.pre-import-url.accept.result',
61 handler: ({ accepted }, { videoImportBody }) => {
62 if (!accepted) return { accepted: false }
63 if (videoImportBody.targetUrl.includes('bad')) return { accepted: false, errorMessage: 'bad target url' }
64
65 return { accepted: true }
66 }
67 })
68
69 registerHook({
70 target: 'filter:api.video.pre-import-torrent.accept.result',
71 handler: ({ accepted }, { videoImportBody }) => {
72 if (!accepted) return { accepted: false }
73 if (videoImportBody.name.includes('bad torrent')) return { accepted: false, errorMessage: 'bad torrent' }
74
75 return { accepted: true }
76 }
77 })
78
79 registerHook({
80 target: 'filter:api.video.post-import-url.accept.result',
81 handler: ({ accepted }, { video }) => {
82 if (!accepted) return { accepted: false }
83 if (video.name.includes('bad word')) return { accepted: false, errorMessage: 'bad word' }
84
85 return { accepted: true }
86 }
87 })
88
89 registerHook({
90 target: 'filter:api.video.post-import-torrent.accept.result',
91 handler: ({ accepted }, { video }) => {
92 if (!accepted) return { accepted: false }
93 if (video.name.includes('bad word')) return { accepted: false, errorMessage: 'bad word' }
89cd1275
C
94
95 return { accepted: true }
96 }
97 })
6691c522
C
98
99 registerHook({
100 target: 'filter:api.video-thread.create.accept.result',
101 handler: ({ accepted }, { commentBody }) => checkCommentBadWord(accepted, commentBody)
102 })
103
104 registerHook({
105 target: 'filter:api.video-comment-reply.create.accept.result',
106 handler: ({ accepted }, { commentBody }) => checkCommentBadWord(accepted, commentBody)
107 })
108
109 registerHook({
110 target: 'filter:api.video-threads.list.params',
111 handler: obj => addToCount(obj)
112 })
113
114 registerHook({
115 target: 'filter:api.video-threads.list.result',
116 handler: obj => addToTotal(obj)
117 })
118
119 registerHook({
120 target: 'filter:api.video-thread-comments.list.result',
121 handler: obj => {
122 obj.data.forEach(c => c.text += ' <3')
123
124 return obj
125 }
126 })
127
128 registerHook({
129 target: 'filter:video.auto-blacklist.result',
130 handler: (blacklisted, { video }) => {
131 if (blacklisted) return true
132 if (video.name.includes('please blacklist me')) return true
133
134 return false
135 }
136 })
4ce7eb71
C
137
138 registerHook({
139 target: 'filter:api.user.signup.allowed.result',
140 handler: (result, params) => {
141 if (params && params.body.email.includes('jma')) {
142 return { allowed: false, errorMessage: 'No jma' }
143 }
144
145 return result
146 }
147 })
9b474844
C
148}
149
150async function unregister () {
151 return
152}
153
154module.exports = {
155 register,
156 unregister
157}
158
159// ############################################################################
160
89cd1275
C
161function addToCount (obj) {
162 return Object.assign({}, obj, { count: obj.count + 1 })
9b474844 163}
6691c522
C
164
165function addToTotal (result) {
166 return {
167 data: result.data,
168 total: result.total + 1
169 }
170}
171
172function checkCommentBadWord (accepted, commentBody) {
173 if (!accepted) return { accepted: false }
174 if (commentBody.text.indexOf('bad word') !== -1) return { accepted: false, errorMessage: 'bad word '}
175
176 return { accepted: true }
177}