]>
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' |
6702a1b2 | 14 | import { PeerTubePluginIndex } from '@shared/models/plugins/peertube-plugin-index.model' |
d00dc28d C |
15 | |
16 | @Injectable() | |
17 | export class PluginApiService { | |
18 | private static BASE_APPLICATION_URL = environment.apiUrl + '/api/v1/plugins' | |
19 | ||
20 | constructor ( | |
21 | private authHttp: HttpClient, | |
22 | private restExtractor: RestExtractor, | |
23 | private restService: RestService, | |
24 | private i18n: I18n | |
25 | ) { } | |
26 | ||
27 | getPluginTypeOptions () { | |
28 | return [ | |
29 | { | |
dba85a1e | 30 | label: this.i18n('Plugins'), |
d00dc28d C |
31 | value: PluginType.PLUGIN |
32 | }, | |
33 | { | |
dba85a1e | 34 | label: this.i18n('Themes'), |
d00dc28d C |
35 | value: PluginType.THEME |
36 | } | |
37 | ] | |
38 | } | |
39 | ||
dba85a1e C |
40 | getPluginTypeLabel (type: PluginType) { |
41 | if (type === PluginType.PLUGIN) { | |
42 | return this.i18n('plugin') | |
43 | } | |
44 | ||
45 | return this.i18n('theme') | |
46 | } | |
47 | ||
d00dc28d | 48 | getPlugins ( |
6702a1b2 | 49 | pluginType: PluginType, |
d00dc28d C |
50 | componentPagination: ComponentPagination, |
51 | sort: string | |
52 | ) { | |
53 | const pagination = this.restService.componentPaginationToRestPagination(componentPagination) | |
54 | ||
55 | let params = new HttpParams() | |
56 | params = this.restService.addRestGetParams(params, pagination, sort) | |
6702a1b2 | 57 | params = params.append('pluginType', pluginType.toString()) |
d00dc28d C |
58 | |
59 | return this.authHttp.get<ResultList<PeerTubePlugin>>(PluginApiService.BASE_APPLICATION_URL, { params }) | |
60 | .pipe(catchError(res => this.restExtractor.handleError(res))) | |
61 | } | |
dba85a1e | 62 | |
6702a1b2 C |
63 | searchAvailablePlugins ( |
64 | pluginType: PluginType, | |
65 | componentPagination: ComponentPagination, | |
66 | sort: string, | |
67 | search?: string | |
68 | ) { | |
69 | const pagination = this.restService.componentPaginationToRestPagination(componentPagination) | |
70 | ||
71 | let params = new HttpParams() | |
72 | params = this.restService.addRestGetParams(params, pagination, sort) | |
73 | params = params.append('pluginType', pluginType.toString()) | |
74 | ||
75 | if (search) params = params.append('search', search) | |
76 | ||
77 | return this.authHttp.get<ResultList<PeerTubePluginIndex>>(PluginApiService.BASE_APPLICATION_URL + '/available', { params }) | |
78 | .pipe(catchError(res => this.restExtractor.handleError(res))) | |
79 | } | |
80 | ||
dba85a1e C |
81 | getPlugin (npmName: string) { |
82 | const path = PluginApiService.BASE_APPLICATION_URL + '/' + npmName | |
83 | ||
84 | return this.authHttp.get<PeerTubePlugin>(path) | |
85 | .pipe(catchError(res => this.restExtractor.handleError(res))) | |
86 | } | |
87 | ||
88 | getPluginRegisteredSettings (pluginName: string, pluginType: PluginType) { | |
89 | const path = PluginApiService.BASE_APPLICATION_URL + '/' + this.nameToNpmName(pluginName, pluginType) + '/registered-settings' | |
90 | ||
91 | return this.authHttp.get<{ settings: RegisterSettingOptions[] }>(path) | |
92 | .pipe(catchError(res => this.restExtractor.handleError(res))) | |
93 | } | |
94 | ||
95 | updatePluginSettings (pluginName: string, pluginType: PluginType, settings: any) { | |
96 | const path = PluginApiService.BASE_APPLICATION_URL + '/' + this.nameToNpmName(pluginName, pluginType) + '/settings' | |
97 | ||
98 | return this.authHttp.put(path, { settings }) | |
99 | .pipe(catchError(res => this.restExtractor.handleError(res))) | |
100 | } | |
101 | ||
102 | uninstall (pluginName: string, pluginType: PluginType) { | |
103 | const body: ManagePlugin = { | |
104 | npmName: this.nameToNpmName(pluginName, pluginType) | |
105 | } | |
106 | ||
107 | return this.authHttp.post(PluginApiService.BASE_APPLICATION_URL + '/uninstall', body) | |
108 | .pipe(catchError(res => this.restExtractor.handleError(res))) | |
109 | } | |
110 | ||
b5f919ac C |
111 | update (pluginName: string, pluginType: PluginType) { |
112 | const body: ManagePlugin = { | |
113 | npmName: this.nameToNpmName(pluginName, pluginType) | |
114 | } | |
115 | ||
116 | return this.authHttp.post(PluginApiService.BASE_APPLICATION_URL + '/update', body) | |
117 | .pipe(catchError(res => this.restExtractor.handleError(res))) | |
118 | } | |
119 | ||
dba85a1e | 120 | install (npmName: string) { |
b5f919ac | 121 | const body: InstallOrUpdatePlugin = { |
dba85a1e C |
122 | npmName |
123 | } | |
124 | ||
125 | return this.authHttp.post(PluginApiService.BASE_APPLICATION_URL + '/install', body) | |
126 | .pipe(catchError(res => this.restExtractor.handleError(res))) | |
127 | } | |
128 | ||
129 | nameToNpmName (name: string, type: PluginType) { | |
130 | const prefix = type === PluginType.PLUGIN | |
131 | ? 'peertube-plugin-' | |
132 | : 'peertube-theme-' | |
133 | ||
134 | return prefix + name | |
135 | } | |
136 | ||
137 | pluginTypeFromNpmName (npmName: string) { | |
138 | return npmName.startsWith('peertube-plugin-') | |
139 | ? PluginType.PLUGIN | |
140 | : PluginType.THEME | |
141 | } | |
d00dc28d | 142 | } |