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'
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
}
}
}

View File

@ -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

View File

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

View File

@ -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) })

View File

@ -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
}

View File

@ -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)

View File

@ -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: {

View File

@ -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)

View File

@ -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]!
}

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')}))
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 ]')

View File

@ -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