]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+admin/plugins/shared/plugin-api.service.ts
Move to sass @use
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / plugins / shared / plugin-api.service.ts
CommitLineData
67ed6552 1import { Observable } from 'rxjs'
d75db01f 2import { catchError, map, switchMap } from 'rxjs/operators'
d00dc28d
C
3import { HttpClient, HttpParams } from '@angular/common/http'
4import { Injectable } from '@angular/core'
67ed6552 5import { ComponentPagination, RestExtractor, RestService } from '@app/core'
23bdacf8 6import { PluginService } from '@app/core/plugins/plugin.service'
bd45d503 7import { peertubeTranslate } from '@shared/core-utils/i18n'
67ed6552
C
8import {
9 InstallOrUpdatePlugin,
10 ManagePlugin,
11 PeerTubePlugin,
12 PeerTubePluginIndex,
67ed6552
C
13 PluginType,
14 RegisteredServerSettings,
15 ResultList
16} from '@shared/models'
17import { environment } from '../../../../environments/environment'
d00dc28d
C
18
19@Injectable()
20export class PluginApiService {
23bdacf8 21 private static BASE_PLUGIN_URL = environment.apiUrl + '/api/v1/plugins'
d00dc28d
C
22
23 constructor (
24 private authHttp: HttpClient,
25 private restExtractor: RestExtractor,
26 private restService: RestService,
23bdacf8 27 private pluginService: PluginService
d00dc28d
C
28 ) { }
29
30 getPluginTypeOptions () {
31 return [
32 {
66357162 33 label: $localize`Plugins`,
d00dc28d
C
34 value: PluginType.PLUGIN
35 },
36 {
66357162 37 label: $localize`Themes`,
d00dc28d
C
38 value: PluginType.THEME
39 }
40 ]
41 }
42
dba85a1e
C
43 getPluginTypeLabel (type: PluginType) {
44 if (type === PluginType.PLUGIN) {
66357162 45 return $localize`plugin`
dba85a1e
C
46 }
47
66357162 48 return $localize`theme`
dba85a1e
C
49 }
50
d00dc28d 51 getPlugins (
6702a1b2 52 pluginType: PluginType,
d00dc28d
C
53 componentPagination: ComponentPagination,
54 sort: string
55 ) {
56 const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
57
58 let params = new HttpParams()
59 params = this.restService.addRestGetParams(params, pagination, sort)
6702a1b2 60 params = params.append('pluginType', pluginType.toString())
d00dc28d 61
23bdacf8 62 return this.authHttp.get<ResultList<PeerTubePlugin>>(PluginApiService.BASE_PLUGIN_URL, { params })
d00dc28d
C
63 .pipe(catchError(res => this.restExtractor.handleError(res)))
64 }
dba85a1e 65
6702a1b2
C
66 searchAvailablePlugins (
67 pluginType: PluginType,
68 componentPagination: ComponentPagination,
69 sort: string,
70 search?: string
71 ) {
72 const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
73
74 let params = new HttpParams()
75 params = this.restService.addRestGetParams(params, pagination, sort)
76 params = params.append('pluginType', pluginType.toString())
77
78 if (search) params = params.append('search', search)
79
23bdacf8 80 return this.authHttp.get<ResultList<PeerTubePluginIndex>>(PluginApiService.BASE_PLUGIN_URL + '/available', { params })
6702a1b2
C
81 .pipe(catchError(res => this.restExtractor.handleError(res)))
82 }
83
dba85a1e 84 getPlugin (npmName: string) {
23bdacf8 85 const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName
dba85a1e
C
86
87 return this.authHttp.get<PeerTubePlugin>(path)
88 .pipe(catchError(res => this.restExtractor.handleError(res)))
89 }
90
91 getPluginRegisteredSettings (pluginName: string, pluginType: PluginType) {
23bdacf8
C
92 const npmName = this.pluginService.nameToNpmName(pluginName, pluginType)
93 const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName + '/registered-settings'
dba85a1e 94
ba211e73 95 return this.authHttp.get<RegisteredServerSettings>(path)
d75db01f 96 .pipe(
d75db01f
C
97 catchError(res => this.restExtractor.handleError(res))
98 )
dba85a1e
C
99 }
100
101 updatePluginSettings (pluginName: string, pluginType: PluginType, settings: any) {
23bdacf8
C
102 const npmName = this.pluginService.nameToNpmName(pluginName, pluginType)
103 const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName + '/settings'
dba85a1e
C
104
105 return this.authHttp.put(path, { settings })
106 .pipe(catchError(res => this.restExtractor.handleError(res)))
107 }
108
109 uninstall (pluginName: string, pluginType: PluginType) {
110 const body: ManagePlugin = {
23bdacf8 111 npmName: this.pluginService.nameToNpmName(pluginName, pluginType)
dba85a1e
C
112 }
113
23bdacf8 114 return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/uninstall', body)
dba85a1e
C
115 .pipe(catchError(res => this.restExtractor.handleError(res)))
116 }
117
b5f919ac
C
118 update (pluginName: string, pluginType: PluginType) {
119 const body: ManagePlugin = {
23bdacf8 120 npmName: this.pluginService.nameToNpmName(pluginName, pluginType)
b5f919ac
C
121 }
122
23bdacf8 123 return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/update', body)
b5f919ac
C
124 .pipe(catchError(res => this.restExtractor.handleError(res)))
125 }
126
dba85a1e 127 install (npmName: string) {
b5f919ac 128 const body: InstallOrUpdatePlugin = {
dba85a1e
C
129 npmName
130 }
131
23bdacf8 132 return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/install', body)
dba85a1e
C
133 .pipe(catchError(res => this.restExtractor.handleError(res)))
134 }
d75db01f 135
078b4716
C
136 getPluginOrThemeHref (type: PluginType, name: string) {
137 const typeString = type === PluginType.PLUGIN
138 ? 'plugin'
139 : 'theme'
140
141 return `https://www.npmjs.com/package/peertube-${typeString}-${name}`
142 }
d00dc28d 143}