]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/plugins/shared/plugin-api.service.ts
Change plugin models names
[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 { environment } from '../../../../environments/environment'
5 import { RestExtractor, RestService } from '../../../shared'
6 import { I18n } from '@ngx-translate/i18n-polyfill'
7 import { PluginType } from '@shared/models/plugins/plugin.type'
8 import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
9 import { ResultList } from '@shared/models'
10 import { PeerTubePlugin } from '@shared/models/plugins/peertube-plugin.model'
11 import { ManagePlugin } from '@shared/models/plugins/manage-plugin.model'
12 import { InstallOrUpdatePlugin } from '@shared/models/plugins/install-plugin.model'
13 import { PeerTubePluginIndex } from '@shared/models/plugins/peertube-plugin-index.model'
14 import { RegisterServerSettingOptions } from '@shared/models/plugins/register-server-setting.model'
15
16 @Injectable()
17 export class PluginApiService {
18 private static BASE_APPLICATION_URL = environment.apiUrl + '/api/v1/plugins'
19
20 constructor (
21 private authHttp: HttpClient,
22 private restExtractor: RestExtractor,
23 private restService: RestService,
24 private i18n: I18n
25 ) { }
26
27 getPluginTypeOptions () {
28 return [
29 {
30 label: this.i18n('Plugins'),
31 value: PluginType.PLUGIN
32 },
33 {
34 label: this.i18n('Themes'),
35 value: PluginType.THEME
36 }
37 ]
38 }
39
40 getPluginTypeLabel (type: PluginType) {
41 if (type === PluginType.PLUGIN) {
42 return this.i18n('plugin')
43 }
44
45 return this.i18n('theme')
46 }
47
48 getPlugins (
49 pluginType: PluginType,
50 componentPagination: ComponentPagination,
51 sort: string
52 ) {
53 const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
54
55 let params = new HttpParams()
56 params = this.restService.addRestGetParams(params, pagination, sort)
57 params = params.append('pluginType', pluginType.toString())
58
59 return this.authHttp.get<ResultList<PeerTubePlugin>>(PluginApiService.BASE_APPLICATION_URL, { params })
60 .pipe(catchError(res => this.restExtractor.handleError(res)))
61 }
62
63 searchAvailablePlugins (
64 pluginType: PluginType,
65 componentPagination: ComponentPagination,
66 sort: string,
67 search?: string
68 ) {
69 const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
70
71 let params = new HttpParams()
72 params = this.restService.addRestGetParams(params, pagination, sort)
73 params = params.append('pluginType', pluginType.toString())
74
75 if (search) params = params.append('search', search)
76
77 return this.authHttp.get<ResultList<PeerTubePluginIndex>>(PluginApiService.BASE_APPLICATION_URL + '/available', { params })
78 .pipe(catchError(res => this.restExtractor.handleError(res)))
79 }
80
81 getPlugin (npmName: string) {
82 const path = PluginApiService.BASE_APPLICATION_URL + '/' + npmName
83
84 return this.authHttp.get<PeerTubePlugin>(path)
85 .pipe(catchError(res => this.restExtractor.handleError(res)))
86 }
87
88 getPluginRegisteredSettings (pluginName: string, pluginType: PluginType) {
89 const path = PluginApiService.BASE_APPLICATION_URL + '/' + this.nameToNpmName(pluginName, pluginType) + '/registered-settings'
90
91 return this.authHttp.get<{ settings: RegisterServerSettingOptions[] }>(path)
92 .pipe(catchError(res => this.restExtractor.handleError(res)))
93 }
94
95 updatePluginSettings (pluginName: string, pluginType: PluginType, settings: any) {
96 const path = PluginApiService.BASE_APPLICATION_URL + '/' + this.nameToNpmName(pluginName, pluginType) + '/settings'
97
98 return this.authHttp.put(path, { settings })
99 .pipe(catchError(res => this.restExtractor.handleError(res)))
100 }
101
102 uninstall (pluginName: string, pluginType: PluginType) {
103 const body: ManagePlugin = {
104 npmName: this.nameToNpmName(pluginName, pluginType)
105 }
106
107 return this.authHttp.post(PluginApiService.BASE_APPLICATION_URL + '/uninstall', body)
108 .pipe(catchError(res => this.restExtractor.handleError(res)))
109 }
110
111 update (pluginName: string, pluginType: PluginType) {
112 const body: ManagePlugin = {
113 npmName: this.nameToNpmName(pluginName, pluginType)
114 }
115
116 return this.authHttp.post(PluginApiService.BASE_APPLICATION_URL + '/update', body)
117 .pipe(catchError(res => this.restExtractor.handleError(res)))
118 }
119
120 install (npmName: string) {
121 const body: InstallOrUpdatePlugin = {
122 npmName
123 }
124
125 return this.authHttp.post(PluginApiService.BASE_APPLICATION_URL + '/install', body)
126 .pipe(catchError(res => this.restExtractor.handleError(res)))
127 }
128
129 nameToNpmName (name: string, type: PluginType) {
130 const prefix = type === PluginType.PLUGIN
131 ? 'peertube-plugin-'
132 : 'peertube-theme-'
133
134 return prefix + name
135 }
136
137 pluginTypeFromNpmName (npmName: string) {
138 return npmName.startsWith('peertube-plugin-')
139 ? PluginType.PLUGIN
140 : PluginType.THEME
141 }
142 }