]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
CommitLineData
67ed6552 1import { Subject } from 'rxjs'
d00dc28d 2import { Component, OnInit } from '@angular/core'
dba85a1e 3import { ActivatedRoute, Router } from '@angular/router'
67ed6552
C
4import { PluginApiService } from '@app/+admin/plugins/shared/plugin-api.service'
5import { ComponentPagination, ConfirmService, hasMoreItems, Notifier } from '@app/core'
23bdacf8 6import { PluginService } from '@app/core/plugins/plugin.service'
15a7eafb 7import { compareSemVer } from '@shared/core-utils'
428ccb8b 8import { PeerTubePlugin, PluginType } from '@shared/models'
d00dc28d
C
9
10@Component({
11 selector: 'my-plugin-list-installed',
12 templateUrl: './plugin-list-installed.component.html',
2accfdd8 13 styleUrls: [ './plugin-list-installed.component.scss' ]
d00dc28d
C
14})
15export class PluginListInstalledComponent implements OnInit {
2accfdd8 16 pluginType: PluginType
d00dc28d
C
17
18 pagination: ComponentPagination = {
19 currentPage: 1,
440d39c5
C
20 itemsPerPage: 10,
21 totalItems: null
d00dc28d
C
22 }
23 sort = 'name'
24
25 plugins: PeerTubePlugin[] = []
b5f919ac
C
26 updating: { [name: string]: boolean } = {}
27
ad453580
C
28 onDataSubject = new Subject<any[]>()
29
d00dc28d 30 constructor (
23bdacf8
C
31 private pluginService: PluginService,
32 private pluginApiService: PluginApiService,
dba85a1e
C
33 private notifier: Notifier,
34 private confirmService: ConfirmService,
35 private router: Router,
36 private route: ActivatedRoute
d00dc28d 37 ) {
d00dc28d
C
38 }
39
40 ngOnInit () {
2accfdd8
C
41 if (!this.route.snapshot.queryParams['pluginType']) {
42 const queryParams = { pluginType: PluginType.PLUGIN }
dba85a1e 43
2accfdd8
C
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 })
d00dc28d
C
54 }
55
56 reloadPlugins () {
57 this.pagination.currentPage = 1
58 this.plugins = []
59
60 this.loadMorePlugins()
61 }
62
63 loadMorePlugins () {
23bdacf8 64 this.pluginApiService.getPlugins(this.pluginType, this.pagination, this.sort)
1378c0d3
C
65 .subscribe({
66 next: res => {
d00dc28d
C
67 this.plugins = this.plugins.concat(res.data)
68 this.pagination.totalItems = res.total
ad453580
C
69
70 this.onDataSubject.next(res.data)
d00dc28d
C
71 },
72
1378c0d3
C
73 error: err => this.notifier.error(err.message)
74 })
d00dc28d
C
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) {
66357162 87 return $localize`You don't have plugins installed yet.`
d00dc28d
C
88 }
89
66357162 90 return $localize`You don't have themes installed yet.`
d00dc28d 91 }
dba85a1e 92
b5f919ac
C
93 isUpdateAvailable (plugin: PeerTubePlugin) {
94 return plugin.latestVersion && compareSemVer(plugin.latestVersion, plugin.version) > 0
95 }
96
97 getUpdateLabel (plugin: PeerTubePlugin) {
66357162 98 return $localize`Update to ${plugin.latestVersion}`
b5f919ac
C
99 }
100
101 isUpdating (plugin: PeerTubePlugin) {
102 return !!this.updating[this.getUpdatingKey(plugin)]
103 }
104
c96e457b
C
105 isTheme (plugin: PeerTubePlugin) {
106 return plugin.type === PluginType.THEME
107 }
108
dba85a1e
C
109 async uninstall (plugin: PeerTubePlugin) {
110 const res = await this.confirmService.confirm(
66357162
C
111 $localize`Do you really want to uninstall ${plugin.name}?`,
112 $localize`Uninstall`
dba85a1e
C
113 )
114 if (res === false) return
115
23bdacf8 116 this.pluginApiService.uninstall(plugin.name, plugin.type)
1378c0d3
C
117 .subscribe({
118 next: () => {
66357162 119 this.notifier.success($localize`${plugin.name} uninstalled.`)
dba85a1e
C
120
121 this.plugins = this.plugins.filter(p => p.name !== plugin.name)
122 this.pagination.totalItems--
123 },
124
1378c0d3
C
125 error: err => this.notifier.error(err.message)
126 })
dba85a1e
C
127 }
128
b5f919ac
C
129 async update (plugin: PeerTubePlugin) {
130 const updatingKey = this.getUpdatingKey(plugin)
131 if (this.updating[updatingKey]) return
132
40a52421
C
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
b5f919ac
C
143 this.updating[updatingKey] = true
144
23bdacf8 145 this.pluginApiService.update(plugin.name, plugin.type)
b5f919ac 146 .pipe()
1378c0d3
C
147 .subscribe({
148 next: res => {
b5f919ac
C
149 this.updating[updatingKey] = false
150
66357162 151 this.notifier.success($localize`${plugin.name} updated.`)
b5f919ac
C
152
153 Object.assign(plugin, res)
154 },
155
1378c0d3
C
156 error: err => this.notifier.error(err.message)
157 })
b5f919ac
C
158 }
159
dba85a1e
C
160 getShowRouterLink (plugin: PeerTubePlugin) {
161 return [ '/admin', 'plugins', 'show', this.pluginService.nameToNpmName(plugin.name, plugin.type) ]
162 }
b5f919ac 163
078b4716
C
164 getPluginOrThemeHref (name: string) {
165 return this.pluginApiService.getPluginOrThemeHref(this.pluginType, name)
166 }
167
b5f919ac
C
168 private getUpdatingKey (plugin: PeerTubePlugin) {
169 return plugin.name + plugin.type
170 }
40a52421
C
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 }
d00dc28d 180}