plugin-index.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. import { sanitizeUrl } from '@server/helpers/core-utils'
  15. async function listAvailablePluginsFromIndex (options: PeertubePluginIndexList) {
  16. const { start = 0, count = 20, search, sort = 'npmName', pluginType } = options
  17. const qs: PeertubePluginIndexList = {
  18. start,
  19. count,
  20. sort,
  21. pluginType,
  22. search,
  23. currentPeerTubeEngine: options.currentPeerTubeEngine || PEERTUBE_VERSION
  24. }
  25. const uri = CONFIG.PLUGINS.INDEX.URL + '/api/v1/plugins'
  26. try {
  27. const { body } = await doRequest<any>({ uri, qs, json: true })
  28. logger.debug('Got result from PeerTube index.', { body })
  29. addInstanceInformation(body)
  30. return body as ResultList<PeerTubePluginIndex>
  31. } catch (err) {
  32. logger.error('Cannot list available plugins from index %s.', uri, { err })
  33. return undefined
  34. }
  35. }
  36. function addInstanceInformation (result: ResultList<PeerTubePluginIndex>) {
  37. for (const d of result.data) {
  38. d.installed = PluginManager.Instance.isRegistered(d.npmName)
  39. d.name = PluginModel.normalizePluginName(d.npmName)
  40. }
  41. return result
  42. }
  43. async function getLatestPluginsVersion (npmNames: string[]): Promise<PeertubePluginLatestVersionResponse> {
  44. const bodyRequest: PeertubePluginLatestVersionRequest = {
  45. npmNames,
  46. currentPeerTubeEngine: PEERTUBE_VERSION
  47. }
  48. const uri = sanitizeUrl(CONFIG.PLUGINS.INDEX.URL) + '/api/v1/plugins/latest-version'
  49. const { body } = await doRequest<any>({ uri, body: bodyRequest, json: true, method: 'POST' })
  50. return body
  51. }
  52. export {
  53. listAvailablePluginsFromIndex,
  54. getLatestPluginsVersion
  55. }