]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/plugins/plugin-index.ts
Merge branch 'release/4.1.0' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / plugins / plugin-index.ts
CommitLineData
db4b15f2 1import { sanitizeUrl } from '@server/helpers/core-utils'
428ccb8b
C
2import { logger } from '@server/helpers/logger'
3import { doJSONRequest } from '@server/helpers/requests'
4import { CONFIG } from '@server/initializers/config'
5import { PEERTUBE_VERSION } from '@server/initializers/constants'
6import { PluginModel } from '@server/models/server/plugin'
6702a1b2 7import {
428ccb8b
C
8 PeerTubePluginIndex,
9 PeertubePluginIndexList,
6702a1b2 10 PeertubePluginLatestVersionRequest,
428ccb8b
C
11 PeertubePluginLatestVersionResponse,
12 ResultList
13} from '@shared/models'
db4b15f2 14import { PluginManager } from './plugin-manager'
6702a1b2
C
15
16async function listAvailablePluginsFromIndex (options: PeertubePluginIndexList) {
17 const { start = 0, count = 20, search, sort = 'npmName', pluginType } = options
18
db4b15f2 19 const searchParams: PeertubePluginIndexList & Record<string, string | number> = {
6702a1b2
C
20 start,
21 count,
22 sort,
23 pluginType,
24 search,
09071200 25 currentPeerTubeEngine: options.currentPeerTubeEngine || PEERTUBE_VERSION
6702a1b2
C
26 }
27
28 const uri = CONFIG.PLUGINS.INDEX.URL + '/api/v1/plugins'
29
e0ce715a 30 try {
db4b15f2 31 const { body } = await doJSONRequest<any>(uri, { searchParams })
6702a1b2 32
e0ce715a 33 logger.debug('Got result from PeerTube index.', { body })
6702a1b2 34
a1587156 35 addInstanceInformation(body)
6702a1b2 36
e0ce715a
C
37 return body as ResultList<PeerTubePluginIndex>
38 } catch (err) {
39 logger.error('Cannot list available plugins from index %s.', uri, { err })
40 return undefined
41 }
6702a1b2
C
42}
43
a1587156 44function addInstanceInformation (result: ResultList<PeerTubePluginIndex>) {
6702a1b2
C
45 for (const d of result.data) {
46 d.installed = PluginManager.Instance.isRegistered(d.npmName)
47 d.name = PluginModel.normalizePluginName(d.npmName)
48 }
49
50 return result
51}
52
53async function getLatestPluginsVersion (npmNames: string[]): Promise<PeertubePluginLatestVersionResponse> {
54 const bodyRequest: PeertubePluginLatestVersionRequest = {
55 npmNames,
66170ca8 56 currentPeerTubeEngine: PEERTUBE_VERSION
6702a1b2
C
57 }
58
5fb2e288 59 const uri = sanitizeUrl(CONFIG.PLUGINS.INDEX.URL) + '/api/v1/plugins/latest-version'
6702a1b2 60
db4b15f2
C
61 const options = {
62 json: bodyRequest,
63 method: 'POST' as 'POST'
64 }
65 const { body } = await doJSONRequest<PeertubePluginLatestVersionResponse>(uri, options)
6702a1b2
C
66
67 return body
68}
69
8280d0c2
C
70async function getLatestPluginVersion (npmName: string) {
71 const results = await getLatestPluginsVersion([ npmName ])
72
73 if (Array.isArray(results) === false || results.length !== 1) {
74 logger.warn('Cannot get latest supported plugin version of %s.', npmName)
75 return undefined
76 }
77
78 return results[0].latestVersion
79}
80
6702a1b2
C
81export {
82 listAvailablePluginsFromIndex,
8280d0c2 83 getLatestPluginVersion,
6702a1b2
C
84 getLatestPluginsVersion
85}