toggle plugins / save dynamic plugin settings

This commit is contained in:
lesion
2022-08-31 11:16:42 +02:00
parent 982db2b51d
commit 246f1dfb20
7 changed files with 433 additions and 377 deletions

View File

@@ -10,7 +10,10 @@ v-container
v-card-text v-card-text
v-form(v-model='valid' ref='form' lazy-validation) v-form(v-model='valid' ref='form' lazy-validation)
div(v-for='(setting, name) in selectedPlugin.settings') div(v-for='(setting, name) in selectedPlugin.settings')
v-text-field(v-model='pluginSettings[name]' type='text' :label='setting.description') v-text-field(v-if='setting.type === "TEXT"' v-model='selectedPlugin.settingsValue[name]' type='text' :label='setting.description')
v-text-field(v-if='setting.type === "NUMBER"' v-model='selectedPlugin.settingsValue[name]' type='number' :label='setting.description')
v-switch(v-if='setting.type === "CHECK"' v-model='selectedPlugin.settingsValue[name]' :label='setting.description')
v-select(v-if='setting.type === "LIST"' v-model='selectedPlugin.settingsValue[name]' :items='setting.items' :label='setting.description')
v-card-actions v-card-actions
v-spacer v-spacer
@@ -19,9 +22,9 @@ v-container
:disable='!valid || loading') {{ $t('common.save') }} :disable='!valid || loading') {{ $t('common.save') }}
v-card-text v-card-text
v-card(v-for='plugin in plugins' :key='plugin.name' max-width="400" elevation='10' color='secondary') v-card(v-for='plugin in plugins' :key='plugin.name' max-width="400" elevation='10' color='secondary' dark)
v-card-title.d-block {{ plugin.name }} v-card-title.d-block {{ plugin.name }}
v-switch.float-right(:label="$t('common.enable')" @change='toggleEnable(plugin)') v-switch.float-right(:label="$t('common.enable')" v-model='plugin.settingsValue.enable' @change='toggleEnable(plugin)')
v-card-text v-card-text
p {{ plugin.description }} p {{ plugin.description }}
blockquote author: {{ plugin.author }} blockquote author: {{ plugin.author }}
@@ -33,6 +36,7 @@ v-container
</template> </template>
<script> <script>
import { mdiPencil, mdiChevronLeft, mdiChevronRight, mdiMagnify, mdiEye } from '@mdi/js' import { mdiPencil, mdiChevronLeft, mdiChevronRight, mdiMagnify, mdiEye } from '@mdi/js'
import { mapActions, mapState } from 'vuex'
export default { export default {
data() { data() {
@@ -42,7 +46,6 @@ export default {
dialog: false, dialog: false,
valid: false, valid: false,
selectedPlugin: {}, selectedPlugin: {},
pluginSettings: {},
plugins: [], plugins: [],
headers: [ headers: [
{ value: 'name', text: 'Name' }, { value: 'name', text: 'Name' },
@@ -51,22 +54,26 @@ export default {
] ]
} }
}, },
async fetch() { async mounted() {
this.plugins = await this.$axios.$get('/plugins') this.plugins = await this.$axios.$get('/plugins')
}, },
computed: mapState(['settings']),
methods: { methods: {
saveSettings() { ...mapActions(['setSetting']),
console.error(this.pluginSettings) async saveSettings() {
this.setSetting({ key: 'plugin_' + this.selectedPlugin.name, value: this.pluginSettings }) this.loading = true
this.setSetting({
key: 'plugin_' + this.selectedPlugin.name,
value: this.selectedPlugin.settingsValue
})
this.loading = false
this.dialog = false
}, },
toggleEnable(plugin) { async toggleEnable(plugin) {
this.pluginSettings.enable = !this.pluginSettings.enable await this.$axios.$put(`/plugin/${plugin.name}`)
this.setSetting({ key: 'plugin_' + this.selectedPlugin.name, value: this.pluginSettings })
}, },
setOptions(plugin) { setOptions(plugin) {
console.error(plugin)
this.selectedPlugin = plugin this.selectedPlugin = plugin
console.error(plugin)
this.dialog = true this.dialog = true
} }
} }

View File

@@ -88,7 +88,8 @@
"max_events": "N. max events", "max_events": "N. max events",
"label": "Label", "label": "Label",
"collections": "Collections", "collections": "Collections",
"close": "Close" "close": "Close",
"plugins": "Plugins"
}, },
"login": { "login": {
"description": "By logging in you can publish new events.", "description": "By logging in you can publish new events.",
@@ -242,7 +243,9 @@
"wrong_domain_warning": "The baseurl configured in config.json <b>({baseurl})</b> differs from the one you're visiting <b>({url})</b>", "wrong_domain_warning": "The baseurl configured in config.json <b>({baseurl})</b> differs from the one you're visiting <b>({url})</b>",
"new_collection": "New collection", "new_collection": "New collection",
"collections_description": "Collections are groupings of events by tags and places. They will be displayed on the home page", "collections_description": "Collections are groupings of events by tags and places. They will be displayed on the home page",
"edit_collection": "Edit Collection" "edit_collection": "Edit Collection",
"config_plugin": "Plugin configuration",
"plugins_description": ""
}, },
"auth": { "auth": {
"not_confirmed": "Not confirmed yet…", "not_confirmed": "Not confirmed yet…",

View File

@@ -69,7 +69,7 @@
"sequelize-slugify": "^1.6.1", "sequelize-slugify": "^1.6.1",
"sharp": "^0.27.2", "sharp": "^0.27.2",
"sqlite3": "^5.0.11", "sqlite3": "^5.0.11",
"telegraf": "^4.9.0", "telegraf": "^4.9.1",
"tiptap": "^1.32.0", "tiptap": "^1.32.0",
"tiptap-extensions": "^1.35.0", "tiptap-extensions": "^1.35.0",
"umzug": "^2.3.0", "umzug": "^2.3.0",

View File

@@ -5,28 +5,69 @@ const config = require('../../config')
const pluginController = { const pluginController = {
plugins: [], plugins: [],
getAll(req, res, next) { getAll(_req, res) {
res.json(pluginController.plugins) const settingsController = require('./settings')
}, // return plugins and inner settings
const plugins = pluginController.plugins.map(p => {
togglePlugin(req, res, next) { if (settingsController.settings['plugin_' + p.name]) {
const plugin = req.params.plugin p.settingsValue = settingsController.settings['plugin_' + p.name]
if (this.plugins[plugin].enable) {
} }
return p
})
return res.json(plugins)
}, },
unloadPlugin(plugin) { togglePlugin(req, res) {
log.info('Unload plugin ' + plugin) const settingsController = require('./settings')
const pluginName = req.params.plugin
const pluginSettings = settingsController.settings['plugin_' + pluginName]
if (!pluginSettings) { return res.sendStatus(404) }
if (!pluginSettings.enable) {
pluginController.loadPlugin(pluginName)
} else {
pluginController.unloadPlugin(pluginName)
}
settingsController.set('plugin_' + pluginName,
{ ...pluginSettings, enable: !pluginSettings.enable })
res.json()
}, },
loadPlugin(pluginName) { unloadPlugin(pluginName) {
const plugin = this.plugins[pluginName] const settingsController = require('./settings')
const plugin = pluginController.plugins.find(p => p.name === pluginName)
const settings = settingsController.settings['plugin_' + pluginName]
if (!plugin) { if (!plugin) {
log.warn(`Plugin ${pluginName} not found`) log.warn(`Plugin ${pluginName} not found`)
return return
} }
const notifier = require('../../notifier')
log.info('Unload plugin ' + plugin)
if (typeof plugin.onEventCreate === 'function') {
notifier.emitter.off('Create', plugin.onEventCreate)
}
if (typeof plugin.onEventDelete === 'function') {
notifier.emitter.off('Delete', plugin.onEventDelete)
}
if (typeof plugin.onEventUpdate === 'function') {
notifier.emitter.off('Update', plugin.onEventUpdate)
}
if (plugin.unload && typeof plugin.unload === 'function') {
plugin.unload({ settings: settingsController.settings }, settings)
}
},
loadPlugin(pluginName) {
const settingsController = require('./settings')
const plugin = pluginController.plugins.find(p => p.name === pluginName)
const settings = settingsController.settings['plugin_' + pluginName]
if (!plugin) {
log.warn(`Plugin ${pluginName} not found`)
return
}
const notifier = require('../../notifier')
log.info('Load plugin ' + plugin)
if (typeof plugin.onEventCreate === 'function') { if (typeof plugin.onEventCreate === 'function') {
notifier.emitter.on('Create', plugin.onEventCreate) notifier.emitter.on('Create', plugin.onEventCreate)
} }
@@ -37,7 +78,9 @@ const pluginController = {
notifier.emitter.on('Update', plugin.onEventUpdate) notifier.emitter.on('Update', plugin.onEventUpdate)
} }
plugin.load({ settings: settingsController.settings }, settingsController.settings.plugins) if (plugin.unload && typeof plugin.unload === 'function') {
plugin.load({ settings: settingsController.settings }, settings)
}
}, },
_load() { _load() {
@@ -46,7 +89,6 @@ const pluginController = {
const plugins_path = config.plugins_path || path.resolve(process.env.cwd || '', 'gancio_plugins') const plugins_path = config.plugins_path || path.resolve(process.env.cwd || '', 'gancio_plugins')
log.info(`Loading plugin ${plugins_path}`) log.info(`Loading plugin ${plugins_path}`)
if (fs.existsSync(plugins_path)) { if (fs.existsSync(plugins_path)) {
const notifier = require('../../notifier')
const plugins = fs.readdirSync(plugins_path) const plugins = fs.readdirSync(plugins_path)
.map(e => path.resolve(plugins_path, e, 'index.js')) .map(e => path.resolve(plugins_path, e, 'index.js'))
.filter(index => fs.existsSync(index)) .filter(index => fs.existsSync(index))
@@ -57,19 +99,11 @@ const pluginController = {
const name = plugin.configuration.name const name = plugin.configuration.name
console.log(`Found plugin '${name}'`) console.log(`Found plugin '${name}'`)
pluginController.plugins.push(plugin.configuration) pluginController.plugins.push(plugin.configuration)
console.error(settingsController.settings['plugin_' + name])
if (settingsController.settings['plugin_' + name]) { if (settingsController.settings['plugin_' + name]) {
const pluginSetting = settingsController.settings['plugin_' + name] const pluginSetting = settingsController.settings['plugin_' + name]
if (pluginSetting.enable) { if (pluginSetting.enable) {
plugin.load({ settings: settingsController.settings }, settingsController.settings.plugins) pluginController.loadPlugin(name)
if (typeof plugin.onEventCreate === 'function') {
notifier.emitter.on('Create', plugin.onEventCreate)
}
if (typeof plugin.onEventDelete === 'function') {
notifier.emitter.on('Delete', plugin.onEventDelete)
}
if (typeof plugin.onEventUpdate === 'function') {
notifier.emitter.on('Update', plugin.onEventUpdate)
}
} }
} else { } else {
settingsController.set('plugin_' + name, { enable: false }) settingsController.set('plugin_' + name, { enable: false })

View File

@@ -192,6 +192,7 @@ if (config.status !== 'READY') {
// - PLUGINS // - PLUGINS
api.get('/plugins', isAdmin, pluginController.getAll) api.get('/plugins', isAdmin, pluginController.getAll)
api.put('/plugin/:plugin', isAdmin, pluginController.togglePlugin)
// OAUTH // OAUTH
api.get('/clients', isAuth, oauthController.getClients) api.get('/clients', isAuth, oauthController.getClients)

View File

@@ -73,14 +73,31 @@ module.exports = {
async initSettings(_req, res, next) { async initSettings(_req, res, next) {
// initialize settings // initialize settings
res.locals.settings = cloneDeep(settingsController.settings) // res.locals.settings = cloneDeep(settingsController.settings)
delete res.locals.settings.smtp const settings = settingsController.settings
delete res.locals.settings.publicKey res.locals.settings = {
res.locals.settings.baseurl = config.baseurl title: settings.title || config.title,
res.locals.settings.hostname = config.hostname description: settings.description || config.description,
res.locals.settings.title = res.locals.settings.title || config.title baseurl: config.baseurl,
res.locals.settings.description = res.locals.settings.description || config.description hostname: config.hostname,
res.locals.settings.version = pkg.version version: pkg.version,
instance_timezone: settings.instance_timezone,
instance_locale: settings.instance_locale,
instance_name: settings.instance_name,
instance_place: settings.instance_place,
allow_registration: settings.allow_registration,
allow_anon_event: settings.allow_anon_event,
allow_recurrent_event: settings.allow_recurrent_event,
recurrent_event_visible: settings.recurrent_event_visible,
enable_federation: settings.enable_federation,
enable_resources: settings.enable_resources,
hide_boosts: settings.hide_boosts,
enable_trusted_instances: settings.enable_trusted_instances,
trusted_instances: settings.trusted_instances,
'theme.is_dark': settings['theme.is_dark'],
'theme.primary': settings['theme.primary'],
footerLinks: settings.footerLinks
}
// set user locale // set user locale
res.locals.user_locale = settingsController.user_locale[res.locals.acceptedLocale] res.locals.user_locale = settingsController.user_locale[res.locals.acceptedLocale]
dayjs.tz.setDefault(res.locals.settings.instance_timezone) dayjs.tz.setDefault(res.locals.settings.instance_timezone)

View File

@@ -4846,11 +4846,6 @@ es-array-method-boxes-properly@^1.0.0:
resolved "https://registry.yarnpkg.com/es-array-method-boxes-properly/-/es-array-method-boxes-properly-1.0.0.tgz#873f3e84418de4ee19c5be752990b2e44718d09e" resolved "https://registry.yarnpkg.com/es-array-method-boxes-properly/-/es-array-method-boxes-properly-1.0.0.tgz#873f3e84418de4ee19c5be752990b2e44718d09e"
integrity sha512-wd6JXUmyHmt8T5a2xreUwKcGPq6f1f+WwIJkijUqiGcJz1qqnZgP6XIK+QyIWU5lT7imeNxUll48bziG+TSYcA== integrity sha512-wd6JXUmyHmt8T5a2xreUwKcGPq6f1f+WwIJkijUqiGcJz1qqnZgP6XIK+QyIWU5lT7imeNxUll48bziG+TSYcA==
es-main@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/es-main/-/es-main-1.2.0.tgz#b85954f1d9d9f542fcb08685ec19515f969bad16"
integrity sha512-A4tCSY43O/mH4rHjG1n0mI4DhK2BmKDr8Lk8PXK/GBB6zxGFGmIW4bbkbTQ2Gi9iNamMZ9vbGrwjZOIeiM7vMw==
es-to-primitive@^1.2.1: es-to-primitive@^1.2.1:
version "1.2.1" version "1.2.1"
resolved "https://registry.yarnpkg.com/es-to-primitive/-/es-to-primitive-1.2.1.tgz#e55cd4c9cdc188bcefb03b366c736323fc5c898a" resolved "https://registry.yarnpkg.com/es-to-primitive/-/es-to-primitive-1.2.1.tgz#e55cd4c9cdc188bcefb03b366c736323fc5c898a"
@@ -8109,11 +8104,6 @@ mkdirp@^1.0.3, mkdirp@^1.0.4:
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e" resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e"
integrity sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw== integrity sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==
module-alias@^2.2.2:
version "2.2.2"
resolved "https://registry.yarnpkg.com/module-alias/-/module-alias-2.2.2.tgz#151cdcecc24e25739ff0aa6e51e1c5716974c0e0"
integrity sha512-A/78XjoX2EmNvppVWEhM2oGk3x4lLxnkEA4jTbaK97QKSDjkIoOsKQlfylt/d3kKKi596Qy3NP5XrXJ6fZIC9Q==
moment-timezone@^0.5.34: moment-timezone@^0.5.34:
version "0.5.37" version "0.5.37"
resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.5.37.tgz#adf97f719c4e458fdb12e2b4e87b8bec9f4eef1e" resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.5.37.tgz#adf97f719c4e458fdb12e2b4e87b8bec9f4eef1e"
@@ -8143,6 +8133,11 @@ move-concurrently@^1.0.1:
rimraf "^2.5.4" rimraf "^2.5.4"
run-queue "^1.0.3" run-queue "^1.0.3"
mri@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/mri/-/mri-1.2.0.tgz#6721480fec2a11a4889861115a48b6cbe7cc8f0b"
integrity sha512-tzzskb3bG8LvYGFF/mDTpq3jpI6Q9wc3LEmBaghu+DdCssd1FakN7Bc0hVNmEyGq1bq3RgfkCb3cmQLpNPOroA==
mrmime@^1.0.0: mrmime@^1.0.0:
version "1.0.1" version "1.0.1"
resolved "https://registry.yarnpkg.com/mrmime/-/mrmime-1.0.1.tgz#5f90c825fad4bdd41dc914eff5d1a8cfdaf24f27" resolved "https://registry.yarnpkg.com/mrmime/-/mrmime-1.0.1.tgz#5f90c825fad4bdd41dc914eff5d1a8cfdaf24f27"
@@ -11672,21 +11667,19 @@ tar@^6.0.2, tar@^6.1.11, tar@^6.1.2:
mkdirp "^1.0.3" mkdirp "^1.0.3"
yallist "^4.0.0" yallist "^4.0.0"
telegraf@^4.9.0: telegraf@^4.9.1:
version "4.9.0" version "4.9.1"
resolved "https://registry.yarnpkg.com/telegraf/-/telegraf-4.9.0.tgz#30997bf6cbc941bff0bb698b13154647fc918032" resolved "https://registry.yarnpkg.com/telegraf/-/telegraf-4.9.1.tgz#bc12e98ea75f2aaf72c5c4a8a0d49c68837bf0b7"
integrity sha512-fL+KLaYRElo4aNtrqAHmFdVsAMHHOHbmdTsAAzMJafhXJu1oYKTRMNipJ/YUvwNd5DnZY+zmGMgVVe1M/dkN4w== integrity sha512-MukWpKvAZ6/HpT3yHXz+jwUf2HsPa9TcsqPLQjJ+kHNGUS2PLgaNX690ExdWmWPuxjVjC4wNHmZ9JetO3C/tVA==
dependencies: dependencies:
abort-controller "^3.0.0" abort-controller "^3.0.0"
debug "^4.3.3" debug "^4.3.3"
es-main "^1.2.0" mri "^1.2.0"
minimist "^1.2.6"
module-alias "^2.2.2"
node-fetch "^2.6.7" node-fetch "^2.6.7"
p-timeout "^4.1.0" p-timeout "^4.1.0"
safe-compare "^1.1.4" safe-compare "^1.1.4"
sandwich-stream "^2.0.2" sandwich-stream "^2.0.2"
typegram "github:MKRhere/typegram#e2ba9f01f14b96c6dbb1c617e66692a3cc776cf6" typegram "^3.11.0"
terminal-link@^2.0.0: terminal-link@^2.0.0:
version "2.1.1" version "2.1.1"
@@ -12069,9 +12062,10 @@ typedarray@^0.0.6:
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777" resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
integrity sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA== integrity sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA==
"typegram@github:MKRhere/typegram#e2ba9f01f14b96c6dbb1c617e66692a3cc776cf6": typegram@^3.11.0:
version "3.10.0" version "3.11.0"
resolved "https://codeload.github.com/MKRhere/typegram/tar.gz/e2ba9f01f14b96c6dbb1c617e66692a3cc776cf6" resolved "https://registry.yarnpkg.com/typegram/-/typegram-3.11.0.tgz#72e3a8ed0d77864916af465d2c809adf2353ee96"
integrity sha512-4p6u+AFognlsDgBue8Hla2jO7Ax+UQXcLa27LC7xDdAeR9LTe+Cr4vJrYpoO1wgj/BFWgXTeboaH/+1YgWyfpA==
ua-parser-js@^1.0.2: ua-parser-js@^1.0.2:
version "1.0.2" version "1.0.2"