]>
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 | ||
6 | ||
7 | - [Concepts](#concepts) | |
8 | - [Hooks](#hooks) | |
9 | - [Static files](#static-files) | |
10 | - [CSS](#css) | |
11 | - [Server helpers (only for plugins)](#server-helpers-only-for-plugins) | |
12 | - [Settings](#settings) | |
13 | - [Storage](#storage) | |
7545a094 | 14 | - [Update video constants](#update-video-constants) |
5e2b2e27 | 15 | - [Add custom routes](#add-custom-routes) |
1a2820e6 | 16 | - [Add external auth methods](#add-external-auth-methods) |
7aca6b24 | 17 | - [Add new transcoding profiles](#add-new-transcoding-profiles) |
7545a094 | 18 | - [Client helpers (themes & plugins)](#client-helpers-themes--plugins) |
b3af2601 C |
19 | - [Plugin static route](#plugin-static-route) |
20 | - [Notifier](#notifier) | |
21 | - [Markdown Renderer](#markdown-renderer) | |
22 | - [Custom Modal](#custom-modal) | |
7545a094 C |
23 | - [Translate](#translate) |
24 | - [Get public settings](#get-public-settings) | |
ec99e848 | 25 | - [Get server config](#get-server-config) |
8546fe87 | 26 | - [Add custom fields to video form](#add-custom-fields-to-video-form) |
d8e9a42c C |
27 | - [Publishing](#publishing) |
28 | - [Write a plugin/theme](#write-a-plugintheme) | |
29 | - [Clone the quickstart repository](#clone-the-quickstart-repository) | |
30 | - [Configure your repository](#configure-your-repository) | |
31 | - [Update README](#update-readme) | |
32 | - [Update package.json](#update-packagejson) | |
33 | - [Write code](#write-code) | |
7545a094 | 34 | - [Add translations](#add-translations) |
8546fe87 | 35 | - [Build your plugin](#build-your-plugin) |
d8e9a42c C |
36 | - [Test your plugin/theme](#test-your-plugintheme) |
37 | - [Publish](#publish) | |
7545a094 | 38 | - [Plugin & Theme hooks/helpers API](#plugin--theme-hookshelpers-api) |
d8e9a42c C |
39 | - [Tips](#tips) |
40 | - [Compatibility with PeerTube](#compatibility-with-peertube) | |
41 | - [Spam/moderation plugin](#spammoderation-plugin) | |
112be80e | 42 | - [Other plugin examples](#other-plugin-examples) |
d8e9a42c C |
43 | |
44 | <!-- END doctoc generated TOC please keep comment here to allow auto update --> | |
45 | ||
662e5d4f C |
46 | ## Concepts |
47 | ||
32d7f2b7 | 48 | Themes are exactly the same as plugins, except that: |
662e5d4f C |
49 | * Their name starts with `peertube-theme-` instead of `peertube-plugin-` |
50 | * They cannot declare server code (so they cannot register server hooks or settings) | |
51 | * CSS files are loaded by client only if the theme is chosen by the administrator or the user | |
52 | ||
53 | ### Hooks | |
54 | ||
55 | A plugin registers functions in JavaScript to execute when PeerTube (server and client) fires events. There are 3 types of hooks: | |
5831dbcb | 56 | * `filter`: used to filter functions parameters or return values. |
662e5d4f C |
57 | For example to replace words in video comments, or change the videos list behaviour |
58 | * `action`: used to do something after a certain trigger. For example to send a hook every time a video is published | |
59 | * `static`: same than `action` but PeerTube waits their execution | |
662e5d4f C |
60 | |
61 | On server side, these hooks are registered by the `library` file defined in `package.json`. | |
62 | ||
63 | ```json | |
64 | { | |
65 | ..., | |
66 | "library": "./main.js", | |
67 | ..., | |
68 | } | |
69 | ``` | |
70 | ||
7545a094 C |
71 | And `main.js` defines a `register` function: |
72 | ||
73 | Example: | |
74 | ||
75 | ```js | |
76 | async function register ({ | |
77 | registerHook, | |
5831dbcb | 78 | |
7545a094 C |
79 | registerSetting, |
80 | settingsManager, | |
5831dbcb | 81 | |
7545a094 | 82 | storageManager, |
5831dbcb | 83 | |
7545a094 C |
84 | videoCategoryManager, |
85 | videoLicenceManager, | |
5e2b2e27 | 86 | videoLanguageManager, |
5831dbcb | 87 | |
5e2b2e27 | 88 | peertubeHelpers, |
5831dbcb C |
89 | |
90 | getRouter, | |
91 | ||
92 | registerExternalAuth, | |
93 | unregisterExternalAuth, | |
94 | registerIdAndPassAuth, | |
95 | unregisterIdAndPassAuth | |
7545a094 C |
96 | }) { |
97 | registerHook({ | |
98 | target: 'action:application.listening', | |
99 | handler: () => displayHelloWorld() | |
100 | }) | |
101 | } | |
102 | ``` | |
103 | ||
662e5d4f C |
104 | |
105 | On client side, these hooks are registered by the `clientScripts` files defined in `package.json`. | |
106 | All client scripts have scopes so PeerTube client only loads scripts it needs: | |
107 | ||
108 | ```json | |
109 | { | |
110 | ..., | |
111 | "clientScripts": [ | |
112 | { | |
113 | "script": "client/common-client-plugin.js", | |
114 | "scopes": [ "common" ] | |
115 | }, | |
116 | { | |
117 | "script": "client/video-watch-client-plugin.js", | |
118 | "scopes": [ "video-watch" ] | |
119 | } | |
120 | ], | |
121 | ... | |
122 | } | |
123 | ``` | |
124 | ||
7545a094 C |
125 | And these scripts also define a `register` function: |
126 | ||
127 | ```js | |
128 | function register ({ registerHook, peertubeHelpers }) { | |
129 | registerHook({ | |
130 | target: 'action:application.init', | |
131 | handler: () => onApplicationInit(peertubeHelpers) | |
132 | }) | |
133 | } | |
134 | ``` | |
135 | ||
662e5d4f C |
136 | ### Static files |
137 | ||
5831dbcb C |
138 | Plugins can declare static directories that PeerTube will serve (images for example) |
139 | from `/plugins/{plugin-name}/{plugin-version}/static/` | |
662e5d4f C |
140 | or `/themes/{theme-name}/{theme-version}/static/` routes. |
141 | ||
142 | ### CSS | |
143 | ||
144 | Plugins can declare CSS files that PeerTube will automatically inject in the client. | |
7545a094 C |
145 | If you need to override existing style, you can use the `#custom-css` selector: |
146 | ||
147 | ``` | |
148 | body#custom-css { | |
149 | color: red; | |
150 | } | |
151 | ||
152 | #custom-css .header { | |
153 | background-color: red; | |
154 | } | |
155 | ``` | |
662e5d4f | 156 | |
9fa6ca16 | 157 | ### Server helpers (only for plugins) |
662e5d4f C |
158 | |
159 | #### Settings | |
160 | ||
161 | Plugins can register settings, that PeerTube will inject in the administration interface. | |
162 | ||
163 | Example: | |
164 | ||
165 | ```js | |
166 | registerSetting({ | |
167 | name: 'admin-name', | |
168 | label: 'Admin name', | |
169 | type: 'input', | |
972de8f1 | 170 | // type: input | input-checkbox | input-password | input-textarea | markdown-text | markdown-enhanced |
662e5d4f C |
171 | default: 'my super name' |
172 | }) | |
173 | ||
174 | const adminName = await settingsManager.getSetting('admin-name') | |
2db9c70f C |
175 | |
176 | const result = await settingsManager.getSettings([ 'admin-name', 'admin-password' ]) | |
177 | result['admin-name] | |
5831dbcb C |
178 | |
179 | settingsManager.onSettingsChange(settings => { | |
180 | settings['admin-name]) | |
181 | }) | |
662e5d4f C |
182 | ``` |
183 | ||
d8e9a42c | 184 | #### Storage |
662e5d4f C |
185 | |
186 | Plugins can store/load JSON data, that PeerTube will store in its database (so don't put files in there). | |
187 | ||
188 | Example: | |
189 | ||
190 | ```js | |
191 | const value = await storageManager.getData('mykey') | |
9fa6ca16 | 192 | await storageManager.storeData('mykey', { subkey: 'value' }) |
662e5d4f C |
193 | ``` |
194 | ||
7545a094 C |
195 | #### Update video constants |
196 | ||
197 | You can add/delete video categories, licences or languages using the appropriate managers: | |
198 | ||
199 | ```js | |
200 | videoLanguageManager.addLanguage('al_bhed', 'Al Bhed') | |
201 | videoLanguageManager.deleteLanguage('fr') | |
202 | ||
203 | videoCategoryManager.addCategory(42, 'Best category') | |
204 | videoCategoryManager.deleteCategory(1) // Music | |
205 | ||
206 | videoLicenceManager.addLicence(42, 'Best licence') | |
207 | videoLicenceManager.deleteLicence(7) // Public domain | |
b3af2601 C |
208 | |
209 | videoPrivacyManager.deletePrivacy(2) // Remove Unlisted video privacy | |
210 | playlistPrivacyManager.deletePlaylistPrivacy(3) // Remove Private video playlist privacy | |
7545a094 C |
211 | ``` |
212 | ||
5e2b2e27 C |
213 | #### Add custom routes |
214 | ||
215 | You can create custom routes using an [express Router](https://expressjs.com/en/4x/api.html#router) for your plugin: | |
216 | ||
217 | ```js | |
218 | const router = getRouter() | |
219 | router.get('/ping', (req, res) => res.json({ message: 'pong' })) | |
220 | ``` | |
221 | ||
222 | The `ping` route can be accessed using: | |
223 | * `/plugins/:pluginName/:pluginVersion/router/ping` | |
224 | * Or `/plugins/:pluginName/router/ping` | |
225 | ||
226 | ||
5831dbcb C |
227 | #### Add external auth methods |
228 | ||
229 | 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): | |
230 | ||
231 | ```js | |
232 | registerIdAndPassAuth({ | |
233 | authName: 'my-auth-method', | |
234 | ||
235 | // PeerTube will try all id and pass plugins in the weight DESC order | |
236 | // Exposing this value in the plugin settings could be interesting | |
237 | getWeight: () => 60, | |
238 | ||
239 | // Optional function called by PeerTube when the user clicked on the logout button | |
240 | onLogout: user => { | |
241 | console.log('User %s logged out.', user.username') | |
242 | }, | |
243 | ||
244 | // Optional function called by PeerTube when the access token or refresh token are generated/refreshed | |
245 | hookTokenValidity: ({ token, type }) => { | |
246 | if (type === 'access') return { valid: true } | |
247 | if (type === 'refresh') return { valid: false } | |
248 | }, | |
249 | ||
250 | // Used by PeerTube when the user tries to authenticate | |
251 | login: ({ id, password }) => { | |
252 | if (id === 'user' && password === 'super password') { | |
253 | return { | |
254 | username: 'user' | |
255 | email: 'user@example.com' | |
256 | role: 2 | |
257 | displayName: 'User display name' | |
258 | } | |
259 | } | |
260 | ||
261 | // Auth failed | |
262 | return null | |
263 | } | |
264 | }) | |
265 | ||
266 | // Unregister this auth method | |
267 | unregisterIdAndPassAuth('my-auth-method') | |
268 | ``` | |
269 | ||
270 | 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): | |
271 | ||
272 | ```js | |
273 | // result contains the userAuthenticated auth method you can call to authenticate a user | |
274 | const result = registerExternalAuth({ | |
275 | authName: 'my-auth-method', | |
276 | ||
277 | // Will be displayed in a button next to the login form | |
278 | authDisplayName: () => 'Auth method' | |
279 | ||
280 | // If the user click on the auth button, PeerTube will forward the request in this function | |
281 | onAuthRequest: (req, res) => { | |
282 | res.redirect('https://external-auth.example.com/auth') | |
283 | }, | |
284 | ||
285 | // Same than registerIdAndPassAuth option | |
286 | // onLogout: ... | |
287 | ||
288 | // Same than registerIdAndPassAuth option | |
289 | // hookTokenValidity: ... | |
290 | }) | |
291 | ||
292 | router.use('/external-auth-callback', (req, res) => { | |
293 | // Forward the request to PeerTube | |
294 | result.userAuthenticated({ | |
295 | req, | |
296 | res, | |
297 | username: 'user' | |
298 | email: 'user@example.com' | |
299 | role: 2 | |
300 | displayName: 'User display name' | |
301 | }) | |
302 | }) | |
303 | ||
304 | // Unregister this external auth method | |
305 | unregisterExternalAuth('my-auth-method) | |
306 | ``` | |
307 | ||
7aca6b24 C |
308 | #### Add new transcoding profiles |
309 | ||
310 | Adding transcoding profiles allow admins to change ffmpeg encoding parameters and/or encoders. | |
311 | A transcoding profile has to be chosen by the admin of the instance using the admin configuration. | |
312 | ||
313 | ```js | |
314 | async function register ({ | |
315 | transcodingManager | |
316 | }) { | |
317 | ||
318 | // Adapt bitrate when using libx264 encoder | |
319 | { | |
320 | const builder = (options) => { | |
321 | const { input, resolution, fps, streamNum } = options | |
322 | ||
323 | const streamString = streamNum ? ':' + streamNum : '' | |
324 | ||
325 | // You can also return a promise | |
326 | return { | |
327 | outputOptions: [ | |
328 | // Use a custom bitrate | |
329 | '-b' + streamString + ' 10K' | |
330 | ] | |
331 | } | |
332 | } | |
333 | ||
334 | const encoder = 'libx264' | |
335 | const profileName = 'low-quality' | |
336 | ||
337 | // Support this profile for VOD transcoding | |
338 | transcodingManager.addVODProfile(encoder, profileName, builder) | |
339 | ||
340 | // And/Or support this profile for live transcoding | |
341 | transcodingManager.addLiveProfile(encoder, profileName, builder) | |
342 | } | |
343 | ||
344 | { | |
345 | const builder = (options) => { | |
346 | const { streamNum } = options | |
347 | ||
348 | const streamString = streamNum ? ':' + streamNum : '' | |
349 | ||
350 | // Always copy stream when PeerTube use libfdk_aac or aac encoders | |
351 | return { | |
352 | copy: true | |
353 | } | |
354 | } | |
355 | ||
356 | const profileName = 'copy-audio' | |
357 | ||
358 | for (const encoder of [ 'libfdk_aac', 'aac' ]) { | |
359 | transcodingManager.addVODProfile(encoder, profileName, builder) | |
360 | } | |
361 | } | |
362 | ``` | |
363 | ||
364 | PeerTube will try different encoders depending on their priority. | |
365 | If the encoder is not available in the current transcoding profile or in ffmpeg, it tries the next one. | |
366 | Plugins can change the order of these encoders and add their custom encoders: | |
367 | ||
368 | ```js | |
369 | async function register ({ | |
370 | transcodingManager | |
371 | }) { | |
372 | ||
373 | // Adapt bitrate when using libx264 encoder | |
374 | { | |
375 | const builder = () => { | |
376 | return { | |
377 | outputOptions: [] | |
378 | } | |
379 | } | |
380 | ||
381 | // Support libopus and libvpx-vp9 encoders (these codecs could be incompatible with the player) | |
382 | transcodingManager.addVODProfile('libopus', 'test-vod-profile', builder) | |
383 | ||
384 | // Default priorities are ~100 | |
385 | // Lowest priority = 1 | |
386 | transcodingManager.addVODEncoderPriority('audio', 'libopus', 1000) | |
387 | ||
388 | transcodingManager.addVODProfile('libvpx-vp9', 'test-vod-profile', builder) | |
389 | transcodingManager.addVODEncoderPriority('video', 'libvpx-vp9', 1000) | |
390 | ||
391 | transcodingManager.addLiveProfile('libopus', 'test-live-profile', builder) | |
392 | transcodingManager.addLiveEncoderPriority('audio', 'libopus', 1000) | |
393 | } | |
394 | ``` | |
395 | ||
7545a094 C |
396 | ### Client helpers (themes & plugins) |
397 | ||
74c2dece | 398 | #### Plugin static route |
7545a094 C |
399 | |
400 | To get your plugin static route: | |
401 | ||
402 | ```js | |
403 | const baseStaticUrl = peertubeHelpers.getBaseStaticRoute() | |
404 | const imageUrl = baseStaticUrl + '/images/chocobo.png' | |
405 | ``` | |
406 | ||
74c2dece K |
407 | #### Notifier |
408 | ||
409 | To notify the user with the PeerTube ToastModule: | |
410 | ||
411 | ```js | |
412 | const { notifier } = peertubeHelpers | |
413 | notifier.success('Success message content.') | |
414 | notifier.error('Error message content.') | |
415 | ``` | |
416 | ||
8c7725dc K |
417 | #### Markdown Renderer |
418 | ||
419 | To render a formatted markdown text to HTML: | |
420 | ||
421 | ```js | |
422 | const { markdownRenderer } = peertubeHelpers | |
423 | ||
424 | await markdownRenderer.textMarkdownToHTML('**My Bold Text**') | |
425 | // return <strong>My Bold Text</strong> | |
426 | ||
427 | await markdownRenderer.enhancedMarkdownToHTML('![alt-img](http://.../my-image.jpg)') | |
428 | // return <img alt=alt-img src=http://.../my-image.jpg /> | |
429 | ``` | |
430 | ||
437e8e06 K |
431 | #### Custom Modal |
432 | ||
433 | To show a custom modal: | |
434 | ||
435 | ```js | |
436 | peertubeHelpers.showModal({ | |
437 | title: 'My custom modal title', | |
438 | content: '<p>My custom modal content</p>', | |
439 | // Optionals parameters : | |
440 | // show close icon | |
441 | close: true, | |
442 | // show cancel button and call action() after hiding modal | |
443 | cancel: { value: 'cancel', action: () => {} }, | |
444 | // show confirm button and call action() after hiding modal | |
445 | confirm: { value: 'confirm', action: () => {} }, | |
446 | }) | |
447 | ``` | |
448 | ||
7545a094 C |
449 | #### Translate |
450 | ||
451 | You can translate some strings of your plugin (PeerTube will use your `translations` object of your `package.json` file): | |
452 | ||
453 | ```js | |
454 | peertubeHelpers.translate('User name') | |
455 | .then(translation => console.log('Translated User name by ' + translation)) | |
456 | ``` | |
457 | ||
458 | #### Get public settings | |
459 | ||
460 | To get your public plugin settings: | |
461 | ||
462 | ```js | |
463 | peertubeHelpers.getSettings() | |
464 | .then(s => { | |
465 | if (!s || !s['site-id'] || !s['url']) { | |
466 | console.error('Matomo settings are not set.') | |
467 | return | |
468 | } | |
5831dbcb | 469 | |
7545a094 C |
470 | // ... |
471 | }) | |
5831dbcb | 472 | ``` |
7545a094 | 473 | |
ec99e848 C |
474 | #### Get server config |
475 | ||
476 | ```js | |
477 | peertubeHelpers.getServerConfig() | |
478 | .then(config => { | |
479 | console.log('Fetched server config.', config) | |
480 | }) | |
481 | ``` | |
482 | ||
8546fe87 | 483 | #### Add custom fields to video form |
e08a26e2 C |
484 | |
485 | To add custom fields in the video form (in *Plugin settings* tab): | |
486 | ||
8546fe87 | 487 | ```js |
e08a26e2 C |
488 | async function register ({ registerVideoField, peertubeHelpers }) { |
489 | const descriptionHTML = await peertubeHelpers.translate(descriptionSource) | |
490 | const commonOptions = { | |
491 | name: 'my-field-name, | |
492 | label: 'My added field', | |
493 | descriptionHTML: 'Optional description', | |
494 | type: 'input-textarea', | |
495 | default: '' | |
496 | } | |
497 | ||
498 | for (const type of [ 'upload', 'import-url', 'import-torrent', 'update' ]) { | |
499 | registerVideoField(commonOptions, { type }) | |
500 | } | |
501 | } | |
502 | ``` | |
503 | ||
504 | PeerTube will send this field value in `body.pluginData['my-field-name']` and fetch it from `video.pluginData['my-field-name']`. | |
505 | ||
506 | So for example, if you want to store an additional metadata for videos, register the following hooks in **server**: | |
507 | ||
8546fe87 | 508 | ```js |
e08a26e2 C |
509 | async function register ({ |
510 | registerHook, | |
511 | storageManager | |
512 | }) { | |
513 | const fieldName = 'my-field-name' | |
514 | ||
515 | // Store data associated to this video | |
516 | registerHook({ | |
517 | target: 'action:api.video.updated', | |
518 | handler: ({ video, body }) => { | |
519 | if (!body.pluginData) return | |
520 | ||
521 | const value = body.pluginData[fieldName] | |
522 | if (!value) return | |
523 | ||
524 | storageManager.storeData(fieldName + '-' + video.id, value) | |
525 | } | |
526 | }) | |
527 | ||
528 | // Add your custom value to the video, so the client autofill your field using the previously stored value | |
529 | registerHook({ | |
530 | target: 'filter:api.video.get.result', | |
531 | handler: async (video) => { | |
532 | if (!video) return video | |
533 | if (!video.pluginData) video.pluginData = {} | |
534 | ||
535 | const result = await storageManager.getData(fieldName + '-' + video.id) | |
536 | video.pluginData[fieldName] = result | |
537 | ||
538 | return video | |
539 | } | |
540 | }) | |
541 | } | |
2498aaea | 542 | ``` |
662e5d4f C |
543 | ### Publishing |
544 | ||
545 | PeerTube plugins and themes should be published on [NPM](https://www.npmjs.com/) so that PeerTube indexes | |
546 | take into account your plugin (after ~ 1 day). An official PeerTube index is available on https://packages.joinpeertube.org/ (it's just a REST API, so don't expect a beautiful website). | |
547 | ||
548 | ## Write a plugin/theme | |
549 | ||
550 | Steps: | |
551 | * Find a name for your plugin or your theme (must not have spaces, it can only contain lowercase letters and `-`) | |
552 | * Add the appropriate prefix: | |
553 | * If you develop a plugin, add `peertube-plugin-` prefix to your plugin name (for example: `peertube-plugin-mysupername`) | |
554 | * If you develop a theme, add `peertube-theme-` prefix to your theme name (for example: `peertube-theme-mysupertheme`) | |
555 | * Clone the quickstart repository | |
556 | * Configure your repository | |
557 | * Update `README.md` | |
558 | * Update `package.json` | |
559 | * Register hooks, add CSS and static files | |
560 | * Test your plugin/theme with a local PeerTube installation | |
561 | * Publish your plugin/theme on NPM | |
562 | ||
563 | ### Clone the quickstart repository | |
564 | ||
565 | If you develop a plugin, clone the `peertube-plugin-quickstart` repository: | |
566 | ||
567 | ``` | |
568 | $ git clone https://framagit.org/framasoft/peertube/peertube-plugin-quickstart.git peertube-plugin-mysupername | |
569 | ``` | |
570 | ||
571 | If you develop a theme, clone the `peertube-theme-quickstart` repository: | |
572 | ||
573 | ``` | |
574 | $ git clone https://framagit.org/framasoft/peertube/peertube-theme-quickstart.git peertube-theme-mysupername | |
575 | ``` | |
576 | ||
577 | ### Configure your repository | |
578 | ||
579 | Set your repository URL: | |
580 | ||
581 | ``` | |
582 | $ cd peertube-plugin-mysupername # or cd peertube-theme-mysupername | |
583 | $ git remote set-url origin https://your-git-repo | |
584 | ``` | |
585 | ||
586 | ### Update README | |
587 | ||
588 | Update `README.md` file: | |
589 | ||
590 | ``` | |
591 | $ $EDITOR README.md | |
592 | ``` | |
593 | ||
594 | ### Update package.json | |
595 | ||
596 | Update the `package.json` fields: | |
597 | * `name` (should start with `peertube-plugin-` or `peertube-theme-`) | |
598 | * `description` | |
599 | * `homepage` | |
600 | * `author` | |
601 | * `bugs` | |
602 | * `engine.peertube` (the PeerTube version compatibility, must be `>=x.y.z` and nothing else) | |
5831dbcb | 603 | |
662e5d4f | 604 | **Caution:** Don't update or remove other keys, or PeerTube will not be able to index/install your plugin. |
5831dbcb | 605 | If you don't need static directories, use an empty `object`: |
662e5d4f C |
606 | |
607 | ```json | |
608 | { | |
609 | ..., | |
610 | "staticDirs": {}, | |
611 | ... | |
612 | } | |
613 | ``` | |
614 | ||
9fa6ca16 | 615 | And if you don't need CSS or client script files, use an empty `array`: |
662e5d4f C |
616 | |
617 | ```json | |
618 | { | |
619 | ..., | |
620 | "css": [], | |
9fa6ca16 | 621 | "clientScripts": [], |
662e5d4f C |
622 | ... |
623 | } | |
624 | ``` | |
625 | ||
626 | ### Write code | |
627 | ||
628 | Now you can register hooks or settings, write CSS and add static directories to your plugin or your theme :) | |
629 | ||
5831dbcb | 630 | **Caution:** It's up to you to check the code you write will be compatible with the PeerTube NodeJS version, |
662e5d4f C |
631 | and will be supported by web browsers. |
632 | If you want to write modern JavaScript, please use a transpiler like [Babel](https://babeljs.io/). | |
633 | ||
7545a094 C |
634 | ### Add translations |
635 | ||
636 | If you want to translate strings of your plugin (like labels of your registered settings), create a file and add it to `package.json`: | |
637 | ||
638 | ```json | |
639 | { | |
640 | ..., | |
641 | "translations": { | |
642 | "fr-FR": "./languages/fr.json", | |
643 | "pt-BR": "./languages/pt-BR.json" | |
644 | }, | |
645 | ... | |
646 | } | |
647 | ``` | |
648 | ||
649 | The key should be one of the locales defined in [i18n.ts](https://github.com/Chocobozzz/PeerTube/blob/develop/shared/models/i18n/i18n.ts). | |
650 | You **must** use the complete locales (`fr-FR` instead of `fr`). | |
651 | ||
112be80e C |
652 | Translation files are just objects, with the english sentence as the key and the translation as the value. |
653 | `fr.json` could contain for example: | |
654 | ||
655 | ```json | |
656 | { | |
657 | "Hello world": "Hello le monde" | |
658 | } | |
659 | ``` | |
660 | ||
36578353 C |
661 | ### Build your plugin |
662 | ||
663 | If you added client scripts, you'll need to build them using webpack. | |
664 | ||
665 | Install webpack: | |
666 | ||
667 | ``` | |
668 | $ npm install | |
669 | ``` | |
670 | ||
671 | Add/update your files in the `clientFiles` array of `webpack.config.js`: | |
672 | ||
673 | ``` | |
674 | $ $EDITOR ./webpack.config.js | |
675 | ``` | |
676 | ||
677 | Build your client files: | |
678 | ||
679 | ``` | |
680 | $ npm run build | |
681 | ``` | |
682 | ||
683 | You built files are in the `dist/` directory. Check `package.json` to correctly point to them. | |
684 | ||
685 | ||
662e5d4f C |
686 | ### Test your plugin/theme |
687 | ||
688 | You'll need to have a local PeerTube instance: | |
5831dbcb | 689 | * Follow the [dev prerequisites](https://github.com/Chocobozzz/PeerTube/blob/develop/.github/CONTRIBUTING.md#prerequisites) |
662e5d4f | 690 | (to clone the repository, install dependencies and prepare the database) |
5831dbcb | 691 | * Build PeerTube (`--light` to only build the english language): |
662e5d4f C |
692 | |
693 | ``` | |
694 | $ npm run build -- --light | |
9fa6ca16 C |
695 | ``` |
696 | ||
697 | * Build the CLI: | |
5831dbcb | 698 | |
9fa6ca16 C |
699 | ``` |
700 | $ npm run setup:cli | |
662e5d4f | 701 | ``` |
5831dbcb C |
702 | |
703 | * Run PeerTube (you can access to your instance on http://localhost:9000): | |
662e5d4f C |
704 | |
705 | ``` | |
706 | $ NODE_ENV=test npm start | |
707 | ``` | |
708 | ||
5831dbcb | 709 | * Register the instance via the CLI: |
662e5d4f C |
710 | |
711 | ``` | |
712 | $ node ./dist/server/tools/peertube.js auth add -u 'http://localhost:9000' -U 'root' --password 'test' | |
713 | ``` | |
714 | ||
715 | Then, you can install or reinstall your local plugin/theme by running: | |
716 | ||
717 | ``` | |
718 | $ node ./dist/server/tools/peertube.js plugins install --path /your/absolute/plugin-or-theme/path | |
719 | ``` | |
720 | ||
721 | ### Publish | |
722 | ||
723 | Go in your plugin/theme directory, and run: | |
724 | ||
725 | ``` | |
726 | $ npm publish | |
727 | ``` | |
728 | ||
729 | Every time you want to publish another version of your plugin/theme, just update the `version` key from the `package.json` | |
730 | and republish it on NPM. Remember that the PeerTube index will take into account your new plugin/theme version after ~24 hours. | |
731 | ||
d8e9a42c | 732 | |
bfa1a32b C |
733 | ## Plugin & Theme hooks/helpers API |
734 | ||
7cf88d09 | 735 | See the dedicated documentation: https://docs.joinpeertube.org/api-plugins |
bfa1a32b C |
736 | |
737 | ||
d8e9a42c C |
738 | ## Tips |
739 | ||
740 | ### Compatibility with PeerTube | |
741 | ||
742 | 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`). | |
743 | So please: | |
744 | * Don't make assumptions and check every parameter you want to use. For example: | |
745 | ||
746 | ```js | |
747 | registerHook({ | |
748 | target: 'filter:api.video.get.result', | |
749 | handler: video => { | |
750 | // We check the parameter exists and the name field exists too, to avoid exceptions | |
751 | if (video && video.name) video.name += ' <3' | |
752 | ||
753 | return video | |
754 | } | |
755 | }) | |
756 | ``` | |
a4879b53 | 757 | * 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 | 758 | * Don't use PeerTube dependencies. Use your own :) |
d8e9a42c | 759 | |
51326912 | 760 | If your plugin is broken with a new PeerTube release, update your code and the `peertubeEngine` field of your `package.json` field. |
5831dbcb | 761 | This way, older PeerTube versions will still use your old plugin, and new PeerTube versions will use your updated plugin. |
d8e9a42c C |
762 | |
763 | ### Spam/moderation plugin | |
764 | ||
765 | If you want to create an antispam/moderation plugin, you could use the following hooks: | |
766 | * `filter:api.video.upload.accept.result`: to accept or not local uploads | |
767 | * `filter:api.video-thread.create.accept.result`: to accept or not local thread | |
768 | * `filter:api.video-comment-reply.create.accept.result`: to accept or not local replies | |
769 | * `filter:api.video-threads.list.result`: to change/hide the text of threads | |
770 | * `filter:api.video-thread-comments.list.result`: to change/hide the text of replies | |
771 | * `filter:video.auto-blacklist.result`: to automatically blacklist local or remote videos | |
5831dbcb | 772 | |
112be80e C |
773 | ### Other plugin examples |
774 | ||
775 | You can take a look to "official" PeerTube plugins if you want to take inspiration from them: https://framagit.org/framasoft/peertube/official-plugins |