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