diff options
author | Chocobozzz <me@florianbigard.com> | 2019-07-09 11:45:19 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2019-07-24 10:58:16 +0200 |
commit | 7cd4d2ba10106c10602c86f74f55743ded588896 (patch) | |
tree | 81f0dd7a7ef763511158d1035f3e09e09d5dcd2c /server/lib/plugins | |
parent | 8d76959e11ab7172040853fa4fadaf8d53e6aa12 (diff) | |
download | PeerTube-7cd4d2ba10106c10602c86f74f55743ded588896.tar.gz PeerTube-7cd4d2ba10106c10602c86f74f55743ded588896.tar.zst PeerTube-7cd4d2ba10106c10602c86f74f55743ded588896.zip |
WIP plugins: add theme support
Diffstat (limited to 'server/lib/plugins')
-rw-r--r-- | server/lib/plugins/plugin-manager.ts | 34 | ||||
-rw-r--r-- | server/lib/plugins/theme-utils.ts | 24 |
2 files changed, 54 insertions, 4 deletions
diff --git a/server/lib/plugins/plugin-manager.ts b/server/lib/plugins/plugin-manager.ts index 7cbfa8569..8496979f8 100644 --- a/server/lib/plugins/plugin-manager.ts +++ b/server/lib/plugins/plugin-manager.ts | |||
@@ -11,6 +11,7 @@ import { PLUGIN_GLOBAL_CSS_PATH } from '../../initializers/constants' | |||
11 | import { PluginType } from '../../../shared/models/plugins/plugin.type' | 11 | import { PluginType } from '../../../shared/models/plugins/plugin.type' |
12 | import { installNpmPlugin, installNpmPluginFromDisk, removeNpmPlugin } from './yarn' | 12 | import { installNpmPlugin, installNpmPluginFromDisk, removeNpmPlugin } from './yarn' |
13 | import { outputFile } from 'fs-extra' | 13 | import { outputFile } from 'fs-extra' |
14 | import { ServerConfigPlugin } from '../../../shared/models/server' | ||
14 | 15 | ||
15 | export interface RegisteredPlugin { | 16 | export interface RegisteredPlugin { |
16 | name: string | 17 | name: string |
@@ -47,7 +48,7 @@ export class PluginManager { | |||
47 | private constructor () { | 48 | private constructor () { |
48 | } | 49 | } |
49 | 50 | ||
50 | async registerPlugins () { | 51 | async registerPluginsAndThemes () { |
51 | await this.resetCSSGlobalFile() | 52 | await this.resetCSSGlobalFile() |
52 | 53 | ||
53 | const plugins = await PluginModel.listEnabledPluginsAndThemes() | 54 | const plugins = await PluginModel.listEnabledPluginsAndThemes() |
@@ -63,12 +64,20 @@ export class PluginManager { | |||
63 | this.sortHooksByPriority() | 64 | this.sortHooksByPriority() |
64 | } | 65 | } |
65 | 66 | ||
67 | getRegisteredPluginOrTheme (name: string) { | ||
68 | return this.registeredPlugins[name] | ||
69 | } | ||
70 | |||
66 | getRegisteredPlugin (name: string) { | 71 | getRegisteredPlugin (name: string) { |
67 | return this.registeredPlugins[ name ] | 72 | const registered = this.getRegisteredPluginOrTheme(name) |
73 | |||
74 | if (!registered || registered.type !== PluginType.PLUGIN) return undefined | ||
75 | |||
76 | return registered | ||
68 | } | 77 | } |
69 | 78 | ||
70 | getRegisteredTheme (name: string) { | 79 | getRegisteredTheme (name: string) { |
71 | const registered = this.getRegisteredPlugin(name) | 80 | const registered = this.getRegisteredPluginOrTheme(name) |
72 | 81 | ||
73 | if (!registered || registered.type !== PluginType.THEME) return undefined | 82 | if (!registered || registered.type !== PluginType.THEME) return undefined |
74 | 83 | ||
@@ -76,7 +85,11 @@ export class PluginManager { | |||
76 | } | 85 | } |
77 | 86 | ||
78 | getRegisteredPlugins () { | 87 | getRegisteredPlugins () { |
79 | return this.registeredPlugins | 88 | return this.getRegisteredPluginsOrThemes(PluginType.PLUGIN) |
89 | } | ||
90 | |||
91 | getRegisteredThemes () { | ||
92 | return this.getRegisteredPluginsOrThemes(PluginType.THEME) | ||
80 | } | 93 | } |
81 | 94 | ||
82 | async runHook (hookName: string, param?: any) { | 95 | async runHook (hookName: string, param?: any) { |
@@ -309,6 +322,19 @@ export class PluginManager { | |||
309 | } | 322 | } |
310 | } | 323 | } |
311 | 324 | ||
325 | private getRegisteredPluginsOrThemes (type: PluginType) { | ||
326 | const plugins: RegisteredPlugin[] = [] | ||
327 | |||
328 | for (const pluginName of Object.keys(this.registeredPlugins)) { | ||
329 | const plugin = this.registeredPlugins[ pluginName ] | ||
330 | if (plugin.type !== type) continue | ||
331 | |||
332 | plugins.push(plugin) | ||
333 | } | ||
334 | |||
335 | return plugins | ||
336 | } | ||
337 | |||
312 | static get Instance () { | 338 | static get Instance () { |
313 | return this.instance || (this.instance = new this()) | 339 | return this.instance || (this.instance = new this()) |
314 | } | 340 | } |
diff --git a/server/lib/plugins/theme-utils.ts b/server/lib/plugins/theme-utils.ts new file mode 100644 index 000000000..066339e65 --- /dev/null +++ b/server/lib/plugins/theme-utils.ts | |||
@@ -0,0 +1,24 @@ | |||
1 | import { DEFAULT_THEME } from '../../initializers/constants' | ||
2 | import { PluginManager } from './plugin-manager' | ||
3 | import { CONFIG } from '../../initializers/config' | ||
4 | |||
5 | function getThemeOrDefault (name: string) { | ||
6 | if (isThemeRegistered(name)) return name | ||
7 | |||
8 | // Fallback to admin default theme | ||
9 | if (name !== CONFIG.THEME.DEFAULT) return getThemeOrDefault(CONFIG.THEME.DEFAULT) | ||
10 | |||
11 | return DEFAULT_THEME | ||
12 | } | ||
13 | |||
14 | function isThemeRegistered (name: string) { | ||
15 | if (name === DEFAULT_THEME) return true | ||
16 | |||
17 | return !!PluginManager.Instance.getRegisteredThemes() | ||
18 | .find(r => r.name === name) | ||
19 | } | ||
20 | |||
21 | export { | ||
22 | getThemeOrDefault, | ||
23 | isThemeRegistered | ||
24 | } | ||