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