diff --git a/app/actions/views/channel.js b/app/actions/views/channel.js index ee419268f3..49a3ff7d54 100644 --- a/app/actions/views/channel.js +++ b/app/actions/views/channel.js @@ -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)); } diff --git a/app/mm-redux/actions/channels.test.js b/app/mm-redux/actions/channels.test.js index 23f5283f95..0789fa5e43 100644 --- a/app/mm-redux/actions/channels.test.js +++ b/app/mm-redux/actions/channels.test.js @@ -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)); diff --git a/app/mm-redux/actions/channels.ts b/app/mm-redux/actions/channels.ts index b1196cef5c..c244b875ad 100644 --- a/app/mm-redux/actions/channels.ts +++ b/app/mm-redux/actions/channels.ts @@ -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}; }; }