Add public settings endpoint

This commit is contained in:
Chocobozzz 2019-07-26 09:35:43 +02:00
parent 23bdacf8ec
commit ba211e7386
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
10 changed files with 94 additions and 28 deletions

View File

@ -66,7 +66,7 @@ export class PluginShowInstalledComponent extends FormReactive implements OnInit
this.pluginService.getPlugin(npmName)
.pipe(switchMap(plugin => {
return this.pluginService.getPluginRegisteredSettings(plugin.name, plugin.type)
.pipe(map(data => ({ plugin, registeredSettings: data.settings })))
.pipe(map(data => ({ plugin, registeredSettings: data.registeredSettings })))
}))
.subscribe(
({ plugin, registeredSettings }) => {

View File

@ -11,7 +11,7 @@ import { PeerTubePlugin } from '@shared/models/plugins/peertube-plugin.model'
import { ManagePlugin } from '@shared/models/plugins/manage-plugin.model'
import { InstallOrUpdatePlugin } from '@shared/models/plugins/install-plugin.model'
import { PeerTubePluginIndex } from '@shared/models/plugins/peertube-plugin-index.model'
import { RegisterServerSettingOptions } from '@shared/models/plugins/register-server-setting.model'
import { RegisteredServerSettings, RegisterServerSettingOptions } from '@shared/models/plugins/register-server-setting.model'
import { PluginService } from '@app/core/plugins/plugin.service'
@Injectable()
@ -91,7 +91,7 @@ export class PluginApiService {
const npmName = this.pluginService.nameToNpmName(pluginName, pluginType)
const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName + '/registered-settings'
return this.authHttp.get<{ settings: RegisterServerSettingOptions[] }>(path)
return this.authHttp.get<RegisteredServerSettings>(path)
.pipe(catchError(res => this.restExtractor.handleError(res)))
}

View File

@ -15,6 +15,7 @@ import { PeerTubePlugin } from '@shared/models/plugins/peertube-plugin.model'
import { HttpClient } from '@angular/common/http'
import { RestExtractor } from '@app/shared/rest'
import { PluginType } from '@shared/models/plugins/plugin.type'
import { PublicServerSetting } from '@shared/models/plugins/public-server.setting'
interface HookStructValue extends RegisterClientHookOptions {
plugin: ServerConfigPlugin
@ -241,11 +242,11 @@ export class PluginService implements ClientHook {
getSettings: () => {
const npmName = this.nameToNpmName(pluginInfo.plugin.name, pluginInfo.pluginType)
const path = PluginService.BASE_PLUGIN_URL + '/' + npmName
const path = PluginService.BASE_PLUGIN_URL + '/' + npmName + '/public-settings'
return this.authHttp.get<PeerTubePlugin>(path)
return this.authHttp.get<PublicServerSetting>(path)
.pipe(
map(p => p.settings),
map(p => p.publicSettings),
catchError(res => this.restExtractor.handleError(res))
)
.toPromise()

View File

@ -26,6 +26,7 @@ import { logger } from '../../helpers/logger'
import { listAvailablePluginsFromIndex } from '../../lib/plugins/plugin-index'
import { PeertubePluginIndexList } from '../../../shared/models/plugins/peertube-plugin-index-list.model'
import { RegisteredServerSettings } from '../../../shared/models/plugins/register-server-setting.model'
import { PublicServerSetting } from '../../../shared/models/plugins/public-server.setting'
const pluginRouter = express.Router()
@ -51,13 +52,6 @@ pluginRouter.get('/',
asyncMiddleware(listPlugins)
)
pluginRouter.get('/:npmName',
authenticate,
ensureUserHasRight(UserRight.MANAGE_PLUGINS),
asyncMiddleware(existingPluginValidator),
getPlugin
)
pluginRouter.get('/:npmName/registered-settings',
authenticate,
ensureUserHasRight(UserRight.MANAGE_PLUGINS),
@ -65,6 +59,11 @@ pluginRouter.get('/:npmName/registered-settings',
getPluginRegisteredSettings
)
pluginRouter.get('/:npmName/public-settings',
asyncMiddleware(existingPluginValidator),
getPublicPluginSettings
)
pluginRouter.put('/:npmName/settings',
authenticate,
ensureUserHasRight(UserRight.MANAGE_PLUGINS),
@ -73,6 +72,13 @@ pluginRouter.put('/:npmName/settings',
asyncMiddleware(updatePluginSettings)
)
pluginRouter.get('/:npmName',
authenticate,
ensureUserHasRight(UserRight.MANAGE_PLUGINS),
asyncMiddleware(existingPluginValidator),
getPlugin
)
pluginRouter.post('/install',
authenticate,
ensureUserHasRight(UserRight.MANAGE_PLUGINS),
@ -161,10 +167,20 @@ async function uninstallPlugin (req: express.Request, res: express.Response) {
return res.sendStatus(204)
}
function getPluginRegisteredSettings (req: express.Request, res: express.Response) {
const settings = PluginManager.Instance.getRegisteredSettings(req.params.npmName)
function getPublicPluginSettings (req: express.Request, res: express.Response) {
const plugin = res.locals.plugin
const registeredSettings = PluginManager.Instance.getRegisteredSettings(req.params.npmName)
const publicSettings = plugin.getPublicSettings(registeredSettings)
const json: RegisteredServerSettings = { settings }
const json: PublicServerSetting = { publicSettings }
return res.json(json)
}
function getPluginRegisteredSettings (req: express.Request, res: express.Response) {
const registeredSettings = PluginManager.Instance.getRegisteredSettings(req.params.npmName)
const json: RegisteredServerSettings = { registeredSettings }
return res.json(json)
}

View File

@ -10,7 +10,7 @@ import {
import { PluginType } from '../../../shared/models/plugins/plugin.type'
import { PeerTubePlugin } from '../../../shared/models/plugins/peertube-plugin.model'
import { FindAndCountOptions, json } from 'sequelize'
import { PeerTubePluginIndex } from '../../../shared/models/plugins/peertube-plugin-index.model'
import { RegisterServerSettingOptions } from '../../../shared/models/plugins/register-server-setting.model'
@DefaultScope(() => ({
attributes: {
@ -238,6 +238,19 @@ export class PluginModel extends Model<PluginModel> {
return 'peertube-plugin-' + name
}
getPublicSettings (registeredSettings: RegisterServerSettingOptions[]) {
const result: { [ name: string ]: string } = {}
const settings = this.settings || {}
for (const r of registeredSettings) {
if (r.private !== false) continue
result[r.name] = settings[r.name] || r.default || null
}
return result
}
toFormattedJSON (): PeerTubePlugin {
return {
name: this.name,

View File

@ -281,7 +281,7 @@ describe('Test server plugins API validators', function () {
})
})
describe('When getting a plugin or the registered settings', function () {
describe('When getting a plugin or the registered settings or public settings', function () {
const path = '/api/v1/plugins/'
it('Should fail with an invalid token', async function () {
@ -307,7 +307,7 @@ describe('Test server plugins API validators', function () {
})
it('Should fail with an invalid npm name', async function () {
for (const suffix of [ 'toto', 'toto/registered-settings' ]) {
for (const suffix of [ 'toto', 'toto/registered-settings', 'toto/public-settings' ]) {
await makeGetRequest({
url: server.url,
path: path + suffix,
@ -316,7 +316,7 @@ describe('Test server plugins API validators', function () {
})
}
for (const suffix of [ 'peertube-plugin-TOTO', 'peertube-plugin-TOTO/registered-settings' ]) {
for (const suffix of [ 'peertube-plugin-TOTO', 'peertube-plugin-TOTO/registered-settings', 'peertube-plugin-TOTO/public-settings' ]) {
await makeGetRequest({
url: server.url,
path: path + suffix,
@ -327,7 +327,7 @@ describe('Test server plugins API validators', function () {
})
it('Should fail with an unknown plugin', async function () {
for (const suffix of [ 'peertube-plugin-toto', 'peertube-plugin-toto/registered-settings' ]) {
for (const suffix of [ 'peertube-plugin-toto', 'peertube-plugin-toto/registered-settings', 'peertube-plugin-toto/public-settings' ]) {
await makeGetRequest({
url: server.url,
path: path + suffix,
@ -338,7 +338,7 @@ describe('Test server plugins API validators', function () {
})
it('Should succeed with the correct parameters', async function () {
for (const suffix of [ npmPlugin, `${npmPlugin}/registered-settings` ]) {
for (const suffix of [ npmPlugin, `${npmPlugin}/registered-settings`, `${npmPlugin}/public-settings` ]) {
await makeGetRequest({
url: server.url,
path: path + suffix,

View File

@ -18,7 +18,7 @@ import {
setPluginVersion, uninstallPlugin,
updateCustomSubConfig, updateMyUser, updatePluginPackageJSON, updatePlugin,
updatePluginSettings,
wait
wait, getPublicSettings
} from '../../../../shared/extra-utils'
import { PluginType } from '../../../../shared/models/plugins/plugin.type'
import { PeerTubePluginIndex } from '../../../../shared/models/plugins/peertube-plugin-index.model'
@ -27,6 +27,7 @@ import { PeerTubePlugin } from '../../../../shared/models/plugins/peertube-plugi
import { User } from '../../../../shared/models/users'
import { PluginPackageJson } from '../../../../shared/models/plugins/plugin-package-json.model'
import { RegisteredServerSettings } from '../../../../shared/models/plugins/register-server-setting.model'
import { PublicServerSetting } from '../../../../shared/models/plugins/public-server.setting'
const expect = chai.expect
@ -217,14 +218,24 @@ describe('Test plugins', function () {
npmName: 'peertube-plugin-hello-world'
})
const settings = (res.body as RegisteredServerSettings).settings
const registeredSettings = (res.body as RegisteredServerSettings).registeredSettings
expect(settings).to.have.length.at.least(1)
expect(registeredSettings).to.have.length.at.least(1)
const adminNameSettings = settings.find(s => s.name === 'admin-name')
const adminNameSettings = registeredSettings.find(s => s.name === 'admin-name')
expect(adminNameSettings).to.not.be.undefined
})
it('Should get public settings', async function () {
const res = await getPublicSettings({ url: server.url, npmName: 'peertube-plugin-hello-world' })
const publicSettings = (res.body as PublicServerSetting).publicSettings
expect(Object.keys(publicSettings)).to.have.lengthOf(1)
expect(Object.keys(publicSettings)).to.deep.equal([ 'user-name' ])
expect(publicSettings['user-name']).to.be.null
})
it('Should update the settings', async function () {
const settings = {
'admin-name': 'Cid'

View File

@ -119,6 +119,21 @@ function getPluginRegisteredSettings (parameters: {
})
}
function getPublicSettings (parameters: {
url: string,
npmName: string,
expectedStatus?: number
}) {
const { url, npmName, expectedStatus = 200 } = parameters
const path = '/api/v1/plugins/' + npmName + '/public-settings'
return makeGetRequest({
url,
path,
statusCodeExpected: expectedStatus
})
}
function installPlugin (parameters: {
url: string,
accessToken: string,
@ -218,5 +233,6 @@ export {
getPackageJSONPath,
updatePluginPackageJSON,
getPluginPackageJSON,
getPluginTestPath
getPluginTestPath,
getPublicSettings
}

View File

@ -0,0 +1,3 @@
export interface PublicServerSetting {
publicSettings: { [ name: string ]: string }
}

View File

@ -2,9 +2,15 @@ export interface RegisterServerSettingOptions {
name: string
label: string
type: 'input'
// If the setting is not private, anyone can view its value
// Mainly used by the PeerTube client to get admin config
private: boolean
// Default setting value
default?: string
}
export interface RegisteredServerSettings {
settings: RegisterServerSettingOptions[]
registeredSettings: RegisterServerSettingOptions[]
}