summaryrefslogtreecommitdiffstats
path: root/webapp/routes/route_team.jsx
blob: 59cf6d6738db1dc25d11e673b971c1e39d908c50 (plain)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

import $ from 'jquery';
import * as RouteUtils from 'routes/route_utils.jsx';
import {browserHistory} from 'react-router/es6';

import TeamStore from 'stores/team_store.jsx';
import UserStore from 'stores/user_store.jsx';
import * as GlobalActions from 'actions/global_actions.jsx';
import {loadStatusesForChannelAndSidebar} from 'actions/status_actions.jsx';
import {openDirectChannelToUser} from 'actions/channel_actions.jsx';
import {reconnect} from 'actions/websocket_actions.jsx';
import AppDispatcher from 'dispatcher/app_dispatcher.jsx';
import Constants from 'utils/constants.jsx';
const ActionTypes = Constants.ActionTypes;
import ChannelStore from 'stores/channel_store.jsx';
import BrowserStore from 'stores/browser_store.jsx';
import * as Utils from 'utils/utils.jsx';

import emojiRoute from 'routes/route_emoji.jsx';
import integrationsRoute from 'routes/route_integrations.jsx';

import {loadNewDMIfNeeded, loadNewGMIfNeeded, loadProfilesForSidebar} from 'actions/user_actions.jsx';

// Redux actions
import store from 'stores/redux_store.jsx';
const dispatch = store.dispatch;
const getState = store.getState;

import {fetchMyChannelsAndMembers, joinChannel} from 'mattermost-redux/actions/channels';
import {getMyTeamUnreads} from 'mattermost-redux/actions/teams';
import {getUser, getUserByUsername, getUserByEmail} from 'mattermost-redux/actions/users';

function onChannelEnter(nextState, replace, callback) {
    doChannelChange(nextState, replace, callback);
}

function doChannelChange(state, replace, callback) {
    let channel;
    if (state.location.query.fakechannel) {
        channel = JSON.parse(state.location.query.fakechannel);
    } else {
        channel = ChannelStore.getByName(state.params.channel);

        if (channel && channel.type === Constants.DM_CHANNEL) {
            loadNewDMIfNeeded(channel.id);
        } else if (channel && channel.type === Constants.GM_CHANNEL) {
            loadNewGMIfNeeded(channel.id);
        }

        if (!channel) {
            joinChannel(UserStore.getCurrentId(), TeamStore.getCurrentId(), null, state.params.channel)(dispatch, getState).then(
                (result) => {
                    if (result.data) {
                        GlobalActions.emitChannelClickEvent(result.data.channel);
                    } else if (result.error) {
                        if (state.params.team) {
                            replace('/' + state.params.team + '/channels/town-square');
                        } else {
                            replace('/');
                        }
                    }
                    callback();
                }
            );
            return;
        }
    }
    GlobalActions.emitChannelClickEvent(channel);
    callback();
}

let wakeUpInterval;
let lastTime = (new Date()).getTime();
const WAKEUP_CHECK_INTERVAL = 30000; // 30 seconds
const WAKEUP_THRESHOLD = 60000; // 60 seconds

function preNeedsTeam(nextState, replace, callback) {
    if (RouteUtils.checkIfMFARequired(nextState)) {
        browserHistory.push('/mfa/setup');
        return;
    }

    clearInterval(wakeUpInterval);

    wakeUpInterval = setInterval(() => {
        const currentTime = (new Date()).getTime();
        if (currentTime > (lastTime + WAKEUP_THRESHOLD)) {  // ignore small delays
            console.log('computer woke up - fetching latest'); //eslint-disable-line no-console
            reconnect(false);
        }
        lastTime = currentTime;
    }, WAKEUP_CHECK_INTERVAL);

    // First check to make sure you're in the current team
    // for the current url.
    const teamName = nextState.params.team;
    const team = TeamStore.getByName(teamName);

    if (!team) {
        browserHistory.push('/?redirect_to=' + encodeURIComponent(nextState.location.pathname));
        return;
    }

    // If current team is set, then this is not first load
    // The first load action pulls team unreads
    if (TeamStore.getCurrentId()) {
        getMyTeamUnreads()(dispatch, getState);
    }

    TeamStore.saveMyTeam(team);
    BrowserStore.setGlobalItem('team', team.id);
    TeamStore.emitChange();
    GlobalActions.emitCloseRightHandSide();

    const d1 = $.Deferred(); //eslint-disable-line new-cap

    fetchMyChannelsAndMembers(team.id)(dispatch, getState).then(
        () => {
            loadStatusesForChannelAndSidebar();
            loadProfilesForSidebar();

            d1.resolve();
        }
    );

    $.when(d1).done(() => {
        callback();
    });
}

function selectLastChannel(nextState, replace, callback) {
    const team = TeamStore.getByName(nextState.params.team);
    const channelId = BrowserStore.getGlobalItem(team.id);
    const channel = ChannelStore.getChannelById(channelId);

    let channelName = 'town-square';
    if (channel) {
        channelName = channel.name;
    }

    replace(`/${team.name}/channels/${channelName}`);
    callback();
}

function onPermalinkEnter(nextState, replace, callback) {
    const postId = nextState.params.postid;
    GlobalActions.emitPostFocusEvent(
        postId,
        () => callback()
    );
}

/**
* identifier may either be:
* - A DM user_id length 26 chars
* - A DM channel_id (id1_id2) length 54 chars
* - A GM generated_id length 40 chars
* - A username that starts with an @ sign
* - An email containing an @ sign
**/
function onChannelByIdentifierEnter(state, replace, callback) {
    const {identifier} = state.params;
    if (identifier.indexOf('@') === -1) {
        // DM user_id or id1_id2 identifier
        if (identifier.length === 26 || identifier.length === 54) {
            const userId = (identifier.length === 26) ? identifier : Utils.getUserIdFromChannelId(identifier);
            const teammate = UserStore.getProfile(userId);
            if (teammate) {
                replace(`/${state.params.team}/messages/@${teammate.username}`);
                callback();
            } else {
                getUser(userId)(dispatch, getState).then(
                    (profile) => {
                        if (profile) {
                            replace(`/${state.params.team}/messages/@${profile.username}`);
                            callback();
                        } else if (profile == null) {
                            handleError(state, replace, callback);
                        }
                    }
                );
            }

        // GM generated_id identifier
        } else if (identifier.length === 40) {
            const channel = ChannelStore.getByName(identifier);
            if (channel) {
                loadNewGMIfNeeded(channel.id);
                GlobalActions.emitChannelClickEvent(channel);
                callback();
            } else {
                joinChannel(UserStore.getCurrentId(), TeamStore.getCurrentId(), null, identifier)(dispatch, getState).then(
                    (result) => {
                        if (result.data) {
                            GlobalActions.emitChannelClickEvent(result.data.channel);
                            callback();
                        } else if (result.error) {
                            handleError(state, replace, callback);
                        }
                    }
                );
            }
        } else {
            handleError(state, replace, callback);
        }
    } else {
        function success(profile) {
            AppDispatcher.handleServerAction({
                type: ActionTypes.RECEIVED_PROFILE,
                profile
            });
            directChannelToUser(profile, state, replace, callback);
        }

        function error() {
            handleError(state, replace, callback);
        }

        if (identifier.indexOf('@') === 0) { // @username identifier
            const username = identifier.slice(1, identifier.length);
            const teammate = UserStore.getProfileByUsername(username);
            if (teammate) {
                directChannelToUser(teammate, state, replace, callback);
            } else {
                getUserByUsername(username)(dispatch, getState).then(
                    (data) => {
                        if (data && success) {
                            success(data);
                        } else if (data == null && error) {
                            const serverError = getState().requests.users.getUserByUsername.error;
                            error({id: serverError.server_error_id, ...serverError});
                        }
                    }
                );
            }
        } else if (identifier.indexOf('@') > 0) { // email identifier
            const email = identifier;
            const teammate = UserStore.getProfileByEmail(email);
            if (teammate) {
                directChannelToUser(teammate, state, replace, callback);
            } else {
                getUserByEmail(email)(dispatch, getState).then(
                    (data) => {
                        if (data && success) {
                            success(data);
                        } else if (data == null && error) {
                            const serverError = getState().requests.users.getUser.error;
                            error({id: serverError.server_error_id, ...serverError});
                        }
                    }
                );
            }
        }
    }
}

function directChannelToUser(profile, state, replace, callback) {
    openDirectChannelToUser(
        profile.id,
        (channel) => {
            GlobalActions.emitChannelClickEvent(channel);
            callback();
        },
        () => {
            handleError(state, replace, callback);
        }
    );
}

function handleError(state, replace, callback) {
    if (state.params.team) {
        replace(`/${state.params.team}/channels/${Constants.DEFAULT_CHANNEL}`);
    } else {
        replace('/');
    }
    callback();
}

export default {
    path: ':team',
    onEnter: preNeedsTeam,
    indexRoute: {onEnter: selectLastChannel},
    childRoutes: [
        integrationsRoute,
        emojiRoute,
        {
            getComponents: (location, callback) => {
                System.import('components/needs_team').then(RouteUtils.importComponentSuccess(callback));
            },
            childRoutes: [
                {
                    path: 'channels/:channel',
                    onEnter: onChannelEnter,
                    getComponents: (location, callback) => {
                        Promise.all([
                            System.import('components/team_sidebar'),
                            System.import('components/sidebar.jsx'),
                            System.import('components/channel_view.jsx')
                        ]).then(
                        (comarr) => callback(null, {team_sidebar: comarr[0].default, sidebar: comarr[1].default, center: comarr[2].default})
                        );
                    }
                },
                {
                    path: 'pl/:postid',
                    onEnter: onPermalinkEnter,
                    getComponents: (location, callback) => {
                        Promise.all([
                            System.import('components/team_sidebar'),
                            System.import('components/sidebar.jsx'),
                            System.import('components/permalink_view.jsx')
                        ]).then(
                        (comarr) => callback(null, {team_sidebar: comarr[0].default, sidebar: comarr[1].default, center: comarr[2].default})
                        );
                    }
                },
                {
                    path: 'messages/:identifier',
                    onEnter: onChannelByIdentifierEnter,
                    getComponents: (location, callback) => {
                        Promise.all([
                            System.import('components/team_sidebar'),
                            System.import('components/sidebar.jsx'),
                            System.import('components/channel_view.jsx')
                        ]).then(
                        (comarr) => callback(null, {team_sidebar: comarr[0].default, sidebar: comarr[1].default, center: comarr[2].default})
                        );
                    }
                },
                {
                    path: 'tutorial',
                    getComponents: (location, callback) => {
                        Promise.all([
                            System.import('components/team_sidebar'),
                            System.import('components/sidebar.jsx'),
                            System.import('components/tutorial/tutorial_view.jsx')
                        ]).then(
                        (comarr) => callback(null, {team_sidebar: comarr[0].default, sidebar: comarr[1].default, center: comarr[2].default})
                        );
                    }
                }
            ]
        }
    ]
};