]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+admin/plugins/shared/plugin-api.service.ts
Add public settings endpoint
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / plugins / shared / plugin-api.service.ts
CommitLineData
d00dc28d
C
1import { catchError } from 'rxjs/operators'
2import { HttpClient, HttpParams } from '@angular/common/http'
3import { Injectable } from '@angular/core'
4import { environment } from '../../../../environments/environment'
5import { RestExtractor, RestService } from '../../../shared'
6import { I18n } from '@ngx-translate/i18n-polyfill'
7import { PluginType } from '@shared/models/plugins/plugin.type'
8import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
9import { ResultList } from '@shared/models'
10import { PeerTubePlugin } from '@shared/models/plugins/peertube-plugin.model'
dba85a1e 11import { ManagePlugin } from '@shared/models/plugins/manage-plugin.model'
b5f919ac 12import { InstallOrUpdatePlugin } from '@shared/models/plugins/install-plugin.model'
6702a1b2 13import { PeerTubePluginIndex } from '@shared/models/plugins/peertube-plugin-index.model'
ba211e73 14import { RegisteredServerSettings, RegisterServerSettingOptions } from '@shared/models/plugins/register-server-setting.model'
23bdacf8 15import { PluginService } from '@app/core/plugins/plugin.service'
d00dc28d
C
16
17@Injectable()
18export class PluginApiService {
23bdacf8 19 private static BASE_PLUGIN_URL = environment.apiUrl + '/api/v1/plugins'
d00dc28d
C
20
21 constructor (
22 private authHttp: HttpClient,
23 private restExtractor: RestExtractor,
24 private restService: RestService,
23bdacf8
C
25 private i18n: I18n,
26 private pluginService: PluginService
d00dc28d
C
27 ) { }
28
29 getPluginTypeOptions () {
30 return [
31 {
dba85a1e 32 label: this.i18n('Plugins'),
d00dc28d
C
33 value: PluginType.PLUGIN
34 },
35 {
dba85a1e 36 label: this.i18n('Themes'),
d00dc28d
C
37 value: PluginType.THEME
38 }
39 ]
40 }
41
dba85a1e
C
42 getPluginTypeLabel (type: PluginType) {
43 if (type === PluginType.PLUGIN) {
44 return this.i18n('plugin')
45 }
46
47 return this.i18n('theme')
48 }
49
d00dc28d 50 getPlugins (
6702a1b2 51 pluginType: PluginType,
d00dc28d
C
52 componentPagination: ComponentPagination,
53 sort: string
54 ) {
55 const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
56
57 let params = new HttpParams()
58 params = this.restService.addRestGetParams(params, pagination, sort)
6702a1b2 59 params = params.append('pluginType', pluginType.toString())
d00dc28d 60
23bdacf8 61 return this.authHttp.get<ResultList<PeerTubePlugin>>(PluginApiService.BASE_PLUGIN_URL, { params })
d00dc28d
C
62 .pipe(catchError(res => this.restExtractor.handleError(res)))
63 }
dba85a1e 64
6702a1b2
C
65 searchAvailablePlugins (
66 pluginType: PluginType,
67 componentPagination: ComponentPagination,
68 sort: string,
69 search?: string
70 ) {
71 const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
72
73 let params = new HttpParams()
74 params = this.restService.addRestGetParams(params, pagination, sort)
75 params = params.append('pluginType', pluginType.toString())
76
77 if (search) params = params.append('search', search)
78
23bdacf8 79 return this.authHttp.get<ResultList<PeerTubePluginIndex>>(PluginApiService.BASE_PLUGIN_URL + '/available', { params })
6702a1b2
C
80 .pipe(catchError(res => this.restExtractor.handleError(res)))
81 }
82
dba85a1e 83 getPlugin (npmName: string) {
23bdacf8 84 const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName
dba85a1e
C
85
86 return this.authHttp.get<PeerTubePlugin>(path)
87 .pipe(catchError(res => this.restExtractor.handleError(res)))
88 }
89
90 getPluginRegisteredSettings (pluginName: string, pluginType: PluginType) {
23bdacf8
C
91 const npmName = this.pluginService.nameToNpmName(pluginName, pluginType)
92 const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName + '/registered-settings'
dba85a1e 93
ba211e73 94 return this.authHttp.get<RegisteredServerSettings>(path)
dba85a1e
C
95 .pipe(catchError(res => this.restExtractor.handleError(res)))
96 }
97
98 updatePluginSettings (pluginName: string, pluginType: PluginType, settings: any) {
23bdacf8
C
99 const npmName = this.pluginService.nameToNpmName(pluginName, pluginType)
100 const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName + '/settings'
dba85a1e
C
101
102 return this.authHttp.put(path, { settings })
103 .pipe(catchError(res => this.restExtractor.handleError(res)))
104 }
105
106 uninstall (pluginName: string, pluginType: PluginType) {
107 const body: ManagePlugin = {
23bdacf8 108 npmName: this.pluginService.nameToNpmName(pluginName, pluginType)
dba85a1e
C
109 }
110
23bdacf8 111 return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/uninstall', body)
dba85a1e
C
112 .pipe(catchError(res => this.restExtractor.handleError(res)))
113 }
114
b5f919ac
C
115 update (pluginName: string, pluginType: PluginType) {
116 const body: ManagePlugin = {
23bdacf8 117 npmName: this.pluginService.nameToNpmName(pluginName, pluginType)
b5f919ac
C
118 }
119
23bdacf8 120 return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/update', body)
b5f919ac
C
121 .pipe(catchError(res => this.restExtractor.handleError(res)))
122 }
123
dba85a1e 124 install (npmName: string) {
b5f919ac 125 const body: InstallOrUpdatePlugin = {
dba85a1e
C
126 npmName
127 }
128
23bdacf8 129 return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/install', body)
dba85a1e
C
130 .pipe(catchError(res => this.restExtractor.handleError(res)))
131 }
d00dc28d 132}