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