plugin-api.service.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { catchError } from 'rxjs/operators'
  2. import { HttpClient, HttpParams } from '@angular/common/http'
  3. import { Injectable } from '@angular/core'
  4. import { environment } from '../../../../environments/environment'
  5. import { RestExtractor, RestService } from '../../../shared'
  6. import { I18n } from '@ngx-translate/i18n-polyfill'
  7. import { PluginType } from '@shared/models/plugins/plugin.type'
  8. import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
  9. import { ResultList } from '@shared/models'
  10. import { PeerTubePlugin } from '@shared/models/plugins/peertube-plugin.model'
  11. import { ManagePlugin } from '@shared/models/plugins/manage-plugin.model'
  12. import { InstallOrUpdatePlugin } from '@shared/models/plugins/install-plugin.model'
  13. import { RegisterSettingOptions } from '@shared/models/plugins/register-setting.model'
  14. import { PeerTubePluginIndex } from '@shared/models/plugins/peertube-plugin-index.model'
  15. @Injectable()
  16. export class PluginApiService {
  17. private static BASE_APPLICATION_URL = environment.apiUrl + '/api/v1/plugins'
  18. constructor (
  19. private authHttp: HttpClient,
  20. private restExtractor: RestExtractor,
  21. private restService: RestService,
  22. private i18n: I18n
  23. ) { }
  24. getPluginTypeOptions () {
  25. return [
  26. {
  27. label: this.i18n('Plugins'),
  28. value: PluginType.PLUGIN
  29. },
  30. {
  31. label: this.i18n('Themes'),
  32. value: PluginType.THEME
  33. }
  34. ]
  35. }
  36. getPluginTypeLabel (type: PluginType) {
  37. if (type === PluginType.PLUGIN) {
  38. return this.i18n('plugin')
  39. }
  40. return this.i18n('theme')
  41. }
  42. getPlugins (
  43. pluginType: PluginType,
  44. componentPagination: ComponentPagination,
  45. sort: string
  46. ) {
  47. const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
  48. let params = new HttpParams()
  49. params = this.restService.addRestGetParams(params, pagination, sort)
  50. params = params.append('pluginType', pluginType.toString())
  51. return this.authHttp.get<ResultList<PeerTubePlugin>>(PluginApiService.BASE_APPLICATION_URL, { params })
  52. .pipe(catchError(res => this.restExtractor.handleError(res)))
  53. }
  54. searchAvailablePlugins (
  55. pluginType: PluginType,
  56. componentPagination: ComponentPagination,
  57. sort: string,
  58. search?: string
  59. ) {
  60. const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
  61. let params = new HttpParams()
  62. params = this.restService.addRestGetParams(params, pagination, sort)
  63. params = params.append('pluginType', pluginType.toString())
  64. if (search) params = params.append('search', search)
  65. return this.authHttp.get<ResultList<PeerTubePluginIndex>>(PluginApiService.BASE_APPLICATION_URL + '/available', { params })
  66. .pipe(catchError(res => this.restExtractor.handleError(res)))
  67. }
  68. getPlugin (npmName: string) {
  69. const path = PluginApiService.BASE_APPLICATION_URL + '/' + npmName
  70. return this.authHttp.get<PeerTubePlugin>(path)
  71. .pipe(catchError(res => this.restExtractor.handleError(res)))
  72. }
  73. getPluginRegisteredSettings (pluginName: string, pluginType: PluginType) {
  74. const path = PluginApiService.BASE_APPLICATION_URL + '/' + this.nameToNpmName(pluginName, pluginType) + '/registered-settings'
  75. return this.authHttp.get<{ settings: RegisterSettingOptions[] }>(path)
  76. .pipe(catchError(res => this.restExtractor.handleError(res)))
  77. }
  78. updatePluginSettings (pluginName: string, pluginType: PluginType, settings: any) {
  79. const path = PluginApiService.BASE_APPLICATION_URL + '/' + this.nameToNpmName(pluginName, pluginType) + '/settings'
  80. return this.authHttp.put(path, { settings })
  81. .pipe(catchError(res => this.restExtractor.handleError(res)))
  82. }
  83. uninstall (pluginName: string, pluginType: PluginType) {
  84. const body: ManagePlugin = {
  85. npmName: this.nameToNpmName(pluginName, pluginType)
  86. }
  87. return this.authHttp.post(PluginApiService.BASE_APPLICATION_URL + '/uninstall', body)
  88. .pipe(catchError(res => this.restExtractor.handleError(res)))
  89. }
  90. update (pluginName: string, pluginType: PluginType) {
  91. const body: ManagePlugin = {
  92. npmName: this.nameToNpmName(pluginName, pluginType)
  93. }
  94. return this.authHttp.post(PluginApiService.BASE_APPLICATION_URL + '/update', body)
  95. .pipe(catchError(res => this.restExtractor.handleError(res)))
  96. }
  97. install (npmName: string) {
  98. const body: InstallOrUpdatePlugin = {
  99. npmName
  100. }
  101. return this.authHttp.post(PluginApiService.BASE_APPLICATION_URL + '/install', body)
  102. .pipe(catchError(res => this.restExtractor.handleError(res)))
  103. }
  104. nameToNpmName (name: string, type: PluginType) {
  105. const prefix = type === PluginType.PLUGIN
  106. ? 'peertube-plugin-'
  107. : 'peertube-theme-'
  108. return prefix + name
  109. }
  110. pluginTypeFromNpmName (npmName: string) {
  111. return npmName.startsWith('peertube-plugin-')
  112. ? PluginType.PLUGIN
  113. : PluginType.THEME
  114. }
  115. }