]>
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 | ||
28 | getPluginTypeOptions () { | |
29 | return [ | |
30 | { | |
66357162 | 31 | label: $localize`Plugins`, |
d00dc28d C |
32 | value: PluginType.PLUGIN |
33 | }, | |
34 | { | |
66357162 | 35 | label: $localize`Themes`, |
d00dc28d C |
36 | value: PluginType.THEME |
37 | } | |
38 | ] | |
39 | } | |
40 | ||
dba85a1e C |
41 | getPluginTypeLabel (type: PluginType) { |
42 | if (type === PluginType.PLUGIN) { | |
66357162 | 43 | return $localize`plugin` |
dba85a1e C |
44 | } |
45 | ||
66357162 | 46 | return $localize`theme` |
dba85a1e C |
47 | } |
48 | ||
d00dc28d | 49 | getPlugins ( |
6702a1b2 | 50 | pluginType: PluginType, |
d00dc28d C |
51 | componentPagination: ComponentPagination, |
52 | sort: string | |
53 | ) { | |
54 | const pagination = this.restService.componentPaginationToRestPagination(componentPagination) | |
55 | ||
56 | let params = new HttpParams() | |
57 | params = this.restService.addRestGetParams(params, pagination, sort) | |
6702a1b2 | 58 | params = params.append('pluginType', pluginType.toString()) |
d00dc28d | 59 | |
23bdacf8 | 60 | return this.authHttp.get<ResultList<PeerTubePlugin>>(PluginApiService.BASE_PLUGIN_URL, { params }) |
d00dc28d C |
61 | .pipe(catchError(res => this.restExtractor.handleError(res))) |
62 | } | |
dba85a1e | 63 | |
6702a1b2 C |
64 | searchAvailablePlugins ( |
65 | pluginType: PluginType, | |
66 | componentPagination: ComponentPagination, | |
67 | sort: string, | |
68 | search?: string | |
69 | ) { | |
70 | const pagination = this.restService.componentPaginationToRestPagination(componentPagination) | |
71 | ||
72 | let params = new HttpParams() | |
73 | params = this.restService.addRestGetParams(params, pagination, sort) | |
74 | params = params.append('pluginType', pluginType.toString()) | |
75 | ||
76 | if (search) params = params.append('search', search) | |
77 | ||
23bdacf8 | 78 | return this.authHttp.get<ResultList<PeerTubePluginIndex>>(PluginApiService.BASE_PLUGIN_URL + '/available', { params }) |
6702a1b2 C |
79 | .pipe(catchError(res => this.restExtractor.handleError(res))) |
80 | } | |
81 | ||
dba85a1e | 82 | getPlugin (npmName: string) { |
23bdacf8 | 83 | const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName |
dba85a1e C |
84 | |
85 | return this.authHttp.get<PeerTubePlugin>(path) | |
86 | .pipe(catchError(res => this.restExtractor.handleError(res))) | |
87 | } | |
88 | ||
89 | getPluginRegisteredSettings (pluginName: string, pluginType: PluginType) { | |
23bdacf8 C |
90 | const npmName = this.pluginService.nameToNpmName(pluginName, pluginType) |
91 | const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName + '/registered-settings' | |
dba85a1e | 92 | |
ba211e73 | 93 | return this.authHttp.get<RegisteredServerSettings>(path) |
d75db01f | 94 | .pipe( |
d75db01f C |
95 | catchError(res => this.restExtractor.handleError(res)) |
96 | ) | |
dba85a1e C |
97 | } |
98 | ||
99 | updatePluginSettings (pluginName: string, pluginType: PluginType, settings: any) { | |
23bdacf8 C |
100 | const npmName = this.pluginService.nameToNpmName(pluginName, pluginType) |
101 | const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName + '/settings' | |
dba85a1e C |
102 | |
103 | return this.authHttp.put(path, { settings }) | |
104 | .pipe(catchError(res => this.restExtractor.handleError(res))) | |
105 | } | |
106 | ||
107 | uninstall (pluginName: string, pluginType: PluginType) { | |
108 | const body: ManagePlugin = { | |
23bdacf8 | 109 | npmName: this.pluginService.nameToNpmName(pluginName, pluginType) |
dba85a1e C |
110 | } |
111 | ||
23bdacf8 | 112 | return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/uninstall', body) |
dba85a1e C |
113 | .pipe(catchError(res => this.restExtractor.handleError(res))) |
114 | } | |
115 | ||
b5f919ac C |
116 | update (pluginName: string, pluginType: PluginType) { |
117 | const body: ManagePlugin = { | |
23bdacf8 | 118 | npmName: this.pluginService.nameToNpmName(pluginName, pluginType) |
b5f919ac C |
119 | } |
120 | ||
23bdacf8 | 121 | return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/update', body) |
b5f919ac C |
122 | .pipe(catchError(res => this.restExtractor.handleError(res))) |
123 | } | |
124 | ||
dba85a1e | 125 | install (npmName: string) { |
b5f919ac | 126 | const body: InstallOrUpdatePlugin = { |
dba85a1e C |
127 | npmName |
128 | } | |
129 | ||
23bdacf8 | 130 | return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/install', body) |
dba85a1e C |
131 | .pipe(catchError(res => this.restExtractor.handleError(res))) |
132 | } | |
d75db01f | 133 | |
078b4716 C |
134 | getPluginOrThemeHref (type: PluginType, name: string) { |
135 | const typeString = type === PluginType.PLUGIN | |
136 | ? 'plugin' | |
137 | : 'theme' | |
138 | ||
139 | return `https://www.npmjs.com/package/peertube-${typeString}-${name}` | |
140 | } | |
d00dc28d | 141 | } |