ActivityPub-federated video streaming platform using P2P directly in your web browser
Add plugin settings change watcher
Chocobozzza5896799f169
diff --git a/server/controllers/api/config.ts b/server/controllers/api/config.tsindex 85f3ad3d94..edcb0b99ec 100644--- a/server/controllers/api/config.ts+++ b/server/controllers/api/config.ts@@ -299,7 +299,7 @@ function getExternalAuthsPlugins () {name: p.name,version: p.version,authName: auth.authName,- authDisplayName: auth.authDisplayName+ authDisplayName: auth.authDisplayName()})}}diff --git a/server/controllers/api/plugins.ts b/server/controllers/api/plugins.tsindex 6b7562fd31..f8a0d19ca5 100644--- a/server/controllers/api/plugins.ts+++ b/server/controllers/api/plugins.ts@@ -191,6 +191,8 @@ async function updatePluginSettings (req: express.Request, res: express.Responseplugin.settings = req.body.settingsawait plugin.save()+ await PluginManager.Instance.onSettingsChanged(plugin.name, plugin.settings)+return res.sendStatus(204)}diff --git a/server/lib/plugins/plugin-manager.ts b/server/lib/plugins/plugin-manager.tsindex f7b84b1ff1..950acf7ad7 100644--- a/server/lib/plugins/plugin-manager.ts+++ b/server/lib/plugins/plugin-manager.ts@@ -144,20 +144,6 @@ export class PluginManager implements ServerHook {return this.translations[locale] || {}}- onLogout (npmName: string, authName: string, user: MUser) {- const auth = this.getAuth(npmName, authName)-- if (auth?.onLogout) {- logger.info('Running onLogout function from auth %s of plugin %s', authName, npmName)-- try {- auth.onLogout(user)- } catch (err) {- logger.warn('Cannot run onLogout function from auth %s of plugin %s.', authName, npmName, { err })- }- }- }-async isTokenValid (token: MOAuthTokenUser, type: 'access' | 'refresh') {const auth = this.getAuth(token.User.pluginAuth, token.authName)if (!auth) return true@@ -180,6 +166,37 @@ export class PluginManager implements ServerHook {return true}+ // ###################### External events ######################++ onLogout (npmName: string, authName: string, user: MUser) {+ const auth = this.getAuth(npmName, authName)++ if (auth?.onLogout) {+ logger.info('Running onLogout function from auth %s of plugin %s', authName, npmName)++ try {+ auth.onLogout(user)+ } catch (err) {+ logger.warn('Cannot run onLogout function from auth %s of plugin %s.', authName, npmName, { err })+ }+ }+ }++ onSettingsChanged (name: string, settings: any) {+ const registered = this.getRegisteredPluginByShortName(name)+ if (!registered) {+ logger.error('Cannot find plugin %s to call on settings changed.', name)+ }++ for (const cb of registered.registerHelpersStore.getOnSettingsChangedCallbacks()) {+ try {+ cb(settings)+ } catch (err) {+ logger.error('Cannot run on settings changed callback for %s.', registered.npmName, { err })+ }+ }+ }+// ###################### Hooks ######################async runHook<T> (hookName: ServerHookName, result?: T, params?: any): Promise<T> {diff --git a/server/lib/plugins/register-helpers-store.ts b/server/lib/plugins/register-helpers-store.tsindex 151196bf19..6317ac2cf0 100644--- a/server/lib/plugins/register-helpers-store.ts+++ b/server/lib/plugins/register-helpers-store.ts@@ -52,6 +52,8 @@ export class RegisterHelpersStore {private readonly idAndPassAuths: RegisterServerAuthPassOptions[] = []private readonly externalAuths: RegisterServerAuthExternalOptions[] = []+ private readonly onSettingsChangeCallbacks: ((settings: any) => void)[] = []+private readonly router: express.Routerconstructor (@@ -149,6 +151,10 @@ export class RegisterHelpersStore {return this.externalAuths}+ getOnSettingsChangedCallbacks () {+ return this.onSettingsChangeCallbacks+ }+private buildGetRouter () {return () => this.router}@@ -185,7 +191,7 @@ export class RegisterHelpersStore {const self = thisreturn (options: RegisterServerAuthExternalOptions) => {- if (!options.authName || !options.onAuthRequest || typeof options.onAuthRequest !== 'function') {+ if (!options.authName || typeof options.authDisplayName !== 'function' || typeof options.onAuthRequest !== 'function') {logger.error('Cannot register auth plugin %s: authName of getWeight or login are not valid.', this.npmName)return}@@ -212,7 +218,9 @@ export class RegisterHelpersStore {getSettings: (names: string[]) => PluginModel.getSettings(this.plugin.name, this.plugin.type, names),- setSetting: (name: string, value: string) => PluginModel.setSetting(this.plugin.name, this.plugin.type, name, value)+ setSetting: (name: string, value: string) => PluginModel.setSetting(this.plugin.name, this.plugin.type, name, value),++ onSettingsChange: (cb: (settings: any) => void) => this.onSettingsChangeCallbacks.push(cb)}}diff --git a/server/tests/api/server/plugins.ts b/server/tests/api/server/plugins.tsindex 452d050125..9885be4e84 100644--- a/server/tests/api/server/plugins.ts+++ b/server/tests/api/server/plugins.ts@@ -27,7 +27,8 @@ import {updatePlugin,updatePluginPackageJSON,updatePluginSettings,- wait+ wait,+ waitUntilLog} from '../../../../shared/extra-utils'import { PluginType } from '../../../../shared/models/plugins/plugin.type'import { PeerTubePluginIndex } from '../../../../shared/models/plugins/peertube-plugin-index.model'@@ -142,7 +143,7 @@ describe('Test plugins', function () {it('Should have the correct global css', async function () {const res = await getPluginsCSS(server.url)- expect(res.text).to.contain('--mainBackgroundColor')+ expect(res.text).to.contain('background-color: red')})it('Should have the plugin loaded in the configuration', async function () {@@ -258,6 +259,12 @@ describe('Test plugins', function () {})})+ it('Should have watched settings changes', async function () {+ this.timeout(10000)++ await waitUntilLog(server, 'Settings changed!')+ })+it('Should get a plugin and a theme', async function () {{const res = await getPlugin({diff --git a/server/tests/fixtures/peertube-plugin-test-external-auth-one/main.js b/server/tests/fixtures/peertube-plugin-test-external-auth-one/main.jsindex f29fd1f303..91c67e5504 100644--- a/server/tests/fixtures/peertube-plugin-test-external-auth-one/main.js+++ b/server/tests/fixtures/peertube-plugin-test-external-auth-one/main.js@@ -5,7 +5,7 @@ async function register ({{const result = registerExternalAuth({authName: 'external-auth-1',- authDisplayName: 'External Auth 1',+ authDisplayName: () => 'External Auth 1',onLogout: user => peertubeHelpers.logger.info('On logout %s', user.username),onAuthRequest: (req, res) => {const username = req.query.username@@ -23,7 +23,7 @@ async function register ({{const result = registerExternalAuth({authName: 'external-auth-2',- authDisplayName: 'External Auth 2',+ authDisplayName: () => 'External Auth 2',onAuthRequest: (req, res) => {result.userAuthenticated({req,diff --git a/server/tests/fixtures/peertube-plugin-test-external-auth-two/main.js b/server/tests/fixtures/peertube-plugin-test-external-auth-two/main.jsindex 34fec1bb35..126905ffcb 100644--- a/server/tests/fixtures/peertube-plugin-test-external-auth-two/main.js+++ b/server/tests/fixtures/peertube-plugin-test-external-auth-two/main.js@@ -5,7 +5,7 @@ async function register ({{const result = registerExternalAuth({authName: 'external-auth-3',- authDisplayName: 'External Auth 3',+ authDisplayName: () => 'External Auth 3',onAuthRequest: (req, res) => {result.userAuthenticated({req,diff --git a/shared/models/plugins/plugin-settings-manager.model.ts b/shared/models/plugins/plugin-settings-manager.model.tsindex f83f53b8f8..db88ae6e79 100644--- a/shared/models/plugins/plugin-settings-manager.model.ts+++ b/shared/models/plugins/plugin-settings-manager.model.ts@@ -6,4 +6,6 @@ export interface PluginSettingsManager {getSettings: (names: string[]) => Bluebird<{ [settingName: string]: string | boolean }>setSetting: (name: string, value: string) => Bluebird<any>++ onSettingsChange: (cb: (names: string[]) => void) => void}diff --git a/shared/models/plugins/register-server-auth.model.ts b/shared/models/plugins/register-server-auth.model.tsindex 6539dc8882..4ffce94569 100644--- a/shared/models/plugins/register-server-auth.model.ts+++ b/shared/models/plugins/register-server-auth.model.ts@@ -42,7 +42,7 @@ export interface RegisterServerAuthPassOptions extends RegisterServerAuthBase {export interface RegisterServerAuthExternalOptions extends RegisterServerAuthBase {// Will be displayed in a block next to the login form- authDisplayName: string+ authDisplayName: () => stringonAuthRequest: (req: express.Request, res: express.Response) => void}