forked from mattermost/mattermost-webapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.js
85 lines (68 loc) · 3.43 KB
/
actions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {getChannel, selectChannel, joinChannel, getChannelStats} from 'mattermost-redux/actions/channels';
import {getPostThread} from 'mattermost-redux/actions/posts';
import {getCurrentTeamId, getTeam} from 'mattermost-redux/selectors/entities/teams';
import {getUser} from 'mattermost-redux/selectors/entities/users';
import {loadChannelsForCurrentUser} from 'actions/channel_actions.jsx';
import {loadNewDMIfNeeded, loadNewGMIfNeeded} from 'actions/user_actions.jsx';
import {browserHistory} from 'utils/browser_history';
import {ActionTypes, Constants, ErrorPageTypes} from 'utils/constants';
import {getUserIdFromChannelId} from 'utils/utils';
export function focusPost(postId, returnTo = '', currentUserId) {
return async (dispatch, getState) => {
const {data} = await dispatch(getPostThread(postId));
if (!data) {
browserHistory.replace(`/error?type=${ErrorPageTypes.PERMALINK_NOT_FOUND}&returnTo=${returnTo}`);
return;
}
const state = getState();
const channelId = data.posts[data.order[0]].channel_id;
let channel = state.entities.channels.channels[channelId];
const teamId = getCurrentTeamId(state);
if (!channel) {
const {data: channelData} = await dispatch(getChannel(channelId));
if (!channelData) {
browserHistory.replace(`/error?type=${ErrorPageTypes.PERMALINK_NOT_FOUND}&returnTo=${returnTo}`);
return;
}
channel = channelData;
}
const myMember = state.entities.channels.myMembers[channelId];
if (!myMember) {
// If it's a DM or GM channel and we don't have a channel member for it already, user is not a member
if (channel.type === Constants.DM_CHANNEL || channel.type === Constants.GM_CHANNEL) {
browserHistory.replace(`/error?type=${ErrorPageTypes.PERMALINK_NOT_FOUND}&returnTo=${returnTo}`);
return;
}
await dispatch(joinChannel(currentUserId, null, channelId));
}
if (channel.team_id && channel.team_id !== teamId) {
browserHistory.replace(`/error?type=${ErrorPageTypes.PERMALINK_NOT_FOUND}&returnTo=${returnTo}`);
return;
}
if (channel && channel.type === Constants.DM_CHANNEL) {
dispatch(loadNewDMIfNeeded(channel.id));
} else if (channel && channel.type === Constants.GM_CHANNEL) {
dispatch(loadNewGMIfNeeded(channel.id));
}
dispatch(selectChannel(channelId));
dispatch({
type: ActionTypes.RECEIVED_FOCUSED_POST,
data: postId,
channelId,
});
const team = getTeam(state, channel.team_id || teamId);
if (channel.type === Constants.DM_CHANNEL) {
const userId = getUserIdFromChannelId(channel.name);
const user = getUser(state, userId);
browserHistory.replace(`/${team.name}/messages/@${user.username}/${postId}`);
} else if (channel.type === Constants.GM_CHANNEL) {
browserHistory.replace(`/${team.name}/messages/${channel.name}/${postId}`);
} else {
browserHistory.replace(`/${team.name}/channels/${channel.name}/${postId}`);
}
dispatch(loadChannelsForCurrentUser());
dispatch(getChannelStats(channelId));
};
}