]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/plugins/plugin-index.ts
Add ability to search available plugins
[github/Chocobozzz/PeerTube.git] / server / lib / plugins / plugin-index.ts
CommitLineData
6702a1b2
C
1import { doRequest } from '../../helpers/requests'
2import { CONFIG } from '../../initializers/config'
3import {
4 PeertubePluginLatestVersionRequest,
5 PeertubePluginLatestVersionResponse
6} from '../../../shared/models/plugins/peertube-plugin-latest-version.model'
7import { PeertubePluginIndexList } from '../../../shared/models/plugins/peertube-plugin-index-list.model'
8import { ResultList } from '../../../shared/models'
9import { PeerTubePluginIndex } from '../../../shared/models/plugins/peertube-plugin-index.model'
10import { PluginModel } from '../../models/server/plugin'
11import { PluginManager } from './plugin-manager'
12import { logger } from '../../helpers/logger'
13
14const packageJSON = require('../../../../package.json')
15
16async function listAvailablePluginsFromIndex (options: PeertubePluginIndexList) {
17 const { start = 0, count = 20, search, sort = 'npmName', pluginType } = options
18
19 const qs: PeertubePluginIndexList = {
20 start,
21 count,
22 sort,
23 pluginType,
24 search,
25 currentPeerTubeEngine: packageJSON.version
26 }
27
28 const uri = CONFIG.PLUGINS.INDEX.URL + '/api/v1/plugins'
29
30 const { body } = await doRequest({ uri, qs, json: true })
31
32 logger.debug('Got result from PeerTube index.', { body })
33
34 await addInstanceInformation(body)
35
36 return body as ResultList<PeerTubePluginIndex>
37}
38
39async function addInstanceInformation (result: ResultList<PeerTubePluginIndex>) {
40 for (const d of result.data) {
41 d.installed = PluginManager.Instance.isRegistered(d.npmName)
42 d.name = PluginModel.normalizePluginName(d.npmName)
43 }
44
45 return result
46}
47
48async function getLatestPluginsVersion (npmNames: string[]): Promise<PeertubePluginLatestVersionResponse> {
49 const bodyRequest: PeertubePluginLatestVersionRequest = {
50 npmNames,
51 currentPeerTubeEngine: packageJSON.version
52 }
53
54 const uri = CONFIG.PLUGINS.INDEX.URL + '/api/v1/plugins/latest-version'
55
56 const { body } = await doRequest({ uri, body: bodyRequest })
57
58 return body
59}
60
61export {
62 listAvailablePluginsFromIndex,
63 getLatestPluginsVersion
64}