From dd318eb2db314563e423ddd4f2993149f5a0739b Mon Sep 17 00:00:00 2001 From: NGPixel Date: Mon, 28 May 2018 19:36:35 -0400 Subject: [PATCH] feat: locale namespacing save + auth fetch fix --- client/components/admin/admin-locale.vue | 14 ++++++++-- client/graph/admin-locale-mutation-save.gql | 4 +-- client/graph/admin-locale-query-list.gql | 2 ++ server/core/auth.js | 2 +- server/core/config.js | 13 ++++----- server/core/localization.js | 5 ++-- server/graph/resolvers/authentication.js | 21 +++++++------- server/graph/resolvers/localization.js | 14 ++++++---- server/graph/schemas/localization.graphql | 4 +++ server/jobs/sync-graph-locales.js | 10 +++---- server/setup.js | 31 +++++++++++---------- 11 files changed, 69 insertions(+), 51 deletions(-) diff --git a/client/components/admin/admin-locale.vue b/client/components/admin/admin-locale.vue index d0816cca..216f7c8a 100644 --- a/client/components/admin/admin-locale.vue +++ b/client/components/admin/admin-locale.vue @@ -64,7 +64,7 @@ :value='true' icon='warning' ) - span The locale code will be prefixed to all paths. (e.g. /en/page-name) + span The locale code will be prefixed to all paths. (e.g. /{{ selectedLocale }}/page-name) .caption.grey--text Paths without a locale code will be automatically redirected to the base locale defined above. v-divider v-select( @@ -181,7 +181,9 @@ export default { mutation: localesSaveMutation, variables: { locale: this.selectedLocale, - autoUpdate: this.autoUpdate + autoUpdate: this.autoUpdate, + namespacing: this.namespacing, + namespaces: this.namespaces } }) const resp = _.get(respRaw, 'data.localization.updateLocale.responseResult', {}) @@ -216,6 +218,14 @@ export default { autoUpdate: { query: localesQuery, update: (data) => data.localization.config.autoUpdate + }, + namespacing: { + query: localesQuery, + update: (data) => data.localization.config.namespacing + }, + namespaces: { + query: localesQuery, + update: (data) => data.localization.config.namespaces } } } diff --git a/client/graph/admin-locale-mutation-save.gql b/client/graph/admin-locale-mutation-save.gql index ce0f4de8..df38c1a2 100644 --- a/client/graph/admin-locale-mutation-save.gql +++ b/client/graph/admin-locale-mutation-save.gql @@ -1,6 +1,6 @@ -mutation($locale: String!, $autoUpdate: Boolean!) { +mutation($locale: String!, $autoUpdate: Boolean!, $namespacing: Boolean!, $namespaces: [String]!) { localization { - updateLocale(locale: $locale, autoUpdate: $autoUpdate) { + updateLocale(locale: $locale, autoUpdate: $autoUpdate, namespacing: $namespacing, namespaces: $namespaces) { responseResult { succeeded errorCode diff --git a/client/graph/admin-locale-query-list.gql b/client/graph/admin-locale-query-list.gql index 9012e446..63402965 100644 --- a/client/graph/admin-locale-query-list.gql +++ b/client/graph/admin-locale-query-list.gql @@ -13,6 +13,8 @@ config { locale autoUpdate + namespacing + namespaces } } } diff --git a/server/core/auth.js b/server/core/auth.js index e0b66e71..7ef0deb8 100644 --- a/server/core/auth.js +++ b/server/core/auth.js @@ -34,7 +34,7 @@ module.exports = { async activateStrategies() { try { // Unload any active strategies - WIKI.auth.strategies = [] + WIKI.auth.strategies = {} const currentStrategies = _.keys(passport._strategies) _.pull(currentStrategies, 'session') _.forEach(currentStrategies, stg => { passport.unuse(stg) }) diff --git a/server/core/config.js b/server/core/config.js index 62a20964..d6168491 100644 --- a/server/core/config.js +++ b/server/core/config.js @@ -67,19 +67,18 @@ module.exports = { * @returns Promise */ async saveToDb(keys) { - let trx = await WIKI.db.Objection.transaction.start(WIKI.db.knex) - try { for (let key of keys) { - const value = _.get(WIKI.config, key, null) - let affectedRows = await WIKI.db.settings.query(trx).patch({ value }).where('key', key) + let value = _.get(WIKI.config, key, null) + if (!_.isPlainObject(value)) { + value = { v: value } + } + let affectedRows = await WIKI.db.settings.query().patch({ value }).where('key', key) if (affectedRows === 0 && value) { - await WIKI.db.settings.query(trx).insert({ key, value }) + await WIKI.db.settings.query().insert({ key, value }) } } - await trx.commit() } catch (err) { - await trx.rollback(err) WIKI.logger.error(`Failed to save configuration to DB: ${err.message}`) return false } diff --git a/server/core/localization.js b/server/core/localization.js index 1acbcc24..8a3f112c 100644 --- a/server/core/localization.js +++ b/server/core/localization.js @@ -17,7 +17,7 @@ module.exports = { ns: this.namespaces, defaultNS: 'common', saveMissing: false, - lng: WIKI.config.lang, + lng: WIKI.config.lang.code, fallbackLng: 'en' }) @@ -31,7 +31,7 @@ module.exports = { } // Load current language - this.loadLocale(WIKI.config.lang, { silent: true }) + this.loadLocale(WIKI.config.lang.code, { silent: true }) return this }, @@ -55,7 +55,6 @@ module.exports = { const res = await WIKI.db.locales.query().findOne('code', locale) if (res) { if (_.isPlainObject(res.strings)) { - console.info(res.strings) _.forOwn(res.strings, (data, ns) => { this.namespaces.push(ns) this.engine.addResourceBundle(locale, ns, data, true, true) diff --git a/server/graph/resolvers/authentication.js b/server/graph/resolvers/authentication.js index f639e281..967b5279 100644 --- a/server/graph/resolvers/authentication.js +++ b/server/graph/resolvers/authentication.js @@ -15,18 +15,17 @@ module.exports = { async authentication() { return {} } }, AuthenticationQuery: { - providers(obj, args, context, info) { - let prv = _.map(WIKI.auth.strategies, str => ({ - isEnabled: str.config.isEnabled, - key: str.key, - props: str.props, - title: str.title, - useForm: str.useForm, - config: [] + async providers(obj, args, context, info) { + let strategies = await WIKI.db.authentication.query().orderBy('title') + strategies = strategies.map(stg => ({ + ...stg, + config: _.transform(stg.config, (res, value, key) => { + res.push({ key, value }) + }, []) })) - if (args.filter) { prv = graphHelper.filter(prv, args.filter) } - if (args.orderBy) { prv = graphHelper.orderBy(prv, args.orderBy) } - return prv + if (args.filter) { strategies = graphHelper.filter(strategies, args.filter) } + if (args.orderBy) { strategies = graphHelper.orderBy(strategies, args.orderBy) } + return strategies } }, AuthenticationMutation: { diff --git a/server/graph/resolvers/localization.js b/server/graph/resolvers/localization.js index d0ed3310..ece867c2 100644 --- a/server/graph/resolvers/localization.js +++ b/server/graph/resolvers/localization.js @@ -26,8 +26,10 @@ module.exports = { }, async config(obj, args, context, info) { return { - locale: WIKI.config.site.lang, - autoUpdate: WIKI.config.site.langAutoUpdate + locale: WIKI.config.lang.code, + autoUpdate: WIKI.config.lang.autoUpdate, + namespacing: WIKI.config.lang.namespacing, + namespaces: WIKI.config.lang.namespaces } } }, @@ -49,9 +51,11 @@ module.exports = { }, async updateLocale(obj, args, context) { try { - WIKI.config.site.lang = args.locale - WIKI.config.site.langAutoUpdate = args.autoUpdate - await WIKI.configSvc.saveToDb(['site']) + WIKI.config.lang.code = args.locale + WIKI.config.lang.autoUpdate = args.autoUpdate + WIKI.config.lang.namespacing = args.namespacing + WIKI.config.lang.namespaces = args.namespaces + await WIKI.configSvc.saveToDb(['lang']) await WIKI.lang.setCurrentLocale(args.locale) diff --git a/server/graph/schemas/localization.graphql b/server/graph/schemas/localization.graphql index b82466f1..f035485e 100644 --- a/server/graph/schemas/localization.graphql +++ b/server/graph/schemas/localization.graphql @@ -31,6 +31,8 @@ type LocalizationMutation { updateLocale( locale: String! autoUpdate: Boolean! + namespacing: Boolean! + namespaces: [String]! ): DefaultResponse } @@ -52,4 +54,6 @@ type LocalizationLocale { type LocalizationConfig { locale: String! autoUpdate: Boolean! + namespacing: Boolean! + namespaces: [String]! } diff --git a/server/jobs/sync-graph-locales.js b/server/jobs/sync-graph-locales.js index 431f8d77..81c53190 100644 --- a/server/jobs/sync-graph-locales.js +++ b/server/jobs/sync-graph-locales.js @@ -34,11 +34,11 @@ module.exports = async (job) => { }) const locales = _.sortBy(_.get(respList, 'data.localization.locales', []), 'name').map(lc => ({...lc, isInstalled: (lc.code === 'en')})) WIKI.redis.set('locales', JSON.stringify(locales)) - const currentLocale = _.find(locales, ['code', WIKI.config.site.lang]) + const currentLocale = _.find(locales, ['code', WIKI.config.lang.code]) // -> Download locale strings - if (WIKI.config.site.langAutoUpdate) { + if (WIKI.config.langAutoUpdate) { const respStrings = await apollo({ query: `query ($code: String!) { localization { @@ -49,7 +49,7 @@ module.exports = async (job) => { } }`, variables: { - code: WIKI.config.site.lang + code: WIKI.config.lang.code } }) const strings = _.get(respStrings, 'data.localization.strings', []) @@ -60,12 +60,12 @@ module.exports = async (job) => { }) await WIKI.db.locales.query().update({ - code: WIKI.config.site.lang, + code: WIKI.config.lang.code, strings: lcObj, isRTL: currentLocale.isRTL, name: currentLocale.name, nativeName: currentLocale.nativeName - }).where('code', WIKI.config.site.lang) + }).where('code', WIKI.config.lang.code) } WIKI.logger.info('Syncing locales with Graph endpoint: [ COMPLETED ]') diff --git a/server/setup.js b/server/setup.js index 08187067..38c35e9f 100644 --- a/server/setup.js +++ b/server/setup.js @@ -271,30 +271,31 @@ module.exports = () => { await fs.writeFileAsync(path.join(WIKI.ROOTPATH, 'config.yml'), confRaw) // Set config - _.set(WIKI.config, 'defaultEditor', true) + _.set(WIKI.config, 'defaultEditor', 'markdown') _.set(WIKI.config, 'graphEndpoint', 'https://graph.requarks.io') - _.set(WIKI.config, 'lang', 'en') - _.set(WIKI.config, 'langAutoUpdate', true) - _.set(WIKI.config, 'langRTL', false) + _.set(WIKI.config, 'lang.code', 'en') + _.set(WIKI.config, 'lang.autoUpdate', true) + _.set(WIKI.config, 'lang.namespacing', false) + _.set(WIKI.config, 'lang.namespaces', []) _.set(WIKI.config, 'paths.content', req.body.pathContent) + _.set(WIKI.config, 'paths.data', req.body.pathData) _.set(WIKI.config, 'port', req.body.port) _.set(WIKI.config, 'public', req.body.public === 'true') _.set(WIKI.config, 'sessionSecret', (await crypto.randomBytesAsync(32)).toString('hex')) - _.set(WIKI.config, 'telemetry', req.body.telemetry === 'true') + _.set(WIKI.config, 'telemetry.isEnabled', req.body.telemetry === 'true') + _.set(WIKI.config, 'telemetry.clientId', WIKI.telemetry.cid) _.set(WIKI.config, 'title', req.body.title) // Save config to DB WIKI.logger.info('Persisting config to DB...') - await WIKI.db.settings.query().insert([ - { key: 'defaultEditor', value: { v: WIKI.config.defaultEditor } }, - { key: 'graphEndpoint', value: { v: WIKI.config.graphEndpoint } }, - { key: 'lang', value: { v: WIKI.config.lang } }, - { key: 'langAutoUpdate', value: { v: WIKI.config.langAutoUpdate } }, - { key: 'langRTL', value: { v: WIKI.config.langRTL } }, - { key: 'public', value: { v: WIKI.config.public } }, - { key: 'sessionSecret', value: { v: WIKI.config.sessionSecret } }, - { key: 'telemetry', value: { v: WIKI.config.telemetry } }, - { key: 'title', value: { v: WIKI.config.title } } + await WIKI.configSvc.saveToDb([ + 'defaultEditor', + 'graphEndpoint', + 'lang', + 'public', + 'sessionSecret', + 'telemetry', + 'title' ]) // Create default locale