From 6a1755d2e32c3f3bcaa67c33f32cb5eb5ab76ea2 Mon Sep 17 00:00:00 2001
From: =Corey Hulen <corey@hulen.com>
Date: Fri, 16 Oct 2015 09:10:54 -0700
Subject: Inital support for multi-tab loging

---
 web/react/stores/browser_store.jsx | 82 +++++++++++++++++++++++++++++---------
 1 file changed, 63 insertions(+), 19 deletions(-)

(limited to 'web/react/stores/browser_store.jsx')

diff --git a/web/react/stores/browser_store.jsx b/web/react/stores/browser_store.jsx
index c2e7df58e..3eacff8b4 100644
--- a/web/react/stores/browser_store.jsx
+++ b/web/react/stores/browser_store.jsx
@@ -1,12 +1,12 @@
 // Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
 // See License.txt for license information.
 
-var UserStore;
 function getPrefix() {
-    if (!UserStore) {
-        UserStore = require('./user_store.jsx'); //eslint-disable-line global-require
+    if (global.window.mm_user) {
+        return global.window.mm_user.id + '_';
     }
-    return UserStore.getCurrentId() + '_';
+
+    return 'unknown_';
 }
 
 class BrowserStoreClass {
@@ -17,32 +17,50 @@ class BrowserStoreClass {
         this.setGlobalItem = this.setGlobalItem.bind(this);
         this.getGlobalItem = this.getGlobalItem.bind(this);
         this.removeGlobalItem = this.removeGlobalItem.bind(this);
-        this.clear = this.clear.bind(this);
         this.actionOnItemsWithPrefix = this.actionOnItemsWithPrefix.bind(this);
+        this.actionOnGlobalItemsWithPrefix = this.actionOnGlobalItemsWithPrefix.bind(this);
         this.isLocalStorageSupported = this.isLocalStorageSupported.bind(this);
+        this.getLastServerVersion = this.getLastServerVersion.bind(this);
+        this.setLastServerVersion = this.setLastServerVersion.bind(this);
+        this.clear = this.clear.bind(this);
 
-        var currentVersion = localStorage.getItem('local_storage_version');
-        if (currentVersion !== global.window.config.Version) {
-            this.clear();
-            localStorage.setItem('local_storage_version', global.window.config.Version);
+        var currentVersion = sessionStorage.getItem('storage_version');
+        if (currentVersion !== global.window.mm_config.Version) {
+            sessionStorage.clear();
+            sessionStorage.setItem('storage_version', global.window.mm_config.Version);
         }
     }
 
     getItem(name, defaultValue) {
-        return this.getGlobalItem(getPrefix() + name, defaultValue);
+        var result = null;
+        try {
+            result = JSON.parse(sessionStorage.getItem(getPrefix() + name));
+        } catch (err) {
+            result = null;
+        }
+
+        if (result === null && typeof defaultValue !== 'undefined') {
+            result = defaultValue;
+        }
+
+        return result;
     }
 
     setItem(name, value) {
-        this.setGlobalItem(getPrefix() + name, value);
+        sessionStorage.setItem(getPrefix() + name, JSON.stringify(value));
     }
 
     removeItem(name) {
-        localStorage.removeItem(getPrefix() + name);
+        sessionStorage.removeItem(getPrefix() + name);
     }
 
     setGlobalItem(name, value) {
         try {
-            localStorage.setItem(name, JSON.stringify(value));
+            if (this.isLocalStorageSupported()) {
+                localStorage.setItem(name, JSON.stringify(value));
+            } else {
+                sessionStorage.setItem(name, JSON.stringify(value));
+            }
         } catch (err) {
             console.log('An error occurred while setting local storage, clearing all props'); //eslint-disable-line no-console
             localStorage.clear();
@@ -53,7 +71,11 @@ class BrowserStoreClass {
     getGlobalItem(name, defaultValue) {
         var result = null;
         try {
-            result = JSON.parse(localStorage.getItem(name));
+            if (this.isLocalStorageSupported()) {
+                result = JSON.parse(localStorage.getItem(name));
+            } else {
+                result = JSON.parse(sessionStorage.getItem(name));
+            }
         } catch (err) {
             result = null;
         }
@@ -66,19 +88,26 @@ class BrowserStoreClass {
     }
 
     removeGlobalItem(name) {
-        localStorage.removeItem(name);
+        if (this.isLocalStorageSupported()) {
+            localStorage.removeItem(name);
+        } else {
+            sessionStorage.removeItem(name);
+        }
     }
 
-    clear() {
-        localStorage.clear();
-        sessionStorage.clear();
+    getLastServerVersion() {
+        return sessionStorage.getItem('last_server_version');
+    }
+
+    setLastServerVersion(version) {
+        sessionStorage.setItem('last_server_version', version);
     }
 
     /**
      * Preforms the given action on each item that has the given prefix
      * Signature for action is action(key, value)
      */
-    actionOnItemsWithPrefix(prefix, action) {
+    actionOnGlobalItemsWithPrefix(prefix, action) {
         var globalPrefix = getPrefix();
         var globalPrefixiLen = globalPrefix.length;
         for (var key in localStorage) {
@@ -89,6 +118,21 @@ class BrowserStoreClass {
         }
     }
 
+    actionOnItemsWithPrefix(prefix, action) {
+        var globalPrefix = getPrefix();
+        var globalPrefixiLen = globalPrefix.length;
+        for (var key in sessionStorage) {
+            if (key.lastIndexOf(globalPrefix + prefix, 0) === 0) {
+                var userkey = key.substring(globalPrefixiLen);
+                action(userkey, this.getGlobalItem(key));
+            }
+        }
+    }
+
+    clear() {
+        sessionStorage.clear();
+    }
+
     isLocalStorageSupported() {
         try {
             sessionStorage.setItem('testSession', '1');
-- 
cgit v1.2.3-1-g7c22


From cbbee5cadb1c0096bfff9ce1cc80d4be7adb5717 Mon Sep 17 00:00:00 2001
From: =Corey Hulen <corey@hulen.com>
Date: Thu, 22 Oct 2015 09:36:46 -0700
Subject: Addressing issues from group code review

---
 web/react/stores/browser_store.jsx | 27 ++++++++++++++++++++-------
 1 file changed, 20 insertions(+), 7 deletions(-)

(limited to 'web/react/stores/browser_store.jsx')

diff --git a/web/react/stores/browser_store.jsx b/web/react/stores/browser_store.jsx
index 3eacff8b4..adaca44ee 100644
--- a/web/react/stores/browser_store.jsx
+++ b/web/react/stores/browser_store.jsx
@@ -23,6 +23,7 @@ class BrowserStoreClass {
         this.getLastServerVersion = this.getLastServerVersion.bind(this);
         this.setLastServerVersion = this.setLastServerVersion.bind(this);
         this.clear = this.clear.bind(this);
+        this.clearAll = this.clearAll.bind(this);
 
         var currentVersion = sessionStorage.getItem('storage_version');
         if (currentVersion !== global.window.mm_config.Version) {
@@ -57,13 +58,14 @@ class BrowserStoreClass {
     setGlobalItem(name, value) {
         try {
             if (this.isLocalStorageSupported()) {
-                localStorage.setItem(name, JSON.stringify(value));
+                localStorage.setItem(getPrefix() + name, JSON.stringify(value));
             } else {
-                sessionStorage.setItem(name, JSON.stringify(value));
+                sessionStorage.setItem(getPrefix() + name, JSON.stringify(value));
             }
         } catch (err) {
             console.log('An error occurred while setting local storage, clearing all props'); //eslint-disable-line no-console
             localStorage.clear();
+            sessionStorage.clear();
             window.location.href = window.location.href;
         }
     }
@@ -72,9 +74,9 @@ class BrowserStoreClass {
         var result = null;
         try {
             if (this.isLocalStorageSupported()) {
-                result = JSON.parse(localStorage.getItem(name));
+                result = JSON.parse(getPrefix() + localStorage.getItem(name));
             } else {
-                result = JSON.parse(sessionStorage.getItem(name));
+                result = JSON.parse(getPrefix() + sessionStorage.getItem(name));
             }
         } catch (err) {
             result = null;
@@ -89,9 +91,9 @@ class BrowserStoreClass {
 
     removeGlobalItem(name) {
         if (this.isLocalStorageSupported()) {
-            localStorage.removeItem(name);
+            localStorage.removeItem(getPrefix() + name);
         } else {
-            sessionStorage.removeItem(name);
+            sessionStorage.removeItem(getPrefix() + name);
         }
     }
 
@@ -110,7 +112,13 @@ class BrowserStoreClass {
     actionOnGlobalItemsWithPrefix(prefix, action) {
         var globalPrefix = getPrefix();
         var globalPrefixiLen = globalPrefix.length;
-        for (var key in localStorage) {
+
+        var storage = sessionStorage;
+        if (this.isLocalStorageSupported()) {
+            storage = localStorage;
+        }
+
+        for (var key in storage) {
             if (key.lastIndexOf(globalPrefix + prefix, 0) === 0) {
                 var userkey = key.substring(globalPrefixiLen);
                 action(userkey, this.getGlobalItem(key));
@@ -133,6 +141,11 @@ class BrowserStoreClass {
         sessionStorage.clear();
     }
 
+    clearAll() {
+        sessionStorage.clear();
+        localStorage.clear();
+    }
+
     isLocalStorageSupported() {
         try {
             sessionStorage.setItem('testSession', '1');
-- 
cgit v1.2.3-1-g7c22

