]>
Commit | Line | Data |
---|---|---|
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 | ||
16 | async 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: options.currentPeerTubeEngine || PEERTUBE_VERSION | |
26 | } | |
27 | ||
28 | const uri = CONFIG.PLUGINS.INDEX.URL + '/api/v1/plugins' | |
29 | ||
30 | try { | |
31 | const { body } = await doRequest<any>({ uri, qs, json: true }) | |
32 | ||
33 | logger.debug('Got result from PeerTube index.', { body }) | |
34 | ||
35 | addInstanceInformation(body) | |
36 | ||
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 | } | |
42 | } | |
43 | ||
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) | |
48 | } | |
49 | ||
50 | return result | |
51 | } | |
52 | ||
53 | async function getLatestPluginsVersion (npmNames: string[]): Promise<PeertubePluginLatestVersionResponse> { | |
54 | const bodyRequest: PeertubePluginLatestVersionRequest = { | |
55 | npmNames, | |
56 | currentPeerTubeEngine: PEERTUBE_VERSION | |
57 | } | |
58 | ||
59 | const uri = sanitizeUrl(CONFIG.PLUGINS.INDEX.URL) + '/api/v1/plugins/latest-version' | |
60 | ||
61 | const { body } = await doRequest<any>({ uri, body: bodyRequest, json: true, method: 'POST' }) | |
62 | ||
63 | return body | |
64 | } | |
65 | ||
66 | export { | |
67 | listAvailablePluginsFromIndex, | |
68 | getLatestPluginsVersion | |
69 | } |