From db7540b111f8b9f983adcfc73fd897e5d83c2fef Mon Sep 17 00:00:00 2001
From: Joram Wilander <jwawilander@gmail.com>
Date: Wed, 8 Mar 2017 04:13:16 -0500
Subject: Add sync logic for DMs/GMs when network reconnects (#5676)

---
 webapp/actions/channel_actions.jsx | 29 +++++++++++++++++++++++++----
 1 file changed, 25 insertions(+), 4 deletions(-)

(limited to 'webapp/actions/channel_actions.jsx')

diff --git a/webapp/actions/channel_actions.jsx b/webapp/actions/channel_actions.jsx
index df7bacac6..e3563d79c 100644
--- a/webapp/actions/channel_actions.jsx
+++ b/webapp/actions/channel_actions.jsx
@@ -9,14 +9,14 @@ import ChannelStore from 'stores/channel_store.jsx';
 import * as ChannelUtils from 'utils/channel_utils.jsx';
 import PreferenceStore from 'stores/preference_store.jsx';
 
-import {loadProfilesForSidebar} from 'actions/user_actions.jsx';
+import {loadProfilesForSidebar, loadNewDMIfNeeded, loadNewGMIfNeeded} from 'actions/user_actions.jsx';
 import {trackEvent} from 'actions/diagnostics_actions.jsx';
 
 import Client from 'client/web_client.jsx';
 import * as AsyncClient from 'utils/async_client.jsx';
 import * as UserAgent from 'utils/user_agent.jsx';
 import * as Utils from 'utils/utils.jsx';
-import {Preferences, ActionTypes} from 'utils/constants.jsx';
+import {Constants, Preferences, ActionTypes} from 'utils/constants.jsx';
 
 import {browserHistory} from 'react-router/es6';
 
@@ -283,8 +283,29 @@ export function unmarkFavorite(channelId) {
 }
 
 export function loadChannelsForCurrentUser() {
-    AsyncClient.getChannels();
-    AsyncClient.getMyChannelMembers();
+    AsyncClient.getChannels().then(() => {
+        AsyncClient.getMyChannelMembers().then(() => {
+            loadDMsAndGMsForUnreads();
+        });
+    });
+}
+
+export function loadDMsAndGMsForUnreads() {
+    const unreads = ChannelStore.getUnreadCounts();
+    for (const id in unreads) {
+        if (!unreads.hasOwnProperty(id)) {
+            continue;
+        }
+
+        if (unreads[id].msgs > 0 || unreads[id].mentions > 0) {
+            const channel = ChannelStore.get(id);
+            if (channel && channel.type === Constants.DM_CHANNEL) {
+                loadNewDMIfNeeded(Utils.getUserIdFromChannelName(channel));
+            } else if (channel && channel.type === Constants.GM_CHANNEL) {
+                loadNewGMIfNeeded(channel.id);
+            }
+        }
+    }
 }
 
 export function joinChannel(channel, success, error) {
-- 
cgit v1.2.3-1-g7c22

