]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+admin/plugins/plugin-list-installed/plugin-list-installed.component.ts
Cleanup shared models
[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'
67ed6552
C
7import { compareSemVer } from '@shared/core-utils/miscs/miscs'
8import { PeerTubePlugin } from '@shared/models/plugins/peertube-plugin.model'
9import { PluginType } from '@shared/models/plugins/plugin.type'
d00dc28d
C
10
11@Component({
12 selector: 'my-plugin-list-installed',
13 templateUrl: './plugin-list-installed.component.html',
dba85a1e
C
14 styleUrls: [
15 '../shared/toggle-plugin-type.scss',
6702a1b2 16 '../shared/plugin-list.component.scss',
dba85a1e
C
17 './plugin-list-installed.component.scss'
18 ]
d00dc28d
C
19})
20export class PluginListInstalledComponent implements OnInit {
21 pluginTypeOptions: { label: string, value: PluginType }[] = []
22 pluginType: PluginType = PluginType.PLUGIN
23
24 pagination: ComponentPagination = {
25 currentPage: 1,
440d39c5
C
26 itemsPerPage: 10,
27 totalItems: null
d00dc28d
C
28 }
29 sort = 'name'
30
31 plugins: PeerTubePlugin[] = []
b5f919ac
C
32 updating: { [name: string]: boolean } = {}
33
ad453580
C
34 onDataSubject = new Subject<any[]>()
35
d00dc28d 36 constructor (
23bdacf8
C
37 private pluginService: PluginService,
38 private pluginApiService: PluginApiService,
dba85a1e
C
39 private notifier: Notifier,
40 private confirmService: ConfirmService,
41 private router: Router,
42 private route: ActivatedRoute
d00dc28d 43 ) {
23bdacf8 44 this.pluginTypeOptions = this.pluginApiService.getPluginTypeOptions()
d00dc28d
C
45 }
46
47 ngOnInit () {
dba85a1e
C
48 const query = this.route.snapshot.queryParams
49 if (query['pluginType']) this.pluginType = parseInt(query['pluginType'], 10)
50
d00dc28d
C
51 this.reloadPlugins()
52 }
53
54 reloadPlugins () {
55 this.pagination.currentPage = 1
56 this.plugins = []
57
b5f919ac 58 this.router.navigate([], { queryParams: { pluginType: this.pluginType } })
dba85a1e 59
d00dc28d
C
60 this.loadMorePlugins()
61 }
62
63 loadMorePlugins () {
23bdacf8 64 this.pluginApiService.getPlugins(this.pluginType, this.pagination, this.sort)
d00dc28d
C
65 .subscribe(
66 res => {
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
73 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) {
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)
dba85a1e
C
117 .subscribe(
118 () => {
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
125 err => this.notifier.error(err.message)
126 )
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
C
146 .pipe()
147 .subscribe(
148 res => {
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
156 err => this.notifier.error(err.message)
157 )
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}