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