From 6702a1b2ccd666285dee9c72b5bace641d2fce8b Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 16 Jul 2019 11:33:22 +0200 Subject: Add ability to search available plugins --- server/lib/plugins/plugin-index.ts | 64 ++++++++++++++++++++++++++++++++++++ server/lib/plugins/plugin-manager.ts | 4 +++ 2 files changed, 68 insertions(+) create mode 100644 server/lib/plugins/plugin-index.ts (limited to 'server/lib/plugins') diff --git a/server/lib/plugins/plugin-index.ts b/server/lib/plugins/plugin-index.ts new file mode 100644 index 000000000..4a8a90ec8 --- /dev/null +++ b/server/lib/plugins/plugin-index.ts @@ -0,0 +1,64 @@ +import { doRequest } from '../../helpers/requests' +import { CONFIG } from '../../initializers/config' +import { + PeertubePluginLatestVersionRequest, + PeertubePluginLatestVersionResponse +} from '../../../shared/models/plugins/peertube-plugin-latest-version.model' +import { PeertubePluginIndexList } from '../../../shared/models/plugins/peertube-plugin-index-list.model' +import { ResultList } from '../../../shared/models' +import { PeerTubePluginIndex } from '../../../shared/models/plugins/peertube-plugin-index.model' +import { PluginModel } from '../../models/server/plugin' +import { PluginManager } from './plugin-manager' +import { logger } from '../../helpers/logger' + +const packageJSON = require('../../../../package.json') + +async function listAvailablePluginsFromIndex (options: PeertubePluginIndexList) { + const { start = 0, count = 20, search, sort = 'npmName', pluginType } = options + + const qs: PeertubePluginIndexList = { + start, + count, + sort, + pluginType, + search, + currentPeerTubeEngine: packageJSON.version + } + + const uri = CONFIG.PLUGINS.INDEX.URL + '/api/v1/plugins' + + const { body } = await doRequest({ uri, qs, json: true }) + + logger.debug('Got result from PeerTube index.', { body }) + + await addInstanceInformation(body) + + return body as ResultList +} + +async function addInstanceInformation (result: ResultList) { + for (const d of result.data) { + d.installed = PluginManager.Instance.isRegistered(d.npmName) + d.name = PluginModel.normalizePluginName(d.npmName) + } + + return result +} + +async function getLatestPluginsVersion (npmNames: string[]): Promise { + const bodyRequest: PeertubePluginLatestVersionRequest = { + npmNames, + currentPeerTubeEngine: packageJSON.version + } + + const uri = CONFIG.PLUGINS.INDEX.URL + '/api/v1/plugins/latest-version' + + const { body } = await doRequest({ uri, body: bodyRequest }) + + return body +} + +export { + listAvailablePluginsFromIndex, + getLatestPluginsVersion +} diff --git a/server/lib/plugins/plugin-manager.ts b/server/lib/plugins/plugin-manager.ts index 7576b284c..9e4ec5adf 100644 --- a/server/lib/plugins/plugin-manager.ts +++ b/server/lib/plugins/plugin-manager.ts @@ -55,6 +55,10 @@ export class PluginManager { // ###################### Getters ###################### + isRegistered (npmName: string) { + return !!this.getRegisteredPluginOrTheme(npmName) + } + getRegisteredPluginOrTheme (npmName: string) { return this.registeredPlugins[npmName] } -- cgit v1.2.3