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