]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/plugins/shared/plugin-api.service.ts
Reorganize shared models
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / plugins / shared / plugin-api.service.ts
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 { I18n } from '@ngx-translate/i18n-polyfill'
8 import { peertubeTranslate } from '@shared/core-utils/i18n'
9 import {
10 InstallOrUpdatePlugin,
11 ManagePlugin,
12 PeerTubePlugin,
13 PeerTubePluginIndex,
14 PluginType,
15 RegisteredServerSettings,
16 ResultList
17 } from '@shared/models'
18 import { environment } from '../../../../environments/environment'
19
20 @Injectable()
21 export class PluginApiService {
22 private static BASE_PLUGIN_URL = environment.apiUrl + '/api/v1/plugins'
23
24 constructor (
25 private authHttp: HttpClient,
26 private restExtractor: RestExtractor,
27 private restService: RestService,
28 private i18n: I18n,
29 private pluginService: PluginService
30 ) { }
31
32 getPluginTypeOptions () {
33 return [
34 {
35 label: this.i18n('Plugins'),
36 value: PluginType.PLUGIN
37 },
38 {
39 label: this.i18n('Themes'),
40 value: PluginType.THEME
41 }
42 ]
43 }
44
45 getPluginTypeLabel (type: PluginType) {
46 if (type === PluginType.PLUGIN) {
47 return this.i18n('plugin')
48 }
49
50 return this.i18n('theme')
51 }
52
53 getPlugins (
54 pluginType: PluginType,
55 componentPagination: ComponentPagination,
56 sort: string
57 ) {
58 const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
59
60 let params = new HttpParams()
61 params = this.restService.addRestGetParams(params, pagination, sort)
62 params = params.append('pluginType', pluginType.toString())
63
64 return this.authHttp.get<ResultList<PeerTubePlugin>>(PluginApiService.BASE_PLUGIN_URL, { params })
65 .pipe(catchError(res => this.restExtractor.handleError(res)))
66 }
67
68 searchAvailablePlugins (
69 pluginType: PluginType,
70 componentPagination: ComponentPagination,
71 sort: string,
72 search?: string
73 ) {
74 const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
75
76 let params = new HttpParams()
77 params = this.restService.addRestGetParams(params, pagination, sort)
78 params = params.append('pluginType', pluginType.toString())
79
80 if (search) params = params.append('search', search)
81
82 return this.authHttp.get<ResultList<PeerTubePluginIndex>>(PluginApiService.BASE_PLUGIN_URL + '/available', { params })
83 .pipe(catchError(res => this.restExtractor.handleError(res)))
84 }
85
86 getPlugin (npmName: string) {
87 const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName
88
89 return this.authHttp.get<PeerTubePlugin>(path)
90 .pipe(catchError(res => this.restExtractor.handleError(res)))
91 }
92
93 getPluginRegisteredSettings (pluginName: string, pluginType: PluginType) {
94 const npmName = this.pluginService.nameToNpmName(pluginName, pluginType)
95 const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName + '/registered-settings'
96
97 return this.authHttp.get<RegisteredServerSettings>(path)
98 .pipe(
99 switchMap(res => this.translateSettingsLabel(npmName, res)),
100 catchError(res => this.restExtractor.handleError(res))
101 )
102 }
103
104 updatePluginSettings (pluginName: string, pluginType: PluginType, settings: any) {
105 const npmName = this.pluginService.nameToNpmName(pluginName, pluginType)
106 const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName + '/settings'
107
108 return this.authHttp.put(path, { settings })
109 .pipe(catchError(res => this.restExtractor.handleError(res)))
110 }
111
112 uninstall (pluginName: string, pluginType: PluginType) {
113 const body: ManagePlugin = {
114 npmName: this.pluginService.nameToNpmName(pluginName, pluginType)
115 }
116
117 return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/uninstall', body)
118 .pipe(catchError(res => this.restExtractor.handleError(res)))
119 }
120
121 update (pluginName: string, pluginType: PluginType) {
122 const body: ManagePlugin = {
123 npmName: this.pluginService.nameToNpmName(pluginName, pluginType)
124 }
125
126 return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/update', body)
127 .pipe(catchError(res => this.restExtractor.handleError(res)))
128 }
129
130 install (npmName: string) {
131 const body: InstallOrUpdatePlugin = {
132 npmName
133 }
134
135 return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/install', body)
136 .pipe(catchError(res => this.restExtractor.handleError(res)))
137 }
138
139 private translateSettingsLabel (npmName: string, res: RegisteredServerSettings): Observable<RegisteredServerSettings> {
140 return this.pluginService.translationsObservable
141 .pipe(
142 map(allTranslations => allTranslations[npmName]),
143 map(translations => {
144 const registeredSettings = res.registeredSettings
145 .map(r => {
146 return Object.assign({}, r, { label: peertubeTranslate(r.label, translations) })
147 })
148
149 return { registeredSettings }
150 })
151 )
152 }
153 }