1 import { Observable } from 'rxjs'
2 import { catchError, map, switchMap } from 'rxjs/operators'
3 import { HttpClient, HttpParams } from '@angular/common/http'
4 import { Injectable } from '@angular/core'
5 import { ComponentPagination, RestExtractor, RestService } from '@app/core'
6 import { PluginService } from '@app/core/plugins/plugin.service'
7 import { I18n } from '@ngx-translate/i18n-polyfill'
8 import { peertubeTranslate } from '@shared/core-utils/i18n'
10 InstallOrUpdatePlugin,
15 RegisteredServerSettings,
17 } from '@shared/models'
18 import { environment } from '../../../../environments/environment'
21 export class PluginApiService {
22 private static BASE_PLUGIN_URL = environment.apiUrl + '/api/v1/plugins'
25 private authHttp: HttpClient,
26 private restExtractor: RestExtractor,
27 private restService: RestService,
29 private pluginService: PluginService
32 getPluginTypeOptions () {
35 label: this.i18n('Plugins'),
36 value: PluginType.PLUGIN
39 label: this.i18n('Themes'),
40 value: PluginType.THEME
45 getPluginTypeLabel (type: PluginType) {
46 if (type === PluginType.PLUGIN) {
47 return this.i18n('plugin')
50 return this.i18n('theme')
54 pluginType: PluginType,
55 componentPagination: ComponentPagination,
58 const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
60 let params = new HttpParams()
61 params = this.restService.addRestGetParams(params, pagination, sort)
62 params = params.append('pluginType', pluginType.toString())
64 return this.authHttp.get<ResultList<PeerTubePlugin>>(PluginApiService.BASE_PLUGIN_URL, { params })
65 .pipe(catchError(res => this.restExtractor.handleError(res)))
68 searchAvailablePlugins (
69 pluginType: PluginType,
70 componentPagination: ComponentPagination,
74 const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
76 let params = new HttpParams()
77 params = this.restService.addRestGetParams(params, pagination, sort)
78 params = params.append('pluginType', pluginType.toString())
80 if (search) params = params.append('search', search)
82 return this.authHttp.get<ResultList<PeerTubePluginIndex>>(PluginApiService.BASE_PLUGIN_URL + '/available', { params })
83 .pipe(catchError(res => this.restExtractor.handleError(res)))
86 getPlugin (npmName: string) {
87 const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName
89 return this.authHttp.get<PeerTubePlugin>(path)
90 .pipe(catchError(res => this.restExtractor.handleError(res)))
93 getPluginRegisteredSettings (pluginName: string, pluginType: PluginType) {
94 const npmName = this.pluginService.nameToNpmName(pluginName, pluginType)
95 const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName + '/registered-settings'
97 return this.authHttp.get<RegisteredServerSettings>(path)
99 switchMap(res => this.translateSettingsLabel(npmName, res)),
100 catchError(res => this.restExtractor.handleError(res))
104 updatePluginSettings (pluginName: string, pluginType: PluginType, settings: any) {
105 const npmName = this.pluginService.nameToNpmName(pluginName, pluginType)
106 const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName + '/settings'
108 return this.authHttp.put(path, { settings })
109 .pipe(catchError(res => this.restExtractor.handleError(res)))
112 uninstall (pluginName: string, pluginType: PluginType) {
113 const body: ManagePlugin = {
114 npmName: this.pluginService.nameToNpmName(pluginName, pluginType)
117 return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/uninstall', body)
118 .pipe(catchError(res => this.restExtractor.handleError(res)))
121 update (pluginName: string, pluginType: PluginType) {
122 const body: ManagePlugin = {
123 npmName: this.pluginService.nameToNpmName(pluginName, pluginType)
126 return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/update', body)
127 .pipe(catchError(res => this.restExtractor.handleError(res)))
130 install (npmName: string) {
131 const body: InstallOrUpdatePlugin = {
135 return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/install', body)
136 .pipe(catchError(res => this.restExtractor.handleError(res)))
139 private translateSettingsLabel (npmName: string, res: RegisteredServerSettings): Observable<RegisteredServerSettings> {
140 return this.pluginService.translationsObservable
142 map(allTranslations => allTranslations[npmName]),
143 map(translations => {
144 const registeredSettings = res.registeredSettings
146 return Object.assign({}, r, { label: peertubeTranslate(r.label, translations) })
149 return { registeredSettings }