]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/plugins/plugin-search/plugin-search.component.ts
Add ability to install a plugin from the admin page
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / plugins / plugin-search / plugin-search.component.ts
1 import { Component, OnInit } from '@angular/core'
2 import { Notifier, ServerService } from '@app/core'
3 import { ConfirmService } from '../../../core'
4 import { I18n } from '@ngx-translate/i18n-polyfill'
5 import { PluginType } from '@shared/models/plugins/plugin.type'
6 import { PluginApiService } from '@app/+admin/plugins/shared/plugin-api.service'
7 import { ComponentPagination, hasMoreItems } from '@app/shared/rest/component-pagination.model'
8 import { ActivatedRoute, Router } from '@angular/router'
9 import { PeerTubePluginIndex } from '@shared/models/plugins/peertube-plugin-index.model'
10 import { Subject } from 'rxjs'
11 import { debounceTime, distinctUntilChanged } from 'rxjs/operators'
12
13 @Component({
14 selector: 'my-plugin-search',
15 templateUrl: './plugin-search.component.html',
16 styleUrls: [
17 '../shared/toggle-plugin-type.scss',
18 '../shared/plugin-list.component.scss',
19 './plugin-search.component.scss'
20 ]
21 })
22 export class PluginSearchComponent implements OnInit {
23 pluginTypeOptions: { label: string, value: PluginType }[] = []
24 pluginType: PluginType = PluginType.PLUGIN
25
26 pagination: ComponentPagination = {
27 currentPage: 1,
28 itemsPerPage: 10
29 }
30 sort = '-popularity'
31
32 search = ''
33 isSearching = false
34
35 plugins: PeerTubePluginIndex[] = []
36 installing: { [name: string]: boolean } = {}
37 pluginInstalled = false
38
39 private searchSubject = new Subject<string>()
40
41 constructor (
42 private server: ServerService,
43 private i18n: I18n,
44 private pluginService: PluginApiService,
45 private notifier: Notifier,
46 private confirmService: ConfirmService,
47 private router: Router,
48 private route: ActivatedRoute
49 ) {
50 this.pluginTypeOptions = this.pluginService.getPluginTypeOptions()
51 }
52
53 ngOnInit () {
54 const query = this.route.snapshot.queryParams
55 if (query['pluginType']) this.pluginType = parseInt(query['pluginType'], 10)
56
57 this.searchSubject.asObservable()
58 .pipe(
59 debounceTime(400),
60 distinctUntilChanged()
61 )
62 .subscribe(search => {
63 this.search = search
64 this.reloadPlugins()
65 })
66
67 this.reloadPlugins()
68 }
69
70 onSearchChange (search: string) {
71 this.searchSubject.next(search)
72 }
73
74 reloadPlugins () {
75 this.pagination.currentPage = 1
76 this.plugins = []
77
78 this.router.navigate([], { queryParams: { pluginType: this.pluginType } })
79
80 this.loadMorePlugins()
81 }
82
83 loadMorePlugins () {
84 this.isSearching = true
85
86 this.pluginService.searchAvailablePlugins(this.pluginType, this.pagination, this.sort, this.search)
87 .subscribe(
88 res => {
89 this.isSearching = false
90
91 this.plugins = this.plugins.concat(res.data)
92 this.pagination.totalItems = res.total
93 },
94
95 err => this.notifier.error(err.message)
96 )
97 }
98
99 onNearOfBottom () {
100 if (!hasMoreItems(this.pagination)) return
101
102 this.pagination.currentPage += 1
103
104 this.loadMorePlugins()
105 }
106
107 isInstalling (plugin: PeerTubePluginIndex) {
108 return !!this.installing[plugin.npmName]
109 }
110
111 async install (plugin: PeerTubePluginIndex) {
112 if (this.installing[plugin.npmName]) return
113
114 const res = await this.confirmService.confirm(
115 this.i18n('Please only install plugins or themes you trust, since they can execute any code on your instance.'),
116 this.i18n('Install {{pluginName}}?', { pluginName: plugin.name })
117 )
118 if (res === false) return
119
120 this.installing[plugin.npmName] = true
121
122 this.pluginService.install(plugin.npmName)
123 .subscribe(
124 () => {
125 this.installing[plugin.npmName] = false
126 this.pluginInstalled = true
127
128 this.notifier.success(this.i18n('{{pluginName}} installed.', { pluginName: plugin.name }))
129
130 plugin.installed = true
131 },
132
133 err => this.notifier.error(err.message)
134 )
135 }
136 }