]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/plugins/shared/plugin-api.service.ts
Merge branch 'release/3.1.0' into develop
[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 { peertubeTranslate } from '@shared/core-utils/i18n'
8 import {
9 InstallOrUpdatePlugin,
10 ManagePlugin,
11 PeerTubePlugin,
12 PeerTubePluginIndex,
13 PluginType,
14 RegisteredServerSettings,
15 ResultList
16 } from '@shared/models'
17 import { environment } from '../../../../environments/environment'
18
19 @Injectable()
20 export class PluginApiService {
21 private static BASE_PLUGIN_URL = environment.apiUrl + '/api/v1/plugins'
22
23 constructor (
24 private authHttp: HttpClient,
25 private restExtractor: RestExtractor,
26 private restService: RestService,
27 private pluginService: PluginService
28 ) { }
29
30 getPluginTypeOptions () {
31 return [
32 {
33 label: $localize`Plugins`,
34 value: PluginType.PLUGIN
35 },
36 {
37 label: $localize`Themes`,
38 value: PluginType.THEME
39 }
40 ]
41 }
42
43 getPluginTypeLabel (type: PluginType) {
44 if (type === PluginType.PLUGIN) {
45 return $localize`plugin`
46 }
47
48 return $localize`theme`
49 }
50
51 getPlugins (
52 pluginType: PluginType,
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)
60 params = params.append('pluginType', pluginType.toString())
61
62 return this.authHttp.get<ResultList<PeerTubePlugin>>(PluginApiService.BASE_PLUGIN_URL, { params })
63 .pipe(catchError(res => this.restExtractor.handleError(res)))
64 }
65
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
80 return this.authHttp.get<ResultList<PeerTubePluginIndex>>(PluginApiService.BASE_PLUGIN_URL + '/available', { params })
81 .pipe(catchError(res => this.restExtractor.handleError(res)))
82 }
83
84 getPlugin (npmName: string) {
85 const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName
86
87 return this.authHttp.get<PeerTubePlugin>(path)
88 .pipe(catchError(res => this.restExtractor.handleError(res)))
89 }
90
91 getPluginRegisteredSettings (pluginName: string, pluginType: PluginType) {
92 const npmName = this.pluginService.nameToNpmName(pluginName, pluginType)
93 const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName + '/registered-settings'
94
95 return this.authHttp.get<RegisteredServerSettings>(path)
96 .pipe(
97 switchMap(res => this.translateSettingsLabel(npmName, res)),
98 catchError(res => this.restExtractor.handleError(res))
99 )
100 }
101
102 updatePluginSettings (pluginName: string, pluginType: PluginType, settings: any) {
103 const npmName = this.pluginService.nameToNpmName(pluginName, pluginType)
104 const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName + '/settings'
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 = {
112 npmName: this.pluginService.nameToNpmName(pluginName, pluginType)
113 }
114
115 return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/uninstall', body)
116 .pipe(catchError(res => this.restExtractor.handleError(res)))
117 }
118
119 update (pluginName: string, pluginType: PluginType) {
120 const body: ManagePlugin = {
121 npmName: this.pluginService.nameToNpmName(pluginName, pluginType)
122 }
123
124 return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/update', body)
125 .pipe(catchError(res => this.restExtractor.handleError(res)))
126 }
127
128 install (npmName: string) {
129 const body: InstallOrUpdatePlugin = {
130 npmName
131 }
132
133 return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/install', body)
134 .pipe(catchError(res => this.restExtractor.handleError(res)))
135 }
136
137 getPluginOrThemeHref (type: PluginType, name: string) {
138 const typeString = type === PluginType.PLUGIN
139 ? 'plugin'
140 : 'theme'
141
142 return `https://www.npmjs.com/package/peertube-${typeString}-${name}`
143 }
144
145 private translateSettingsLabel (npmName: string, res: RegisteredServerSettings): Observable<RegisteredServerSettings> {
146 return this.pluginService.translationsObservable
147 .pipe(
148 map(allTranslations => allTranslations[npmName]),
149 map(translations => {
150 const registeredSettings = res.registeredSettings
151 .map(r => {
152 return Object.assign({}, r, { label: peertubeTranslate(r.label, translations) })
153 })
154
155 return { registeredSettings }
156 })
157 )
158 }
159 }