]>
Commit | Line | Data |
---|---|---|
662e5d4f C |
1 | # Plugins & Themes |
2 | ||
d8e9a42c C |
3 | <!-- START doctoc generated TOC please keep comment here to allow auto update --> |
4 | <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> | |
5 | ||
d8e9a42c C |
6 | - [Concepts](#concepts) |
7 | - [Hooks](#hooks) | |
8 | - [Static files](#static-files) | |
9 | - [CSS](#css) | |
d2466f0a | 10 | - [Server API (only for plugins)](#server-api-only-for-plugins) |
d8e9a42c C |
11 | - [Settings](#settings) |
12 | - [Storage](#storage) | |
7545a094 | 13 | - [Update video constants](#update-video-constants) |
5e2b2e27 | 14 | - [Add custom routes](#add-custom-routes) |
9d4c60dc | 15 | - [Add custom WebSocket handlers](#add-custom-websocket-handlers) |
1a2820e6 | 16 | - [Add external auth methods](#add-external-auth-methods) |
7aca6b24 | 17 | - [Add new transcoding profiles](#add-new-transcoding-profiles) |
62bc0352 | 18 | - [Server helpers](#server-helpers) |
d2466f0a | 19 | - [Client API (themes & plugins)](#client-api-themes--plugins) |
3c33d714 | 20 | - [Get plugin static and router routes](#get-plugin-static-and-router-routes) |
b3af2601 C |
21 | - [Notifier](#notifier) |
22 | - [Markdown Renderer](#markdown-renderer) | |
096231d0 | 23 | - [Auth header](#auth-header) |
b3af2601 | 24 | - [Custom Modal](#custom-modal) |
7545a094 C |
25 | - [Translate](#translate) |
26 | - [Get public settings](#get-public-settings) | |
ec99e848 | 27 | - [Get server config](#get-server-config) |
8546fe87 | 28 | - [Add custom fields to video form](#add-custom-fields-to-video-form) |
d2466f0a | 29 | - [Register settings script](#register-settings-script) |
8afade26 | 30 | - [Plugin selector on HTML elements](#plugin-selector-on-html-elements) |
62bc0352 | 31 | - [HTML placeholder elements](#html-placeholder-elements) |
8beea2d3 | 32 | - [Add/remove left menu links](#addremove-left-menu-links) |
3c33d714 | 33 | - [Create client page](#create-client-page) |
d8e9a42c C |
34 | - [Publishing](#publishing) |
35 | - [Write a plugin/theme](#write-a-plugintheme) | |
36 | - [Clone the quickstart repository](#clone-the-quickstart-repository) | |
37 | - [Configure your repository](#configure-your-repository) | |
38 | - [Update README](#update-readme) | |
39 | - [Update package.json](#update-packagejson) | |
40 | - [Write code](#write-code) | |
7545a094 | 41 | - [Add translations](#add-translations) |
8546fe87 | 42 | - [Build your plugin](#build-your-plugin) |
d8e9a42c C |
43 | - [Test your plugin/theme](#test-your-plugintheme) |
44 | - [Publish](#publish) | |
d61515e1 | 45 | - [Unpublish](#unpublish) |
7545a094 | 46 | - [Plugin & Theme hooks/helpers API](#plugin--theme-hookshelpers-api) |
d8e9a42c C |
47 | - [Tips](#tips) |
48 | - [Compatibility with PeerTube](#compatibility-with-peertube) | |
49 | - [Spam/moderation plugin](#spammoderation-plugin) | |
112be80e | 50 | - [Other plugin examples](#other-plugin-examples) |
d8e9a42c C |
51 | |
52 | <!-- END doctoc generated TOC please keep comment here to allow auto update --> | |
53 | ||
662e5d4f C |
54 | ## Concepts |
55 | ||
32d7f2b7 | 56 | Themes are exactly the same as plugins, except that: |
662e5d4f C |
57 | * Their name starts with `peertube-theme-` instead of `peertube-plugin-` |
58 | * They cannot declare server code (so they cannot register server hooks or settings) | |
59 | * CSS files are loaded by client only if the theme is chosen by the administrator or the user | |
60 | ||
61 | ### Hooks | |
62 | ||
63 | A plugin registers functions in JavaScript to execute when PeerTube (server and client) fires events. There are 3 types of hooks: | |
5831dbcb | 64 | * `filter`: used to filter functions parameters or return values. |
662e5d4f C |
65 | For example to replace words in video comments, or change the videos list behaviour |
66 | * `action`: used to do something after a certain trigger. For example to send a hook every time a video is published | |
67 | * `static`: same than `action` but PeerTube waits their execution | |
662e5d4f C |
68 | |
69 | On server side, these hooks are registered by the `library` file defined in `package.json`. | |
70 | ||
71 | ```json | |
72 | { | |
73 | ..., | |
74 | "library": "./main.js", | |
75 | ..., | |
76 | } | |
77 | ``` | |
78 | ||
7545a094 C |
79 | And `main.js` defines a `register` function: |
80 | ||
81 | Example: | |
82 | ||
83 | ```js | |
84 | async function register ({ | |
85 | registerHook, | |
5831dbcb | 86 | |
7545a094 C |
87 | registerSetting, |
88 | settingsManager, | |
5831dbcb | 89 | |
7545a094 | 90 | storageManager, |
5831dbcb | 91 | |
7545a094 C |
92 | videoCategoryManager, |
93 | videoLicenceManager, | |
5e2b2e27 | 94 | videoLanguageManager, |
5831dbcb | 95 | |
5e2b2e27 | 96 | peertubeHelpers, |
5831dbcb C |
97 | |
98 | getRouter, | |
99 | ||
100 | registerExternalAuth, | |
101 | unregisterExternalAuth, | |
102 | registerIdAndPassAuth, | |
103 | unregisterIdAndPassAuth | |
7545a094 C |
104 | }) { |
105 | registerHook({ | |
106 | target: 'action:application.listening', | |
107 | handler: () => displayHelloWorld() | |
108 | }) | |
109 | } | |
110 | ``` | |
111 | ||
7226e90f | 112 | Hooks prefixed by `action:api` also give access the original **express** [Request](http://expressjs.com/en/api.html#req) and [Response](http://expressjs.com/en/api.html#res): |
113 | ||
114 | ```js | |
115 | async function register ({ | |
116 | registerHook, | |
117 | peertubeHelpers: { logger } | |
118 | }) { | |
119 | registerHook({ | |
120 | target: 'action:api.video.updated', | |
121 | handler: ({ req, res }) => logger.debug('original request parameters', { params: req.params }) | |
122 | }) | |
123 | } | |
124 | ``` | |
125 | ||
662e5d4f C |
126 | |
127 | On client side, these hooks are registered by the `clientScripts` files defined in `package.json`. | |
128 | All client scripts have scopes so PeerTube client only loads scripts it needs: | |
129 | ||
130 | ```json | |
131 | { | |
132 | ..., | |
133 | "clientScripts": [ | |
134 | { | |
135 | "script": "client/common-client-plugin.js", | |
136 | "scopes": [ "common" ] | |
137 | }, | |
138 | { | |
139 | "script": "client/video-watch-client-plugin.js", | |
140 | "scopes": [ "video-watch" ] | |
141 | } | |
142 | ], | |
143 | ... | |
144 | } | |
145 | ``` | |
146 | ||
7545a094 C |
147 | And these scripts also define a `register` function: |
148 | ||
149 | ```js | |
150 | function register ({ registerHook, peertubeHelpers }) { | |
151 | registerHook({ | |
152 | target: 'action:application.init', | |
153 | handler: () => onApplicationInit(peertubeHelpers) | |
154 | }) | |
155 | } | |
156 | ``` | |
157 | ||
662e5d4f C |
158 | ### Static files |
159 | ||
5831dbcb C |
160 | Plugins can declare static directories that PeerTube will serve (images for example) |
161 | from `/plugins/{plugin-name}/{plugin-version}/static/` | |
662e5d4f C |
162 | or `/themes/{theme-name}/{theme-version}/static/` routes. |
163 | ||
164 | ### CSS | |
165 | ||
166 | Plugins can declare CSS files that PeerTube will automatically inject in the client. | |
7545a094 C |
167 | If you need to override existing style, you can use the `#custom-css` selector: |
168 | ||
169 | ``` | |
170 | body#custom-css { | |
171 | color: red; | |
172 | } | |
173 | ||
174 | #custom-css .header { | |
175 | background-color: red; | |
176 | } | |
177 | ``` | |
662e5d4f | 178 | |
d2466f0a | 179 | ### Server API (only for plugins) |
662e5d4f C |
180 | |
181 | #### Settings | |
182 | ||
183 | Plugins can register settings, that PeerTube will inject in the administration interface. | |
d2466f0a C |
184 | The following fields will be automatically translated using the plugin translation files: `label`, `html`, `descriptionHTML`, `options.label`. |
185 | **These fields are injected in the plugin settings page as HTML, so pay attention to your translation files.** | |
662e5d4f C |
186 | |
187 | Example: | |
188 | ||
189 | ```js | |
d2466f0a C |
190 | function register (...) { |
191 | registerSetting({ | |
192 | name: 'admin-name', | |
193 | label: 'Admin name', | |
248875d2 | 194 | |
d2466f0a | 195 | type: 'input', |
248875d2 C |
196 | // type: 'input' | 'input-checkbox' | 'input-password' | 'input-textarea' | 'markdown-text' | 'markdown-enhanced' | 'select' | 'html' |
197 | ||
2bee9db5 C |
198 | // If type: 'select', give the select available options |
199 | options: [ | |
200 | { label: 'Label 1', value: 'value1' }, | |
201 | { label: 'Label 2', value: 'value2' } | |
202 | ], | |
203 | ||
204 | // If type: 'html', set the HTML that will be injected in the page | |
205 | html: '<strong class="...">Hello</strong><br /><br />' | |
206 | ||
248875d2 C |
207 | // Optional |
208 | descriptionHTML: 'The purpose of this field is...', | |
209 | ||
210 | default: 'my super name', | |
211 | ||
212 | // If the setting is not private, anyone can view its value (client code included) | |
213 | // If the setting is private, only server-side hooks can access it | |
214 | private: false | |
d2466f0a C |
215 | }) |
216 | ||
217 | const adminName = await settingsManager.getSetting('admin-name') | |
218 | ||
219 | const result = await settingsManager.getSettings([ 'admin-name', 'admin-password' ]) | |
220 | result['admin-name] | |
221 | ||
222 | settingsManager.onSettingsChange(settings => { | |
2bee9db5 | 223 | settings['admin-name'] |
d2466f0a C |
224 | }) |
225 | } | |
662e5d4f C |
226 | ``` |
227 | ||
d8e9a42c | 228 | #### Storage |
662e5d4f C |
229 | |
230 | Plugins can store/load JSON data, that PeerTube will store in its database (so don't put files in there). | |
231 | ||
232 | Example: | |
233 | ||
234 | ```js | |
302eba0d C |
235 | function register ({ |
236 | storageManager | |
237 | }) { | |
d2466f0a C |
238 | const value = await storageManager.getData('mykey') |
239 | await storageManager.storeData('mykey', { subkey: 'value' }) | |
240 | } | |
662e5d4f C |
241 | ``` |
242 | ||
096231d0 | 243 | You can also store files in the plugin data directory (`/{plugins-directory}/data/{npm-plugin-name}`) **in PeerTube >= 3.2**. |
302eba0d C |
244 | This directory and its content won't be deleted when your plugin is uninstalled/upgraded. |
245 | ||
246 | ```js | |
247 | function register ({ | |
248 | storageManager, | |
249 | peertubeHelpers | |
250 | }) { | |
251 | const basePath = peertubeHelpers.plugin.getDataDirectoryPath() | |
252 | ||
253 | fs.writeFile(path.join(basePath, 'filename.txt'), 'content of my file', function (err) { | |
254 | ... | |
255 | }) | |
256 | } | |
257 | ``` | |
258 | ||
7545a094 C |
259 | #### Update video constants |
260 | ||
dc3d9022 | 261 | You can add/delete video categories, licences or languages using the appropriate constant managers: |
7545a094 C |
262 | |
263 | ```js | |
2bee9db5 C |
264 | function register ({ |
265 | videoLanguageManager, | |
266 | videoCategoryManager, | |
267 | videoLicenceManager, | |
268 | videoPrivacyManager, | |
269 | playlistPrivacyManager | |
dc3d9022 | 270 | }) { |
271 | videoLanguageManager.addConstant('al_bhed', 'Al Bhed') | |
272 | videoLanguageManager.deleteConstant('fr') | |
7545a094 | 273 | |
dc3d9022 | 274 | videoCategoryManager.addConstant(42, 'Best category') |
275 | videoCategoryManager.deleteConstant(1) // Music | |
276 | videoCategoryManager.resetConstants() // Reset to initial categories | |
277 | videoCategoryManager.getConstants() // Retrieve all category constants | |
7545a094 | 278 | |
dc3d9022 | 279 | videoLicenceManager.addConstant(42, 'Best licence') |
280 | videoLicenceManager.deleteConstant(7) // Public domain | |
b3af2601 | 281 | |
dc3d9022 | 282 | videoPrivacyManager.deleteConstant(2) // Remove Unlisted video privacy |
283 | playlistPrivacyManager.deleteConstant(3) // Remove Private video playlist privacy | |
d2466f0a | 284 | } |
7545a094 C |
285 | ``` |
286 | ||
5e2b2e27 C |
287 | #### Add custom routes |
288 | ||
289 | You can create custom routes using an [express Router](https://expressjs.com/en/4x/api.html#router) for your plugin: | |
290 | ||
291 | ```js | |
302eba0d | 292 | function register ({ |
3b2844f8 | 293 | getRouter |
302eba0d | 294 | }) { |
d2466f0a C |
295 | const router = getRouter() |
296 | router.get('/ping', (req, res) => res.json({ message: 'pong' })) | |
302eba0d C |
297 | |
298 | // Users are automatically authenticated | |
b31d7262 C |
299 | router.get('/auth', async (res, res) => { |
300 | const user = await peertubeHelpers.user.getAuthUser(res) | |
302eba0d C |
301 | |
302 | const isAdmin = user.role === 0 | |
303 | const isModerator = user.role === 1 | |
304 | const isUser = user.role === 2 | |
305 | ||
306 | res.json({ | |
307 | username: user.username, | |
308 | isAdmin, | |
309 | isModerator, | |
310 | isUser | |
311 | }) | |
312 | }) | |
d2466f0a | 313 | } |
5e2b2e27 C |
314 | ``` |
315 | ||
316 | The `ping` route can be accessed using: | |
317 | * `/plugins/:pluginName/:pluginVersion/router/ping` | |
318 | * Or `/plugins/:pluginName/router/ping` | |
319 | ||
320 | ||
9d4c60dc C |
321 | #### Add custom WebSocket handlers |
322 | ||
fbe3fa54 JL |
323 | **PeerTube >= 5.0** |
324 | ||
9d4c60dc C |
325 | You can create custom WebSocket servers (like [ws](https://github.com/websockets/ws) for example) using `registerWebSocketRoute`: |
326 | ||
327 | ```js | |
328 | function register ({ | |
329 | registerWebSocketRoute, | |
330 | peertubeHelpers | |
331 | }) { | |
332 | const wss = new WebSocketServer({ noServer: true }) | |
333 | ||
334 | wss.on('connection', function connection(ws) { | |
335 | peertubeHelpers.logger.info('WebSocket connected!') | |
336 | ||
337 | setInterval(() => { | |
338 | ws.send('WebSocket message sent by server'); | |
339 | }, 1000) | |
340 | }) | |
341 | ||
342 | registerWebSocketRoute({ | |
343 | route: '/my-websocket-route', | |
344 | ||
345 | handler: (request, socket, head) => { | |
346 | wss.handleUpgrade(request, socket, head, ws => { | |
347 | wss.emit('connection', ws, request) | |
348 | }) | |
349 | } | |
350 | }) | |
351 | } | |
352 | ``` | |
353 | ||
354 | The `my-websocket-route` route can be accessed using: | |
355 | * `/plugins/:pluginName/:pluginVersion/ws/my-websocket-route` | |
356 | * Or `/plugins/:pluginName/ws/my-websocket-route` | |
357 | ||
5831dbcb C |
358 | #### Add external auth methods |
359 | ||
360 | If you want to add a classic username/email and password auth method (like [LDAP](https://framagit.org/framasoft/peertube/official-plugins/-/tree/master/peertube-plugin-auth-ldap) for example): | |
361 | ||
362 | ```js | |
d2466f0a C |
363 | function register (...) { |
364 | ||
365 | registerIdAndPassAuth({ | |
366 | authName: 'my-auth-method', | |
367 | ||
368 | // PeerTube will try all id and pass plugins in the weight DESC order | |
369 | // Exposing this value in the plugin settings could be interesting | |
370 | getWeight: () => 60, | |
371 | ||
372 | // Optional function called by PeerTube when the user clicked on the logout button | |
373 | onLogout: user => { | |
374 | console.log('User %s logged out.', user.username') | |
375 | }, | |
376 | ||
377 | // Optional function called by PeerTube when the access token or refresh token are generated/refreshed | |
378 | hookTokenValidity: ({ token, type }) => { | |
379 | if (type === 'access') return { valid: true } | |
380 | if (type === 'refresh') return { valid: false } | |
381 | }, | |
382 | ||
383 | // Used by PeerTube when the user tries to authenticate | |
384 | login: ({ id, password }) => { | |
385 | if (id === 'user' && password === 'super password') { | |
386 | return { | |
387 | username: 'user' | |
388 | email: 'user@example.com' | |
389 | role: 2 | |
390 | displayName: 'User display name' | |
391 | } | |
5831dbcb | 392 | } |
5831dbcb | 393 | |
d2466f0a C |
394 | // Auth failed |
395 | return null | |
396 | } | |
397 | }) | |
5831dbcb | 398 | |
d2466f0a C |
399 | // Unregister this auth method |
400 | unregisterIdAndPassAuth('my-auth-method') | |
401 | } | |
5831dbcb C |
402 | ``` |
403 | ||
404 | You can also add an external auth method (like [OpenID](https://framagit.org/framasoft/peertube/official-plugins/-/tree/master/peertube-plugin-auth-openid-connect), [SAML2](https://framagit.org/framasoft/peertube/official-plugins/-/tree/master/peertube-plugin-auth-saml2) etc): | |
405 | ||
406 | ```js | |
d2466f0a C |
407 | function register (...) { |
408 | ||
409 | // result contains the userAuthenticated auth method you can call to authenticate a user | |
410 | const result = registerExternalAuth({ | |
411 | authName: 'my-auth-method', | |
412 | ||
413 | // Will be displayed in a button next to the login form | |
414 | authDisplayName: () => 'Auth method' | |
415 | ||
416 | // If the user click on the auth button, PeerTube will forward the request in this function | |
417 | onAuthRequest: (req, res) => { | |
418 | res.redirect('https://external-auth.example.com/auth') | |
419 | }, | |
420 | ||
421 | // Same than registerIdAndPassAuth option | |
422 | // onLogout: ... | |
423 | ||
424 | // Same than registerIdAndPassAuth option | |
425 | // hookTokenValidity: ... | |
426 | }) | |
427 | ||
428 | router.use('/external-auth-callback', (req, res) => { | |
429 | // Forward the request to PeerTube | |
430 | result.userAuthenticated({ | |
431 | req, | |
432 | res, | |
433 | username: 'user' | |
434 | email: 'user@example.com' | |
435 | role: 2 | |
60b880ac C |
436 | displayName: 'User display name', |
437 | ||
438 | // Custom admin flags (bypass video auto moderation etc.) | |
439 | // https://github.com/Chocobozzz/PeerTube/blob/develop/shared/models/users/user-flag.model.ts | |
440 | // PeerTube >= 5.1 | |
441 | adminFlags: 0, | |
442 | // Quota in bytes | |
443 | // PeerTube >= 5.1 | |
444 | videoQuota: 1024 * 1024 * 1024, // 1GB | |
445 | // PeerTube >= 5.1 | |
446 | videoQuotaDaily: -1, // Unlimited | |
447 | ||
448 | // Update the user profile if it already exists | |
449 | // Default behaviour is no update | |
450 | // Introduced in PeerTube >= 5.1 | |
451 | userUpdater: ({ fieldName, currentValue, newValue }) => { | |
452 | // Always use new value except for videoQuotaDaily field | |
453 | if (fieldName === 'videoQuotaDaily') return currentValue | |
454 | ||
455 | return newValue | |
456 | } | |
d2466f0a | 457 | }) |
5831dbcb | 458 | }) |
5831dbcb | 459 | |
d2466f0a C |
460 | // Unregister this external auth method |
461 | unregisterExternalAuth('my-auth-method) | |
462 | } | |
5831dbcb C |
463 | ``` |
464 | ||
7aca6b24 C |
465 | #### Add new transcoding profiles |
466 | ||
467 | Adding transcoding profiles allow admins to change ffmpeg encoding parameters and/or encoders. | |
468 | A transcoding profile has to be chosen by the admin of the instance using the admin configuration. | |
469 | ||
470 | ```js | |
471 | async function register ({ | |
472 | transcodingManager | |
473 | }) { | |
474 | ||
475 | // Adapt bitrate when using libx264 encoder | |
476 | { | |
477 | const builder = (options) => { | |
478 | const { input, resolution, fps, streamNum } = options | |
479 | ||
480 | const streamString = streamNum ? ':' + streamNum : '' | |
481 | ||
482 | // You can also return a promise | |
a60696ab | 483 | // All these options are optional |
7aca6b24 | 484 | return { |
a60696ab C |
485 | scaleFilter: { |
486 | // Used to define an alternative scale filter, needed by some encoders | |
487 | // Default to 'scale' | |
488 | name: 'scale_vaapi' | |
489 | }, | |
490 | // Default to [] | |
5fb7cfba | 491 | inputOptions: [], |
a60696ab | 492 | // Default to [] |
7aca6b24 C |
493 | outputOptions: [ |
494 | // Use a custom bitrate | |
495 | '-b' + streamString + ' 10K' | |
496 | ] | |
497 | } | |
498 | } | |
499 | ||
500 | const encoder = 'libx264' | |
501 | const profileName = 'low-quality' | |
502 | ||
503 | // Support this profile for VOD transcoding | |
504 | transcodingManager.addVODProfile(encoder, profileName, builder) | |
505 | ||
506 | // And/Or support this profile for live transcoding | |
507 | transcodingManager.addLiveProfile(encoder, profileName, builder) | |
508 | } | |
509 | ||
510 | { | |
511 | const builder = (options) => { | |
512 | const { streamNum } = options | |
513 | ||
514 | const streamString = streamNum ? ':' + streamNum : '' | |
515 | ||
516 | // Always copy stream when PeerTube use libfdk_aac or aac encoders | |
517 | return { | |
518 | copy: true | |
519 | } | |
520 | } | |
521 | ||
522 | const profileName = 'copy-audio' | |
523 | ||
524 | for (const encoder of [ 'libfdk_aac', 'aac' ]) { | |
525 | transcodingManager.addVODProfile(encoder, profileName, builder) | |
526 | } | |
527 | } | |
528 | ``` | |
529 | ||
530 | PeerTube will try different encoders depending on their priority. | |
531 | If the encoder is not available in the current transcoding profile or in ffmpeg, it tries the next one. | |
532 | Plugins can change the order of these encoders and add their custom encoders: | |
533 | ||
534 | ```js | |
535 | async function register ({ | |
536 | transcodingManager | |
537 | }) { | |
538 | ||
539 | // Adapt bitrate when using libx264 encoder | |
540 | { | |
541 | const builder = () => { | |
542 | return { | |
5fb7cfba | 543 | inputOptions: [], |
7aca6b24 C |
544 | outputOptions: [] |
545 | } | |
546 | } | |
547 | ||
548 | // Support libopus and libvpx-vp9 encoders (these codecs could be incompatible with the player) | |
549 | transcodingManager.addVODProfile('libopus', 'test-vod-profile', builder) | |
550 | ||
551 | // Default priorities are ~100 | |
552 | // Lowest priority = 1 | |
553 | transcodingManager.addVODEncoderPriority('audio', 'libopus', 1000) | |
554 | ||
555 | transcodingManager.addVODProfile('libvpx-vp9', 'test-vod-profile', builder) | |
556 | transcodingManager.addVODEncoderPriority('video', 'libvpx-vp9', 1000) | |
557 | ||
558 | transcodingManager.addLiveProfile('libopus', 'test-live-profile', builder) | |
559 | transcodingManager.addLiveEncoderPriority('audio', 'libopus', 1000) | |
560 | } | |
561 | ``` | |
562 | ||
d5fc35c2 TLC |
563 | During live transcode input options are applied once for each target resolution. |
564 | Plugins are responsible for detecting such situation and applying input options only once if necessary. | |
565 | ||
62bc0352 | 566 | #### Server helpers |
d2466f0a C |
567 | |
568 | PeerTube provides your plugin some helpers. For example: | |
569 | ||
570 | ```js | |
571 | async function register ({ | |
572 | peertubeHelpers | |
573 | }) { | |
574 | // Block a server | |
575 | { | |
576 | const serverActor = await peertubeHelpers.server.getServerActor() | |
577 | ||
578 | await peertubeHelpers.moderation.blockServer({ byAccountId: serverActor.Account.id, hostToBlock: '...' }) | |
579 | } | |
580 | ||
581 | // Load a video | |
582 | { | |
583 | const video = await peertubeHelpers.videos.loadByUrl('...') | |
584 | } | |
585 | } | |
586 | ``` | |
587 | ||
c5c95361 | 588 | See the [plugin API reference](https://docs.joinpeertube.org/api/plugins) to see the complete helpers list. |
d2466f0a C |
589 | |
590 | ### Client API (themes & plugins) | |
7545a094 | 591 | |
3c33d714 | 592 | #### Get plugin static and router routes |
7545a094 C |
593 | |
594 | To get your plugin static route: | |
595 | ||
596 | ```js | |
d2466f0a C |
597 | function register (...) { |
598 | const baseStaticUrl = peertubeHelpers.getBaseStaticRoute() | |
599 | const imageUrl = baseStaticUrl + '/images/chocobo.png' | |
600 | } | |
7545a094 C |
601 | ``` |
602 | ||
3c33d714 C |
603 | And to get your plugin router route, use `peertubeHelpers.getBaseRouterRoute()`: |
604 | ||
605 | ```js | |
606 | function register (...) { | |
607 | registerHook({ | |
608 | target: 'action:video-watch.video.loaded', | |
609 | handler: ({ video }) => { | |
610 | fetch(peertubeHelpers.getBaseRouterRoute() + '/my/plugin/api', { | |
611 | method: 'GET', | |
612 | headers: peertubeHelpers.getAuthHeader() | |
613 | }).then(res => res.json()) | |
614 | .then(data => console.log('Hi %s.', data)) | |
615 | } | |
616 | }) | |
617 | } | |
618 | ``` | |
619 | ||
620 | ||
74c2dece K |
621 | #### Notifier |
622 | ||
623 | To notify the user with the PeerTube ToastModule: | |
624 | ||
625 | ```js | |
d2466f0a C |
626 | function register (...) { |
627 | const { notifier } = peertubeHelpers | |
628 | notifier.success('Success message content.') | |
629 | notifier.error('Error message content.') | |
630 | } | |
74c2dece K |
631 | ``` |
632 | ||
8c7725dc K |
633 | #### Markdown Renderer |
634 | ||
635 | To render a formatted markdown text to HTML: | |
636 | ||
637 | ```js | |
d2466f0a C |
638 | function register (...) { |
639 | const { markdownRenderer } = peertubeHelpers | |
8c7725dc | 640 | |
d2466f0a C |
641 | await markdownRenderer.textMarkdownToHTML('**My Bold Text**') |
642 | // return <strong>My Bold Text</strong> | |
8c7725dc | 643 | |
d2466f0a C |
644 | await markdownRenderer.enhancedMarkdownToHTML('![alt-img](http://.../my-image.jpg)') |
645 | // return <img alt=alt-img src=http://.../my-image.jpg /> | |
646 | } | |
8c7725dc K |
647 | ``` |
648 | ||
096231d0 C |
649 | #### Auth header |
650 | ||
651 | **PeerTube >= 3.2** | |
652 | ||
653 | To make your own HTTP requests using the current authenticated user, use an helper to automatically set appropriate headers: | |
654 | ||
655 | ```js | |
656 | function register (...) { | |
657 | registerHook({ | |
658 | target: 'action:auth-user.information-loaded', | |
659 | handler: ({ user }) => { | |
660 | ||
661 | // Useless because we have the same info in the ({ user }) parameter | |
662 | // It's just an example | |
663 | fetch('/api/v1/users/me', { | |
664 | method: 'GET', | |
665 | headers: peertubeHelpers.getAuthHeader() | |
666 | }).then(res => res.json()) | |
667 | .then(data => console.log('Hi %s.', data.username)) | |
668 | } | |
669 | }) | |
670 | } | |
671 | ``` | |
672 | ||
437e8e06 K |
673 | #### Custom Modal |
674 | ||
675 | To show a custom modal: | |
676 | ||
677 | ```js | |
d2466f0a C |
678 | function register (...) { |
679 | peertubeHelpers.showModal({ | |
680 | title: 'My custom modal title', | |
681 | content: '<p>My custom modal content</p>', | |
682 | // Optionals parameters : | |
683 | // show close icon | |
684 | close: true, | |
685 | // show cancel button and call action() after hiding modal | |
686 | cancel: { value: 'cancel', action: () => {} }, | |
687 | // show confirm button and call action() after hiding modal | |
688 | confirm: { value: 'confirm', action: () => {} }, | |
689 | }) | |
690 | } | |
437e8e06 K |
691 | ``` |
692 | ||
7545a094 C |
693 | #### Translate |
694 | ||
695 | You can translate some strings of your plugin (PeerTube will use your `translations` object of your `package.json` file): | |
696 | ||
697 | ```js | |
d2466f0a C |
698 | function register (...) { |
699 | peertubeHelpers.translate('User name') | |
700 | .then(translation => console.log('Translated User name by ' + translation)) | |
701 | } | |
7545a094 C |
702 | ``` |
703 | ||
704 | #### Get public settings | |
705 | ||
706 | To get your public plugin settings: | |
707 | ||
708 | ```js | |
d2466f0a C |
709 | function register (...) { |
710 | peertubeHelpers.getSettings() | |
711 | .then(s => { | |
712 | if (!s || !s['site-id'] || !s['url']) { | |
713 | console.error('Matomo settings are not set.') | |
714 | return | |
715 | } | |
5831dbcb | 716 | |
d2466f0a C |
717 | // ... |
718 | }) | |
719 | } | |
5831dbcb | 720 | ``` |
7545a094 | 721 | |
ec99e848 C |
722 | #### Get server config |
723 | ||
724 | ```js | |
d2466f0a C |
725 | function register (...) { |
726 | peertubeHelpers.getServerConfig() | |
727 | .then(config => { | |
728 | console.log('Fetched server config.', config) | |
729 | }) | |
730 | } | |
ec99e848 C |
731 | ``` |
732 | ||
8546fe87 | 733 | #### Add custom fields to video form |
e08a26e2 C |
734 | |
735 | To add custom fields in the video form (in *Plugin settings* tab): | |
736 | ||
8546fe87 | 737 | ```js |
e08a26e2 C |
738 | async function register ({ registerVideoField, peertubeHelpers }) { |
739 | const descriptionHTML = await peertubeHelpers.translate(descriptionSource) | |
740 | const commonOptions = { | |
741 | name: 'my-field-name, | |
742 | label: 'My added field', | |
743 | descriptionHTML: 'Optional description', | |
03a65456 C |
744 | |
745 | // type: 'input' | 'input-checkbox' | 'input-password' | 'input-textarea' | 'markdown-text' | 'markdown-enhanced' | 'select' | 'html' | |
746 | // /!\ 'input-checkbox' could send "false" and "true" strings instead of boolean | |
e08a26e2 | 747 | type: 'input-textarea', |
03a65456 | 748 | |
0f319334 | 749 | default: '', |
3c065fe3 | 750 | |
0f319334 C |
751 | // Optional, to hide a field depending on the current form state |
752 | // liveVideo is in the options object when the user is creating/updating a live | |
753 | // videoToUpdate is in the options object when the user is updating a video | |
754 | hidden: ({ formValues, videoToUpdate, liveVideo }) => { | |
755 | return formValues.pluginData['other-field'] === 'toto' | |
3c065fe3 C |
756 | }, |
757 | ||
758 | // Optional, to display an error depending on the form state | |
759 | error: ({ formValues, value }) => { | |
760 | if (formValues['privacy'] !== 1 && formValues['privacy'] !== 2) return { error: false } | |
761 | if (value === true) return { error: false } | |
762 | ||
763 | return { error: true, text: 'Should be enabled' } | |
0f319334 | 764 | } |
e08a26e2 C |
765 | } |
766 | ||
3c065fe3 C |
767 | const videoFormOptions = { |
768 | // Optional, to choose to put your setting in a specific tab in video form | |
769 | // type: 'main' | 'plugin-settings' | |
770 | tab: 'main' | |
771 | } | |
772 | ||
87e0b71d | 773 | for (const type of [ 'upload', 'import-url', 'import-torrent', 'update', 'go-live' ]) { |
3c065fe3 | 774 | registerVideoField(commonOptions, { type, ...videoFormOptions }) |
e08a26e2 C |
775 | } |
776 | } | |
777 | ``` | |
778 | ||
779 | PeerTube will send this field value in `body.pluginData['my-field-name']` and fetch it from `video.pluginData['my-field-name']`. | |
780 | ||
781 | So for example, if you want to store an additional metadata for videos, register the following hooks in **server**: | |
782 | ||
8546fe87 | 783 | ```js |
e08a26e2 C |
784 | async function register ({ |
785 | registerHook, | |
786 | storageManager | |
787 | }) { | |
788 | const fieldName = 'my-field-name' | |
789 | ||
790 | // Store data associated to this video | |
791 | registerHook({ | |
792 | target: 'action:api.video.updated', | |
793 | handler: ({ video, body }) => { | |
794 | if (!body.pluginData) return | |
795 | ||
796 | const value = body.pluginData[fieldName] | |
797 | if (!value) return | |
798 | ||
799 | storageManager.storeData(fieldName + '-' + video.id, value) | |
800 | } | |
801 | }) | |
802 | ||
803 | // Add your custom value to the video, so the client autofill your field using the previously stored value | |
804 | registerHook({ | |
805 | target: 'filter:api.video.get.result', | |
806 | handler: async (video) => { | |
807 | if (!video) return video | |
808 | if (!video.pluginData) video.pluginData = {} | |
809 | ||
810 | const result = await storageManager.getData(fieldName + '-' + video.id) | |
811 | video.pluginData[fieldName] = result | |
812 | ||
813 | return video | |
814 | } | |
815 | }) | |
816 | } | |
2498aaea | 817 | ``` |
d2466f0a C |
818 | |
819 | #### Register settings script | |
820 | ||
821 | To hide some fields in your settings plugin page depending on the form state: | |
822 | ||
823 | ```js | |
824 | async function register ({ registerSettingsScript }) { | |
825 | registerSettingsScript({ | |
826 | isSettingHidden: options => { | |
827 | if (options.setting.name === 'my-setting' && options.formValues['field45'] === '2') { | |
828 | return true | |
829 | } | |
830 | ||
831 | return false | |
832 | } | |
833 | }) | |
834 | } | |
835 | ``` | |
8afade26 C |
836 | #### Plugin selector on HTML elements |
837 | ||
838 | PeerTube provides some selectors (using `id` HTML attribute) on important blocks so plugins can easily change their style. | |
839 | ||
840 | For example `#plugin-selector-login-form` could be used to hide the login form. | |
841 | ||
c5c95361 | 842 | See the complete list on https://docs.joinpeertube.org/api/plugins |
d2466f0a | 843 | |
62bc0352 C |
844 | #### HTML placeholder elements |
845 | ||
846 | PeerTube provides some HTML id so plugins can easily insert their own element: | |
847 | ||
b044cb18 | 848 | ```js |
62bc0352 C |
849 | async function register (...) { |
850 | const elem = document.createElement('div') | |
851 | elem.className = 'hello-world-h4' | |
852 | elem.innerHTML = '<h4>Hello everybody! This is an element next to the player</h4>' | |
853 | ||
854 | document.getElementById('plugin-placeholder-player-next').appendChild(elem) | |
855 | } | |
856 | ``` | |
857 | ||
c5c95361 | 858 | See the complete list on https://docs.joinpeertube.org/api/plugins |
d2466f0a | 859 | |
8beea2d3 C |
860 | #### Add/remove left menu links |
861 | ||
862 | Left menu links can be filtered (add/remove a section or add/remove links) using the `filter:left-menu.links.create.result` client hook. | |
863 | ||
3c33d714 C |
864 | #### Create client page |
865 | ||
866 | To create a client page, register a new client route: | |
867 | ||
868 | ```js | |
869 | function register ({ registerClientRoute }) { | |
870 | registerClientRoute({ | |
871 | route: 'my-super/route', | |
872 | onMount: ({ rootEl }) => { | |
873 | rootEl.innerHTML = 'hello' | |
874 | } | |
875 | }) | |
876 | } | |
877 | ``` | |
878 | ||
8beea2d3 | 879 | |
662e5d4f C |
880 | ### Publishing |
881 | ||
754c73f3 | 882 | PeerTube plugins and themes should be published on [NPM](https://www.npmjs.com/) so that PeerTube indexes take into account your plugin (after ~ 1 day). An official plugin index is available on [packages.joinpeertube.org](https://packages.joinpeertube.org/api/v1/plugins), with no interface to present packages. |
662e5d4f | 883 | |
4c0a6954 | 884 | > The official plugin index source code is available at https://framagit.org/framasoft/peertube/plugin-index |
885 | ||
662e5d4f C |
886 | ## Write a plugin/theme |
887 | ||
888 | Steps: | |
889 | * Find a name for your plugin or your theme (must not have spaces, it can only contain lowercase letters and `-`) | |
890 | * Add the appropriate prefix: | |
891 | * If you develop a plugin, add `peertube-plugin-` prefix to your plugin name (for example: `peertube-plugin-mysupername`) | |
892 | * If you develop a theme, add `peertube-theme-` prefix to your theme name (for example: `peertube-theme-mysupertheme`) | |
893 | * Clone the quickstart repository | |
894 | * Configure your repository | |
895 | * Update `README.md` | |
896 | * Update `package.json` | |
897 | * Register hooks, add CSS and static files | |
898 | * Test your plugin/theme with a local PeerTube installation | |
899 | * Publish your plugin/theme on NPM | |
900 | ||
901 | ### Clone the quickstart repository | |
902 | ||
903 | If you develop a plugin, clone the `peertube-plugin-quickstart` repository: | |
904 | ||
905 | ``` | |
906 | $ git clone https://framagit.org/framasoft/peertube/peertube-plugin-quickstart.git peertube-plugin-mysupername | |
907 | ``` | |
908 | ||
909 | If you develop a theme, clone the `peertube-theme-quickstart` repository: | |
910 | ||
911 | ``` | |
912 | $ git clone https://framagit.org/framasoft/peertube/peertube-theme-quickstart.git peertube-theme-mysupername | |
913 | ``` | |
914 | ||
915 | ### Configure your repository | |
916 | ||
917 | Set your repository URL: | |
918 | ||
919 | ``` | |
920 | $ cd peertube-plugin-mysupername # or cd peertube-theme-mysupername | |
921 | $ git remote set-url origin https://your-git-repo | |
922 | ``` | |
923 | ||
924 | ### Update README | |
925 | ||
926 | Update `README.md` file: | |
927 | ||
928 | ``` | |
929 | $ $EDITOR README.md | |
930 | ``` | |
931 | ||
932 | ### Update package.json | |
933 | ||
934 | Update the `package.json` fields: | |
935 | * `name` (should start with `peertube-plugin-` or `peertube-theme-`) | |
936 | * `description` | |
937 | * `homepage` | |
938 | * `author` | |
939 | * `bugs` | |
940 | * `engine.peertube` (the PeerTube version compatibility, must be `>=x.y.z` and nothing else) | |
5831dbcb | 941 | |
662e5d4f | 942 | **Caution:** Don't update or remove other keys, or PeerTube will not be able to index/install your plugin. |
5831dbcb | 943 | If you don't need static directories, use an empty `object`: |
662e5d4f C |
944 | |
945 | ```json | |
946 | { | |
947 | ..., | |
948 | "staticDirs": {}, | |
949 | ... | |
950 | } | |
951 | ``` | |
952 | ||
9fa6ca16 | 953 | And if you don't need CSS or client script files, use an empty `array`: |
662e5d4f C |
954 | |
955 | ```json | |
956 | { | |
957 | ..., | |
958 | "css": [], | |
9fa6ca16 | 959 | "clientScripts": [], |
662e5d4f C |
960 | ... |
961 | } | |
962 | ``` | |
963 | ||
964 | ### Write code | |
965 | ||
966 | Now you can register hooks or settings, write CSS and add static directories to your plugin or your theme :) | |
b969539c C |
967 | It's up to you to check the code you write will be compatible with the PeerTube NodeJS version, and will be supported by web browsers. |
968 | ||
969 | **JavaScript** | |
662e5d4f | 970 | |
662e5d4f | 971 | If you want to write modern JavaScript, please use a transpiler like [Babel](https://babeljs.io/). |
8b03e2ce | 972 | |
b969539c C |
973 | **Typescript** |
974 | ||
56162c6a JL |
975 | The easiest way to use __Typescript__ for both front-end and backend code is to clone [peertube-plugin-quickstart-typescript](https://github.com/JohnXLivingston/peertube-plugin-quickstart-typescript/) (also available on [framagit](https://framagit.org/Livingston/peertube-plugin-quickstart-typescript/)) instead of `peertube-plugin-quickstart`. |
976 | Please read carefully the [README file](https://github.com/JohnXLivingston/peertube-plugin-quickstart-typescript/blob/main/README.md), as there are some other differences with `peertube-plugin-quickstart` (using SCSS instead of CSS, linting rules, ...). | |
977 | ||
978 | If you don't want to use `peertube-plugin-quickstart-typescript`, you can also manually add a dev dependency to __Peertube__ types: | |
8b03e2ce | 979 | |
8b03e2ce | 980 | ``` |
b8fa3e8c | 981 | npm install --save-dev @peertube/peertube-types |
8b03e2ce | 982 | ``` |
983 | ||
984 | This package exposes *server* definition files by default: | |
985 | ```ts | |
b969539c | 986 | import { RegisterServerOptions } from '@peertube/peertube-types' |
8b03e2ce | 987 | |
988 | export async function register ({ registerHook }: RegisterServerOptions) { | |
989 | registerHook({ | |
990 | target: 'action:application.listening', | |
991 | handler: () => displayHelloWorld() | |
992 | }) | |
993 | } | |
994 | ``` | |
995 | ||
996 | But it also exposes client types and various models used in __PeerTube__: | |
997 | ```ts | |
b969539c C |
998 | import { Video } from '@peertube/peertube-types'; |
999 | import { RegisterClientOptions } from '@peertube/peertube-types/client'; | |
8b03e2ce | 1000 | |
b8fa3e8c | 1001 | function register({ registerHook, peertubeHelpers }: RegisterClientOptions) { |
8b03e2ce | 1002 | registerHook({ |
b8fa3e8c | 1003 | target: 'action:admin-plugin-settings.init', |
1004 | handler: ({ npmName }: { npmName: string }) => { | |
1005 | if ('peertube-plugin-transcription' !== npmName) { | |
1006 | return; | |
1007 | } | |
1008 | }, | |
1009 | }); | |
1010 | ||
1011 | registerHook({ | |
1012 | target: 'action:video-watch.video.loaded', | |
1013 | handler: ({ video }: { video: Video }) => { | |
1014 | fetch(`${peertubeHelpers.getBaseRouterRoute()}/videos/${video.uuid}/captions`, { | |
1015 | method: 'PUT', | |
1016 | headers: peertubeHelpers.getAuthHeader(), | |
b969539c C |
1017 | }).then((res) => res.json()) |
1018 | .then((data) => console.log('Hi %s.', data)); | |
b8fa3e8c | 1019 | }, |
1020 | }); | |
8b03e2ce | 1021 | } |
b8fa3e8c | 1022 | |
1023 | export { register }; | |
8b03e2ce | 1024 | ``` |
662e5d4f | 1025 | |
7545a094 C |
1026 | ### Add translations |
1027 | ||
1028 | If you want to translate strings of your plugin (like labels of your registered settings), create a file and add it to `package.json`: | |
1029 | ||
1030 | ```json | |
1031 | { | |
1032 | ..., | |
1033 | "translations": { | |
67baf647 | 1034 | "fr": "./languages/fr.json", |
7545a094 C |
1035 | "pt-BR": "./languages/pt-BR.json" |
1036 | }, | |
1037 | ... | |
1038 | } | |
1039 | ``` | |
1040 | ||
1041 | The key should be one of the locales defined in [i18n.ts](https://github.com/Chocobozzz/PeerTube/blob/develop/shared/models/i18n/i18n.ts). | |
7545a094 | 1042 | |
112be80e C |
1043 | Translation files are just objects, with the english sentence as the key and the translation as the value. |
1044 | `fr.json` could contain for example: | |
1045 | ||
1046 | ```json | |
1047 | { | |
1048 | "Hello world": "Hello le monde" | |
1049 | } | |
1050 | ``` | |
1051 | ||
36578353 C |
1052 | ### Build your plugin |
1053 | ||
1054 | If you added client scripts, you'll need to build them using webpack. | |
1055 | ||
1056 | Install webpack: | |
1057 | ||
1058 | ``` | |
1059 | $ npm install | |
1060 | ``` | |
1061 | ||
1062 | Add/update your files in the `clientFiles` array of `webpack.config.js`: | |
1063 | ||
1064 | ``` | |
1065 | $ $EDITOR ./webpack.config.js | |
1066 | ``` | |
1067 | ||
1068 | Build your client files: | |
1069 | ||
1070 | ``` | |
1071 | $ npm run build | |
1072 | ``` | |
1073 | ||
1074 | You built files are in the `dist/` directory. Check `package.json` to correctly point to them. | |
1075 | ||
1076 | ||
662e5d4f C |
1077 | ### Test your plugin/theme |
1078 | ||
09f33366 C |
1079 | PeerTube dev server (ran with `npm run dev` on `localhost:3000`) can't inject plugin CSS. |
1080 | It's the reason why we don't use the dev mode but build PeerTube instead. | |
1081 | ||
662e5d4f | 1082 | You'll need to have a local PeerTube instance: |
5831dbcb | 1083 | * Follow the [dev prerequisites](https://github.com/Chocobozzz/PeerTube/blob/develop/.github/CONTRIBUTING.md#prerequisites) |
662e5d4f | 1084 | (to clone the repository, install dependencies and prepare the database) |
09f33366 | 1085 | * Build PeerTube: |
662e5d4f C |
1086 | |
1087 | ``` | |
09f33366 | 1088 | $ npm run build |
9fa6ca16 C |
1089 | ``` |
1090 | ||
1091 | * Build the CLI: | |
5831dbcb | 1092 | |
9fa6ca16 C |
1093 | ``` |
1094 | $ npm run setup:cli | |
662e5d4f | 1095 | ``` |
5831dbcb | 1096 | |
1ab94472 | 1097 | * Run PeerTube (you can access to your instance on `localhost:9000`): |
662e5d4f C |
1098 | |
1099 | ``` | |
9452d4fd | 1100 | $ NODE_ENV=dev npm start |
662e5d4f C |
1101 | ``` |
1102 | ||
5831dbcb | 1103 | * Register the instance via the CLI: |
662e5d4f C |
1104 | |
1105 | ``` | |
1106 | $ node ./dist/server/tools/peertube.js auth add -u 'http://localhost:9000' -U 'root' --password 'test' | |
1107 | ``` | |
1108 | ||
1109 | Then, you can install or reinstall your local plugin/theme by running: | |
1110 | ||
1111 | ``` | |
1112 | $ node ./dist/server/tools/peertube.js plugins install --path /your/absolute/plugin-or-theme/path | |
1113 | ``` | |
1114 | ||
1115 | ### Publish | |
1116 | ||
1117 | Go in your plugin/theme directory, and run: | |
1118 | ||
1119 | ``` | |
1120 | $ npm publish | |
1121 | ``` | |
1122 | ||
1123 | Every time you want to publish another version of your plugin/theme, just update the `version` key from the `package.json` | |
1124 | and republish it on NPM. Remember that the PeerTube index will take into account your new plugin/theme version after ~24 hours. | |
1125 | ||
4c0a6954 | 1126 | > If you need to force your plugin update on a specific __PeerTube__ instance, you may update the latest available version manually: |
1127 | > ```sql | |
1128 | > UPDATE "plugin" SET "latestVersion" = 'X.X.X' WHERE "plugin"."name" = 'plugin-shortname'; | |
1129 | > ``` | |
1130 | > You'll then be able to click the __Update plugin__ button on the plugin list. | |
1131 | ||
d61515e1 C |
1132 | ### Unpublish |
1133 | ||
1134 | If for a particular reason you don't want to maintain your plugin/theme anymore | |
1135 | you can deprecate it. The plugin index will automatically remove it preventing users to find/install it from the PeerTube admin interface: | |
1136 | ||
1137 | ```bash | |
1138 | $ npm deprecate peertube-plugin-xxx@"> 0.0.0" "explain here why you deprecate your plugin/theme" | |
1139 | ``` | |
d8e9a42c | 1140 | |
bfa1a32b C |
1141 | ## Plugin & Theme hooks/helpers API |
1142 | ||
c5c95361 | 1143 | See the dedicated documentation: https://docs.joinpeertube.org/api/plugins |
bfa1a32b C |
1144 | |
1145 | ||
d8e9a42c C |
1146 | ## Tips |
1147 | ||
1148 | ### Compatibility with PeerTube | |
1149 | ||
1150 | Unfortunately, we don't have enough resources to provide hook compatibility between minor releases of PeerTube (for example between `1.2.x` and `1.3.x`). | |
1151 | So please: | |
1152 | * Don't make assumptions and check every parameter you want to use. For example: | |
1153 | ||
1154 | ```js | |
1155 | registerHook({ | |
1156 | target: 'filter:api.video.get.result', | |
1157 | handler: video => { | |
1158 | // We check the parameter exists and the name field exists too, to avoid exceptions | |
1159 | if (video && video.name) video.name += ' <3' | |
1160 | ||
1161 | return video | |
1162 | } | |
1163 | }) | |
1164 | ``` | |
a4879b53 | 1165 | * Don't try to require parent PeerTube modules, only use `peertubeHelpers`. If you need another helper or a specific hook, please [create an issue](https://github.com/Chocobozzz/PeerTube/issues/new/choose) |
5831dbcb | 1166 | * Don't use PeerTube dependencies. Use your own :) |
d8e9a42c | 1167 | |
51326912 | 1168 | If your plugin is broken with a new PeerTube release, update your code and the `peertubeEngine` field of your `package.json` field. |
5831dbcb | 1169 | This way, older PeerTube versions will still use your old plugin, and new PeerTube versions will use your updated plugin. |
d8e9a42c C |
1170 | |
1171 | ### Spam/moderation plugin | |
1172 | ||
1173 | If you want to create an antispam/moderation plugin, you could use the following hooks: | |
1174 | * `filter:api.video.upload.accept.result`: to accept or not local uploads | |
1175 | * `filter:api.video-thread.create.accept.result`: to accept or not local thread | |
1176 | * `filter:api.video-comment-reply.create.accept.result`: to accept or not local replies | |
1177 | * `filter:api.video-threads.list.result`: to change/hide the text of threads | |
1178 | * `filter:api.video-thread-comments.list.result`: to change/hide the text of replies | |
1179 | * `filter:video.auto-blacklist.result`: to automatically blacklist local or remote videos | |
5831dbcb | 1180 | |
112be80e C |
1181 | ### Other plugin examples |
1182 | ||
1183 | You can take a look to "official" PeerTube plugins if you want to take inspiration from them: https://framagit.org/framasoft/peertube/official-plugins |