From c6881c5e3094553e3a60b63d161b1a67da61ac54 Mon Sep 17 00:00:00 2001 From: Matteo Pagliazzi Date: Thu, 14 Sep 2017 18:55:17 +0200 Subject: [PATCH] More Client Fixes (#9036) * add automatic user syncing when user._v does not match with server * fix google signup * fixes to user sync * check for next cron on activity * add comment --- website/client/app.vue | 34 +++++++++++++++++ website/client/components/notifications.vue | 41 ++++++++++++--------- website/client/components/static/home.vue | 2 +- website/server/middlewares/response.js | 8 +--- 4 files changed, 59 insertions(+), 26 deletions(-) diff --git a/website/client/app.vue b/website/client/app.vue index 648ae75e32..5e15471a20 100644 --- a/website/client/app.vue +++ b/website/client/app.vue @@ -130,6 +130,7 @@ export default { this.selectedItemToBuy = item; }); + // @TODO split up this file, it's too big // Set up Error interceptors axios.interceptors.response.use((response) => { if (this.user) { @@ -149,6 +150,39 @@ export default { return Promise.reject(error); }); + axios.interceptors.response.use((response) => { + // Verify that the user was not updated from another browser/app/client + // If it was, sync + const url = response.config.url; + const method = response.config.method; + + const isApiCall = url.indexOf('api/v3') !== -1; + const userV = response.data && response.data.userV; + + if (this.isUserLoaded && isApiCall && userV) { + const oldUserV = this.user._v; + this.user._v = userV; + + // Do not sync again if already syncing + const isUserSync = url.indexOf('/api/v3/user') === 0 && method === 'get'; + const isTasksSync = url.indexOf('/api/v3/tasks/user') === 0 && method === 'get'; + // exclude chat seen requests because with real time chat they would be too many + const isChatSeen = url.indexOf('/chat/seen') !== -1 && method === 'post'; + // exclude POST /api/v3/cron because the user is synced automatically after cron runs + const isCron = url.indexOf('/api/v3/cron') === 0 && method === 'post'; + + // Something has changed on the user object that was not tracked here, sync the user + if (userV - oldUserV > 1 && !isCron && !isChatSeen && !isUserSync && !isTasksSync) { + Promise.all([ + this.$store.dispatch('user:fetch', {forceLoad: true}), + this.$store.dispatch('tasks:fetchUserTasks', {forceLoad: true}), + ]); + } + } + + return response; + }); + // Setup listener for title this.$store.watch(state => state.title, (title) => { document.title = title; diff --git a/website/client/components/notifications.vue b/website/client/components/notifications.vue index ff034cdc8e..cb58da5aac 100644 --- a/website/client/components/notifications.vue +++ b/website/client/components/notifications.vue @@ -86,6 +86,7 @@ div