diff options
Diffstat (limited to 'webapp/actions/channel_actions.jsx')
-rw-r--r-- | webapp/actions/channel_actions.jsx | 137 |
1 files changed, 133 insertions, 4 deletions
diff --git a/webapp/actions/channel_actions.jsx b/webapp/actions/channel_actions.jsx index ed8e00db6..8364fe9b6 100644 --- a/webapp/actions/channel_actions.jsx +++ b/webapp/actions/channel_actions.jsx @@ -1,18 +1,26 @@ // Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. // See License.txt for license information. -import {browserHistory} from 'react-router/es6'; -import * as Utils from 'utils/utils.jsx'; +import AppDispatcher from 'dispatcher/app_dispatcher.jsx'; + import TeamStore from 'stores/team_store.jsx'; import UserStore from 'stores/user_store.jsx'; import ChannelStore from 'stores/channel_store.jsx'; +import PreferenceStore from 'stores/preference_store.jsx'; + +import {loadProfilesAndTeamMembersForDMSidebar} from 'actions/user_actions.jsx'; + +import Client from 'client/web_client.jsx'; import * as AsyncClient from 'utils/async_client.jsx'; import * as UserAgent from 'utils/user_agent.jsx'; -import Client from 'client/web_client.jsx'; +import * as Utils from 'utils/utils.jsx'; +import {Preferences, ActionTypes} from 'utils/constants.jsx'; + +import {browserHistory} from 'react-router/es6'; export function goToChannel(channel) { if (channel.fake) { - Utils.openDirectChannelToUser( + openDirectChannelToUser( UserStore.getProfileByUsername(channel.display_name), () => { browserHistory.push(TeamStore.getCurrentTeamRelativeUrl() + '/channels/' + channel.name); @@ -53,3 +61,124 @@ export function setChannelAsRead(channelIdParam) { ChannelStore.emitLastViewed(Number.MAX_VALUE, false); } } + +export function addUserToChannel(channelId, userId, success, error) { + Client.addChannelMember( + channelId, + userId, + (data) => { + UserStore.removeProfileNotInChannel(channelId, userId); + const profile = UserStore.getProfile(userId); + if (profile) { + UserStore.saveProfileInChannel(channelId, profile); + UserStore.emitInChannelChange(); + } + UserStore.emitNotInChannelChange(); + + if (success) { + success(data); + } + }, + (err) => { + AsyncClient.dispatchError(err, 'addChannelMember'); + + if (error) { + error(err); + } + } + ); +} + +export function removeUserFromChannel(channelId, userId, success, error) { + Client.removeChannelMember( + channelId, + userId, + (data) => { + UserStore.removeProfileInChannel(channelId, userId); + const profile = UserStore.getProfile(userId); + if (profile) { + UserStore.saveProfileNotInChannel(channelId, profile); + UserStore.emitNotInChannelChange(); + } + UserStore.emitInChannelChange(); + + if (success) { + success(data); + } + }, + (err) => { + AsyncClient.dispatchError(err, 'removeChannelMember'); + + if (error) { + error(err); + } + } + ); +} + +export function openDirectChannelToUser(user, success, error) { + const channelName = Utils.getDirectChannelName(UserStore.getCurrentId(), user.id); + let channel = ChannelStore.getByName(channelName); + + if (channel) { + PreferenceStore.setPreference(Preferences.CATEGORY_DIRECT_CHANNEL_SHOW, user.id, 'true'); + loadProfilesAndTeamMembersForDMSidebar(); + + AsyncClient.savePreference( + Preferences.CATEGORY_DIRECT_CHANNEL_SHOW, + user.id, + 'true' + ); + + if (success) { + success(channel, true); + } + + return; + } + + channel = { + name: channelName, + last_post_at: 0, + total_msg_count: 0, + type: 'D', + display_name: user.username, + teammate_id: user.id, + status: UserStore.getStatus(user.id) + }; + + Client.createDirectChannel( + user.id, + (data) => { + Client.getChannel( + data.id, + (data2) => { + AppDispatcher.handleServerAction({ + type: ActionTypes.RECEIVED_CHANNEL, + channel: data2.channel, + member: data2.member + }); + + PreferenceStore.setPreference(Preferences.CATEGORY_DIRECT_CHANNEL_SHOW, user.id, 'true'); + loadProfilesAndTeamMembersForDMSidebar(); + + AsyncClient.savePreference( + Preferences.CATEGORY_DIRECT_CHANNEL_SHOW, + user.id, + 'true' + ); + + if (success) { + success(data2.channel, false); + } + } + ); + }, + () => { + browserHistory.push(TeamStore.getCurrentTeamUrl() + '/channels/' + channelName); + if (error) { + error(); + } + } + ); +} |