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'
11 selector: 'my-plugin-list-installed',
12 templateUrl: './plugin-list-installed.component.html',
13 styleUrls: [ './plugin-list-installed.component.scss' ]
15 export class PluginListInstalledComponent implements OnInit {
16 pluginType: PluginType
18 pagination: ComponentPagination = {
25 plugins: PeerTubePlugin[] = []
26 updating: { [name: string]: boolean } = {}
28 onDataSubject = new Subject<any[]>()
31 private pluginService: PluginService,
32 private pluginApiService: PluginApiService,
33 private notifier: Notifier,
34 private confirmService: ConfirmService,
35 private router: Router,
36 private route: ActivatedRoute
41 if (!this.route.snapshot.queryParams['pluginType']) {
42 const queryParams = { pluginType: PluginType.PLUGIN }
44 this.router.navigate([], { queryParams })
47 this.route.queryParams.subscribe(query => {
48 if (!query['pluginType']) return
50 this.pluginType = parseInt(query['pluginType'], 10)
57 this.pagination.currentPage = 1
60 this.loadMorePlugins()
64 this.pluginApiService.getPlugins(this.pluginType, this.pagination, this.sort)
67 this.plugins = this.plugins.concat(res.data)
68 this.pagination.totalItems = res.total
70 this.onDataSubject.next(res.data)
73 error: err => this.notifier.error(err.message)
78 if (!hasMoreItems(this.pagination)) return
80 this.pagination.currentPage += 1
82 this.loadMorePlugins()
85 getNoResultMessage () {
86 if (this.pluginType === PluginType.PLUGIN) {
87 return $localize`You don't have plugins installed yet.`
90 return $localize`You don't have themes installed yet.`
93 isUpdateAvailable (plugin: PeerTubePlugin) {
94 return plugin.latestVersion && compareSemVer(plugin.latestVersion, plugin.version) > 0
97 getUpdateLabel (plugin: PeerTubePlugin) {
98 return $localize`Update to ${plugin.latestVersion}`
101 isUpdating (plugin: PeerTubePlugin) {
102 return !!this.updating[this.getUpdatingKey(plugin)]
105 isTheme (plugin: PeerTubePlugin) {
106 return plugin.type === PluginType.THEME
109 async uninstall (plugin: PeerTubePlugin) {
110 const res = await this.confirmService.confirm(
111 $localize`Do you really want to uninstall ${plugin.name}?`,
114 if (res === false) return
116 this.pluginApiService.uninstall(plugin.name, plugin.type)
119 this.notifier.success($localize`${plugin.name} uninstalled.`)
121 this.plugins = this.plugins.filter(p => p.name !== plugin.name)
122 this.pagination.totalItems--
125 error: err => this.notifier.error(err.message)
129 async update (plugin: PeerTubePlugin) {
130 const updatingKey = this.getUpdatingKey(plugin)
131 if (this.updating[updatingKey]) return
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.`,
137 $localize`Proceed upgrade`
140 if (res === false) return
143 this.updating[updatingKey] = true
145 this.pluginApiService.update(plugin.name, plugin.type)
149 this.updating[updatingKey] = false
151 this.notifier.success($localize`${plugin.name} updated.`)
153 Object.assign(plugin, res)
156 error: err => this.notifier.error(err.message)
160 getShowRouterLink (plugin: PeerTubePlugin) {
161 return [ '/admin', 'plugins', 'show', this.pluginService.nameToNpmName(plugin.name, plugin.type) ]
164 getPluginOrThemeHref (name: string) {
165 return this.pluginApiService.getPluginOrThemeHref(this.pluginType, name)
168 private getUpdatingKey (plugin: PeerTubePlugin) {
169 return plugin.name + plugin.type
172 private isMajorUpgrade (plugin: PeerTubePlugin) {
173 if (!plugin.latestVersion) return false
175 const latestMajor = plugin.latestVersion.split('.')[0]
176 const currentMajor = plugin.version.split('.')[0]
178 return latestMajor > currentMajor