From fa2218a8352b9c718c18741b7dce0c3e2535cde0 Mon Sep 17 00:00:00 2001 From: enahum Date: Mon, 12 Jun 2017 12:03:59 -0400 Subject: [PATCH] RN-185 ensure leaving channels and teams when the app is closed or disconnected (#617) --- app/actions/views/channel.js | 18 +++++++----------- app/actions/views/root.js | 11 +++++++---- app/screens/load_team/load_team.js | 12 +++++++----- yarn.lock | 2 +- 4 files changed, 22 insertions(+), 21 deletions(-) diff --git a/app/actions/views/channel.js b/app/actions/views/channel.js index fc3f00775..83e3e2d42 100644 --- a/app/actions/views/channel.js +++ b/app/actions/views/channel.js @@ -27,7 +27,6 @@ import { isDirectChannel, isGroupChannel } from 'mattermost-redux/utils/channel_utils'; -import {getLastUpdateAt} from 'mattermost-redux/utils/post_utils'; import {getPreferencesByCategory} from 'mattermost-redux/utils/preference_utils'; export function loadChannelsIfNecessary(teamId) { @@ -139,22 +138,17 @@ export function loadProfilesAndTeamMembersForDMSidebar(teamId) { export function loadPostsIfNecessary(channel) { return async (dispatch, getState) => { const state = getState(); - const {posts, postsInChannel} = state.entities.posts; + const {channelsLastFetch} = state.internal; + const lastFetchTime = channelsLastFetch[channel.id] || 0; + const {postsInChannel} = state.entities.posts; const postsIds = postsInChannel[channel.id]; // Get the first page of posts if it appears we haven't gotten it yet, like the webapp - if (!postsIds || postsIds.length < Posts.POST_CHUNK_SIZE) { + if (!postsIds || postsIds.length < Posts.POST_CHUNK_SIZE || !lastFetchTime) { return getPosts(channel.id)(dispatch, getState); } - const postsForChannel = postsIds.map((id) => posts[id]); - const latestPostTime = getLastUpdateAt(postsForChannel); - - if (latestPostTime) { - return getPostsSince(channel.id, latestPostTime)(dispatch, getState); - } - - return null; + return getPostsSince(channel.id, lastFetchTime)(dispatch, getState); }; } @@ -191,12 +185,14 @@ export function selectInitialChannel(teamId) { const channel = Object.values(channels).find((c) => c.team_id === teamId && c.name === General.DEFAULT_CHANNEL); if (channel) { + dispatch(setChannelDisplayName('')); await handleSelectChannel(channel.id)(dispatch, getState); } else { // Handle case when the default channel cannot be found // so we need to get the first available channel of the team const channelsInTeam = Object.values(channels).filter((c) => c.team_id === teamId); const firstChannel = channelsInTeam[0].id; + dispatch(setChannelDisplayName('')); await handleSelectChannel(firstChannel.id)(dispatch, getState); } }; diff --git a/app/actions/views/root.js b/app/actions/views/root.js index f27a9821a..0454acd01 100644 --- a/app/actions/views/root.js +++ b/app/actions/views/root.js @@ -5,7 +5,8 @@ import {ViewTypes} from 'app/constants'; import { handleSelectChannel, loadChannelsIfNecessary, - loadProfilesAndTeamMembersForDMSidebar + loadProfilesAndTeamMembersForDMSidebar, + setChannelDisplayName } from 'app/actions/views/channel'; import {handleTeamChange, selectFirstAvailableTeam} from 'app/actions/views/select_team'; @@ -37,6 +38,7 @@ export function goToNotification(notification) { const state = getState(); const {data} = notification; const {currentTeamId, teams} = state.entities.teams; + const {currentChannelId} = state.entities.channels; const channelId = data.channel_id; // if the notification does not have a team id is because its from a DM or GM @@ -53,9 +55,10 @@ export function goToNotification(notification) { viewChannel(channelId)(dispatch, getState); loadProfilesAndTeamMembersForDMSidebar(teamId)(dispatch, getState); - // when the notification is tapped go to the channel view before selecting the channel to prevent - // weird behavior - handleSelectChannel(channelId)(dispatch, getState); + if (channelId !== currentChannelId) { + dispatch(setChannelDisplayName('')); + handleSelectChannel(channelId)(dispatch, getState); + } markChannelAsRead(teamId, channelId)(dispatch, getState).then(() => true).catch(() => true); }; } diff --git a/app/screens/load_team/load_team.js b/app/screens/load_team/load_team.js index e27fb57d5..30bc8d841 100644 --- a/app/screens/load_team/load_team.js +++ b/app/screens/load_team/load_team.js @@ -30,12 +30,14 @@ export default class LoadTeam extends PureComponent { if (notification) { clearNotification(); goToNotification(notification); - this.goToChannelView(); - } else if (currentTeam) { - this.onSelectTeam(currentTeam); - } else if (!currentTeam) { - this.selectFirstTeam(teams, myMembers); + return this.goToChannelView(); } + + if (currentTeam && myMembers[currentTeam.id]) { + return this.onSelectTeam(currentTeam); + } + + return this.selectFirstTeam(teams, myMembers); } componentWillReceiveProps(nextProps) { diff --git a/yarn.lock b/yarn.lock index 3d0aa2e23..8d636f924 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3645,7 +3645,7 @@ makeerror@1.0.x: mattermost-redux@mattermost/mattermost-redux#master: version "0.0.1" - resolved "https://codeload.github.com/mattermost/mattermost-redux/tar.gz/488faa22c03e1c187c714a5db7f0a42b69d50ae8" + resolved "https://codeload.github.com/mattermost/mattermost-redux/tar.gz/909f75d50678998a1fcb9d3b35d1fae9e918147f" dependencies: deep-equal "1.0.1" harmony-reflect "1.5.1"