toggle plugins / save dynamic plugin settings

This commit is contained in:
lesion 2022-08-31 11:16:42 +02:00
parent 982db2b51d
commit 246f1dfb20
No known key found for this signature in database
GPG key ID: 352918250B012177
7 changed files with 433 additions and 377 deletions

View file

@ -10,7 +10,10 @@ v-container
v-card-text
v-form(v-model='valid' ref='form' lazy-validation)
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-spacer
@ -19,9 +22,9 @@ v-container
:disable='!valid || loading') {{ $t('common.save') }}
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-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
p {{ plugin.description }}
blockquote author: {{ plugin.author }}
@ -33,6 +36,7 @@ v-container
</template>
<script>
import { mdiPencil, mdiChevronLeft, mdiChevronRight, mdiMagnify, mdiEye } from '@mdi/js'
import { mapActions, mapState } from 'vuex'
export default {
data() {
@ -42,7 +46,6 @@ export default {
dialog: false,
valid: false,
selectedPlugin: {},
pluginSettings: {},
plugins: [],
headers: [
{ value: 'name', text: 'Name' },
@ -51,22 +54,26 @@ export default {
]
}
},
async fetch() {
async mounted() {
this.plugins = await this.$axios.$get('/plugins')
},
computed: mapState(['settings']),
methods: {
saveSettings() {
console.error(this.pluginSettings)
this.setSetting({ key: 'plugin_' + this.selectedPlugin.name, value: this.pluginSettings })
...mapActions(['setSetting']),
async saveSettings() {
this.loading = true
this.setSetting({
key: 'plugin_' + this.selectedPlugin.name,
value: this.selectedPlugin.settingsValue
})
this.loading = false
this.dialog = false
},
toggleEnable(plugin) {
this.pluginSettings.enable = !this.pluginSettings.enable
this.setSetting({ key: 'plugin_' + this.selectedPlugin.name, value: this.pluginSettings })
async toggleEnable(plugin) {
await this.$axios.$put(`/plugin/${plugin.name}`)
},
setOptions(plugin) {
console.error(plugin)
this.selectedPlugin = plugin
console.error(plugin)
this.dialog = true
}
}

View file

@ -88,7 +88,8 @@
"max_events": "N. max events",
"label": "Label",
"collections": "Collections",
"close": "Close"
"close": "Close",
"plugins": "Plugins"
},
"login": {
"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>",
"new_collection": "New collection",
"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": {
"not_confirmed": "Not confirmed yet…",

View file

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

View file

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

View file

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

View file

@ -48,14 +48,14 @@ domPurify.addHook('beforeSanitizeElements', node => {
module.exports = {
randomString (length = 12) {
randomString(length = 12) {
const wishlist = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
return Array.from(crypto.randomFillSync(new Uint32Array(length)))
.map(x => wishlist[x % wishlist.length])
.join('')
},
sanitizeHTML (html) {
sanitizeHTML(html) {
return domPurify.sanitize(html, {
ALLOWED_TAGS: ['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'br', 'i', 'span',
'h6', 'b', 'a', 'li', 'ul', 'ol', 'code', 'blockquote', 'u', 's', 'strong'],
@ -63,7 +63,7 @@ module.exports = {
})
},
async setUserLocale (req, res, next) {
async setUserLocale(req, res, next) {
// select locale based on cookie? and accept-language header
acceptLanguage.languages(Object.keys(locales))
res.locals.acceptedLocale = acceptLanguage.get(req.headers['accept-language'])
@ -71,26 +71,43 @@ module.exports = {
next()
},
async initSettings (_req, res, next) {
async initSettings(_req, res, next) {
// initialize settings
res.locals.settings = cloneDeep(settingsController.settings)
delete res.locals.settings.smtp
delete res.locals.settings.publicKey
res.locals.settings.baseurl = config.baseurl
res.locals.settings.hostname = config.hostname
res.locals.settings.title = res.locals.settings.title || config.title
res.locals.settings.description = res.locals.settings.description || config.description
res.locals.settings.version = pkg.version
// res.locals.settings = cloneDeep(settingsController.settings)
const settings = settingsController.settings
res.locals.settings = {
title: settings.title || config.title,
description: settings.description || config.description,
baseurl: config.baseurl,
hostname: config.hostname,
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
res.locals.user_locale = settingsController.user_locale[res.locals.acceptedLocale]
dayjs.tz.setDefault(res.locals.settings.instance_timezone)
next()
},
serveStatic () {
serveStatic() {
const router = express.Router()
// serve images/thumb
router.use('/media/', express.static(config.upload_path, { immutable: true, maxAge: '1y' } ), (_req, res) => res.sendStatus(404))
router.use('/media/', express.static(config.upload_path, { immutable: true, maxAge: '1y' }), (_req, res) => res.sendStatus(404))
router.use('/noimg.svg', express.static('./static/noimg.svg'))
router.use('/logo.png', (req, res, next) => {
@ -106,12 +123,12 @@ module.exports = {
return router
},
logRequest (req, _res, next) {
logRequest(req, _res, next) {
log.debug(`${req.method} ${req.path}`)
next()
},
col (field) {
col(field) {
if (config.db.dialect === 'postgres') {
return '"' + field.split('.').join('"."') + '"'
} else if (config.db.dialect === 'mariadb') {
@ -121,14 +138,14 @@ module.exports = {
}
},
async getImageFromURL (url) {
async getImageFromURL(url) {
log.debug(`getImageFromURL ${url}`)
const filename = crypto.randomBytes(16).toString('hex')
const sharpStream = sharp({ failOnError: true })
const promises = [
sharpStream.clone().resize(500, null, { withoutEnlargement: true }).jpeg({ effort: 6, mozjpeg: true }).toFile(path.resolve(config.upload_path, 'thumb', filename + '.jpg')),
sharpStream.clone().resize(1200, null, { withoutEnlargement: true } ).jpeg({ quality: 95, effort: 6, mozjpeg: true}).toFile(path.resolve(config.upload_path, filename + '.jpg')),
sharpStream.clone().resize(1200, null, { withoutEnlargement: true }).jpeg({ quality: 95, effort: 6, mozjpeg: true }).toFile(path.resolve(config.upload_path, filename + '.jpg')),
]
const response = await axios({ method: 'GET', url: encodeURI(url), responseType: 'stream' })
@ -157,7 +174,7 @@ module.exports = {
* Import events from url
* It does supports ICS and H-EVENT
*/
async importURL (req, res) {
async importURL(req, res) {
const URL = req.query.URL
try {
const response = await axios.get(URL)
@ -210,7 +227,7 @@ module.exports = {
}
},
getWeekdayN (date, n, weekday) {
getWeekdayN(date, n, weekday) {
let cursor
if (n === -1) {
cursor = date.endOf('month')
@ -228,7 +245,7 @@ module.exports = {
return cursor
},
async APRedirect (req, res, next) {
async APRedirect(req, res, next) {
const acceptJson = req.accepts('html', 'application/activity+json') === 'application/activity+json'
if (acceptJson) {
const eventController = require('../server/api/controller/event')
@ -240,7 +257,7 @@ module.exports = {
next()
},
async feedRedirect (req, res, next) {
async feedRedirect(req, res, next) {
const accepted = req.accepts('html', 'application/rss+xml', 'text/calendar')
if (['application/rss+xml', 'text/calendar'].includes(accepted) && /^\/(tag|place|collection)\/.*/.test(req.path)) {
return res.redirect((accepted === 'application/rss+xml' ? '/feed/rss' : '/feed/ics') + req.path)

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"
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:
version "1.2.1"
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"
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:
version "0.5.37"
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"
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:
version "1.0.1"
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"
yallist "^4.0.0"
telegraf@^4.9.0:
version "4.9.0"
resolved "https://registry.yarnpkg.com/telegraf/-/telegraf-4.9.0.tgz#30997bf6cbc941bff0bb698b13154647fc918032"
integrity sha512-fL+KLaYRElo4aNtrqAHmFdVsAMHHOHbmdTsAAzMJafhXJu1oYKTRMNipJ/YUvwNd5DnZY+zmGMgVVe1M/dkN4w==
telegraf@^4.9.1:
version "4.9.1"
resolved "https://registry.yarnpkg.com/telegraf/-/telegraf-4.9.1.tgz#bc12e98ea75f2aaf72c5c4a8a0d49c68837bf0b7"
integrity sha512-MukWpKvAZ6/HpT3yHXz+jwUf2HsPa9TcsqPLQjJ+kHNGUS2PLgaNX690ExdWmWPuxjVjC4wNHmZ9JetO3C/tVA==
dependencies:
abort-controller "^3.0.0"
debug "^4.3.3"
es-main "^1.2.0"
minimist "^1.2.6"
module-alias "^2.2.2"
mri "^1.2.0"
node-fetch "^2.6.7"
p-timeout "^4.1.0"
safe-compare "^1.1.4"
sandwich-stream "^2.0.2"
typegram "github:MKRhere/typegram#e2ba9f01f14b96c6dbb1c617e66692a3cc776cf6"
typegram "^3.11.0"
terminal-link@^2.0.0:
version "2.1.1"
@ -12069,9 +12062,10 @@ typedarray@^0.0.6:
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
integrity sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA==
"typegram@github:MKRhere/typegram#e2ba9f01f14b96c6dbb1c617e66692a3cc776cf6":
version "3.10.0"
resolved "https://codeload.github.com/MKRhere/typegram/tar.gz/e2ba9f01f14b96c6dbb1c617e66692a3cc776cf6"
typegram@^3.11.0:
version "3.11.0"
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:
version "1.0.2"