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 { peertubeTranslate } from '@shared/core-utils/i18n'
14 RegisteredServerSettings,
16 } from '@shared/models'
17 import { environment } from '../../../../environments/environment'
20 export class PluginApiService {
21 private static BASE_PLUGIN_URL = environment.apiUrl + '/api/v1/plugins'
24 private authHttp: HttpClient,
25 private restExtractor: RestExtractor,
26 private restService: RestService,
27 private pluginService: PluginService
30 getPluginTypeOptions () {
33 label: $localize`Plugins`,
34 value: PluginType.PLUGIN
37 label: $localize`Themes`,
38 value: PluginType.THEME
43 getPluginTypeLabel (type: PluginType) {
44 if (type === PluginType.PLUGIN) {
45 return $localize`plugin`
48 return $localize`theme`
52 pluginType: PluginType,
53 componentPagination: ComponentPagination,
56 const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
58 let params = new HttpParams()
59 params = this.restService.addRestGetParams(params, pagination, sort)
60 params = params.append('pluginType', pluginType.toString())
62 return this.authHttp.get<ResultList<PeerTubePlugin>>(PluginApiService.BASE_PLUGIN_URL, { params })
63 .pipe(catchError(res => this.restExtractor.handleError(res)))
66 searchAvailablePlugins (
67 pluginType: PluginType,
68 componentPagination: ComponentPagination,
72 const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
74 let params = new HttpParams()
75 params = this.restService.addRestGetParams(params, pagination, sort)
76 params = params.append('pluginType', pluginType.toString())
78 if (search) params = params.append('search', search)
80 return this.authHttp.get<ResultList<PeerTubePluginIndex>>(PluginApiService.BASE_PLUGIN_URL + '/available', { params })
81 .pipe(catchError(res => this.restExtractor.handleError(res)))
84 getPlugin (npmName: string) {
85 const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName
87 return this.authHttp.get<PeerTubePlugin>(path)
88 .pipe(catchError(res => this.restExtractor.handleError(res)))
91 getPluginRegisteredSettings (pluginName: string, pluginType: PluginType) {
92 const npmName = this.pluginService.nameToNpmName(pluginName, pluginType)
93 const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName + '/registered-settings'
95 return this.authHttp.get<RegisteredServerSettings>(path)
97 catchError(res => this.restExtractor.handleError(res))
101 updatePluginSettings (pluginName: string, pluginType: PluginType, settings: any) {
102 const npmName = this.pluginService.nameToNpmName(pluginName, pluginType)
103 const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName + '/settings'
105 return this.authHttp.put(path, { settings })
106 .pipe(catchError(res => this.restExtractor.handleError(res)))
109 uninstall (pluginName: string, pluginType: PluginType) {
110 const body: ManagePlugin = {
111 npmName: this.pluginService.nameToNpmName(pluginName, pluginType)
114 return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/uninstall', body)
115 .pipe(catchError(res => this.restExtractor.handleError(res)))
118 update (pluginName: string, pluginType: PluginType) {
119 const body: ManagePlugin = {
120 npmName: this.pluginService.nameToNpmName(pluginName, pluginType)
123 return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/update', body)
124 .pipe(catchError(res => this.restExtractor.handleError(res)))
127 install (npmName: string) {
128 const body: InstallOrUpdatePlugin = {
132 return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/install', body)
133 .pipe(catchError(res => this.restExtractor.handleError(res)))
136 getPluginOrThemeHref (type: PluginType, name: string) {
137 const typeString = type === PluginType.PLUGIN
141 return `https://www.npmjs.com/package/peertube-${typeString}-${name}`