]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/plugins/shared/plugin-api.service.ts
Add setting helper to client plugins
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / plugins / shared / plugin-api.service.ts
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'
16
17 @Injectable()
18 export class PluginApiService {
19 private static BASE_PLUGIN_URL = environment.apiUrl + '/api/v1/plugins'
20
21 constructor (
22 private authHttp: HttpClient,
23 private restExtractor: RestExtractor,
24 private restService: RestService,
25 private i18n: I18n,
26 private pluginService: PluginService
27 ) { }
28
29 getPluginTypeOptions () {
30 return [
31 {
32 label: this.i18n('Plugins'),
33 value: PluginType.PLUGIN
34 },
35 {
36 label: this.i18n('Themes'),
37 value: PluginType.THEME
38 }
39 ]
40 }
41
42 getPluginTypeLabel (type: PluginType) {
43 if (type === PluginType.PLUGIN) {
44 return this.i18n('plugin')
45 }
46
47 return this.i18n('theme')
48 }
49
50 getPlugins (
51 pluginType: PluginType,
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)
59 params = params.append('pluginType', pluginType.toString())
60
61 return this.authHttp.get<ResultList<PeerTubePlugin>>(PluginApiService.BASE_PLUGIN_URL, { params })
62 .pipe(catchError(res => this.restExtractor.handleError(res)))
63 }
64
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
79 return this.authHttp.get<ResultList<PeerTubePluginIndex>>(PluginApiService.BASE_PLUGIN_URL + '/available', { params })
80 .pipe(catchError(res => this.restExtractor.handleError(res)))
81 }
82
83 getPlugin (npmName: string) {
84 const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName
85
86 return this.authHttp.get<PeerTubePlugin>(path)
87 .pipe(catchError(res => this.restExtractor.handleError(res)))
88 }
89
90 getPluginRegisteredSettings (pluginName: string, pluginType: PluginType) {
91 const npmName = this.pluginService.nameToNpmName(pluginName, pluginType)
92 const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName + '/registered-settings'
93
94 return this.authHttp.get<{ settings: RegisterServerSettingOptions[] }>(path)
95 .pipe(catchError(res => this.restExtractor.handleError(res)))
96 }
97
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'
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 = {
108 npmName: this.pluginService.nameToNpmName(pluginName, pluginType)
109 }
110
111 return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/uninstall', body)
112 .pipe(catchError(res => this.restExtractor.handleError(res)))
113 }
114
115 update (pluginName: string, pluginType: PluginType) {
116 const body: ManagePlugin = {
117 npmName: this.pluginService.nameToNpmName(pluginName, pluginType)
118 }
119
120 return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/update', body)
121 .pipe(catchError(res => this.restExtractor.handleError(res)))
122 }
123
124 install (npmName: string) {
125 const body: InstallOrUpdatePlugin = {
126 npmName
127 }
128
129 return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/install', body)
130 .pipe(catchError(res => this.restExtractor.handleError(res)))
131 }
132 }