diff --git a/app/api/bell-data.js b/app/api/bell-data.js index 61e76a6..b893139 100644 --- a/app/api/bell-data.js +++ b/app/api/bell-data.js @@ -180,12 +180,66 @@ class BellData { user.settings.period_names = values; + let now = Date.now(); + let arr = user.stats; + + // if last period update was within the last 5 minutes, save stat as one + if (typeof arr.updated_period_names === 'object') { + if (arr.updated_period_names.length > 8) + for (let i = 1; i < arr.updated_period_names.length; i++) + arr.updated_period_names.splice(i, 1); + + for (let i = arr.updated_period_names.length - 1; i >= 0; i--) + if (now - 300000 < arr.updated_period_names[i]) + arr.updated_period_names.splice(i, 1); + else + break; + } else + arr.updated_period_names = []; + + arr.updated_period_names.push(now); + + await this.setObjectToUser('settings', user.email, user.settings); + await this.setObjectToUser('stats', user.email, user.stats); return {}; } + async updateTheme(device_id, theme) { + + let user = await this.getUserByDeviceId(device_id); + + if (user.error) return { error: user.error }; + + user.settings.theme = theme; + + let now = Date.now(); + let arr = user.stats; + + // if last theme update was within the last 5 minutes, save stat as one + if (typeof arr.updated_theme === 'object') { + if (arr.updated_theme.length > 8) + for (let i = 1; i < arr.updated_theme.length; i++) + arr.updated_theme.splice(i, 1); + + for (let i = arr.updated_theme.length - 1; i >= 0; i--) + if (now - 300000 < arr.updated_theme[i]) + arr.updated_theme.splice(i, 1); + else + break; + } else + arr.updated_theme = []; + + arr.updated_theme.push(now); + + await this.setObjectToUser('settings', user.email, user.settings); + await this.setObjectToUser('stats', user.email, user.stats); + + return {}; + } + updateUser(vals) { return this.query( 'UPDATE users SET first_name = ?, last_name = ?, profile_pic = ? WHERE email = ?', diff --git a/app/api/plans/bell-data.js b/app/api/plans/bell-data.js index 899cd05..285446d 100644 --- a/app/api/plans/bell-data.js +++ b/app/api/plans/bell-data.js @@ -54,16 +54,12 @@ var schema = { 6: 'Spanish', 7: 'Survey Comp/Lit' }, - theme: [ - 'default', 'default_reverse' // last being most recent, delete when too long - ] + theme: 'MVHS Light' }, devices: { 'HJnbG8jDRG': 1526165118086 // date added to this profile }, stats: { - created: 1526165118086, - viewed_last: 1526165118086, updated_period_names: [1526165118086, 1526165189886] } } diff --git a/app/api/v1.js b/app/api/v1.js index 26e1627..b76f234 100644 --- a/app/api/v1.js +++ b/app/api/v1.js @@ -31,11 +31,12 @@ module.exports = async (path, postData) => { if (!postData.data) return responses.missing_data; let device_id = postData.device_id; + let data = postData.data; switch (path) { case '/init': - let {user_agent, platform, browser} = postData.data; + let {user_agent, platform, browser} = data; if (user_agent && platform && browser) { // if we need to register a new device @@ -43,13 +44,13 @@ module.exports = async (path, postData) => { if (typeof browser === 'object') { let keys = Object.keys(browser); if (keys.length === 0) - postData.data.browser = 'unknown'; + data.browser = 'unknown'; else - postData.data.browser = Object.keys(browser)[0]; + data.browser = Object.keys(browser)[0]; } return generateResponse(true, null, { - device_id: await bellData.createNewDevice(postData.data) + device_id: await bellData.createNewDevice(data) }); } else if (device_id) { @@ -75,7 +76,7 @@ module.exports = async (path, postData) => { break; case '/write/login': - let {email, first_name, last_name, profile_pic} = postData.data; + let {email, first_name, last_name, profile_pic} = data; if (!device_id || !email || !first_name || !last_name || !profile_pic) return responses.missing_data; @@ -84,8 +85,8 @@ module.exports = async (path, postData) => { if (emailUserData !== false) { // already have an account - if (!bellData.isThisMe(postData.data, emailUserData)) - await bellData.updateUser(postData.data); + if (!bellData.isThisMe(data, emailUserData)) + await bellData.updateUser(data); await bellData.registerDevice(device_id, email); @@ -101,7 +102,7 @@ module.exports = async (path, postData) => { }); } else { - await bellData.createNewUser(postData.data); + await bellData.createNewUser(data); await bellData.registerDevice(device_id, email); return generateResponse(true, null, { @@ -127,11 +128,10 @@ module.exports = async (path, postData) => { return responses.bad_data; - break; case '/write/analytics': - let {pathname, prefs, referrer, speed, new_load, registered_to} = postData.data; + let {pathname, prefs, referrer, speed, new_load, registered_to} = data; if (!device_id || !pathname || !prefs || typeof referrer !== 'string' || !speed || typeof new_load !== 'boolean') return responses.missing_data; @@ -172,7 +172,6 @@ module.exports = async (path, postData) => { break; case '/update/period_names': - let {data} = postData; if (!device_id || !data) return responses.missing_data; @@ -189,6 +188,22 @@ module.exports = async (path, postData) => { else return responses.success; + break; + case '/update/theme': + + if (!device_id || !data) + return responses.missing_data; + + if (typeof data.new_theme !== 'string' || data.new_theme.length > 20) + return responses.bad_data; + + let val = await bellData.updateTheme(device_id, data.new_theme); + + if (val.error) + return generateResponse(false, res.error); + else + return responses.success; + break; default: return responses.bad_path; diff --git a/build-client.sh b/build-client.sh index b9c093e..d8d7b46 100755 --- a/build-client.sh +++ b/build-client.sh @@ -1,4 +1,4 @@ -# dist/client/js cannot exist! delete before running +# client-dist/js cannot exist! delete before running # javascript --------------------------- diff --git a/public/index.html b/public/index.html index 20a2304..f53cb93 100644 --- a/public/index.html +++ b/public/index.html @@ -56,7 +56,7 @@ Your Settings
- In order to change settings, you must + to sign into Google and change settings
diff --git a/public/js/PrefManager.js b/public/js/PrefManager.js index f40c94e..2030f53 100644 --- a/public/js/PrefManager.js +++ b/public/js/PrefManager.js @@ -15,7 +15,7 @@ class PrefManager { if (Storage.prefsExist()) this.setAllPreferences(Storage.getPrefs()); else // default prefs - this.setTheme(1); + this.setTheme(0); } initVars() { @@ -108,11 +108,21 @@ class PrefManager { this.theme.color.background = this.theme_options.background[num]; this.theme.color.text = this.theme_options.text[num]; this.theme.name = this.theme_options.name[num]; - this.save(); + + if (this.isLoggedIn()) + return RequestManager.updateTheme(this.theme.name).then(data => { + if (data.success) { + this.save(); + return true; + } + return false; + }); + else + this.save(); } setThemeByName(name) { - this.setTheme(this.theme_options.name.indexOf(name)); + return this.setTheme(this.theme_options.name.indexOf(name)); } getThemeName() { return this.theme.name } diff --git a/public/js/RequestManager.js b/public/js/RequestManager.js index 20bc90f..e5ec272 100644 --- a/public/js/RequestManager.js +++ b/public/js/RequestManager.js @@ -130,6 +130,17 @@ class RequestManager { }).then(res => res.json); } + static updateTheme(new_theme) { + return this.ajax({ + url: '/api/v1/update/theme', + type: 'POST', + data: JSON.stringify({ + device_id: Storage.getDeviceId(), + data: { new_theme } + }) + }).then(res => res.json); + } + static getTime() { return this.ajax({ url: '/api/time' diff --git a/public/js/main.js b/public/js/main.js index 8bcce2f..d56de89 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -142,10 +142,12 @@ render.settings = () => { elements.settings.themeSelector.onchange = () => { let val = elements.settings.themeSelector.value; - prefManager.setThemeByName(val); - - elements.applyPreferencesToElements(prefManager.getAllPreferences()); - + prefManager.setThemeByName(val).then(success => { + if (success) + elements.applyPreferencesToElements(prefManager.getAllPreferences()); + else + window.alert('We are having trouble saving your theme change. Try again later.'); // TODO give some error! + }); } }