]>
Commit | Line | Data |
---|---|---|
89cd1275 C |
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 | ||
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 | ||
89cd1275 C |
42 | registerHook({ |
43 | target: 'filter:api.video.get.result', | |
44 | handler: video => { | |
45 | video.name += ' <3' | |
9b474844 | 46 | |
89cd1275 C |
47 | return video |
48 | } | |
49 | }) | |
50 | ||
3cabf353 C |
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 } | |
2158ac90 | 56 | |
3cabf353 C |
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 | } | |
2158ac90 RK |
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' } | |
89cd1275 C |
103 | |
104 | return { accepted: true } | |
105 | } | |
106 | }) | |
6691c522 C |
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 | }) | |
4ce7eb71 C |
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 | }) | |
9b474844 C |
157 | } |
158 | ||
159 | async function unregister () { | |
160 | return | |
161 | } | |
162 | ||
163 | module.exports = { | |
164 | register, | |
165 | unregister | |
166 | } | |
167 | ||
168 | // ############################################################################ | |
169 | ||
89cd1275 C |
170 | function addToCount (obj) { |
171 | return Object.assign({}, obj, { count: obj.count + 1 }) | |
9b474844 | 172 | } |
6691c522 C |
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 | } |