]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/plugins/shared/plugin-api.service.ts
WIP plugins: add plugin settings/uninstall in client
[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 { InstallPlugin } from '@shared/models/plugins/install-plugin.model'
13 import { RegisterSettingOptions } from '@shared/models/plugins/register-setting.model'
14
15 @Injectable()
16 export class PluginApiService {
17 private static BASE_APPLICATION_URL = environment.apiUrl + '/api/v1/plugins'
18
19 constructor (
20 private authHttp: HttpClient,
21 private restExtractor: RestExtractor,
22 private restService: RestService,
23 private i18n: I18n
24 ) { }
25
26 getPluginTypeOptions () {
27 return [
28 {
29 label: this.i18n('Plugins'),
30 value: PluginType.PLUGIN
31 },
32 {
33 label: this.i18n('Themes'),
34 value: PluginType.THEME
35 }
36 ]
37 }
38
39 getPluginTypeLabel (type: PluginType) {
40 if (type === PluginType.PLUGIN) {
41 return this.i18n('plugin')
42 }
43
44 return this.i18n('theme')
45 }
46
47 getPlugins (
48 type: PluginType,
49 componentPagination: ComponentPagination,
50 sort: string
51 ) {
52 const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
53
54 let params = new HttpParams()
55 params = this.restService.addRestGetParams(params, pagination, sort)
56 params = params.append('type', type.toString())
57
58 return this.authHttp.get<ResultList<PeerTubePlugin>>(PluginApiService.BASE_APPLICATION_URL, { params })
59 .pipe(catchError(res => this.restExtractor.handleError(res)))
60 }
61
62 getPlugin (npmName: string) {
63 const path = PluginApiService.BASE_APPLICATION_URL + '/' + npmName
64
65 return this.authHttp.get<PeerTubePlugin>(path)
66 .pipe(catchError(res => this.restExtractor.handleError(res)))
67 }
68
69 getPluginRegisteredSettings (pluginName: string, pluginType: PluginType) {
70 const path = PluginApiService.BASE_APPLICATION_URL + '/' + this.nameToNpmName(pluginName, pluginType) + '/registered-settings'
71
72 return this.authHttp.get<{ settings: RegisterSettingOptions[] }>(path)
73 .pipe(catchError(res => this.restExtractor.handleError(res)))
74 }
75
76 updatePluginSettings (pluginName: string, pluginType: PluginType, settings: any) {
77 const path = PluginApiService.BASE_APPLICATION_URL + '/' + this.nameToNpmName(pluginName, pluginType) + '/settings'
78
79 return this.authHttp.put(path, { settings })
80 .pipe(catchError(res => this.restExtractor.handleError(res)))
81 }
82
83 uninstall (pluginName: string, pluginType: PluginType) {
84 const body: ManagePlugin = {
85 npmName: this.nameToNpmName(pluginName, pluginType)
86 }
87
88 return this.authHttp.post(PluginApiService.BASE_APPLICATION_URL + '/uninstall', body)
89 .pipe(catchError(res => this.restExtractor.handleError(res)))
90 }
91
92 install (npmName: string) {
93 const body: InstallPlugin = {
94 npmName
95 }
96
97 return this.authHttp.post(PluginApiService.BASE_APPLICATION_URL + '/install', body)
98 .pipe(catchError(res => this.restExtractor.handleError(res)))
99 }
100
101 nameToNpmName (name: string, type: PluginType) {
102 const prefix = type === PluginType.PLUGIN
103 ? 'peertube-plugin-'
104 : 'peertube-theme-'
105
106 return prefix + name
107 }
108
109 pluginTypeFromNpmName (npmName: string) {
110 return npmName.startsWith('peertube-plugin-')
111 ? PluginType.PLUGIN
112 : PluginType.THEME
113 }
114 }