1 import { sanitizeUrl } from '@server/helpers/core-utils'
2 import { logger } from '@server/helpers/logger'
3 import { doJSONRequest } from '@server/helpers/requests'
4 import { CONFIG } from '@server/initializers/config'
5 import { PEERTUBE_VERSION } from '@server/initializers/constants'
6 import { PluginModel } from '@server/models/server/plugin'
9 PeertubePluginIndexList,
10 PeertubePluginLatestVersionRequest,
11 PeertubePluginLatestVersionResponse,
13 } from '@shared/models'
14 import { PluginManager } from './plugin-manager'
16 async function listAvailablePluginsFromIndex (options: PeertubePluginIndexList) {
17 const { start = 0, count = 20, search, sort = 'npmName', pluginType } = options
19 const searchParams: PeertubePluginIndexList & Record<string, string | number> = {
25 currentPeerTubeEngine: options.currentPeerTubeEngine || PEERTUBE_VERSION
28 const uri = CONFIG.PLUGINS.INDEX.URL + '/api/v1/plugins'
31 const { body } = await doJSONRequest<any>(uri, { searchParams })
33 logger.debug('Got result from PeerTube index.', { body })
35 addInstanceInformation(body)
37 return body as ResultList<PeerTubePluginIndex>
39 logger.error('Cannot list available plugins from index %s.', uri, { err })
44 function addInstanceInformation (result: ResultList<PeerTubePluginIndex>) {
45 for (const d of result.data) {
46 d.installed = PluginManager.Instance.isRegistered(d.npmName)
47 d.name = PluginModel.normalizePluginName(d.npmName)
53 async function getLatestPluginsVersion (npmNames: string[]): Promise<PeertubePluginLatestVersionResponse> {
54 const bodyRequest: PeertubePluginLatestVersionRequest = {
56 currentPeerTubeEngine: PEERTUBE_VERSION
59 const uri = sanitizeUrl(CONFIG.PLUGINS.INDEX.URL) + '/api/v1/plugins/latest-version'
63 method: 'POST' as 'POST'
65 const { body } = await doJSONRequest<PeertubePluginLatestVersionResponse>(uri, options)
70 async function getLatestPluginVersion (npmName: string) {
71 const results = await getLatestPluginsVersion([ npmName ])
73 if (Array.isArray(results) === false || results.length !== 1) {
74 logger.warn('Cannot get latest supported plugin version of %s.', npmName)
78 return results[0].latestVersion
82 listAvailablePluginsFromIndex,
83 getLatestPluginVersion,
84 getLatestPluginsVersion