Skip to content

Commit dd318eb

Browse files
committed
feat: locale namespacing save + auth fetch fix
1 parent 416755f commit dd318eb

11 files changed

+69
-51
lines changed

client/components/admin/admin-locale.vue

+12-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
:value='true'
6565
icon='warning'
6666
)
67-
span The locale code will be prefixed to all paths. (e.g. /en/page-name)
67+
span The locale code will be prefixed to all paths. (e.g. /{{ selectedLocale }}/page-name)
6868
.caption.grey--text Paths without a locale code will be automatically redirected to the base locale defined above.
6969
v-divider
7070
v-select(
@@ -181,7 +181,9 @@ export default {
181181
mutation: localesSaveMutation,
182182
variables: {
183183
locale: this.selectedLocale,
184-
autoUpdate: this.autoUpdate
184+
autoUpdate: this.autoUpdate,
185+
namespacing: this.namespacing,
186+
namespaces: this.namespaces
185187
}
186188
})
187189
const resp = _.get(respRaw, 'data.localization.updateLocale.responseResult', {})
@@ -216,6 +218,14 @@ export default {
216218
autoUpdate: {
217219
query: localesQuery,
218220
update: (data) => data.localization.config.autoUpdate
221+
},
222+
namespacing: {
223+
query: localesQuery,
224+
update: (data) => data.localization.config.namespacing
225+
},
226+
namespaces: {
227+
query: localesQuery,
228+
update: (data) => data.localization.config.namespaces
219229
}
220230
}
221231
}

client/graph/admin-locale-mutation-save.gql

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
mutation($locale: String!, $autoUpdate: Boolean!) {
1+
mutation($locale: String!, $autoUpdate: Boolean!, $namespacing: Boolean!, $namespaces: [String]!) {
22
localization {
3-
updateLocale(locale: $locale, autoUpdate: $autoUpdate) {
3+
updateLocale(locale: $locale, autoUpdate: $autoUpdate, namespacing: $namespacing, namespaces: $namespaces) {
44
responseResult {
55
succeeded
66
errorCode

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

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
config {
1414
locale
1515
autoUpdate
16+
namespacing
17+
namespaces
1618
}
1719
}
1820
}

server/core/auth.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module.exports = {
3434
async activateStrategies() {
3535
try {
3636
// Unload any active strategies
37-
WIKI.auth.strategies = []
37+
WIKI.auth.strategies = {}
3838
const currentStrategies = _.keys(passport._strategies)
3939
_.pull(currentStrategies, 'session')
4040
_.forEach(currentStrategies, stg => { passport.unuse(stg) })

server/core/config.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,18 @@ module.exports = {
6767
* @returns Promise
6868
*/
6969
async saveToDb(keys) {
70-
let trx = await WIKI.db.Objection.transaction.start(WIKI.db.knex)
71-
7270
try {
7371
for (let key of keys) {
74-
const value = _.get(WIKI.config, key, null)
75-
let affectedRows = await WIKI.db.settings.query(trx).patch({ value }).where('key', key)
72+
let value = _.get(WIKI.config, key, null)
73+
if (!_.isPlainObject(value)) {
74+
value = { v: value }
75+
}
76+
let affectedRows = await WIKI.db.settings.query().patch({ value }).where('key', key)
7677
if (affectedRows === 0 && value) {
77-
await WIKI.db.settings.query(trx).insert({ key, value })
78+
await WIKI.db.settings.query().insert({ key, value })
7879
}
7980
}
80-
await trx.commit()
8181
} catch (err) {
82-
await trx.rollback(err)
8382
WIKI.logger.error(`Failed to save configuration to DB: ${err.message}`)
8483
return false
8584
}

server/core/localization.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module.exports = {
1717
ns: this.namespaces,
1818
defaultNS: 'common',
1919
saveMissing: false,
20-
lng: WIKI.config.lang,
20+
lng: WIKI.config.lang.code,
2121
fallbackLng: 'en'
2222
})
2323

@@ -31,7 +31,7 @@ module.exports = {
3131
}
3232

3333
// Load current language
34-
this.loadLocale(WIKI.config.lang, { silent: true })
34+
this.loadLocale(WIKI.config.lang.code, { silent: true })
3535

3636
return this
3737
},
@@ -55,7 +55,6 @@ module.exports = {
5555
const res = await WIKI.db.locales.query().findOne('code', locale)
5656
if (res) {
5757
if (_.isPlainObject(res.strings)) {
58-
console.info(res.strings)
5958
_.forOwn(res.strings, (data, ns) => {
6059
this.namespaces.push(ns)
6160
this.engine.addResourceBundle(locale, ns, data, true, true)

server/graph/resolvers/authentication.js

+10-11
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,17 @@ module.exports = {
1515
async authentication() { return {} }
1616
},
1717
AuthenticationQuery: {
18-
providers(obj, args, context, info) {
19-
let prv = _.map(WIKI.auth.strategies, str => ({
20-
isEnabled: str.config.isEnabled,
21-
key: str.key,
22-
props: str.props,
23-
title: str.title,
24-
useForm: str.useForm,
25-
config: []
18+
async providers(obj, args, context, info) {
19+
let strategies = await WIKI.db.authentication.query().orderBy('title')
20+
strategies = strategies.map(stg => ({
21+
...stg,
22+
config: _.transform(stg.config, (res, value, key) => {
23+
res.push({ key, value })
24+
}, [])
2625
}))
27-
if (args.filter) { prv = graphHelper.filter(prv, args.filter) }
28-
if (args.orderBy) { prv = graphHelper.orderBy(prv, args.orderBy) }
29-
return prv
26+
if (args.filter) { strategies = graphHelper.filter(strategies, args.filter) }
27+
if (args.orderBy) { strategies = graphHelper.orderBy(strategies, args.orderBy) }
28+
return strategies
3029
}
3130
},
3231
AuthenticationMutation: {

server/graph/resolvers/localization.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ module.exports = {
2626
},
2727
async config(obj, args, context, info) {
2828
return {
29-
locale: WIKI.config.site.lang,
30-
autoUpdate: WIKI.config.site.langAutoUpdate
29+
locale: WIKI.config.lang.code,
30+
autoUpdate: WIKI.config.lang.autoUpdate,
31+
namespacing: WIKI.config.lang.namespacing,
32+
namespaces: WIKI.config.lang.namespaces
3133
}
3234
}
3335
},
@@ -49,9 +51,11 @@ module.exports = {
4951
},
5052
async updateLocale(obj, args, context) {
5153
try {
52-
WIKI.config.site.lang = args.locale
53-
WIKI.config.site.langAutoUpdate = args.autoUpdate
54-
await WIKI.configSvc.saveToDb(['site'])
54+
WIKI.config.lang.code = args.locale
55+
WIKI.config.lang.autoUpdate = args.autoUpdate
56+
WIKI.config.lang.namespacing = args.namespacing
57+
WIKI.config.lang.namespaces = args.namespaces
58+
await WIKI.configSvc.saveToDb(['lang'])
5559

5660
await WIKI.lang.setCurrentLocale(args.locale)
5761

server/graph/schemas/localization.graphql

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ type LocalizationMutation {
3131
updateLocale(
3232
locale: String!
3333
autoUpdate: Boolean!
34+
namespacing: Boolean!
35+
namespaces: [String]!
3436
): DefaultResponse
3537
}
3638

@@ -52,4 +54,6 @@ type LocalizationLocale {
5254
type LocalizationConfig {
5355
locale: String!
5456
autoUpdate: Boolean!
57+
namespacing: Boolean!
58+
namespaces: [String]!
5559
}

server/jobs/sync-graph-locales.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ module.exports = async (job) => {
3434
})
3535
const locales = _.sortBy(_.get(respList, 'data.localization.locales', []), 'name').map(lc => ({...lc, isInstalled: (lc.code === 'en')}))
3636
WIKI.redis.set('locales', JSON.stringify(locales))
37-
const currentLocale = _.find(locales, ['code', WIKI.config.site.lang])
37+
const currentLocale = _.find(locales, ['code', WIKI.config.lang.code])
3838

3939
// -> Download locale strings
4040

41-
if (WIKI.config.site.langAutoUpdate) {
41+
if (WIKI.config.langAutoUpdate) {
4242
const respStrings = await apollo({
4343
query: `query ($code: String!) {
4444
localization {
@@ -49,7 +49,7 @@ module.exports = async (job) => {
4949
}
5050
}`,
5151
variables: {
52-
code: WIKI.config.site.lang
52+
code: WIKI.config.lang.code
5353
}
5454
})
5555
const strings = _.get(respStrings, 'data.localization.strings', [])
@@ -60,12 +60,12 @@ module.exports = async (job) => {
6060
})
6161

6262
await WIKI.db.locales.query().update({
63-
code: WIKI.config.site.lang,
63+
code: WIKI.config.lang.code,
6464
strings: lcObj,
6565
isRTL: currentLocale.isRTL,
6666
name: currentLocale.name,
6767
nativeName: currentLocale.nativeName
68-
}).where('code', WIKI.config.site.lang)
68+
}).where('code', WIKI.config.lang.code)
6969
}
7070

7171
WIKI.logger.info('Syncing locales with Graph endpoint: [ COMPLETED ]')

server/setup.js

+16-15
Original file line numberDiff line numberDiff line change
@@ -271,30 +271,31 @@ module.exports = () => {
271271
await fs.writeFileAsync(path.join(WIKI.ROOTPATH, 'config.yml'), confRaw)
272272

273273
// Set config
274-
_.set(WIKI.config, 'defaultEditor', true)
274+
_.set(WIKI.config, 'defaultEditor', 'markdown')
275275
_.set(WIKI.config, 'graphEndpoint', 'https://graph.requarks.io')
276-
_.set(WIKI.config, 'lang', 'en')
277-
_.set(WIKI.config, 'langAutoUpdate', true)
278-
_.set(WIKI.config, 'langRTL', false)
276+
_.set(WIKI.config, 'lang.code', 'en')
277+
_.set(WIKI.config, 'lang.autoUpdate', true)
278+
_.set(WIKI.config, 'lang.namespacing', false)
279+
_.set(WIKI.config, 'lang.namespaces', [])
279280
_.set(WIKI.config, 'paths.content', req.body.pathContent)
281+
_.set(WIKI.config, 'paths.data', req.body.pathData)
280282
_.set(WIKI.config, 'port', req.body.port)
281283
_.set(WIKI.config, 'public', req.body.public === 'true')
282284
_.set(WIKI.config, 'sessionSecret', (await crypto.randomBytesAsync(32)).toString('hex'))
283-
_.set(WIKI.config, 'telemetry', req.body.telemetry === 'true')
285+
_.set(WIKI.config, 'telemetry.isEnabled', req.body.telemetry === 'true')
286+
_.set(WIKI.config, 'telemetry.clientId', WIKI.telemetry.cid)
284287
_.set(WIKI.config, 'title', req.body.title)
285288

286289
// Save config to DB
287290
WIKI.logger.info('Persisting config to DB...')
288-
await WIKI.db.settings.query().insert([
289-
{ key: 'defaultEditor', value: { v: WIKI.config.defaultEditor } },
290-
{ key: 'graphEndpoint', value: { v: WIKI.config.graphEndpoint } },
291-
{ key: 'lang', value: { v: WIKI.config.lang } },
292-
{ key: 'langAutoUpdate', value: { v: WIKI.config.langAutoUpdate } },
293-
{ key: 'langRTL', value: { v: WIKI.config.langRTL } },
294-
{ key: 'public', value: { v: WIKI.config.public } },
295-
{ key: 'sessionSecret', value: { v: WIKI.config.sessionSecret } },
296-
{ key: 'telemetry', value: { v: WIKI.config.telemetry } },
297-
{ key: 'title', value: { v: WIKI.config.title } }
291+
await WIKI.configSvc.saveToDb([
292+
'defaultEditor',
293+
'graphEndpoint',
294+
'lang',
295+
'public',
296+
'sessionSecret',
297+
'telemetry',
298+
'title'
298299
])
299300

300301
// Create default locale

0 commit comments

Comments
 (0)