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 { RegisterSettingOptions } from '@shared/models/plugins/register-setting.model'
14 import { PeerTubePluginIndex } from '@shared/models/plugins/peertube-plugin-index.model'
17 export class PluginApiService {
18 private static BASE_APPLICATION_URL = environment.apiUrl + '/api/v1/plugins'
21 private authHttp: HttpClient,
22 private restExtractor: RestExtractor,
23 private restService: RestService,
27 getPluginTypeOptions () {
30 label: this.i18n('Plugins'),
31 value: PluginType.PLUGIN
34 label: this.i18n('Themes'),
35 value: PluginType.THEME
40 getPluginTypeLabel (type: PluginType) {
41 if (type === PluginType.PLUGIN) {
42 return this.i18n('plugin')
45 return this.i18n('theme')
49 pluginType: PluginType,
50 componentPagination: ComponentPagination,
53 const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
55 let params = new HttpParams()
56 params = this.restService.addRestGetParams(params, pagination, sort)
57 params = params.append('pluginType', pluginType.toString())
59 return this.authHttp.get<ResultList<PeerTubePlugin>>(PluginApiService.BASE_APPLICATION_URL, { params })
60 .pipe(catchError(res => this.restExtractor.handleError(res)))
63 searchAvailablePlugins (
64 pluginType: PluginType,
65 componentPagination: ComponentPagination,
69 const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
71 let params = new HttpParams()
72 params = this.restService.addRestGetParams(params, pagination, sort)
73 params = params.append('pluginType', pluginType.toString())
75 if (search) params = params.append('search', search)
77 return this.authHttp.get<ResultList<PeerTubePluginIndex>>(PluginApiService.BASE_APPLICATION_URL + '/available', { params })
78 .pipe(catchError(res => this.restExtractor.handleError(res)))
81 getPlugin (npmName: string) {
82 const path = PluginApiService.BASE_APPLICATION_URL + '/' + npmName
84 return this.authHttp.get<PeerTubePlugin>(path)
85 .pipe(catchError(res => this.restExtractor.handleError(res)))
88 getPluginRegisteredSettings (pluginName: string, pluginType: PluginType) {
89 const path = PluginApiService.BASE_APPLICATION_URL + '/' + this.nameToNpmName(pluginName, pluginType) + '/registered-settings'
91 return this.authHttp.get<{ settings: RegisterSettingOptions[] }>(path)
92 .pipe(catchError(res => this.restExtractor.handleError(res)))
95 updatePluginSettings (pluginName: string, pluginType: PluginType, settings: any) {
96 const path = PluginApiService.BASE_APPLICATION_URL + '/' + this.nameToNpmName(pluginName, pluginType) + '/settings'
98 return this.authHttp.put(path, { settings })
99 .pipe(catchError(res => this.restExtractor.handleError(res)))
102 uninstall (pluginName: string, pluginType: PluginType) {
103 const body: ManagePlugin = {
104 npmName: this.nameToNpmName(pluginName, pluginType)
107 return this.authHttp.post(PluginApiService.BASE_APPLICATION_URL + '/uninstall', body)
108 .pipe(catchError(res => this.restExtractor.handleError(res)))
111 update (pluginName: string, pluginType: PluginType) {
112 const body: ManagePlugin = {
113 npmName: this.nameToNpmName(pluginName, pluginType)
116 return this.authHttp.post(PluginApiService.BASE_APPLICATION_URL + '/update', body)
117 .pipe(catchError(res => this.restExtractor.handleError(res)))
120 install (npmName: string) {
121 const body: InstallOrUpdatePlugin = {
125 return this.authHttp.post(PluginApiService.BASE_APPLICATION_URL + '/install', body)
126 .pipe(catchError(res => this.restExtractor.handleError(res)))
129 nameToNpmName (name: string, type: PluginType) {
130 const prefix = type === PluginType.PLUGIN
137 pluginTypeFromNpmName (npmName: string) {
138 return npmName.startsWith('peertube-plugin-')