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'
16 export class PluginApiService {
17 private static BASE_APPLICATION_URL = environment.apiUrl + '/api/v1/plugins'
20 private authHttp: HttpClient,
21 private restExtractor: RestExtractor,
22 private restService: RestService,
26 getPluginTypeOptions () {
29 label: this.i18n('Plugins'),
30 value: PluginType.PLUGIN
33 label: this.i18n('Themes'),
34 value: PluginType.THEME
39 getPluginTypeLabel (type: PluginType) {
40 if (type === PluginType.PLUGIN) {
41 return this.i18n('plugin')
44 return this.i18n('theme')
49 componentPagination: ComponentPagination,
52 const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
54 let params = new HttpParams()
55 params = this.restService.addRestGetParams(params, pagination, sort)
56 params = params.append('type', type.toString())
58 return this.authHttp.get<ResultList<PeerTubePlugin>>(PluginApiService.BASE_APPLICATION_URL, { params })
59 .pipe(catchError(res => this.restExtractor.handleError(res)))
62 getPlugin (npmName: string) {
63 const path = PluginApiService.BASE_APPLICATION_URL + '/' + npmName
65 return this.authHttp.get<PeerTubePlugin>(path)
66 .pipe(catchError(res => this.restExtractor.handleError(res)))
69 getPluginRegisteredSettings (pluginName: string, pluginType: PluginType) {
70 const path = PluginApiService.BASE_APPLICATION_URL + '/' + this.nameToNpmName(pluginName, pluginType) + '/registered-settings'
72 return this.authHttp.get<{ settings: RegisterSettingOptions[] }>(path)
73 .pipe(catchError(res => this.restExtractor.handleError(res)))
76 updatePluginSettings (pluginName: string, pluginType: PluginType, settings: any) {
77 const path = PluginApiService.BASE_APPLICATION_URL + '/' + this.nameToNpmName(pluginName, pluginType) + '/settings'
79 return this.authHttp.put(path, { settings })
80 .pipe(catchError(res => this.restExtractor.handleError(res)))
83 uninstall (pluginName: string, pluginType: PluginType) {
84 const body: ManagePlugin = {
85 npmName: this.nameToNpmName(pluginName, pluginType)
88 return this.authHttp.post(PluginApiService.BASE_APPLICATION_URL + '/uninstall', body)
89 .pipe(catchError(res => this.restExtractor.handleError(res)))
92 update (pluginName: string, pluginType: PluginType) {
93 const body: ManagePlugin = {
94 npmName: this.nameToNpmName(pluginName, pluginType)
97 return this.authHttp.post(PluginApiService.BASE_APPLICATION_URL + '/update', body)
98 .pipe(catchError(res => this.restExtractor.handleError(res)))
101 install (npmName: string) {
102 const body: InstallOrUpdatePlugin = {
106 return this.authHttp.post(PluginApiService.BASE_APPLICATION_URL + '/install', body)
107 .pipe(catchError(res => this.restExtractor.handleError(res)))
110 nameToNpmName (name: string, type: PluginType) {
111 const prefix = type === PluginType.PLUGIN
118 pluginTypeFromNpmName (npmName: string) {
119 return npmName.startsWith('peertube-plugin-')