aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/plugins')
-rw-r--r--server/lib/plugins/plugin-helpers-builder.ts17
-rw-r--r--server/lib/plugins/plugin-index.ts38
-rw-r--r--server/lib/plugins/plugin-manager.ts12
-rw-r--r--server/lib/plugins/register-helpers.ts4
-rw-r--r--server/lib/plugins/yarn.ts13
5 files changed, 63 insertions, 21 deletions
diff --git a/server/lib/plugins/plugin-helpers-builder.ts b/server/lib/plugins/plugin-helpers-builder.ts
index dac6b3185..cbd849742 100644
--- a/server/lib/plugins/plugin-helpers-builder.ts
+++ b/server/lib/plugins/plugin-helpers-builder.ts
@@ -12,8 +12,10 @@ import { VideoBlacklistCreate } from '@shared/models'
12import { blacklistVideo, unblacklistVideo } from '../video-blacklist' 12import { blacklistVideo, unblacklistVideo } from '../video-blacklist'
13import { VideoBlacklistModel } from '@server/models/video/video-blacklist' 13import { VideoBlacklistModel } from '@server/models/video/video-blacklist'
14import { AccountBlocklistModel } from '@server/models/account/account-blocklist' 14import { AccountBlocklistModel } from '@server/models/account/account-blocklist'
15import { getServerConfig } from '../config'
16import { MPlugin } from '@server/types/models'
15 17
16function buildPluginHelpers (npmName: string): PeerTubeHelpers { 18function buildPluginHelpers (pluginModel: MPlugin, npmName: string): PeerTubeHelpers {
17 const logger = buildPluginLogger(npmName) 19 const logger = buildPluginLogger(npmName)
18 20
19 const database = buildDatabaseHelpers() 21 const database = buildDatabaseHelpers()
@@ -25,12 +27,15 @@ function buildPluginHelpers (npmName: string): PeerTubeHelpers {
25 27
26 const moderation = buildModerationHelpers() 28 const moderation = buildModerationHelpers()
27 29
30 const plugin = buildPluginRelatedHelpers(pluginModel)
31
28 return { 32 return {
29 logger, 33 logger,
30 database, 34 database,
31 videos, 35 videos,
32 config, 36 config,
33 moderation, 37 moderation,
38 plugin,
34 server 39 server
35 } 40 }
36} 41}
@@ -132,6 +137,16 @@ function buildConfigHelpers () {
132 return { 137 return {
133 getWebserverUrl () { 138 getWebserverUrl () {
134 return WEBSERVER.URL 139 return WEBSERVER.URL
140 },
141
142 getServerConfig () {
143 return getServerConfig()
135 } 144 }
136 } 145 }
137} 146}
147
148function buildPluginRelatedHelpers (plugin: MPlugin) {
149 return {
150 getBaseStaticRoute: () => `/plugins/${plugin.name}/${plugin.version}/static/`
151 }
152}
diff --git a/server/lib/plugins/plugin-index.ts b/server/lib/plugins/plugin-index.ts
index 7bcb6ed4c..165bc91b3 100644
--- a/server/lib/plugins/plugin-index.ts
+++ b/server/lib/plugins/plugin-index.ts
@@ -1,22 +1,22 @@
1import { doRequest } from '../../helpers/requests' 1import { sanitizeUrl } from '@server/helpers/core-utils'
2import { CONFIG } from '../../initializers/config' 2import { ResultList } from '../../../shared/models'
3import { PeertubePluginIndexList } from '../../../shared/models/plugins/peertube-plugin-index-list.model'
4import { PeerTubePluginIndex } from '../../../shared/models/plugins/peertube-plugin-index.model'
3import { 5import {
4 PeertubePluginLatestVersionRequest, 6 PeertubePluginLatestVersionRequest,
5 PeertubePluginLatestVersionResponse 7 PeertubePluginLatestVersionResponse
6} from '../../../shared/models/plugins/peertube-plugin-latest-version.model' 8} 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' 9import { logger } from '../../helpers/logger'
10import { doJSONRequest } from '../../helpers/requests'
11import { CONFIG } from '../../initializers/config'
13import { PEERTUBE_VERSION } from '../../initializers/constants' 12import { PEERTUBE_VERSION } from '../../initializers/constants'
14import { sanitizeUrl } from '@server/helpers/core-utils' 13import { PluginModel } from '../../models/server/plugin'
14import { PluginManager } from './plugin-manager'
15 15
16async function listAvailablePluginsFromIndex (options: PeertubePluginIndexList) { 16async function listAvailablePluginsFromIndex (options: PeertubePluginIndexList) {
17 const { start = 0, count = 20, search, sort = 'npmName', pluginType } = options 17 const { start = 0, count = 20, search, sort = 'npmName', pluginType } = options
18 18
19 const qs: PeertubePluginIndexList = { 19 const searchParams: PeertubePluginIndexList & Record<string, string | number> = {
20 start, 20 start,
21 count, 21 count,
22 sort, 22 sort,
@@ -28,7 +28,7 @@ async function listAvailablePluginsFromIndex (options: PeertubePluginIndexList)
28 const uri = CONFIG.PLUGINS.INDEX.URL + '/api/v1/plugins' 28 const uri = CONFIG.PLUGINS.INDEX.URL + '/api/v1/plugins'
29 29
30 try { 30 try {
31 const { body } = await doRequest<any>({ uri, qs, json: true }) 31 const { body } = await doJSONRequest<any>(uri, { searchParams })
32 32
33 logger.debug('Got result from PeerTube index.', { body }) 33 logger.debug('Got result from PeerTube index.', { body })
34 34
@@ -58,12 +58,28 @@ async function getLatestPluginsVersion (npmNames: string[]): Promise<PeertubePlu
58 58
59 const uri = sanitizeUrl(CONFIG.PLUGINS.INDEX.URL) + '/api/v1/plugins/latest-version' 59 const uri = sanitizeUrl(CONFIG.PLUGINS.INDEX.URL) + '/api/v1/plugins/latest-version'
60 60
61 const { body } = await doRequest<any>({ uri, body: bodyRequest, json: true, method: 'POST' }) 61 const options = {
62 json: bodyRequest,
63 method: 'POST' as 'POST'
64 }
65 const { body } = await doJSONRequest<PeertubePluginLatestVersionResponse>(uri, options)
62 66
63 return body 67 return body
64} 68}
65 69
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
66export { 81export {
67 listAvailablePluginsFromIndex, 82 listAvailablePluginsFromIndex,
83 getLatestPluginVersion,
68 getLatestPluginsVersion 84 getLatestPluginsVersion
69} 85}
diff --git a/server/lib/plugins/plugin-manager.ts b/server/lib/plugins/plugin-manager.ts
index c19b40135..03ea48416 100644
--- a/server/lib/plugins/plugin-manager.ts
+++ b/server/lib/plugins/plugin-manager.ts
@@ -1,3 +1,4 @@
1import decache from 'decache'
1import * as express from 'express' 2import * as express from 'express'
2import { createReadStream, createWriteStream } from 'fs' 3import { createReadStream, createWriteStream } from 'fs'
3import { outputFile, readJSON } from 'fs-extra' 4import { outputFile, readJSON } from 'fs-extra'
@@ -327,11 +328,18 @@ export class PluginManager implements ServerHook {
327 return plugin 328 return plugin
328 } 329 }
329 330
330 async update (toUpdate: string, version?: string, fromDisk = false) { 331 async update (toUpdate: string, fromDisk = false) {
331 const npmName = fromDisk ? basename(toUpdate) : toUpdate 332 const npmName = fromDisk ? basename(toUpdate) : toUpdate
332 333
333 logger.info('Updating plugin %s.', npmName) 334 logger.info('Updating plugin %s.', npmName)
334 335
336 // Use the latest version from DB, to not upgrade to a version that does not support our PeerTube version
337 let version: string
338 if (!fromDisk) {
339 const plugin = await PluginModel.loadByNpmName(toUpdate)
340 version = plugin.latestVersion
341 }
342
335 // Unregister old hooks 343 // Unregister old hooks
336 await this.unregister(npmName) 344 await this.unregister(npmName)
337 345
@@ -411,7 +419,7 @@ export class PluginManager implements ServerHook {
411 419
412 // Delete cache if needed 420 // Delete cache if needed
413 const modulePath = join(pluginPath, packageJSON.library) 421 const modulePath = join(pluginPath, packageJSON.library)
414 delete require.cache[modulePath] 422 decache(modulePath)
415 const library: PluginLibrary = require(modulePath) 423 const library: PluginLibrary = require(modulePath)
416 424
417 if (!isLibraryCodeValid(library)) { 425 if (!isLibraryCodeValid(library)) {
diff --git a/server/lib/plugins/register-helpers.ts b/server/lib/plugins/register-helpers.ts
index 1f2a88c27..c018e54a8 100644
--- a/server/lib/plugins/register-helpers.ts
+++ b/server/lib/plugins/register-helpers.ts
@@ -7,7 +7,7 @@ import {
7 VIDEO_PLAYLIST_PRIVACIES, 7 VIDEO_PLAYLIST_PRIVACIES,
8 VIDEO_PRIVACIES 8 VIDEO_PRIVACIES
9} from '@server/initializers/constants' 9} from '@server/initializers/constants'
10import { onExternalUserAuthenticated } from '@server/lib/auth' 10import { onExternalUserAuthenticated } from '@server/lib/auth/external-auth'
11import { PluginModel } from '@server/models/server/plugin' 11import { PluginModel } from '@server/models/server/plugin'
12import { 12import {
13 RegisterServerAuthExternalOptions, 13 RegisterServerAuthExternalOptions,
@@ -109,7 +109,7 @@ export class RegisterHelpers {
109 const unregisterIdAndPassAuth = this.buildUnregisterIdAndPassAuth() 109 const unregisterIdAndPassAuth = this.buildUnregisterIdAndPassAuth()
110 const unregisterExternalAuth = this.buildUnregisterExternalAuth() 110 const unregisterExternalAuth = this.buildUnregisterExternalAuth()
111 111
112 const peertubeHelpers = buildPluginHelpers(this.npmName) 112 const peertubeHelpers = buildPluginHelpers(this.plugin, this.npmName)
113 113
114 return { 114 return {
115 registerHook, 115 registerHook,
diff --git a/server/lib/plugins/yarn.ts b/server/lib/plugins/yarn.ts
index e40351b6e..3f45681d3 100644
--- a/server/lib/plugins/yarn.ts
+++ b/server/lib/plugins/yarn.ts
@@ -1,14 +1,17 @@
1import { outputJSON, pathExists } from 'fs-extra'
2import { join } from 'path'
1import { execShell } from '../../helpers/core-utils' 3import { execShell } from '../../helpers/core-utils'
2import { logger } from '../../helpers/logger'
3import { isNpmPluginNameValid, isPluginVersionValid } from '../../helpers/custom-validators/plugins' 4import { isNpmPluginNameValid, isPluginVersionValid } from '../../helpers/custom-validators/plugins'
5import { logger } from '../../helpers/logger'
4import { CONFIG } from '../../initializers/config' 6import { CONFIG } from '../../initializers/config'
5import { outputJSON, pathExists } from 'fs-extra' 7import { getLatestPluginVersion } from './plugin-index'
6import { join } from 'path'
7 8
8async function installNpmPlugin (npmName: string, version?: string) { 9async function installNpmPlugin (npmName: string, versionArg?: string) {
9 // Security check 10 // Security check
10 checkNpmPluginNameOrThrow(npmName) 11 checkNpmPluginNameOrThrow(npmName)
11 if (version) checkPluginVersionOrThrow(version) 12 if (versionArg) checkPluginVersionOrThrow(versionArg)
13
14 const version = versionArg || await getLatestPluginVersion(npmName)
12 15
13 let toInstall = npmName 16 let toInstall = npmName
14 if (version) toInstall += `@${version}` 17 if (version) toInstall += `@${version}`