]>
Commit | Line | Data |
---|---|---|
d00dc28d C |
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' | |
dba85a1e | 11 | import { ManagePlugin } from '@shared/models/plugins/manage-plugin.model' |
b5f919ac | 12 | import { InstallOrUpdatePlugin } from '@shared/models/plugins/install-plugin.model' |
dba85a1e | 13 | import { RegisterSettingOptions } from '@shared/models/plugins/register-setting.model' |
d00dc28d C |
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 | { | |
dba85a1e | 29 | label: this.i18n('Plugins'), |
d00dc28d C |
30 | value: PluginType.PLUGIN |
31 | }, | |
32 | { | |
dba85a1e | 33 | label: this.i18n('Themes'), |
d00dc28d C |
34 | value: PluginType.THEME |
35 | } | |
36 | ] | |
37 | } | |
38 | ||
dba85a1e C |
39 | getPluginTypeLabel (type: PluginType) { |
40 | if (type === PluginType.PLUGIN) { | |
41 | return this.i18n('plugin') | |
42 | } | |
43 | ||
44 | return this.i18n('theme') | |
45 | } | |
46 | ||
d00dc28d C |
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 | } | |
dba85a1e C |
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 | ||
b5f919ac C |
92 | update (pluginName: string, pluginType: PluginType) { |
93 | const body: ManagePlugin = { | |
94 | npmName: this.nameToNpmName(pluginName, pluginType) | |
95 | } | |
96 | ||
97 | return this.authHttp.post(PluginApiService.BASE_APPLICATION_URL + '/update', body) | |
98 | .pipe(catchError(res => this.restExtractor.handleError(res))) | |
99 | } | |
100 | ||
dba85a1e | 101 | install (npmName: string) { |
b5f919ac | 102 | const body: InstallOrUpdatePlugin = { |
dba85a1e C |
103 | npmName |
104 | } | |
105 | ||
106 | return this.authHttp.post(PluginApiService.BASE_APPLICATION_URL + '/install', body) | |
107 | .pipe(catchError(res => this.restExtractor.handleError(res))) | |
108 | } | |
109 | ||
110 | nameToNpmName (name: string, type: PluginType) { | |
111 | const prefix = type === PluginType.PLUGIN | |
112 | ? 'peertube-plugin-' | |
113 | : 'peertube-theme-' | |
114 | ||
115 | return prefix + name | |
116 | } | |
117 | ||
118 | pluginTypeFromNpmName (npmName: string) { | |
119 | return npmName.startsWith('peertube-plugin-') | |
120 | ? PluginType.PLUGIN | |
121 | : PluginType.THEME | |
122 | } | |
d00dc28d | 123 | } |