mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-10-09 07:09:36 +08:00
config is now saved to the database
This commit is contained in:
parent
197e413014
commit
46fdd6da66
5 changed files with 146 additions and 32 deletions
|
@ -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};
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
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};
|
||||
// }
|
||||
// })
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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){
|
||||
//
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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() {
|
||||
|
|
Loading…
Add table
Reference in a new issue