[MM-28100] fix reachability check and ensure channel membership before getting posts (#4810)

* [MM-28100] Reload state for newly fetched channels before checking reachability

* join selected channel if not already a member

* fix destructuring

* Revert joining channels in handleSelectChannel

* refactor channel joining

* use different server endpoint to work around requiring a server update
This commit is contained in:
Ashish Bhate
2020-09-25 00:29:46 +05:30
committed by GitHub
parent 0437b09438
commit 7647367bcf
3 changed files with 51 additions and 11 deletions

View File

@@ -9,6 +9,7 @@ import {ChannelTypes, RoleTypes, GroupTypes} from '@mm-redux/action_types';
import {
fetchMyChannelsAndMembers,
getChannelByNameAndTeamName,
joinChannel,
leaveChannel as serviceLeaveChannel,
} from '@mm-redux/actions/channels';
import {savePreferences} from '@mm-redux/actions/preferences';
@@ -22,6 +23,7 @@ import {
getCurrentChannelId,
getRedirectChannelNameForTeam,
getChannelsNameMapInTeam,
getMyChannelMemberships,
isManuallyUnread,
} from '@mm-redux/selectors/entities/channels';
import {getCurrentUserId} from '@mm-redux/selectors/entities/users';
@@ -210,13 +212,15 @@ export function handleSelectChannel(channelId) {
export function handleSelectChannelByName(channelName, teamName, errorHandler) {
return async (dispatch, getState) => {
const state = getState();
let state = getState();
const {teams: currentTeams, currentTeamId} = state.entities.teams;
const currentTeam = currentTeams[currentTeamId];
const currentTeamName = currentTeam?.name;
const response = await dispatch(getChannelByNameAndTeamName(teamName || currentTeamName, channelName));
const {error, data: channel} = response;
const currentChannelId = getCurrentChannelId(state);
state = getState();
const reachable = getChannelReachable(state, channelName, teamName);
if (!reachable && errorHandler) {
@@ -234,6 +238,17 @@ export function handleSelectChannelByName(channelName, teamName, errorHandler) {
}
if (channel && currentChannelId !== channel.id) {
if (channel.type === General.OPEN_CHANNEL) {
const myMemberships = getMyChannelMemberships(state);
if (!myMemberships[channel.id]) {
const currentUserId = getCurrentUserId(state);
console.log('joining channel', channel?.display_name, channel.id); //eslint-disable-line
const result = await dispatch(joinChannel(currentUserId, teamName, channel.id));
if (result.error || !result.data || !result.data.channel) {
return {error};
}
}
}
dispatch(handleSelectChannel(channel.id));
}

View File

@@ -301,8 +301,12 @@ describe('Actions.Channels', () => {
});
it('getChannelByNameAndTeamName', async () => {
nock(Client4.getTeamsRoute()).
get(`/name/${TestHelper.basicTeam.name}/channels/name/${TestHelper.basicChannel.name}?include_deleted=false`).
nock(Client4.getBaseRoute()).
get(`/teams/name/${TestHelper.basicTeam.name}`).
reply(200, TestHelper.basicTeam);
nock(Client4.getBaseRoute()).
get(`/teams/${TestHelper.basicTeam.id}/channels/name/${TestHelper.basicChannel.name}?include_deleted=false`).
reply(200, TestHelper.basicChannel);
await store.dispatch(Actions.getChannelByNameAndTeamName(TestHelper.basicTeam.name, TestHelper.basicChannel.name));

View File

@@ -2,7 +2,7 @@
// See LICENSE.txt for license information.
import {Client4} from '@mm-redux/client';
import {General, Preferences} from '../constants';
import {ChannelTypes, PreferenceTypes, UserTypes} from '@mm-redux/action_types';
import {ChannelTypes, PreferenceTypes, TeamTypes, UserTypes} from '@mm-redux/action_types';
import {savePreferences, deletePreferences} from './preferences';
import {compareNotifyProps, getChannelsIdForTeam, getChannelByName} from '@mm-redux/utils/channel_utils';
import {
@@ -396,9 +396,24 @@ export function updateChannelNotifyProps(userId: string, channelId: string, prop
export function getChannelByNameAndTeamName(teamName: string, channelName: string, includeDeleted = false): ActionFunc {
return async (dispatch: DispatchFunc, getState: GetStateFunc) => {
let data;
// The getChannelByNameForTeamName server endpoint had permission issues
// which were fixed in v5.28. We use a different endpoint here until
// the minimum server version required is 5.28 or greater.
let team;
try {
data = await Client4.getChannelByNameAndTeamName(teamName, channelName, includeDeleted);
team = await Client4.getTeamByName(teamName);
} catch (error) {
forceLogoutIfNecessary(error, dispatch, getState);
dispatch(batchActions([
{type: TeamTypes.GET_TEAMS_FAILURE, error},
logError(error),
]));
return {error};
}
let channel;
try {
channel = await Client4.getChannelByName(team.id, channelName, includeDeleted);
} catch (error) {
forceLogoutIfNecessary(error, dispatch, getState);
dispatch(batchActions([
@@ -408,12 +423,18 @@ export function getChannelByNameAndTeamName(teamName: string, channelName: strin
return {error};
}
dispatch({
type: ChannelTypes.RECEIVED_CHANNEL,
data,
});
dispatch(batchActions([
{
type: TeamTypes.RECEIVED_TEAM,
data: team,
},
{
type: ChannelTypes.RECEIVED_CHANNEL,
data: channel,
},
]));
return {data};
return {data: channel};
};
}