From 46fdd6da6681281c089a897daaf36e17cb4d4ba9 Mon Sep 17 00:00:00 2001 From: Jack Date: Sat, 27 Jun 2020 23:45:24 +0100 Subject: [PATCH] config is now saved to the database --- functions/index.js | 68 +++++++++++++++++++++++++++++++- public/js/account.js | 20 ++++++++++ public/js/db.js | 1 + public/js/script.js | 2 + public/js/userconfig.js | 87 +++++++++++++++++++++++++++-------------- 5 files changed, 146 insertions(+), 32 deletions(-) diff --git a/functions/index.js b/functions/index.js index ca924d6c3..52057f600 100644 --- a/functions/index.js +++ b/functions/index.js @@ -124,7 +124,7 @@ exports.changeName = functions.https.onCall((request,response) => { exports.checkIfNeedsToChangeName = functions.https.onCall((request,response) => { try{ return admin.auth().getUser(request.uid).then(requestUser => { - + if(!isUsernameValid(requestUser.displayName)){ //invalid name, needs to change console.log(`user ${requestUser.uid} ${requestUser.displayName} needs to change name`); @@ -423,4 +423,68 @@ exports.updateResultTags = functions.https.onCall((request,response) => { console.error(`error updating tags by ${request.uid} - ${e}`); return {resultCode:-999}; } -}) \ No newline at end of file +}) + +function isConfigKeyValid(name){ + if(name === null || name === undefined || name === "") return false; + if(name.length > 20) return false; + return /^[0-9a-zA-Z_.-]+$/.test(name); +} + +exports.saveConfig = functions.https.onCall((request,response) => { + try{ + if(request.uid === undefined || request.obj === undefined){ + console.error(`error saving config for ${request.uid} - missing input`); + return -1; + } + + let obj = request.obj; + + let err = false; + Object.keys(obj).forEach(key => { + let val = obj[key]; + if(Array.isArray(val)){ + val.forEach(valarr => { + if(!isConfigKeyValid(valarr)) err = true; + }) + }else{ + if(!isConfigKeyValid(val)) err = true; + } + }) + if (err){ + console.error(`error saving config for ${request.uid} - bad input`); + return -1; + } + + return admin.firestore().collection(`users`).doc(request.uid).set({ + config: obj + }, {merge: true}).then(e => { + return 1; + }).catch(e => { + console.error(`error saving config to DB for ${request.uid} - ${e.message}`); + return -1; + }); + }catch(e){ + console.error(`error saving config for ${request.uid} - ${e}`); + return {resultCode:-999}; + } +}) + +// exports.getConfig = functions.https.onCall((request,response) => { +// try{ +// if(request.uid === undefined){ +// console.error(`error getting config for ${request.uid} - missing input`); +// return -1; +// } + +// return admin.firestore().collection(`users`).doc(request.uid).get().then(e => { +// return e.data().config; +// }).catch(e => { +// console.error(`error getting config from DB for ${request.uid} - ${e.message}`); +// return -1; +// }); +// }catch(e){ +// console.error(`error getting config for ${request.uid} - ${e}`); +// return {resultCode:-999}; +// } +// }) diff --git a/public/js/account.js b/public/js/account.js index 7b71ea6d8..8ed631b8b 100644 --- a/public/js/account.js +++ b/public/js/account.js @@ -172,6 +172,26 @@ firebase.auth().onAuthStateChanged(function(user) { } }) refreshTagsSettingsSection(); + if(cookieConfig === null){ + applyConfig(dbSnapshot.config); + // showNotification('Applying db config',3000); + updateSettingsPage(); + saveConfigToCookie(); + }else{ + let configsDifferent = false; + Object.keys(cookieConfig).forEach(key => { + if(!configsDifferent){ + if(key !== 'resultFilters') + if(cookieConfig[key] !== dbSnapshot.config[key]) configsDifferent = true; + } + }) + if(configsDifferent){ + applyConfig(dbSnapshot.config); + // showNotification('Applying db config',3000); + updateSettingsPage(); + saveConfigToCookie(); + } + } }); var displayName = user.displayName; var email = user.email; diff --git a/public/js/db.js b/public/js/db.js index b01bd87b4..417424f97 100644 --- a/public/js/db.js +++ b/public/js/db.js @@ -48,6 +48,7 @@ async function db_getUserSnapshot() { // console.log('getting data from db!'); try{ snap.personalBests = data.data().personalBests; + snap.config = data.data().config; }catch(e){ // } diff --git a/public/js/script.js b/public/js/script.js index a24a13b70..02e22fe62 100644 --- a/public/js/script.js +++ b/public/js/script.js @@ -35,6 +35,8 @@ const addTag = firebase.functions().httpsCallable('addTag'); const editTag = firebase.functions().httpsCallable('editTag'); const removeTag = firebase.functions().httpsCallable('removeTag'); const updateResultTags = firebase.functions().httpsCallable('updateResultTags'); +const saveConfig = firebase.functions().httpsCallable('saveConfig'); + function smooth(arr, windowSize, getter = (value) => value, setter) { const get = getter diff --git a/public/js/userconfig.js b/public/js/userconfig.js index 854dc984a..df8f99125 100644 --- a/public/js/userconfig.js +++ b/public/js/userconfig.js @@ -25,6 +25,8 @@ let defaultConfig = { colorfulMode: true } +let cookieConfig = null; + let config = defaultConfig; //cookies @@ -37,6 +39,20 @@ function saveConfigToCookie() { path: '/' }); restartCount = 0; + saveConfigToDB(); +} + +function saveConfigToDB(){ + if(firebase.auth().currentUser !== null){ + // showNotification('saving config to db',1000); + accountIconLoading(true); + saveConfig({uid:firebase.auth().currentUser.uid,obj:config}).then(d => { + accountIconLoading(false); + if(d.data === 1){ + // showNotification('config saved to db',1000); + } + }) + } } function saveActiveTagsToCookie(){ @@ -63,48 +79,59 @@ function saveActiveTagsToCookie(){ function loadConfigFromCookie() { let newConfig = $.cookie('config'); - if (newConfig && newConfig != null && newConfig != "null") { + if(newConfig !== undefined){ newConfig = JSON.parse(newConfig); - setTheme(newConfig.theme,true); - setQuickTabMode(newConfig.quickTab,true); - setPunctuation(newConfig.punctuation,true); - setKeyTips(newConfig.showKeyTips,true); - changeTimeConfig(newConfig.time,true); - changeWordCount(newConfig.words,true); - changeMode(newConfig.mode,true); - changeLanguage(newConfig.language,true); - changeLayout(newConfig.layout, true); - changeFontSize(newConfig.fontSize,true); - setFreedomMode(newConfig.freedomMode,true); - setCaretStyle(newConfig.caretStyle,true); - setDifficulty(newConfig.difficulty,true); - setBlindMode(newConfig.blindMode,true); - setQuickEnd(newConfig.quickEnd,true); - setFlipTestColors(newConfig.flipTestColors,true); - setDiscordDot(newConfig.hideDiscordDot,true); - setColorfulMode(newConfig.colorfulMode,true); - setMaxConfidence(newConfig.maxConfidence,true); - setTimerStyle(newConfig.timerStyle,true); - if(newConfig.resultFilters == null || newConfig.resultFilters == undefined){ - newConfig.resultFilters = ["all"]; + applyConfig(newConfig); + cookieConfig = newConfig; + saveConfigToCookie(); + } +} + + +function applyConfig(configObj){ + if (configObj && configObj != null && configObj != "null") { + setTheme(configObj.theme,true); + setQuickTabMode(configObj.quickTab,true); + setPunctuation(configObj.punctuation,true); + setKeyTips(configObj.showKeyTips,true); + changeTimeConfig(configObj.time,true); + changeWordCount(configObj.words,true); + changeMode(configObj.mode,true); + changeLanguage(configObj.language,true); + changeLayout(configObj.layout, true); + changeFontSize(configObj.fontSize,true); + setFreedomMode(configObj.freedomMode,true); + setCaretStyle(configObj.caretStyle,true); + setDifficulty(configObj.difficulty,true); + setBlindMode(configObj.blindMode,true); + setQuickEnd(configObj.quickEnd,true); + setFlipTestColors(configObj.flipTestColors,true); + setDiscordDot(configObj.hideDiscordDot,true); + setColorfulMode(configObj.colorfulMode,true); + setMaxConfidence(configObj.maxConfidence,true); + setTimerStyle(configObj.timerStyle,true); + if(configObj.resultFilters == null || configObj.resultFilters == undefined){ + configObj.resultFilters = ["all"]; } - config = newConfig; + config = configObj; } Object.keys(defaultConfig).forEach(configKey => { if(config[configKey] == undefined){ config[configKey] = defaultConfig[configKey]; } }) - saveConfigToCookie(); } + function loadActiveTagsFromCookie(){ let newTags = $.cookie('activeTags'); - newTags = JSON.parse(newTags); - newTags.forEach(ntag => { - toggleTag(ntag, true); - }) - saveActiveTagsToCookie(); + if(newTags !== undefined){ + newTags = JSON.parse(newTags); + newTags.forEach(ntag => { + toggleTag(ntag, true); + }) + saveActiveTagsToCookie(); + } } function showTestConfig() {