From ba6e370ca71abacaa30234cb164427d27c86df13 Mon Sep 17 00:00:00 2001
From: Joram Wilander <jwawilander@gmail.com>
Date: Wed, 21 Dec 2016 16:35:01 -0500
Subject: PLT-5012 Combine updateLastViewedAt, setLastViewedAt and
 setActiveChannel into a single API (#4840)

* Combine updateLastViewedAt, setLastViewedAt and setActiveChannel into a single API

* Remove preference DB writes
---
 webapp/utils/async_client.jsx | 94 +++++--------------------------------------
 1 file changed, 9 insertions(+), 85 deletions(-)

(limited to 'webapp/utils/async_client.jsx')

diff --git a/webapp/utils/async_client.jsx b/webapp/utils/async_client.jsx
index 2d8e76fc2..66f13f04d 100644
--- a/webapp/utils/async_client.jsx
+++ b/webapp/utils/async_client.jsx
@@ -138,33 +138,20 @@ export function getMyChannelMembers() {
     });
 }
 
-export function updateLastViewedAt(id, active) {
-    let channelId;
-    if (id) {
-        channelId = id;
-    } else {
-        channelId = ChannelStore.getCurrentId();
-    }
-
+export function viewChannel(channelId = ChannelStore.getCurrentId(), prevChannelId = '', time = 0) {
     if (channelId == null) {
         return;
     }
 
-    if (isCallInProgress(`updateLastViewed${channelId}`)) {
+    if (isCallInProgress(`viewChannel${channelId}`)) {
         return;
     }
 
-    let isActive;
-    if (active == null) {
-        isActive = true;
-    } else {
-        isActive = active;
-    }
-
-    callTracker[`updateLastViewed${channelId}`] = utils.getTimestamp();
-    Client.updateLastViewedAt(
+    callTracker[`viewChannel${channelId}`] = utils.getTimestamp();
+    Client.viewChannel(
         channelId,
-        isActive,
+        prevChannelId,
+        time,
         () => {
             AppDispatcher.handleServerAction({
                 type: ActionTypes.RECEIVED_PREFERENCE,
@@ -175,59 +162,14 @@ export function updateLastViewedAt(id, active) {
                 }
             });
 
-            callTracker[`updateLastViewed${channelId}`] = 0;
+            callTracker[`viewChannel${channelId}`] = 0;
             ErrorStore.clearLastError();
         },
         (err) => {
-            callTracker[`updateLastViewed${channelId}`] = 0;
+            callTracker[`viewChannel${channelId}`] = 0;
             const count = ErrorStore.getConnectionErrorCount();
             ErrorStore.setConnectionErrorCount(count + 1);
-            dispatchError(err, 'updateLastViewedAt');
-        }
-    );
-}
-
-export function setLastViewedAt(lastViewedAt, id) {
-    let channelId;
-    if (id) {
-        channelId = id;
-    } else {
-        channelId = ChannelStore.getCurrentId();
-    }
-
-    if (channelId == null) {
-        return;
-    }
-
-    if (lastViewedAt == null) {
-        return;
-    }
-
-    if (isCallInProgress(`setLastViewedAt${channelId}${lastViewedAt}`)) {
-        return;
-    }
-
-    callTracker[`setLastViewedAt${channelId}${lastViewedAt}`] = utils.getTimestamp();
-    Client.setLastViewedAt(
-        channelId,
-        lastViewedAt,
-        () => {
-            AppDispatcher.handleServerAction({
-                type: ActionTypes.RECEIVED_PREFERENCE,
-                preference: {
-                    category: 'last',
-                    name: TeamStore.getCurrentId(),
-                    value: channelId
-                }
-            });
-            callTracker[`setLastViewedAt${channelId}${lastViewedAt}`] = 0;
-            ErrorStore.clearLastError();
-        },
-        (err) => {
-            callTracker[`setLastViewedAt${channelId}${lastViewedAt}`] = 0;
-            var count = ErrorStore.getConnectionErrorCount();
-            ErrorStore.setConnectionErrorCount(count + 1);
-            dispatchError(err, 'setLastViewedAt');
+            dispatchError(err, 'viewChannel');
         }
     );
 }
@@ -795,24 +737,6 @@ export function getStatuses() {
     );
 }
 
-export function setActiveChannel(channelId) {
-    if (isCallInProgress(`setActiveChannel${channelId}`)) {
-        return;
-    }
-
-    callTracker[`setActiveChannel${channelId}`] = utils.getTimestamp();
-    Client.setActiveChannel(
-        channelId,
-        () => {
-            callTracker[`setActiveChannel${channelId}`] = 0;
-        },
-        (err) => {
-            callTracker[`setActiveChannel${channelId}`] = 0;
-            dispatchError(err, 'setActiveChannel');
-        }
-    );
-}
-
 export function getMyTeam() {
     if (isCallInProgress('getMyTeam')) {
         return null;
-- 
cgit v1.2.3-1-g7c22

