]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/plugins/shared/plugin-api.service.ts
Remove useless components
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / plugins / shared / plugin-api.service.ts
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 getPluginTypeOptions () {
29 return [
30 {
31 label: $localize`Plugins`,
32 value: PluginType.PLUGIN
33 },
34 {
35 label: $localize`Themes`,
36 value: PluginType.THEME
37 }
38 ]
39 }
40
41 getPluginTypeLabel (type: PluginType) {
42 if (type === PluginType.PLUGIN) {
43 return $localize`plugin`
44 }
45
46 return $localize`theme`
47 }
48
49 getPlugins (
50 pluginType: PluginType,
51 componentPagination: ComponentPagination,
52 sort: string
53 ) {
54 const pagination = this.restService.componentToRestPagination(componentPagination)
55
56 let params = new HttpParams()
57 params = this.restService.addRestGetParams(params, pagination, sort)
58 params = params.append('pluginType', pluginType.toString())
59
60 return this.authHttp.get<ResultList<PeerTubePlugin>>(PluginApiService.BASE_PLUGIN_URL, { params })
61 .pipe(catchError(res => this.restExtractor.handleError(res)))
62 }
63
64 searchAvailablePlugins (
65 pluginType: PluginType,
66 componentPagination: ComponentPagination,
67 sort: string,
68 search?: string
69 ) {
70 const pagination = this.restService.componentToRestPagination(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
78 return this.authHttp.get<ResultList<PeerTubePluginIndex>>(PluginApiService.BASE_PLUGIN_URL + '/available', { params })
79 .pipe(catchError(res => this.restExtractor.handleError(res)))
80 }
81
82 getPlugin (npmName: string) {
83 const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName
84
85 return this.authHttp.get<PeerTubePlugin>(path)
86 .pipe(catchError(res => this.restExtractor.handleError(res)))
87 }
88
89 getPluginRegisteredSettings (pluginName: string, pluginType: PluginType) {
90 const npmName = this.pluginService.nameToNpmName(pluginName, pluginType)
91 const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName + '/registered-settings'
92
93 return this.authHttp.get<RegisteredServerSettings>(path)
94 .pipe(
95 catchError(res => this.restExtractor.handleError(res))
96 )
97 }
98
99 updatePluginSettings (pluginName: string, pluginType: PluginType, settings: any) {
100 const npmName = this.pluginService.nameToNpmName(pluginName, pluginType)
101 const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName + '/settings'
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 = {
109 npmName: this.pluginService.nameToNpmName(pluginName, pluginType)
110 }
111
112 return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/uninstall', body)
113 .pipe(catchError(res => this.restExtractor.handleError(res)))
114 }
115
116 update (pluginName: string, pluginType: PluginType) {
117 const body: ManagePlugin = {
118 npmName: this.pluginService.nameToNpmName(pluginName, pluginType)
119 }
120
121 return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/update', body)
122 .pipe(catchError(res => this.restExtractor.handleError(res)))
123 }
124
125 install (npmName: string) {
126 const body: InstallOrUpdatePlugin = {
127 npmName
128 }
129
130 return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/install', body)
131 .pipe(catchError(res => this.restExtractor.handleError(res)))
132 }
133
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 }
141 }