forked from Ivasoft/mattermost-mobile
[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:
@@ -9,6 +9,7 @@ import {ChannelTypes, RoleTypes, GroupTypes} from '@mm-redux/action_types';
|
|||||||
import {
|
import {
|
||||||
fetchMyChannelsAndMembers,
|
fetchMyChannelsAndMembers,
|
||||||
getChannelByNameAndTeamName,
|
getChannelByNameAndTeamName,
|
||||||
|
joinChannel,
|
||||||
leaveChannel as serviceLeaveChannel,
|
leaveChannel as serviceLeaveChannel,
|
||||||
} from '@mm-redux/actions/channels';
|
} from '@mm-redux/actions/channels';
|
||||||
import {savePreferences} from '@mm-redux/actions/preferences';
|
import {savePreferences} from '@mm-redux/actions/preferences';
|
||||||
@@ -22,6 +23,7 @@ import {
|
|||||||
getCurrentChannelId,
|
getCurrentChannelId,
|
||||||
getRedirectChannelNameForTeam,
|
getRedirectChannelNameForTeam,
|
||||||
getChannelsNameMapInTeam,
|
getChannelsNameMapInTeam,
|
||||||
|
getMyChannelMemberships,
|
||||||
isManuallyUnread,
|
isManuallyUnread,
|
||||||
} from '@mm-redux/selectors/entities/channels';
|
} from '@mm-redux/selectors/entities/channels';
|
||||||
import {getCurrentUserId} from '@mm-redux/selectors/entities/users';
|
import {getCurrentUserId} from '@mm-redux/selectors/entities/users';
|
||||||
@@ -210,13 +212,15 @@ export function handleSelectChannel(channelId) {
|
|||||||
|
|
||||||
export function handleSelectChannelByName(channelName, teamName, errorHandler) {
|
export function handleSelectChannelByName(channelName, teamName, errorHandler) {
|
||||||
return async (dispatch, getState) => {
|
return async (dispatch, getState) => {
|
||||||
const state = getState();
|
let state = getState();
|
||||||
const {teams: currentTeams, currentTeamId} = state.entities.teams;
|
const {teams: currentTeams, currentTeamId} = state.entities.teams;
|
||||||
const currentTeam = currentTeams[currentTeamId];
|
const currentTeam = currentTeams[currentTeamId];
|
||||||
const currentTeamName = currentTeam?.name;
|
const currentTeamName = currentTeam?.name;
|
||||||
const response = await dispatch(getChannelByNameAndTeamName(teamName || currentTeamName, channelName));
|
const response = await dispatch(getChannelByNameAndTeamName(teamName || currentTeamName, channelName));
|
||||||
const {error, data: channel} = response;
|
const {error, data: channel} = response;
|
||||||
const currentChannelId = getCurrentChannelId(state);
|
const currentChannelId = getCurrentChannelId(state);
|
||||||
|
|
||||||
|
state = getState();
|
||||||
const reachable = getChannelReachable(state, channelName, teamName);
|
const reachable = getChannelReachable(state, channelName, teamName);
|
||||||
|
|
||||||
if (!reachable && errorHandler) {
|
if (!reachable && errorHandler) {
|
||||||
@@ -234,6 +238,17 @@ export function handleSelectChannelByName(channelName, teamName, errorHandler) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (channel && currentChannelId !== channel.id) {
|
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));
|
dispatch(handleSelectChannel(channel.id));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -301,8 +301,12 @@ describe('Actions.Channels', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('getChannelByNameAndTeamName', async () => {
|
it('getChannelByNameAndTeamName', async () => {
|
||||||
nock(Client4.getTeamsRoute()).
|
nock(Client4.getBaseRoute()).
|
||||||
get(`/name/${TestHelper.basicTeam.name}/channels/name/${TestHelper.basicChannel.name}?include_deleted=false`).
|
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);
|
reply(200, TestHelper.basicChannel);
|
||||||
|
|
||||||
await store.dispatch(Actions.getChannelByNameAndTeamName(TestHelper.basicTeam.name, TestHelper.basicChannel.name));
|
await store.dispatch(Actions.getChannelByNameAndTeamName(TestHelper.basicTeam.name, TestHelper.basicChannel.name));
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
// See LICENSE.txt for license information.
|
// See LICENSE.txt for license information.
|
||||||
import {Client4} from '@mm-redux/client';
|
import {Client4} from '@mm-redux/client';
|
||||||
import {General, Preferences} from '../constants';
|
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 {savePreferences, deletePreferences} from './preferences';
|
||||||
import {compareNotifyProps, getChannelsIdForTeam, getChannelByName} from '@mm-redux/utils/channel_utils';
|
import {compareNotifyProps, getChannelsIdForTeam, getChannelByName} from '@mm-redux/utils/channel_utils';
|
||||||
import {
|
import {
|
||||||
@@ -396,9 +396,24 @@ export function updateChannelNotifyProps(userId: string, channelId: string, prop
|
|||||||
|
|
||||||
export function getChannelByNameAndTeamName(teamName: string, channelName: string, includeDeleted = false): ActionFunc {
|
export function getChannelByNameAndTeamName(teamName: string, channelName: string, includeDeleted = false): ActionFunc {
|
||||||
return async (dispatch: DispatchFunc, getState: GetStateFunc) => {
|
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 {
|
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) {
|
} catch (error) {
|
||||||
forceLogoutIfNecessary(error, dispatch, getState);
|
forceLogoutIfNecessary(error, dispatch, getState);
|
||||||
dispatch(batchActions([
|
dispatch(batchActions([
|
||||||
@@ -408,12 +423,18 @@ export function getChannelByNameAndTeamName(teamName: string, channelName: strin
|
|||||||
return {error};
|
return {error};
|
||||||
}
|
}
|
||||||
|
|
||||||
dispatch({
|
dispatch(batchActions([
|
||||||
type: ChannelTypes.RECEIVED_CHANNEL,
|
{
|
||||||
data,
|
type: TeamTypes.RECEIVED_TEAM,
|
||||||
});
|
data: team,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: ChannelTypes.RECEIVED_CHANNEL,
|
||||||
|
data: channel,
|
||||||
|
},
|
||||||
|
]));
|
||||||
|
|
||||||
return {data};
|
return {data: channel};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user