1 import { catchError } from 'rxjs/operators'
2 import { HttpClient, HttpParams } from '@angular/common/http'
3 import { Injectable } from '@angular/core'
4 import { environment } from '../../../../environments/environment'
5 import { RestExtractor, RestService } from '../../../shared'
6 import { I18n } from '@ngx-translate/i18n-polyfill'
7 import { PluginType } from '@shared/models/plugins/plugin.type'
8 import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
9 import { ResultList } from '@shared/models'
10 import { PeerTubePlugin } from '@shared/models/plugins/peertube-plugin.model'
11 import { ManagePlugin } from '@shared/models/plugins/manage-plugin.model'
12 import { InstallOrUpdatePlugin } from '@shared/models/plugins/install-plugin.model'
13 import { PeerTubePluginIndex } from '@shared/models/plugins/peertube-plugin-index.model'
14 import { RegisterServerSettingOptions } from '@shared/models/plugins/register-server-setting.model'
15 import { PluginService } from '@app/core/plugins/plugin.service'
18 export class PluginApiService {
19 private static BASE_PLUGIN_URL = environment.apiUrl + '/api/v1/plugins'
22 private authHttp: HttpClient,
23 private restExtractor: RestExtractor,
24 private restService: RestService,
26 private pluginService: PluginService
29 getPluginTypeOptions () {
32 label: this.i18n('Plugins'),
33 value: PluginType.PLUGIN
36 label: this.i18n('Themes'),
37 value: PluginType.THEME
42 getPluginTypeLabel (type: PluginType) {
43 if (type === PluginType.PLUGIN) {
44 return this.i18n('plugin')
47 return this.i18n('theme')
51 pluginType: PluginType,
52 componentPagination: ComponentPagination,
55 const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
57 let params = new HttpParams()
58 params = this.restService.addRestGetParams(params, pagination, sort)
59 params = params.append('pluginType', pluginType.toString())
61 return this.authHttp.get<ResultList<PeerTubePlugin>>(PluginApiService.BASE_PLUGIN_URL, { params })
62 .pipe(catchError(res => this.restExtractor.handleError(res)))
65 searchAvailablePlugins (
66 pluginType: PluginType,
67 componentPagination: ComponentPagination,
71 const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
73 let params = new HttpParams()
74 params = this.restService.addRestGetParams(params, pagination, sort)
75 params = params.append('pluginType', pluginType.toString())
77 if (search) params = params.append('search', search)
79 return this.authHttp.get<ResultList<PeerTubePluginIndex>>(PluginApiService.BASE_PLUGIN_URL + '/available', { params })
80 .pipe(catchError(res => this.restExtractor.handleError(res)))
83 getPlugin (npmName: string) {
84 const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName
86 return this.authHttp.get<PeerTubePlugin>(path)
87 .pipe(catchError(res => this.restExtractor.handleError(res)))
90 getPluginRegisteredSettings (pluginName: string, pluginType: PluginType) {
91 const npmName = this.pluginService.nameToNpmName(pluginName, pluginType)
92 const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName + '/registered-settings'
94 return this.authHttp.get<{ settings: RegisterServerSettingOptions[] }>(path)
95 .pipe(catchError(res => this.restExtractor.handleError(res)))
98 updatePluginSettings (pluginName: string, pluginType: PluginType, settings: any) {
99 const npmName = this.pluginService.nameToNpmName(pluginName, pluginType)
100 const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName + '/settings'
102 return this.authHttp.put(path, { settings })
103 .pipe(catchError(res => this.restExtractor.handleError(res)))
106 uninstall (pluginName: string, pluginType: PluginType) {
107 const body: ManagePlugin = {
108 npmName: this.pluginService.nameToNpmName(pluginName, pluginType)
111 return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/uninstall', body)
112 .pipe(catchError(res => this.restExtractor.handleError(res)))
115 update (pluginName: string, pluginType: PluginType) {
116 const body: ManagePlugin = {
117 npmName: this.pluginService.nameToNpmName(pluginName, pluginType)
120 return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/update', body)
121 .pipe(catchError(res => this.restExtractor.handleError(res)))
124 install (npmName: string) {
125 const body: InstallOrUpdatePlugin = {
129 return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/install', body)
130 .pipe(catchError(res => this.restExtractor.handleError(res)))