]>
Commit | Line | Data |
---|---|---|
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 | uninstalling: { [name: string]: boolean } = {} | |
28 | ||
29 | onDataSubject = new Subject<any[]>() | |
30 | ||
31 | constructor ( | |
32 | private pluginService: PluginService, | |
33 | private pluginApiService: PluginApiService, | |
34 | private notifier: Notifier, | |
35 | private confirmService: ConfirmService, | |
36 | private router: Router, | |
37 | private route: ActivatedRoute | |
38 | ) { | |
39 | } | |
40 | ||
41 | ngOnInit () { | |
42 | if (!this.route.snapshot.queryParams['pluginType']) { | |
43 | const queryParams = { pluginType: PluginType.PLUGIN } | |
44 | ||
45 | this.router.navigate([], { queryParams, replaceUrl: true }) | |
46 | } | |
47 | ||
48 | this.route.queryParams.subscribe(query => { | |
49 | if (!query['pluginType']) return | |
50 | ||
51 | this.pluginType = parseInt(query['pluginType'], 10) | |
52 | ||
53 | this.reloadPlugins() | |
54 | }) | |
55 | } | |
56 | ||
57 | reloadPlugins () { | |
58 | this.pagination.currentPage = 1 | |
59 | this.plugins = [] | |
60 | ||
61 | this.loadMorePlugins() | |
62 | } | |
63 | ||
64 | loadMorePlugins () { | |
65 | this.pluginApiService.getPlugins(this.pluginType, this.pagination, this.sort) | |
66 | .subscribe({ | |
67 | next: res => { | |
68 | this.plugins = this.plugins.concat(res.data) | |
69 | this.pagination.totalItems = res.total | |
70 | ||
71 | this.onDataSubject.next(res.data) | |
72 | }, | |
73 | ||
74 | error: err => this.notifier.error(err.message) | |
75 | }) | |
76 | } | |
77 | ||
78 | onNearOfBottom () { | |
79 | if (!hasMoreItems(this.pagination)) return | |
80 | ||
81 | this.pagination.currentPage += 1 | |
82 | ||
83 | this.loadMorePlugins() | |
84 | } | |
85 | ||
86 | getNoResultMessage () { | |
87 | if (this.pluginType === PluginType.PLUGIN) { | |
88 | return $localize`You don't have plugins installed yet.` | |
89 | } | |
90 | ||
91 | return $localize`You don't have themes installed yet.` | |
92 | } | |
93 | ||
94 | isUpdateAvailable (plugin: PeerTubePlugin) { | |
95 | return plugin.latestVersion && compareSemVer(plugin.latestVersion, plugin.version) > 0 | |
96 | } | |
97 | ||
98 | getUpdateLabel (plugin: PeerTubePlugin) { | |
99 | return $localize`Update to ${plugin.latestVersion}` | |
100 | } | |
101 | ||
102 | isUpdating (plugin: PeerTubePlugin) { | |
103 | return !!this.updating[this.getPluginKey(plugin)] | |
104 | } | |
105 | ||
106 | isUninstalling (plugin: PeerTubePlugin) { | |
107 | return !!this.uninstalling[this.getPluginKey(plugin)] | |
108 | } | |
109 | ||
110 | isTheme (plugin: PeerTubePlugin) { | |
111 | return plugin.type === PluginType.THEME | |
112 | } | |
113 | ||
114 | async uninstall (plugin: PeerTubePlugin) { | |
115 | const pluginKey = this.getPluginKey(plugin) | |
116 | if (this.uninstalling[pluginKey]) return | |
117 | ||
118 | const res = await this.confirmService.confirm( | |
119 | $localize`Do you really want to uninstall ${plugin.name}?`, | |
120 | $localize`Uninstall` | |
121 | ) | |
122 | if (res === false) return | |
123 | ||
124 | this.uninstalling[pluginKey] = true | |
125 | ||
126 | this.pluginApiService.uninstall(plugin.name, plugin.type) | |
127 | .subscribe({ | |
128 | next: () => { | |
129 | this.notifier.success($localize`${plugin.name} uninstalled.`) | |
130 | ||
131 | this.plugins = this.plugins.filter(p => p.name !== plugin.name) | |
132 | this.pagination.totalItems-- | |
133 | ||
134 | this.uninstalling[pluginKey] = false | |
135 | }, | |
136 | ||
137 | error: err => { | |
138 | this.notifier.error(err.message) | |
139 | this.uninstalling[pluginKey] = false | |
140 | } | |
141 | }) | |
142 | } | |
143 | ||
144 | async update (plugin: PeerTubePlugin) { | |
145 | const pluginKey = this.getPluginKey(plugin) | |
146 | if (this.updating[pluginKey]) return | |
147 | ||
148 | if (this.isMajorUpgrade(plugin)) { | |
149 | const res = await this.confirmService.confirm( | |
150 | $localize`This is a major plugin upgrade. Please go on the plugin homepage to check potential release notes.`, | |
151 | $localize`Upgrade`, | |
152 | $localize`Proceed upgrade` | |
153 | ) | |
154 | ||
155 | if (res === false) return | |
156 | } | |
157 | ||
158 | this.updating[pluginKey] = true | |
159 | ||
160 | this.pluginApiService.update(plugin.name, plugin.type) | |
161 | .pipe() | |
162 | .subscribe({ | |
163 | next: res => { | |
164 | this.updating[pluginKey] = false | |
165 | ||
166 | this.notifier.success($localize`${plugin.name} updated.`) | |
167 | ||
168 | Object.assign(plugin, res) | |
169 | }, | |
170 | ||
171 | error: err => { | |
172 | this.notifier.error(err.message) | |
173 | this.updating[pluginKey] = false | |
174 | } | |
175 | }) | |
176 | } | |
177 | ||
178 | getShowRouterLink (plugin: PeerTubePlugin) { | |
179 | return [ '/admin', 'plugins', 'show', this.pluginService.nameToNpmName(plugin.name, plugin.type) ] | |
180 | } | |
181 | ||
182 | getPluginOrThemeHref (name: string) { | |
183 | return this.pluginApiService.getPluginOrThemeHref(this.pluginType, name) | |
184 | } | |
185 | ||
186 | private getPluginKey (plugin: PeerTubePlugin) { | |
187 | return plugin.name + plugin.type | |
188 | } | |
189 | ||
190 | private isMajorUpgrade (plugin: PeerTubePlugin) { | |
191 | if (!plugin.latestVersion) return false | |
192 | ||
193 | const latestMajor = plugin.latestVersion.split('.')[0] | |
194 | const currentMajor = plugin.version.split('.')[0] | |
195 | ||
196 | return latestMajor > currentMajor | |
197 | } | |
198 | } |