]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/plugins/plugin.service.ts
Bumped to version v5.2.1
[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'
0628157f 14import { getKeys } from '@shared/core-utils'
bd45d503 15import { getCompleteLocale, isDefaultLocale, peertubeTranslate } from '@shared/core-utils/i18n'
67ed6552
C
16import {
17 ClientHook,
18 ClientHookName,
67ed6552
C
19 PluginClientScope,
20 PluginTranslation,
21 PluginType,
22 PublicServerSetting,
72f611ca 23 RegisterClientFormFieldOptions,
d63e6d46 24 RegisterClientRouteOptions,
fb3c9e2b 25 RegisterClientSettingsScriptOptions,
72f611ca 26 RegisterClientVideoFieldOptions,
67ed6552
C
27 ServerConfigPlugin
28} from '@shared/models'
29import { environment } from '../../../environments/environment'
67ed6552 30import { RegisterClientHelpers } from '../../../types/register-client-option.model'
584ac47a 31
72f611ca 32type FormFields = {
33 video: {
fb3c9e2b 34 pluginInfo: PluginInfo
72f611ca 35 commonOptions: RegisterClientFormFieldOptions
36 videoFormOptions: RegisterClientVideoFieldOptions
37 }[]
38}
18a6f04c 39
18a6f04c 40@Injectable()
93cae479 41export class PluginService implements ClientHook {
d75db01f
C
42 private static BASE_PLUGIN_API_URL = environment.apiUrl + '/api/v1/plugins'
43 private static BASE_PLUGIN_URL = environment.apiUrl + '/plugins'
23bdacf8 44
d75db01f
C
45 translationsObservable: Observable<PluginTranslation>
46
437e8e06
K
47 customModal: CustomModalComponent
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
fb3c9e2b 137 async translateSetting (npmName: string, setting: RegisterClientFormFieldOptions) {
0628157f 138 for (const key of getKeys(setting, [ 'label', 'html', 'descriptionHTML' ])) {
fb3c9e2b 139 if (setting[key]) setting[key] = await this.translateBy(npmName, setting[key])
c713017f
C
140 }
141
fb3c9e2b
C
142 if (Array.isArray(setting.options)) {
143 const newOptions = []
144
145 for (const o of setting.options) {
146 newOptions.push({
147 value: o.value,
148 label: await this.translateBy(npmName, o.label)
149 })
150 }
151
152 setting.options = newOptions
153 }
c713017f
C
154 }
155
fb3c9e2b
C
156 translateBy (npmName: string, toTranslate: string) {
157 const obs = this.translationsObservable
158 .pipe(
159 map(allTranslations => allTranslations[npmName]),
160 map(translations => peertubeTranslate(toTranslate, translations))
161 )
162
163 return firstValueFrom(obs)
164 }
165
166 private onFormFields (
167 pluginInfo: PluginInfo,
168 commonOptions: RegisterClientFormFieldOptions,
169 videoFormOptions: RegisterClientVideoFieldOptions
170 ) {
72f611ca 171 this.formFields.video.push({
fb3c9e2b 172 pluginInfo,
72f611ca 173 commonOptions,
174 videoFormOptions
16d54696 175 })
18a6f04c
C
176 }
177
d63e6d46 178 private onSettingsScripts (pluginInfo: PluginInfo, options: RegisterClientSettingsScriptOptions) {
fb3c9e2b 179 this.settingsScripts[pluginInfo.plugin.npmName] = options
18a6f04c
C
180 }
181
d63e6d46
C
182 private onClientRoute (options: RegisterClientRouteOptions) {
183 const route = options.route.startsWith('/')
184 ? options.route
185 : `/${options.route}`
186
187 this.clientRoutes[route] = options
188 }
189
d75db01f 190 private buildPeerTubeHelpers (pluginInfo: PluginInfo): RegisterClientHelpers {
f0c5e8b6 191 const { plugin } = pluginInfo
fb3c9e2b 192 const npmName = pluginInfo.plugin.npmName
f0c5e8b6
C
193
194 return {
195 getBaseStaticRoute: () => {
72f611ca 196 const pathPrefix = PluginsManager.getPluginPathPrefix(pluginInfo.isTheme)
f0c5e8b6 197 return environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/static`
23bdacf8
C
198 },
199
9777fe9e 200 getBaseRouterRoute: () => {
72f611ca 201 const pathPrefix = PluginsManager.getPluginPathPrefix(pluginInfo.isTheme)
9777fe9e
JL
202 return environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/router`
203 },
204
9d4c60dc
C
205 getBaseWebSocketRoute: () => {
206 const pathPrefix = PluginsManager.getPluginPathPrefix(pluginInfo.isTheme)
207 return environment.apiUrl + `${pathPrefix}/${plugin.name}/${plugin.version}/ws`
208 },
209
d63e6d46
C
210 getBasePluginClientPath: () => {
211 return '/p'
212 },
213
23bdacf8 214 getSettings: () => {
d75db01f 215 const path = PluginService.BASE_PLUGIN_API_URL + '/' + npmName + '/public-settings'
23bdacf8 216
1378c0d3 217 const obs = this.authHttp.get<PublicServerSetting>(path)
23bdacf8 218 .pipe(
ba211e73 219 map(p => p.publicSettings),
23bdacf8
C
220 catchError(res => this.restExtractor.handleError(res))
221 )
1378c0d3
C
222
223 return firstValueFrom(obs)
d75db01f
C
224 },
225
faeec106 226 getServerConfig: () => {
1378c0d3 227 const obs = this.server.getConfig()
faeec106 228 .pipe(catchError(res => this.restExtractor.handleError(res)))
1378c0d3
C
229
230 return firstValueFrom(obs)
faeec106
C
231 },
232
eb8f702c
RK
233 isLoggedIn: () => {
234 return this.authService.isLoggedIn()
235 },
236
4eca42ff
C
237 getAuthHeader: () => {
238 if (!this.authService.isLoggedIn()) return undefined
239
240 const value = this.authService.getRequestHeaderValue()
9df52d66 241 return { Authorization: value }
4eca42ff
C
242 },
243
f757be65 244 notifier: {
0f6521fa
C
245 info: (text: string, title?: string, timeout?: number) => this.zone.run(() => this.notifier.info(text, title, timeout)),
246 error: (text: string, title?: string, timeout?: number) => this.zone.run(() => this.notifier.error(text, title, timeout)),
247 success: (text: string, title?: string, timeout?: number) => this.zone.run(() => this.notifier.success(text, title, timeout))
f757be65 248 },
74c2dece 249
437e8e06 250 showModal: (input: {
9df52d66
C
251 title: string
252 content: string
253 close?: boolean
254 cancel?: { value: string, action?: () => void }
437e8e06
K
255 confirm?: { value: string, action?: () => void }
256 }) => {
0f6521fa 257 this.zone.run(() => this.customModal.show(input))
437e8e06
K
258 },
259
8c7725dc
K
260 markdownRenderer: {
261 textMarkdownToHTML: (textMarkdown: string) => {
0e45e336 262 return this.markdownRenderer.textMarkdownToHTML({ markdown: textMarkdown })
8c7725dc
K
263 },
264
265 enhancedMarkdownToHTML: (enhancedMarkdown: string) => {
0e45e336 266 return this.markdownRenderer.enhancedMarkdownToHTML({ markdown: enhancedMarkdown })
8c7725dc
K
267 }
268 },
269
d75db01f 270 translate: (value: string) => {
fb3c9e2b 271 return this.translateBy(npmName, value)
f0c5e8b6
C
272 }
273 }
274 }
275
d75db01f
C
276 private loadTranslations () {
277 const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
278
279 // Default locale, nothing to translate
280 if (isDefaultLocale(completeLocale)) this.translationsObservable = of({}).pipe(shareReplay())
281
282 this.translationsObservable = this.authHttp
283 .get<PluginTranslation>(PluginService.BASE_PLUGIN_URL + '/translations/' + completeLocale + '.json')
284 .pipe(shareReplay())
285 }
18a6f04c 286}