]>
Commit | Line | Data |
---|---|---|
1 | import { catchError } from 'rxjs/operators' | |
2 | import { HttpClient, HttpParams } from '@angular/common/http' | |
3 | import { Injectable } from '@angular/core' | |
4 | import { ComponentPagination, RestExtractor, RestService } from '@app/core' | |
5 | import { PluginService } from '@app/core/plugins/plugin.service' | |
6 | import { | |
7 | InstallOrUpdatePlugin, | |
8 | ManagePlugin, | |
9 | PeerTubePlugin, | |
10 | PeerTubePluginIndex, | |
11 | PluginType, | |
12 | RegisteredServerSettings, | |
13 | ResultList | |
14 | } from '@shared/models' | |
15 | import { environment } from '../../../../environments/environment' | |
16 | ||
17 | @Injectable() | |
18 | export class PluginApiService { | |
19 | private static BASE_PLUGIN_URL = environment.apiUrl + '/api/v1/plugins' | |
20 | ||
21 | constructor ( | |
22 | private authHttp: HttpClient, | |
23 | private restExtractor: RestExtractor, | |
24 | private restService: RestService, | |
25 | private pluginService: PluginService | |
26 | ) { } | |
27 | ||
28 | getPluginTypeLabel (type: PluginType) { | |
29 | if (type === PluginType.PLUGIN) { | |
30 | return $localize`plugin` | |
31 | } | |
32 | ||
33 | return $localize`theme` | |
34 | } | |
35 | ||
36 | getPlugins ( | |
37 | pluginType: PluginType, | |
38 | componentPagination: ComponentPagination, | |
39 | sort: string | |
40 | ) { | |
41 | const pagination = this.restService.componentToRestPagination(componentPagination) | |
42 | ||
43 | let params = new HttpParams() | |
44 | params = this.restService.addRestGetParams(params, pagination, sort) | |
45 | params = params.append('pluginType', pluginType.toString()) | |
46 | ||
47 | return this.authHttp.get<ResultList<PeerTubePlugin>>(PluginApiService.BASE_PLUGIN_URL, { params }) | |
48 | .pipe(catchError(res => this.restExtractor.handleError(res))) | |
49 | } | |
50 | ||
51 | searchAvailablePlugins ( | |
52 | pluginType: PluginType, | |
53 | componentPagination: ComponentPagination, | |
54 | sort: string, | |
55 | search?: string | |
56 | ) { | |
57 | const pagination = this.restService.componentToRestPagination(componentPagination) | |
58 | ||
59 | let params = new HttpParams() | |
60 | params = this.restService.addRestGetParams(params, pagination, sort) | |
61 | params = params.append('pluginType', pluginType.toString()) | |
62 | ||
63 | if (search) params = params.append('search', search) | |
64 | ||
65 | return this.authHttp.get<ResultList<PeerTubePluginIndex>>(PluginApiService.BASE_PLUGIN_URL + '/available', { params }) | |
66 | .pipe(catchError(res => this.restExtractor.handleError(res))) | |
67 | } | |
68 | ||
69 | getPlugin (npmName: string) { | |
70 | const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName | |
71 | ||
72 | return this.authHttp.get<PeerTubePlugin>(path) | |
73 | .pipe(catchError(res => this.restExtractor.handleError(res))) | |
74 | } | |
75 | ||
76 | getPluginRegisteredSettings (pluginName: string, pluginType: PluginType) { | |
77 | const npmName = this.pluginService.nameToNpmName(pluginName, pluginType) | |
78 | const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName + '/registered-settings' | |
79 | ||
80 | return this.authHttp.get<RegisteredServerSettings>(path) | |
81 | .pipe( | |
82 | catchError(res => this.restExtractor.handleError(res)) | |
83 | ) | |
84 | } | |
85 | ||
86 | updatePluginSettings (pluginName: string, pluginType: PluginType, settings: any) { | |
87 | const npmName = this.pluginService.nameToNpmName(pluginName, pluginType) | |
88 | const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName + '/settings' | |
89 | ||
90 | return this.authHttp.put(path, { settings }) | |
91 | .pipe(catchError(res => this.restExtractor.handleError(res))) | |
92 | } | |
93 | ||
94 | uninstall (pluginName: string, pluginType: PluginType) { | |
95 | const body: ManagePlugin = { | |
96 | npmName: this.pluginService.nameToNpmName(pluginName, pluginType) | |
97 | } | |
98 | ||
99 | return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/uninstall', body) | |
100 | .pipe(catchError(res => this.restExtractor.handleError(res))) | |
101 | } | |
102 | ||
103 | update (pluginName: string, pluginType: PluginType) { | |
104 | const body: ManagePlugin = { | |
105 | npmName: this.pluginService.nameToNpmName(pluginName, pluginType) | |
106 | } | |
107 | ||
108 | return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/update', body) | |
109 | .pipe(catchError(res => this.restExtractor.handleError(res))) | |
110 | } | |
111 | ||
112 | install (npmName: string) { | |
113 | const body: InstallOrUpdatePlugin = { | |
114 | npmName | |
115 | } | |
116 | ||
117 | return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/install', body) | |
118 | .pipe(catchError(res => this.restExtractor.handleError(res))) | |
119 | } | |
120 | ||
121 | getPluginOrThemeHref (type: PluginType, name: string) { | |
122 | const typeString = type === PluginType.PLUGIN | |
123 | ? 'plugin' | |
124 | : 'theme' | |
125 | ||
126 | return `https://www.npmjs.com/package/peertube-${typeString}-${name}` | |
127 | } | |
128 | } |