plugin-index.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { doRequest } from '../../helpers/requests'
  2. import { CONFIG } from '../../initializers/config'
  3. import {
  4. PeertubePluginLatestVersionRequest,
  5. PeertubePluginLatestVersionResponse
  6. } from '../../../shared/models/plugins/peertube-plugin-latest-version.model'
  7. import { PeertubePluginIndexList } from '../../../shared/models/plugins/peertube-plugin-index-list.model'
  8. import { ResultList } from '../../../shared/models'
  9. import { PeerTubePluginIndex } from '../../../shared/models/plugins/peertube-plugin-index.model'
  10. import { PluginModel } from '../../models/server/plugin'
  11. import { PluginManager } from './plugin-manager'
  12. import { logger } from '../../helpers/logger'
  13. import { PEERTUBE_VERSION } from '../../initializers/constants'
  14. async function listAvailablePluginsFromIndex (options: PeertubePluginIndexList) {
  15. const { start = 0, count = 20, search, sort = 'npmName', pluginType } = options
  16. const qs: PeertubePluginIndexList = {
  17. start,
  18. count,
  19. sort,
  20. pluginType,
  21. search,
  22. currentPeerTubeEngine: PEERTUBE_VERSION
  23. }
  24. const uri = CONFIG.PLUGINS.INDEX.URL + '/api/v1/plugins'
  25. try {
  26. const { body } = await doRequest({ uri, qs, json: true })
  27. logger.debug('Got result from PeerTube index.', { body })
  28. await addInstanceInformation(body)
  29. return body as ResultList<PeerTubePluginIndex>
  30. } catch (err) {
  31. logger.error('Cannot list available plugins from index %s.', uri, { err })
  32. return undefined
  33. }
  34. }
  35. async function addInstanceInformation (result: ResultList<PeerTubePluginIndex>) {
  36. for (const d of result.data) {
  37. d.installed = PluginManager.Instance.isRegistered(d.npmName)
  38. d.name = PluginModel.normalizePluginName(d.npmName)
  39. }
  40. return result
  41. }
  42. async function getLatestPluginsVersion (npmNames: string[]): Promise<PeertubePluginLatestVersionResponse> {
  43. const bodyRequest: PeertubePluginLatestVersionRequest = {
  44. npmNames,
  45. currentPeerTubeEngine: PEERTUBE_VERSION
  46. }
  47. const uri = CONFIG.PLUGINS.INDEX.URL + '/api/v1/plugins/latest-version'
  48. const { body } = await doRequest({ uri, body: bodyRequest, json: true, method: 'POST' })
  49. return body
  50. }
  51. export {
  52. listAvailablePluginsFromIndex,
  53. getLatestPluginsVersion
  54. }