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