]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/plugins/plugin-list-installed/plugin-list-installed.component.ts
Skip videos count on client if we don't use it
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / plugins / plugin-list-installed / plugin-list-installed.component.ts
1 import { Component, OnInit } from '@angular/core'
2 import { PluginType } from '@shared/models/plugins/plugin.type'
3 import { I18n } from '@ngx-translate/i18n-polyfill'
4 import { PluginApiService } from '@app/+admin/plugins/shared/plugin-api.service'
5 import { ComponentPagination, hasMoreItems } from '@app/shared/rest/component-pagination.model'
6 import { ConfirmService, Notifier } from '@app/core'
7 import { PeerTubePlugin } from '@shared/models/plugins/peertube-plugin.model'
8 import { ActivatedRoute, Router } from '@angular/router'
9 import { compareSemVer } from '@shared/core-utils/miscs/miscs'
10 import { PluginService } from '@app/core/plugins/plugin.service'
11 import { Subject } from 'rxjs'
12
13 @Component({
14 selector: 'my-plugin-list-installed',
15 templateUrl: './plugin-list-installed.component.html',
16 styleUrls: [
17 '../shared/toggle-plugin-type.scss',
18 '../shared/plugin-list.component.scss',
19 './plugin-list-installed.component.scss'
20 ]
21 })
22 export class PluginListInstalledComponent implements OnInit {
23 pluginTypeOptions: { label: string, value: PluginType }[] = []
24 pluginType: PluginType = PluginType.PLUGIN
25
26 pagination: ComponentPagination = {
27 currentPage: 1,
28 itemsPerPage: 10,
29 totalItems: null
30 }
31 sort = 'name'
32
33 plugins: PeerTubePlugin[] = []
34 updating: { [name: string]: boolean } = {}
35
36 PluginType = PluginType
37
38 onDataSubject = new Subject<any[]>()
39
40 constructor (
41 private i18n: I18n,
42 private pluginService: PluginService,
43 private pluginApiService: PluginApiService,
44 private notifier: Notifier,
45 private confirmService: ConfirmService,
46 private router: Router,
47 private route: ActivatedRoute
48 ) {
49 this.pluginTypeOptions = this.pluginApiService.getPluginTypeOptions()
50 }
51
52 ngOnInit () {
53 const query = this.route.snapshot.queryParams
54 if (query['pluginType']) this.pluginType = parseInt(query['pluginType'], 10)
55
56 this.reloadPlugins()
57 }
58
59 reloadPlugins () {
60 this.pagination.currentPage = 1
61 this.plugins = []
62
63 this.router.navigate([], { queryParams: { pluginType: this.pluginType } })
64
65 this.loadMorePlugins()
66 }
67
68 loadMorePlugins () {
69 this.pluginApiService.getPlugins(this.pluginType, this.pagination, this.sort)
70 .subscribe(
71 res => {
72 this.plugins = this.plugins.concat(res.data)
73 this.pagination.totalItems = res.total
74
75 this.onDataSubject.next(res.data)
76 },
77
78 err => this.notifier.error(err.message)
79 )
80 }
81
82 onNearOfBottom () {
83 if (!hasMoreItems(this.pagination)) return
84
85 this.pagination.currentPage += 1
86
87 this.loadMorePlugins()
88 }
89
90 getNoResultMessage () {
91 if (this.pluginType === PluginType.PLUGIN) {
92 return this.i18n('You don\'t have plugins installed yet.')
93 }
94
95 return this.i18n('You don\'t have themes installed yet.')
96 }
97
98 isUpdateAvailable (plugin: PeerTubePlugin) {
99 return plugin.latestVersion && compareSemVer(plugin.latestVersion, plugin.version) > 0
100 }
101
102 getUpdateLabel (plugin: PeerTubePlugin) {
103 return this.i18n('Update to {{version}}', { version: plugin.latestVersion })
104 }
105
106 isUpdating (plugin: PeerTubePlugin) {
107 return !!this.updating[this.getUpdatingKey(plugin)]
108 }
109
110 async uninstall (plugin: PeerTubePlugin) {
111 const res = await this.confirmService.confirm(
112 this.i18n('Do you really want to uninstall {{pluginName}}?', { pluginName: plugin.name }),
113 this.i18n('Uninstall')
114 )
115 if (res === false) return
116
117 this.pluginApiService.uninstall(plugin.name, plugin.type)
118 .subscribe(
119 () => {
120 this.notifier.success(this.i18n('{{pluginName}} uninstalled.', { pluginName: plugin.name }))
121
122 this.plugins = this.plugins.filter(p => p.name !== plugin.name)
123 this.pagination.totalItems--
124 },
125
126 err => this.notifier.error(err.message)
127 )
128 }
129
130 async update (plugin: PeerTubePlugin) {
131 const updatingKey = this.getUpdatingKey(plugin)
132 if (this.updating[updatingKey]) return
133
134 this.updating[updatingKey] = true
135
136 this.pluginApiService.update(plugin.name, plugin.type)
137 .pipe()
138 .subscribe(
139 res => {
140 this.updating[updatingKey] = false
141
142 this.notifier.success(this.i18n('{{pluginName}} updated.', { pluginName: plugin.name }))
143
144 Object.assign(plugin, res)
145 },
146
147 err => this.notifier.error(err.message)
148 )
149 }
150
151 getShowRouterLink (plugin: PeerTubePlugin) {
152 return [ '/admin', 'plugins', 'show', this.pluginService.nameToNpmName(plugin.name, plugin.type) ]
153 }
154
155 private getUpdatingKey (plugin: PeerTubePlugin) {
156 return plugin.name + plugin.type
157 }
158 }