Browse Source

feat: locale namespacing save + auth fetch fix

pull/621/head
NGPixel 6 years ago
parent
commit
dd318eb2db
11 changed files with 69 additions and 51 deletions
  1. 14
      client/components/admin/admin-locale.vue
  2. 4
      client/graph/admin-locale-mutation-save.gql
  3. 2
      client/graph/admin-locale-query-list.gql
  4. 2
      server/core/auth.js
  5. 13
      server/core/config.js
  6. 5
      server/core/localization.js
  7. 21
      server/graph/resolvers/authentication.js
  8. 14
      server/graph/resolvers/localization.js
  9. 4
      server/graph/schemas/localization.graphql
  10. 10
      server/jobs/sync-graph-locales.js
  11. 31
      server/setup.js

14
client/components/admin/admin-locale.vue

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

4
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 { localization {
updateLocale(locale: $locale, autoUpdate: $autoUpdate) {
updateLocale(locale: $locale, autoUpdate: $autoUpdate, namespacing: $namespacing, namespaces: $namespaces) {
responseResult { responseResult {
succeeded succeeded
errorCode errorCode

2
client/graph/admin-locale-query-list.gql

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

2
server/core/auth.js

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

13
server/core/config.js

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

5
server/core/localization.js

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

21
server/graph/resolvers/authentication.js

@ -15,18 +15,17 @@ module.exports = {
async authentication() { return {} } async authentication() { return {} }
}, },
AuthenticationQuery: { 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: { AuthenticationMutation: {

14
server/graph/resolvers/localization.js

@ -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,
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) { async updateLocale(obj, args, context) {
try { 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) await WIKI.lang.setCurrentLocale(args.locale)

4
server/graph/schemas/localization.graphql

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

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

31
server/setup.js

@ -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, '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.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([
{ 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 // Create default locale

Loading…
Cancel
Save