aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-07-17 15:46:51 +0200
committerChocobozzz <chocobozzz@cpy.re>2019-07-24 10:58:16 +0200
commit9fa6ca160a9dda057c3980c6ee19f0ee426fd0a0 (patch)
tree7a107a1abfc474e7590d3e64fac4b5b01c12c7f4
parent662e5d4fe4b0ac61867f3f4fa3bb38a8b8e5d0f5 (diff)
downloadPeerTube-9fa6ca160a9dda057c3980c6ee19f0ee426fd0a0.tar.gz
PeerTube-9fa6ca160a9dda057c3980c6ee19f0ee426fd0a0.tar.zst
PeerTube-9fa6ca160a9dda057c3980c6ee19f0ee426fd0a0.zip
Some plugins fixes and doc enhancements
-rw-r--r--client/src/app/core/plugins/plugin.service.ts11
-rw-r--r--server/initializers/constants.ts2
-rw-r--r--server/lib/plugins/plugin-manager.ts12
-rw-r--r--server/models/server/plugin.ts9
-rw-r--r--shared/models/plugins/plugin-library.model.ts2
-rw-r--r--support/doc/plugins/quickstart.md28
6 files changed, 45 insertions, 19 deletions
diff --git a/client/src/app/core/plugins/plugin.service.ts b/client/src/app/core/plugins/plugin.service.ts
index 525740a01..af330c2eb 100644
--- a/client/src/app/core/plugins/plugin.service.ts
+++ b/client/src/app/core/plugins/plugin.service.ts
@@ -125,8 +125,15 @@ export class PluginService {
125 125
126 for (const hook of this.hooks[hookName]) { 126 for (const hook of this.hooks[hookName]) {
127 try { 127 try {
128 if (wait) result = await hook.handler(param) 128 const p = hook.handler(param)
129 else result = hook.handler() 129
130 if (wait) {
131 result = await p
132 } else if (p.catch) {
133 p.catch((err: Error) => {
134 console.error('Cannot run hook %s of script %s of plugin %s.', hookName, hook.plugin, hook.clientScript, err)
135 })
136 }
130 } catch (err) { 137 } catch (err) {
131 console.error('Cannot run hook %s of script %s of plugin %s.', hookName, hook.plugin, hook.clientScript, err) 138 console.error('Cannot run hook %s of script %s of plugin %s.', hookName, hook.plugin, hook.clientScript, err)
132 } 139 }
diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts
index 93ccb7da8..1111fd97f 100644
--- a/server/initializers/constants.ts
+++ b/server/initializers/constants.ts
@@ -19,7 +19,7 @@ const LAST_MIGRATION_VERSION = 400
19// --------------------------------------------------------------------------- 19// ---------------------------------------------------------------------------
20 20
21const API_VERSION = 'v1' 21const API_VERSION = 'v1'
22const PEERTUBE_VERSION = process.env.npm_package_version || 'unknown' 22const PEERTUBE_VERSION = require(join(root(), 'package.json')).version
23 23
24const PAGINATION = { 24const PAGINATION = {
25 COUNT: { 25 COUNT: {
diff --git a/server/lib/plugins/plugin-manager.ts b/server/lib/plugins/plugin-manager.ts
index 9e4ec5adf..570b56193 100644
--- a/server/lib/plugins/plugin-manager.ts
+++ b/server/lib/plugins/plugin-manager.ts
@@ -104,10 +104,12 @@ export class PluginManager {
104 104
105 for (const hook of this.hooks[hookName]) { 105 for (const hook of this.hooks[hookName]) {
106 try { 106 try {
107 const p = hook.handler(param)
108
107 if (wait) { 109 if (wait) {
108 result = await hook.handler(param) 110 result = await p
109 } else { 111 } else if (p.catch) {
110 result = hook.handler() 112 p.catch(err => logger.warn('Hook %s of plugin %s thrown an error.', hookName, hook.pluginName, { err }))
111 } 113 }
112 } catch (err) { 114 } catch (err) {
113 logger.error('Cannot run hook %s of plugin %s.', hookName, hook.pluginName, { err }) 115 logger.error('Cannot run hook %s of plugin %s.', hookName, hook.pluginName, { err })
@@ -329,7 +331,7 @@ export class PluginManager {
329 registerSetting, 331 registerSetting,
330 settingsManager, 332 settingsManager,
331 storageManager 333 storageManager
332 }) 334 }).catch(err => logger.error('Cannot register plugin %s.', npmName, { err }))
333 335
334 logger.info('Add plugin %s CSS to global file.', npmName) 336 logger.info('Add plugin %s CSS to global file.', npmName)
335 337
@@ -365,7 +367,7 @@ export class PluginManager {
365 private async regeneratePluginGlobalCSS () { 367 private async regeneratePluginGlobalCSS () {
366 await this.resetCSSGlobalFile() 368 await this.resetCSSGlobalFile()
367 369
368 for (const key of Object.keys(this.registeredPlugins)) { 370 for (const key of Object.keys(this.getRegisteredPlugins())) {
369 const plugin = this.registeredPlugins[key] 371 const plugin = this.registeredPlugins[key]
370 372
371 await this.addCSSToGlobalFile(plugin.path, plugin.css) 373 await this.addCSSToGlobalFile(plugin.path, plugin.css)
diff --git a/server/models/server/plugin.ts b/server/models/server/plugin.ts
index 50963ba57..f39b97ef0 100644
--- a/server/models/server/plugin.ts
+++ b/server/models/server/plugin.ts
@@ -156,6 +156,15 @@ export class PluginModel extends Model<PluginModel> {
156 return PluginModel.findOne(query) 156 return PluginModel.findOne(query)
157 .then((c: any) => { 157 .then((c: any) => {
158 if (!c) return undefined 158 if (!c) return undefined
159 const value = c.value
160
161 if (typeof value === 'string' && value.startsWith('{')) {
162 try {
163 return JSON.parse(value)
164 } catch {
165 return value
166 }
167 }
159 168
160 return c.value 169 return c.value
161 }) 170 })
diff --git a/shared/models/plugins/plugin-library.model.ts b/shared/models/plugins/plugin-library.model.ts
index df6499b6b..fd90a3b46 100644
--- a/shared/models/plugins/plugin-library.model.ts
+++ b/shared/models/plugins/plugin-library.model.ts
@@ -1,7 +1,7 @@
1import { RegisterOptions } from './register-options.model' 1import { RegisterOptions } from './register-options.model'
2 2
3export interface PluginLibrary { 3export interface PluginLibrary {
4 register: (options: RegisterOptions) => void 4 register: (options: RegisterOptions) => Promise<any>
5 5
6 unregister: () => Promise<any> 6 unregister: () => Promise<any>
7} 7}
diff --git a/support/doc/plugins/quickstart.md b/support/doc/plugins/quickstart.md
index a018aa50a..0e125e707 100644
--- a/support/doc/plugins/quickstart.md
+++ b/support/doc/plugins/quickstart.md
@@ -18,10 +18,13 @@ A plugin registers functions in JavaScript to execute when PeerTube (server and
18Example: 18Example:
19 19
20```js 20```js
21registerHook({ 21// This register function is called by PeerTube, and **must** return a promise
22 target: 'action:application.listening', 22async function register ({ registerHook }) {
23 handler: () => displayHelloWorld() 23 registerHook({
24}) 24 target: 'action:application.listening',
25 handler: () => displayHelloWorld()
26 })
27}
25``` 28```
26 29
27On server side, these hooks are registered by the `library` file defined in `package.json`. 30On server side, these hooks are registered by the `library` file defined in `package.json`.
@@ -65,9 +68,7 @@ or `/themes/{theme-name}/{theme-version}/static/` routes.
65 68
66Plugins can declare CSS files that PeerTube will automatically inject in the client. 69Plugins can declare CSS files that PeerTube will automatically inject in the client.
67 70
68### Server helpers 71### Server helpers (only for plugins)
69
70**Only for plugins**
71 72
72#### Settings 73#### Settings
73 74
@@ -94,7 +95,7 @@ Example:
94 95
95```js 96```js
96const value = await storageManager.getData('mykey') 97const value = await storageManager.getData('mykey')
97await storageManager.storeData('mykey', 'myvalue') 98await storageManager.storeData('mykey', { subkey: 'value' })
98``` 99```
99 100
100### Publishing 101### Publishing
@@ -169,12 +170,13 @@ If you don't need static directories, use an empty `object`:
169} 170}
170``` 171```
171 172
172And if you don't need CSS files, use an empty `array`: 173And if you don't need CSS or client script files, use an empty `array`:
173 174
174```json 175```json
175{ 176{
176 ..., 177 ...,
177 "css": [], 178 "css": [],
179 "clientScripts": [],
178 ... 180 ...
179} 181}
180``` 182```
@@ -197,8 +199,14 @@ You'll need to have a local PeerTube instance:
197``` 199```
198$ npm run build -- --light 200$ npm run build -- --light
199``` 201```
202
203 * Build the CLI:
204
205```
206$ npm run setup:cli
207```
200 208
201 * Run it (you can access to your instance on http://localhost:9000): 209 * Run PeerTube (you can access to your instance on http://localhost:9000):
202 210
203``` 211```
204$ NODE_ENV=test npm start 212$ NODE_ENV=test npm start