diff options
Diffstat (limited to 'support/doc/plugins/quickstart.md')
-rw-r--r-- | support/doc/plugins/quickstart.md | 229 |
1 files changed, 229 insertions, 0 deletions
diff --git a/support/doc/plugins/quickstart.md b/support/doc/plugins/quickstart.md new file mode 100644 index 000000000..a018aa50a --- /dev/null +++ b/support/doc/plugins/quickstart.md | |||
@@ -0,0 +1,229 @@ | |||
1 | # Plugins & Themes | ||
2 | |||
3 | ## Concepts | ||
4 | |||
5 | Themes are exactly the same than plugins, except that: | ||
6 | * Their name starts with `peertube-theme-` instead of `peertube-plugin-` | ||
7 | * They cannot declare server code (so they cannot register server hooks or settings) | ||
8 | * CSS files are loaded by client only if the theme is chosen by the administrator or the user | ||
9 | |||
10 | ### Hooks | ||
11 | |||
12 | A plugin registers functions in JavaScript to execute when PeerTube (server and client) fires events. There are 3 types of hooks: | ||
13 | * `filter`: used to filter functions parameters or return values. | ||
14 | For example to replace words in video comments, or change the videos list behaviour | ||
15 | * `action`: used to do something after a certain trigger. For example to send a hook every time a video is published | ||
16 | * `static`: same than `action` but PeerTube waits their execution | ||
17 | |||
18 | Example: | ||
19 | |||
20 | ```js | ||
21 | registerHook({ | ||
22 | target: 'action:application.listening', | ||
23 | handler: () => displayHelloWorld() | ||
24 | }) | ||
25 | ``` | ||
26 | |||
27 | On server side, these hooks are registered by the `library` file defined in `package.json`. | ||
28 | |||
29 | ```json | ||
30 | { | ||
31 | ..., | ||
32 | "library": "./main.js", | ||
33 | ..., | ||
34 | } | ||
35 | ``` | ||
36 | |||
37 | |||
38 | On client side, these hooks are registered by the `clientScripts` files defined in `package.json`. | ||
39 | All client scripts have scopes so PeerTube client only loads scripts it needs: | ||
40 | |||
41 | ```json | ||
42 | { | ||
43 | ..., | ||
44 | "clientScripts": [ | ||
45 | { | ||
46 | "script": "client/common-client-plugin.js", | ||
47 | "scopes": [ "common" ] | ||
48 | }, | ||
49 | { | ||
50 | "script": "client/video-watch-client-plugin.js", | ||
51 | "scopes": [ "video-watch" ] | ||
52 | } | ||
53 | ], | ||
54 | ... | ||
55 | } | ||
56 | ``` | ||
57 | |||
58 | ### Static files | ||
59 | |||
60 | Plugins can declare static directories that PeerTube will serve (images for example) | ||
61 | from `/plugins/{plugin-name}/{plugin-version}/static/` | ||
62 | or `/themes/{theme-name}/{theme-version}/static/` routes. | ||
63 | |||
64 | ### CSS | ||
65 | |||
66 | Plugins can declare CSS files that PeerTube will automatically inject in the client. | ||
67 | |||
68 | ### Server helpers | ||
69 | |||
70 | **Only for plugins** | ||
71 | |||
72 | #### Settings | ||
73 | |||
74 | Plugins can register settings, that PeerTube will inject in the administration interface. | ||
75 | |||
76 | Example: | ||
77 | |||
78 | ```js | ||
79 | registerSetting({ | ||
80 | name: 'admin-name', | ||
81 | label: 'Admin name', | ||
82 | type: 'input', | ||
83 | default: 'my super name' | ||
84 | }) | ||
85 | |||
86 | const adminName = await settingsManager.getSetting('admin-name') | ||
87 | ``` | ||
88 | |||
89 | ##### Storage | ||
90 | |||
91 | Plugins can store/load JSON data, that PeerTube will store in its database (so don't put files in there). | ||
92 | |||
93 | Example: | ||
94 | |||
95 | ```js | ||
96 | const value = await storageManager.getData('mykey') | ||
97 | await storageManager.storeData('mykey', 'myvalue') | ||
98 | ``` | ||
99 | |||
100 | ### Publishing | ||
101 | |||
102 | PeerTube plugins and themes should be published on [NPM](https://www.npmjs.com/) so that PeerTube indexes | ||
103 | 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). | ||
104 | |||
105 | ## Write a plugin/theme | ||
106 | |||
107 | Steps: | ||
108 | * Find a name for your plugin or your theme (must not have spaces, it can only contain lowercase letters and `-`) | ||
109 | * Add the appropriate prefix: | ||
110 | * If you develop a plugin, add `peertube-plugin-` prefix to your plugin name (for example: `peertube-plugin-mysupername`) | ||
111 | * If you develop a theme, add `peertube-theme-` prefix to your theme name (for example: `peertube-theme-mysupertheme`) | ||
112 | * Clone the quickstart repository | ||
113 | * Configure your repository | ||
114 | * Update `README.md` | ||
115 | * Update `package.json` | ||
116 | * Register hooks, add CSS and static files | ||
117 | * Test your plugin/theme with a local PeerTube installation | ||
118 | * Publish your plugin/theme on NPM | ||
119 | |||
120 | ### Clone the quickstart repository | ||
121 | |||
122 | If you develop a plugin, clone the `peertube-plugin-quickstart` repository: | ||
123 | |||
124 | ``` | ||
125 | $ git clone https://framagit.org/framasoft/peertube/peertube-plugin-quickstart.git peertube-plugin-mysupername | ||
126 | ``` | ||
127 | |||
128 | If you develop a theme, clone the `peertube-theme-quickstart` repository: | ||
129 | |||
130 | ``` | ||
131 | $ git clone https://framagit.org/framasoft/peertube/peertube-theme-quickstart.git peertube-theme-mysupername | ||
132 | ``` | ||
133 | |||
134 | ### Configure your repository | ||
135 | |||
136 | Set your repository URL: | ||
137 | |||
138 | ``` | ||
139 | $ cd peertube-plugin-mysupername # or cd peertube-theme-mysupername | ||
140 | $ git remote set-url origin https://your-git-repo | ||
141 | ``` | ||
142 | |||
143 | ### Update README | ||
144 | |||
145 | Update `README.md` file: | ||
146 | |||
147 | ``` | ||
148 | $ $EDITOR README.md | ||
149 | ``` | ||
150 | |||
151 | ### Update package.json | ||
152 | |||
153 | Update the `package.json` fields: | ||
154 | * `name` (should start with `peertube-plugin-` or `peertube-theme-`) | ||
155 | * `description` | ||
156 | * `homepage` | ||
157 | * `author` | ||
158 | * `bugs` | ||
159 | * `engine.peertube` (the PeerTube version compatibility, must be `>=x.y.z` and nothing else) | ||
160 | |||
161 | **Caution:** Don't update or remove other keys, or PeerTube will not be able to index/install your plugin. | ||
162 | If you don't need static directories, use an empty `object`: | ||
163 | |||
164 | ```json | ||
165 | { | ||
166 | ..., | ||
167 | "staticDirs": {}, | ||
168 | ... | ||
169 | } | ||
170 | ``` | ||
171 | |||
172 | And if you don't need CSS files, use an empty `array`: | ||
173 | |||
174 | ```json | ||
175 | { | ||
176 | ..., | ||
177 | "css": [], | ||
178 | ... | ||
179 | } | ||
180 | ``` | ||
181 | |||
182 | ### Write code | ||
183 | |||
184 | Now you can register hooks or settings, write CSS and add static directories to your plugin or your theme :) | ||
185 | |||
186 | **Caution:** It's up to you to check the code you write will be compatible with the PeerTube NodeJS version, | ||
187 | and will be supported by web browsers. | ||
188 | If you want to write modern JavaScript, please use a transpiler like [Babel](https://babeljs.io/). | ||
189 | |||
190 | ### Test your plugin/theme | ||
191 | |||
192 | You'll need to have a local PeerTube instance: | ||
193 | * Follow the [dev prerequisites](https://github.com/Chocobozzz/PeerTube/blob/develop/.github/CONTRIBUTING.md#prerequisites) | ||
194 | (to clone the repository, install dependencies and prepare the database) | ||
195 | * Build PeerTube (`--light` to only build the english language): | ||
196 | |||
197 | ``` | ||
198 | $ npm run build -- --light | ||
199 | ``` | ||
200 | |||
201 | * Run it (you can access to your instance on http://localhost:9000): | ||
202 | |||
203 | ``` | ||
204 | $ NODE_ENV=test npm start | ||
205 | ``` | ||
206 | |||
207 | * Register the instance via the CLI: | ||
208 | |||
209 | ``` | ||
210 | $ node ./dist/server/tools/peertube.js auth add -u 'http://localhost:9000' -U 'root' --password 'test' | ||
211 | ``` | ||
212 | |||
213 | Then, you can install or reinstall your local plugin/theme by running: | ||
214 | |||
215 | ``` | ||
216 | $ node ./dist/server/tools/peertube.js plugins install --path /your/absolute/plugin-or-theme/path | ||
217 | ``` | ||
218 | |||
219 | ### Publish | ||
220 | |||
221 | Go in your plugin/theme directory, and run: | ||
222 | |||
223 | ``` | ||
224 | $ npm publish | ||
225 | ``` | ||
226 | |||
227 | Every time you want to publish another version of your plugin/theme, just update the `version` key from the `package.json` | ||
228 | and republish it on NPM. Remember that the PeerTube index will take into account your new plugin/theme version after ~24 hours. | ||
229 | |||