]>
Commit | Line | Data |
---|---|---|
67ed6552 | 1 | import { Observable } from 'rxjs' |
d75db01f | 2 | import { catchError, map, switchMap } from 'rxjs/operators' |
d00dc28d C |
3 | import { HttpClient, HttpParams } from '@angular/common/http' |
4 | import { Injectable } from '@angular/core' | |
67ed6552 | 5 | import { ComponentPagination, RestExtractor, RestService } from '@app/core' |
23bdacf8 | 6 | import { PluginService } from '@app/core/plugins/plugin.service' |
67ed6552 | 7 | import { I18n } from '@ngx-translate/i18n-polyfill' |
bd45d503 | 8 | import { peertubeTranslate } from '@shared/core-utils/i18n' |
67ed6552 C |
9 | import { |
10 | InstallOrUpdatePlugin, | |
11 | ManagePlugin, | |
12 | PeerTubePlugin, | |
13 | PeerTubePluginIndex, | |
67ed6552 C |
14 | PluginType, |
15 | RegisteredServerSettings, | |
16 | ResultList | |
17 | } from '@shared/models' | |
18 | import { environment } from '../../../../environments/environment' | |
d00dc28d C |
19 | |
20 | @Injectable() | |
21 | export class PluginApiService { | |
23bdacf8 | 22 | private static BASE_PLUGIN_URL = environment.apiUrl + '/api/v1/plugins' |
d00dc28d C |
23 | |
24 | constructor ( | |
25 | private authHttp: HttpClient, | |
26 | private restExtractor: RestExtractor, | |
27 | private restService: RestService, | |
23bdacf8 C |
28 | private i18n: I18n, |
29 | private pluginService: PluginService | |
d00dc28d C |
30 | ) { } |
31 | ||
32 | getPluginTypeOptions () { | |
33 | return [ | |
34 | { | |
dba85a1e | 35 | label: this.i18n('Plugins'), |
d00dc28d C |
36 | value: PluginType.PLUGIN |
37 | }, | |
38 | { | |
dba85a1e | 39 | label: this.i18n('Themes'), |
d00dc28d C |
40 | value: PluginType.THEME |
41 | } | |
42 | ] | |
43 | } | |
44 | ||
dba85a1e C |
45 | getPluginTypeLabel (type: PluginType) { |
46 | if (type === PluginType.PLUGIN) { | |
47 | return this.i18n('plugin') | |
48 | } | |
49 | ||
50 | return this.i18n('theme') | |
51 | } | |
52 | ||
d00dc28d | 53 | getPlugins ( |
6702a1b2 | 54 | pluginType: PluginType, |
d00dc28d C |
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) | |
6702a1b2 | 62 | params = params.append('pluginType', pluginType.toString()) |
d00dc28d | 63 | |
23bdacf8 | 64 | return this.authHttp.get<ResultList<PeerTubePlugin>>(PluginApiService.BASE_PLUGIN_URL, { params }) |
d00dc28d C |
65 | .pipe(catchError(res => this.restExtractor.handleError(res))) |
66 | } | |
dba85a1e | 67 | |
6702a1b2 C |
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 | ||
23bdacf8 | 82 | return this.authHttp.get<ResultList<PeerTubePluginIndex>>(PluginApiService.BASE_PLUGIN_URL + '/available', { params }) |
6702a1b2 C |
83 | .pipe(catchError(res => this.restExtractor.handleError(res))) |
84 | } | |
85 | ||
dba85a1e | 86 | getPlugin (npmName: string) { |
23bdacf8 | 87 | const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName |
dba85a1e C |
88 | |
89 | return this.authHttp.get<PeerTubePlugin>(path) | |
90 | .pipe(catchError(res => this.restExtractor.handleError(res))) | |
91 | } | |
92 | ||
93 | getPluginRegisteredSettings (pluginName: string, pluginType: PluginType) { | |
23bdacf8 C |
94 | const npmName = this.pluginService.nameToNpmName(pluginName, pluginType) |
95 | const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName + '/registered-settings' | |
dba85a1e | 96 | |
ba211e73 | 97 | return this.authHttp.get<RegisteredServerSettings>(path) |
d75db01f C |
98 | .pipe( |
99 | switchMap(res => this.translateSettingsLabel(npmName, res)), | |
100 | catchError(res => this.restExtractor.handleError(res)) | |
101 | ) | |
dba85a1e C |
102 | } |
103 | ||
104 | updatePluginSettings (pluginName: string, pluginType: PluginType, settings: any) { | |
23bdacf8 C |
105 | const npmName = this.pluginService.nameToNpmName(pluginName, pluginType) |
106 | const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName + '/settings' | |
dba85a1e C |
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 = { | |
23bdacf8 | 114 | npmName: this.pluginService.nameToNpmName(pluginName, pluginType) |
dba85a1e C |
115 | } |
116 | ||
23bdacf8 | 117 | return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/uninstall', body) |
dba85a1e C |
118 | .pipe(catchError(res => this.restExtractor.handleError(res))) |
119 | } | |
120 | ||
b5f919ac C |
121 | update (pluginName: string, pluginType: PluginType) { |
122 | const body: ManagePlugin = { | |
23bdacf8 | 123 | npmName: this.pluginService.nameToNpmName(pluginName, pluginType) |
b5f919ac C |
124 | } |
125 | ||
23bdacf8 | 126 | return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/update', body) |
b5f919ac C |
127 | .pipe(catchError(res => this.restExtractor.handleError(res))) |
128 | } | |
129 | ||
dba85a1e | 130 | install (npmName: string) { |
b5f919ac | 131 | const body: InstallOrUpdatePlugin = { |
dba85a1e C |
132 | npmName |
133 | } | |
134 | ||
23bdacf8 | 135 | return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/install', body) |
dba85a1e C |
136 | .pipe(catchError(res => this.restExtractor.handleError(res))) |
137 | } | |
d75db01f C |
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 | } | |
d00dc28d | 153 | } |