]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/core/plugins/plugin.service.ts
add client hook filter:videojs.options
[github/Chocobozzz/PeerTube.git] / client / src / app / core / plugins / plugin.service.ts
1 import { Observable, of } from 'rxjs'
2 import { catchError, map, shareReplay } from 'rxjs/operators'
3 import { HttpClient } from '@angular/common/http'
4 import { Inject, Injectable, LOCALE_ID, NgZone } from '@angular/core'
5 import { VideoEditType } from '@app/+videos/+video-edit/shared/video-edit.type'
6 import { AuthService } from '@app/core/auth'
7 import { Notifier } from '@app/core/notification'
8 import { MarkdownService } from '@app/core/renderer'
9 import { RestExtractor } from '@app/core/rest'
10 import { ServerService } from '@app/core/server/server.service'
11 import { getDevLocale, isOnDevLocale } from '@app/helpers'
12 import { CustomModalComponent } from '@app/modal/custom-modal.component'
13 import { PluginInfo, PluginsManager } from '@root-helpers/plugins-manager'
14 import { getCompleteLocale, isDefaultLocale, peertubeTranslate } from '@shared/core-utils/i18n'
15 import {
16 ClientHook,
17 ClientHookName,
18 PluginClientScope,
19 PluginTranslation,
20 PluginType,
21 PublicServerSetting,
22 RegisterClientFormFieldOptions,
23 RegisterClientSettingsScript,
24 RegisterClientVideoFieldOptions,
25 ServerConfigPlugin
26 } from '@shared/models'
27 import { environment } from '../../../environments/environment'
28 import { RegisterClientHelpers } from '../../../types/register-client-option.model'
29
30 type FormFields = {
31 video: {
32 commonOptions: RegisterClientFormFieldOptions
33 videoFormOptions: RegisterClientVideoFieldOptions
34 }[]
35 }
36
37 @Injectable()
38 export class PluginService implements ClientHook {
39 private static BASE_PLUGIN_API_URL = environment.apiUrl + '/api/v1/plugins'
40 private static BASE_PLUGIN_URL = environment.apiUrl + '/plugins'
41
42 translationsObservable: Observable<PluginTranslation>
43
44 customModal: CustomModalComponent
45
46 private helpers: { [ npmName: string ]: RegisterClientHelpers } = {}
47
48 private formFields: FormFields = {
49 video: []
50 }
51 private settingsScripts: { [ npmName: string ]: RegisterClientSettingsScript } = {}
52
53 private pluginsManager: PluginsManager
54
55 constructor (
56 private authService: AuthService,
57 private notifier: Notifier,
58 private markdownRenderer: MarkdownService,
59 private server: ServerService,
60 private zone: NgZone,
61 private authHttp: HttpClient,
62 private restExtractor: RestExtractor,
63 @Inject(LOCALE_ID) private localeId: string
64 ) {
65 this.loadTranslations()
66
67 this.pluginsManager = new PluginsManager({
68 peertubeHelpersFactory: this.buildPeerTubeHelpers.bind(this),
69 onFormFields: this.onFormFields.bind(this),
70 onSettingsScripts: this.onSettingsScripts.bind(this)
71 })
72 }
73
74 initializePlugins () {
75 this.pluginsManager.loadPluginsList(this.server.getHTMLConfig())
76
77 this.pluginsManager.ensurePluginsAreLoaded('common')
78 }
79
80 initializeCustomModal (customModal: CustomModalComponent) {
81 this.customModal = customModal
82 }
83
84 runHook <T> (hookName: ClientHookName, result?: T, params?: any): Promise<T> {
85 return this.zone.runOutsideAngular(() => {
86 return this.pluginsManager.runHook(hookName, result, params)
87 })
88 }
89
90 ensurePluginsAreLoaded (scope: PluginClientScope) {
91 return this.pluginsManager.ensurePluginsAreLoaded(scope)
92 }
93
94 reloadLoadedScopes () {
95 return this.pluginsManager.reloadLoadedScopes()
96 }
97
98 getPluginsManager () {
99 return this.pluginsManager
100 }
101
102 addPlugin (plugin: ServerConfigPlugin, isTheme = false) {
103 return this.pluginsManager.addPlugin(plugin, isTheme)
104 }
105
106 removePlugin (plugin: ServerConfigPlugin) {
107 return this.pluginsManager.removePlugin(plugin)
108 }
109
110 nameToNpmName (name: string, type: PluginType) {
111 const prefix = type === PluginType.PLUGIN
112 ? 'peertube-plugin-'
113 : 'peertube-theme-'
114
115 return prefix + name
116 }
117
118 getRegisteredVideoFormFields (type: VideoEditType) {
119 return this.formFields.video.filter(f => f.videoFormOptions.type === type)
120 }
121
122 getRegisteredSettingsScript (npmName: string) {
123 return this.settingsScripts[npmName]
124 }
125
126 translateBy (npmName: string, toTranslate: string) {
127 const helpers = this.helpers[npmName]
128 if (!helpers) {
129 console.error('Unknown helpers to translate %s from %s.', toTranslate, npmName)
130 return toTranslate
131 }
132
133 return helpers.translate(toTranslate)
134 }
135
136 private onFormFields (commonOptions: RegisterClientFormFieldOptions, videoFormOptions: RegisterClientVideoFieldOptions) {
137 this.formFields.video.push({
138 commonOptions,
139 videoFormOptions
140 })
141 }
142
143 private onSettingsScripts (pluginInfo: PluginInfo, options: RegisterClientSettingsScript) {
144 const npmName = this.nameToNpmName(pluginInfo.plugin.name, pluginInfo.pluginType)
145
146 this.settingsScripts[npmName] = options
147 }
148
149 private buildPeerTubeHelpers (pluginInfo: PluginInfo): RegisterClientHelpers {
150 const { plugin } = pluginInfo
151 const npmName = this.nameToNpmName(pluginInfo.plugin.name, pluginInfo.pluginType)
152
153 return {
154 getBaseStaticRoute: () => {
155 const pathPrefix = PluginsManager.getPluginPathPrefix(pluginInfo.isTheme)
156 return environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/static`
157 },
158
159 getBaseRouterRoute: () => {
160 const pathPrefix = PluginsManager.getPluginPathPrefix(pluginInfo.isTheme)
161 return environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/router`
162 },
163
164 getSettings: () => {
165 const path = PluginService.BASE_PLUGIN_API_URL + '/' + npmName + '/public-settings'
166
167 return this.authHttp.get<PublicServerSetting>(path)
168 .pipe(
169 map(p => p.publicSettings),
170 catchError(res => this.restExtractor.handleError(res))
171 )
172 .toPromise()
173 },
174
175 getServerConfig: () => {
176 return this.server.getConfig()
177 .pipe(catchError(res => this.restExtractor.handleError(res)))
178 .toPromise()
179 },
180
181 isLoggedIn: () => {
182 return this.authService.isLoggedIn()
183 },
184
185 getAuthHeader: () => {
186 if (!this.authService.isLoggedIn()) return undefined
187
188 const value = this.authService.getRequestHeaderValue()
189 return { 'Authorization': value }
190 },
191
192 notifier: {
193 info: (text: string, title?: string, timeout?: number) => this.notifier.info(text, title, timeout),
194 error: (text: string, title?: string, timeout?: number) => this.notifier.error(text, title, timeout),
195 success: (text: string, title?: string, timeout?: number) => this.notifier.success(text, title, timeout)
196 },
197
198 showModal: (input: {
199 title: string,
200 content: string,
201 close?: boolean,
202 cancel?: { value: string, action?: () => void },
203 confirm?: { value: string, action?: () => void }
204 }) => {
205 this.customModal.show(input)
206 },
207
208 markdownRenderer: {
209 textMarkdownToHTML: (textMarkdown: string) => {
210 return this.markdownRenderer.textMarkdownToHTML(textMarkdown)
211 },
212
213 enhancedMarkdownToHTML: (enhancedMarkdown: string) => {
214 return this.markdownRenderer.enhancedMarkdownToHTML(enhancedMarkdown)
215 }
216 },
217
218 translate: (value: string) => {
219 return this.translationsObservable
220 .pipe(map(allTranslations => allTranslations[npmName]))
221 .pipe(map(translations => peertubeTranslate(value, translations)))
222 .toPromise()
223 }
224 }
225 }
226
227 private loadTranslations () {
228 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
229
230 // Default locale, nothing to translate
231 if (isDefaultLocale(completeLocale)) this.translationsObservable = of({}).pipe(shareReplay())
232
233 this.translationsObservable = this.authHttp
234 .get<PluginTranslation>(PluginService.BASE_PLUGIN_URL + '/translations/' + completeLocale + '.json')
235 .pipe(shareReplay())
236 }
237 }