feat: locale namespacing save + auth fetch fix

This commit is contained in:
NGPixel 2018-05-28 19:36:35 -04:00
parent 416755f17a
commit dd318eb2db
11 changed files with 69 additions and 51 deletions

View File

@ -64,7 +64,7 @@
:value='true' :value='true'
icon='warning' 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. .caption.grey--text Paths without a locale code will be automatically redirected to the base locale defined above.
v-divider v-divider
v-select( v-select(
@ -181,7 +181,9 @@ export default {
mutation: localesSaveMutation, mutation: localesSaveMutation,
variables: { variables: {
locale: this.selectedLocale, locale: this.selectedLocale,
autoUpdate: this.autoUpdate autoUpdate: this.autoUpdate,
namespacing: this.namespacing,
namespaces: this.namespaces
} }
}) })
const resp = _.get(respRaw, 'data.localization.updateLocale.responseResult', {}) const resp = _.get(respRaw, 'data.localization.updateLocale.responseResult', {})
@ -216,6 +218,14 @@ export default {
autoUpdate: { autoUpdate: {
query: localesQuery, query: localesQuery,
update: (data) => data.localization.config.autoUpdate update: (data) => data.localization.config.autoUpdate
},
namespacing: {
query: localesQuery,
update: (data) => data.localization.config.namespacing
},
namespaces: {
query: localesQuery,
update: (data) => data.localization.config.namespaces
} }
} }
} }

View File

@ -1,6 +1,6 @@
mutation($locale: String!, $autoUpdate: Boolean!) { mutation($locale: String!, $autoUpdate: Boolean!, $namespacing: Boolean!, $namespaces: [String]!) {
localization { localization {
updateLocale(locale: $locale, autoUpdate: $autoUpdate) { updateLocale(locale: $locale, autoUpdate: $autoUpdate, namespacing: $namespacing, namespaces: $namespaces) {
responseResult { responseResult {
succeeded succeeded
errorCode errorCode

View File

@ -13,6 +13,8 @@
config { config {
locale locale
autoUpdate autoUpdate
namespacing
namespaces
} }
} }
} }

View File

@ -34,7 +34,7 @@ module.exports = {
async activateStrategies() { async activateStrategies() {
try { try {
// Unload any active strategies // Unload any active strategies
WIKI.auth.strategies = [] WIKI.auth.strategies = {}
const currentStrategies = _.keys(passport._strategies) const currentStrategies = _.keys(passport._strategies)
_.pull(currentStrategies, 'session') _.pull(currentStrategies, 'session')
_.forEach(currentStrategies, stg => { passport.unuse(stg) }) _.forEach(currentStrategies, stg => { passport.unuse(stg) })

View File

@ -67,19 +67,18 @@ module.exports = {
* @returns Promise * @returns Promise
*/ */
async saveToDb(keys) { async saveToDb(keys) {
let trx = await WIKI.db.Objection.transaction.start(WIKI.db.knex)
try { try {
for (let key of keys) { for (let key of keys) {
const value = _.get(WIKI.config, key, null) let value = _.get(WIKI.config, key, null)
let affectedRows = await WIKI.db.settings.query(trx).patch({ value }).where('key', key) if (!_.isPlainObject(value)) {
value = { v: value }
}
let affectedRows = await WIKI.db.settings.query().patch({ value }).where('key', key)
if (affectedRows === 0 && value) { 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) { } catch (err) {
await trx.rollback(err)
WIKI.logger.error(`Failed to save configuration to DB: ${err.message}`) WIKI.logger.error(`Failed to save configuration to DB: ${err.message}`)
return false return false
} }

View File

@ -17,7 +17,7 @@ module.exports = {
ns: this.namespaces, ns: this.namespaces,
defaultNS: 'common', defaultNS: 'common',
saveMissing: false, saveMissing: false,
lng: WIKI.config.lang, lng: WIKI.config.lang.code,
fallbackLng: 'en' fallbackLng: 'en'
}) })
@ -31,7 +31,7 @@ module.exports = {
} }
// Load current language // Load current language
this.loadLocale(WIKI.config.lang, { silent: true }) this.loadLocale(WIKI.config.lang.code, { silent: true })
return this return this
}, },
@ -55,7 +55,6 @@ module.exports = {
const res = await WIKI.db.locales.query().findOne('code', locale) const res = await WIKI.db.locales.query().findOne('code', locale)
if (res) { if (res) {
if (_.isPlainObject(res.strings)) { if (_.isPlainObject(res.strings)) {
console.info(res.strings)
_.forOwn(res.strings, (data, ns) => { _.forOwn(res.strings, (data, ns) => {
this.namespaces.push(ns) this.namespaces.push(ns)
this.engine.addResourceBundle(locale, ns, data, true, true) this.engine.addResourceBundle(locale, ns, data, true, true)

View File

@ -15,18 +15,17 @@ module.exports = {
async authentication() { return {} } async authentication() { return {} }
}, },
AuthenticationQuery: { AuthenticationQuery: {
providers(obj, args, context, info) { async providers(obj, args, context, info) {
let prv = _.map(WIKI.auth.strategies, str => ({ let strategies = await WIKI.db.authentication.query().orderBy('title')
isEnabled: str.config.isEnabled, strategies = strategies.map(stg => ({
key: str.key, ...stg,
props: str.props, config: _.transform(stg.config, (res, value, key) => {
title: str.title, res.push({ key, value })
useForm: str.useForm, }, [])
config: []
})) }))
if (args.filter) { prv = graphHelper.filter(prv, args.filter) } if (args.filter) { strategies = graphHelper.filter(strategies, args.filter) }
if (args.orderBy) { prv = graphHelper.orderBy(prv, args.orderBy) } if (args.orderBy) { strategies = graphHelper.orderBy(strategies, args.orderBy) }
return prv return strategies
} }
}, },
AuthenticationMutation: { AuthenticationMutation: {

View File

@ -26,8 +26,10 @@ module.exports = {
}, },
async config(obj, args, context, info) { async config(obj, args, context, info) {
return { return {
locale: WIKI.config.site.lang, locale: WIKI.config.lang.code,
autoUpdate: WIKI.config.site.langAutoUpdate 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) { async updateLocale(obj, args, context) {
try { try {
WIKI.config.site.lang = args.locale WIKI.config.lang.code = args.locale
WIKI.config.site.langAutoUpdate = args.autoUpdate WIKI.config.lang.autoUpdate = args.autoUpdate
await WIKI.configSvc.saveToDb(['site']) WIKI.config.lang.namespacing = args.namespacing
WIKI.config.lang.namespaces = args.namespaces
await WIKI.configSvc.saveToDb(['lang'])
await WIKI.lang.setCurrentLocale(args.locale) await WIKI.lang.setCurrentLocale(args.locale)

View File

@ -31,6 +31,8 @@ type LocalizationMutation {
updateLocale( updateLocale(
locale: String! locale: String!
autoUpdate: Boolean! autoUpdate: Boolean!
namespacing: Boolean!
namespaces: [String]!
): DefaultResponse ): DefaultResponse
} }
@ -52,4 +54,6 @@ type LocalizationLocale {
type LocalizationConfig { type LocalizationConfig {
locale: String! locale: String!
autoUpdate: Boolean! autoUpdate: Boolean!
namespacing: Boolean!
namespaces: [String]!
} }

View File

@ -34,11 +34,11 @@ module.exports = async (job) => {
}) })
const locales = _.sortBy(_.get(respList, 'data.localization.locales', []), 'name').map(lc => ({...lc, isInstalled: (lc.code === 'en')})) const locales = _.sortBy(_.get(respList, 'data.localization.locales', []), 'name').map(lc => ({...lc, isInstalled: (lc.code === 'en')}))
WIKI.redis.set('locales', JSON.stringify(locales)) 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 // -> Download locale strings
if (WIKI.config.site.langAutoUpdate) { if (WIKI.config.langAutoUpdate) {
const respStrings = await apollo({ const respStrings = await apollo({
query: `query ($code: String!) { query: `query ($code: String!) {
localization { localization {
@ -49,7 +49,7 @@ module.exports = async (job) => {
} }
}`, }`,
variables: { variables: {
code: WIKI.config.site.lang code: WIKI.config.lang.code
} }
}) })
const strings = _.get(respStrings, 'data.localization.strings', []) const strings = _.get(respStrings, 'data.localization.strings', [])
@ -60,12 +60,12 @@ module.exports = async (job) => {
}) })
await WIKI.db.locales.query().update({ await WIKI.db.locales.query().update({
code: WIKI.config.site.lang, code: WIKI.config.lang.code,
strings: lcObj, strings: lcObj,
isRTL: currentLocale.isRTL, isRTL: currentLocale.isRTL,
name: currentLocale.name, name: currentLocale.name,
nativeName: currentLocale.nativeName nativeName: currentLocale.nativeName
}).where('code', WIKI.config.site.lang) }).where('code', WIKI.config.lang.code)
} }
WIKI.logger.info('Syncing locales with Graph endpoint: [ COMPLETED ]') WIKI.logger.info('Syncing locales with Graph endpoint: [ COMPLETED ]')

View File

@ -271,30 +271,31 @@ module.exports = () => {
await fs.writeFileAsync(path.join(WIKI.ROOTPATH, 'config.yml'), confRaw) await fs.writeFileAsync(path.join(WIKI.ROOTPATH, 'config.yml'), confRaw)
// Set config // Set config
_.set(WIKI.config, 'defaultEditor', true) _.set(WIKI.config, 'defaultEditor', 'markdown')
_.set(WIKI.config, 'graphEndpoint', 'https://graph.requarks.io') _.set(WIKI.config, 'graphEndpoint', 'https://graph.requarks.io')
_.set(WIKI.config, 'lang', 'en') _.set(WIKI.config, 'lang.code', 'en')
_.set(WIKI.config, 'langAutoUpdate', true) _.set(WIKI.config, 'lang.autoUpdate', true)
_.set(WIKI.config, 'langRTL', false) _.set(WIKI.config, 'lang.namespacing', false)
_.set(WIKI.config, 'lang.namespaces', [])
_.set(WIKI.config, 'paths.content', req.body.pathContent) _.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, 'port', req.body.port)
_.set(WIKI.config, 'public', req.body.public === 'true') _.set(WIKI.config, 'public', req.body.public === 'true')
_.set(WIKI.config, 'sessionSecret', (await crypto.randomBytesAsync(32)).toString('hex')) _.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) _.set(WIKI.config, 'title', req.body.title)
// Save config to DB // Save config to DB
WIKI.logger.info('Persisting config to DB...') WIKI.logger.info('Persisting config to DB...')
await WIKI.db.settings.query().insert([ await WIKI.configSvc.saveToDb([
{ key: 'defaultEditor', value: { v: WIKI.config.defaultEditor } }, 'defaultEditor',
{ key: 'graphEndpoint', value: { v: WIKI.config.graphEndpoint } }, 'graphEndpoint',
{ key: 'lang', value: { v: WIKI.config.lang } }, 'lang',
{ key: 'langAutoUpdate', value: { v: WIKI.config.langAutoUpdate } }, 'public',
{ key: 'langRTL', value: { v: WIKI.config.langRTL } }, 'sessionSecret',
{ key: 'public', value: { v: WIKI.config.public } }, 'telemetry',
{ key: 'sessionSecret', value: { v: WIKI.config.sessionSecret } }, 'title'
{ key: 'telemetry', value: { v: WIKI.config.telemetry } },
{ key: 'title', value: { v: WIKI.config.title } }
]) ])
// Create default locale // Create default locale