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 -->
7 - [Concepts](#concepts)
9 - [Static files](#static-files)
11 - [Server helpers (only for plugins)](#server-helpers-only-for-plugins)
12 - [Settings](#settings)
14 - [Update video constants](#update-video-constants)
15 - [Add custom routes](#add-custom-routes)
16 - [Client helpers (themes & plugins)](#client-helpers-themes--plugins)
17 - [Plugin static route](#plugin-static-route)
18 - [Translate](#translate)
19 - [Get public settings](#get-public-settings)
20 - [Publishing](#publishing)
21 - [Write a plugin/theme](#write-a-plugintheme)
22 - [Clone the quickstart repository](#clone-the-quickstart-repository)
23 - [Configure your repository](#configure-your-repository)
24 - [Update README](#update-readme)
25 - [Update package.json](#update-packagejson)
26 - [Write code](#write-code)
27 - [Add translations](#add-translations)
28 - [Test your plugin/theme](#test-your-plugintheme)
30 - [Plugin & Theme hooks/helpers API](#plugin--theme-hookshelpers-api)
32 - [Compatibility with PeerTube](#compatibility-with-peertube)
33 - [Spam/moderation plugin](#spammoderation-plugin)
34 - [Other plugin examples](#other-plugin-examples)
36 <!-- END doctoc generated TOC please keep comment here to allow auto update -->
40 Themes are exactly the same as plugins, except that:
41 * Their name starts with `peertube-theme-` instead of `peertube-plugin-`
42 * They cannot declare server code (so they cannot register server hooks or settings)
43 * CSS files are loaded by client only if the theme is chosen by the administrator or the user
47 A plugin registers functions in JavaScript to execute when PeerTube (server and client) fires events. There are 3 types of hooks:
48 * `filter`: used to filter functions parameters or return values.
49 For example to replace words in video comments, or change the videos list behaviour
50 * `action`: used to do something after a certain trigger. For example to send a hook every time a video is published
51 * `static`: same than `action` but PeerTube waits their execution
53 On server side, these hooks are registered by the `library` file defined in `package.json`.
58 "library": "./main.js",
63 And `main.js` defines a `register` function:
68 async function register ({
80 target: 'action:application.listening',
81 handler: () => displayHelloWorld()
87 On client side, these hooks are registered by the `clientScripts` files defined in `package.json`.
88 All client scripts have scopes so PeerTube client only loads scripts it needs:
95 "script": "client/common-client-plugin.js",
96 "scopes": [ "common" ]
99 "script": "client/video-watch-client-plugin.js",
100 "scopes": [ "video-watch" ]
107 And these scripts also define a `register` function:
110 function register ({ registerHook, peertubeHelpers }) {
112 target: 'action:application.init',
113 handler: () => onApplicationInit(peertubeHelpers)
120 Plugins can declare static directories that PeerTube will serve (images for example)
121 from `/plugins/{plugin-name}/{plugin-version}/static/`
122 or `/themes/{theme-name}/{theme-version}/static/` routes.
126 Plugins can declare CSS files that PeerTube will automatically inject in the client.
127 If you need to override existing style, you can use the `#custom-css` selector:
134 #custom-css .header {
135 background-color: red;
139 ### Server helpers (only for plugins)
143 Plugins can register settings, that PeerTube will inject in the administration interface.
152 default: 'my super name'
155 const adminName = await settingsManager.getSetting('admin-name')
160 Plugins can store/load JSON data, that PeerTube will store in its database (so don't put files in there).
165 const value = await storageManager.getData('mykey')
166 await storageManager.storeData('mykey', { subkey: 'value' })
169 #### Update video constants
171 You can add/delete video categories, licences or languages using the appropriate managers:
174 videoLanguageManager.addLanguage('al_bhed', 'Al Bhed')
175 videoLanguageManager.deleteLanguage('fr')
177 videoCategoryManager.addCategory(42, 'Best category')
178 videoCategoryManager.deleteCategory(1) // Music
180 videoLicenceManager.addLicence(42, 'Best licence')
181 videoLicenceManager.deleteLicence(7) // Public domain
184 #### Add custom routes
186 You can create custom routes using an [express Router](https://expressjs.com/en/4x/api.html#router) for your plugin:
189 const router = getRouter()
190 router.get('/ping', (req, res) => res.json({ message: 'pong' }))
193 The `ping` route can be accessed using:
194 * `/plugins/:pluginName/:pluginVersion/router/ping`
195 * Or `/plugins/:pluginName/router/ping`
198 ### Client helpers (themes & plugins)
200 #### Plugin static route
202 To get your plugin static route:
205 const baseStaticUrl = peertubeHelpers.getBaseStaticRoute()
206 const imageUrl = baseStaticUrl + '/images/chocobo.png'
211 To notify the user with the PeerTube ToastModule:
214 const { notifier } = peertubeHelpers
215 notifier.success('Success message content.')
216 notifier.error('Error message content.')
221 To show a custom modal:
224 peertubeHelpers.showModal({
225 title: 'My custom modal title',
226 content: '<p>My custom modal content</p>',
227 // Optionals parameters :
230 // show cancel button and call action() after hiding modal
231 cancel: { value: 'cancel', action: () => {} },
232 // show confirm button and call action() after hiding modal
233 confirm: { value: 'confirm', action: () => {} },
239 You can translate some strings of your plugin (PeerTube will use your `translations` object of your `package.json` file):
242 peertubeHelpers.translate('User name')
243 .then(translation => console.log('Translated User name by ' + translation))
246 #### Get public settings
248 To get your public plugin settings:
251 peertubeHelpers.getSettings()
253 if (!s || !s['site-id'] || !s['url']) {
254 console.error('Matomo settings are not set.')
265 PeerTube plugins and themes should be published on [NPM](https://www.npmjs.com/) so that PeerTube indexes
266 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).
268 ## Write a plugin/theme
271 * Find a name for your plugin or your theme (must not have spaces, it can only contain lowercase letters and `-`)
272 * Add the appropriate prefix:
273 * If you develop a plugin, add `peertube-plugin-` prefix to your plugin name (for example: `peertube-plugin-mysupername`)
274 * If you develop a theme, add `peertube-theme-` prefix to your theme name (for example: `peertube-theme-mysupertheme`)
275 * Clone the quickstart repository
276 * Configure your repository
278 * Update `package.json`
279 * Register hooks, add CSS and static files
280 * Test your plugin/theme with a local PeerTube installation
281 * Publish your plugin/theme on NPM
283 ### Clone the quickstart repository
285 If you develop a plugin, clone the `peertube-plugin-quickstart` repository:
288 $ git clone https://framagit.org/framasoft/peertube/peertube-plugin-quickstart.git peertube-plugin-mysupername
291 If you develop a theme, clone the `peertube-theme-quickstart` repository:
294 $ git clone https://framagit.org/framasoft/peertube/peertube-theme-quickstart.git peertube-theme-mysupername
297 ### Configure your repository
299 Set your repository URL:
302 $ cd peertube-plugin-mysupername # or cd peertube-theme-mysupername
303 $ git remote set-url origin https://your-git-repo
308 Update `README.md` file:
314 ### Update package.json
316 Update the `package.json` fields:
317 * `name` (should start with `peertube-plugin-` or `peertube-theme-`)
322 * `engine.peertube` (the PeerTube version compatibility, must be `>=x.y.z` and nothing else)
324 **Caution:** Don't update or remove other keys, or PeerTube will not be able to index/install your plugin.
325 If you don't need static directories, use an empty `object`:
335 And if you don't need CSS or client script files, use an empty `array`:
348 Now you can register hooks or settings, write CSS and add static directories to your plugin or your theme :)
350 **Caution:** It's up to you to check the code you write will be compatible with the PeerTube NodeJS version,
351 and will be supported by web browsers.
352 If you want to write modern JavaScript, please use a transpiler like [Babel](https://babeljs.io/).
356 If you want to translate strings of your plugin (like labels of your registered settings), create a file and add it to `package.json`:
362 "fr-FR": "./languages/fr.json",
363 "pt-BR": "./languages/pt-BR.json"
369 The key should be one of the locales defined in [i18n.ts](https://github.com/Chocobozzz/PeerTube/blob/develop/shared/models/i18n/i18n.ts).
370 You **must** use the complete locales (`fr-FR` instead of `fr`).
372 Translation files are just objects, with the english sentence as the key and the translation as the value.
373 `fr.json` could contain for example:
377 "Hello world": "Hello le monde"
381 ### Test your plugin/theme
383 You'll need to have a local PeerTube instance:
384 * Follow the [dev prerequisites](https://github.com/Chocobozzz/PeerTube/blob/develop/.github/CONTRIBUTING.md#prerequisites)
385 (to clone the repository, install dependencies and prepare the database)
386 * Build PeerTube (`--light` to only build the english language):
389 $ npm run build -- --light
398 * Run PeerTube (you can access to your instance on http://localhost:9000):
401 $ NODE_ENV=test npm start
404 * Register the instance via the CLI:
407 $ node ./dist/server/tools/peertube.js auth add -u 'http://localhost:9000' -U 'root' --password 'test'
410 Then, you can install or reinstall your local plugin/theme by running:
413 $ node ./dist/server/tools/peertube.js plugins install --path /your/absolute/plugin-or-theme/path
418 Go in your plugin/theme directory, and run:
424 Every time you want to publish another version of your plugin/theme, just update the `version` key from the `package.json`
425 and republish it on NPM. Remember that the PeerTube index will take into account your new plugin/theme version after ~24 hours.
428 ## Plugin & Theme hooks/helpers API
430 See the dedicated documentation: https://docs.joinpeertube.org/#/api-plugins
435 ### Compatibility with PeerTube
437 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`).
439 * Don't make assumptions and check every parameter you want to use. For example:
443 target: 'filter:api.video.get.result',
445 // We check the parameter exists and the name field exists too, to avoid exceptions
446 if (video && video.name) video.name += ' <3'
452 * 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)
453 * Don't use PeerTube dependencies. Use your own :)
455 If your plugin is broken with a new PeerTube release, update your code and the `peertubeEngine` field of your `package.json` field.
456 This way, older PeerTube versions will still use your old plugin, and new PeerTube versions will use your updated plugin.
458 ### Spam/moderation plugin
460 If you want to create an antispam/moderation plugin, you could use the following hooks:
461 * `filter:api.video.upload.accept.result`: to accept or not local uploads
462 * `filter:api.video-thread.create.accept.result`: to accept or not local thread
463 * `filter:api.video-comment-reply.create.accept.result`: to accept or not local replies
464 * `filter:api.video-threads.list.result`: to change/hide the text of threads
465 * `filter:api.video-thread-comments.list.result`: to change/hide the text of replies
466 * `filter:video.auto-blacklist.result`: to automatically blacklist local or remote videos
468 ### Other plugin examples
470 You can take a look to "official" PeerTube plugins if you want to take inspiration from them: https://framagit.org/framasoft/peertube/official-plugins