mattermost-mobile/app/actions/views/more_dms.js
enahum 4a53d47887 PLT-5598 Android back button support (#388)
* Add shadow to the drawer

* PLT-5964 open a DM with someone outside the team

* PLT-5598 Android back button support
2017-03-23 19:10:48 -03:00

32 lines
1.5 KiB
JavaScript

// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import {getDirectChannelName} from 'mattermost-redux/utils/channel_utils';
import {createDirectChannel} from 'mattermost-redux/actions/channels';
import {getProfilesByIds, getStatusesByIds} from 'mattermost-redux/actions/users';
import {handleSelectChannel, toggleDMChannel} from 'app/actions/views/channel';
export function makeDirectChannel(otherUserId) {
return async (dispatch, getState) => {
const state = getState();
const {currentUserId} = state.entities.users;
const channelName = getDirectChannelName(currentUserId, otherUserId);
const {channels, myMembers} = state.entities.channels;
const channel = Object.values(channels).find((c) => c.name === channelName);
const {currentTeamId} = state.entities.teams;
getProfilesByIds([otherUserId])(dispatch, getState);
getStatusesByIds([otherUserId])(dispatch, getState);
if (channel && myMembers[channel.id]) {
await toggleDMChannel(otherUserId, 'true')(dispatch, getState);
handleSelectChannel(channel.id)(dispatch, getState);
} else {
const created = await createDirectChannel(currentTeamId, currentUserId, otherUserId)(dispatch, getState);
if (created) {
await toggleDMChannel(otherUserId, 'true')(dispatch, getState);
handleSelectChannel(created.id)(dispatch, getState);
}
}
};
}