This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
channel.js
328 lines (282 loc) · 10.8 KB
/
channel.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
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
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {batchActions} from 'redux-batched-actions';
import {
leaveChannel as leaveChannelRedux,
joinChannel,
markChannelAsRead,
unfavoriteChannel,
} from 'mattermost-redux/actions/channels';
import * as PostActions from 'mattermost-redux/actions/posts';
import {TeamTypes} from 'mattermost-redux/action_types';
import {autocompleteUsers} from 'mattermost-redux/actions/users';
import {selectTeam} from 'mattermost-redux/actions/teams';
import {Posts} from 'mattermost-redux/constants';
import {
getChannel,
getChannelsNameMapInCurrentTeam,
getCurrentChannel,
getRedirectChannelNameForTeam,
getMyChannels,
getMyChannelMemberships,
isManuallyUnread,
} from 'mattermost-redux/selectors/entities/channels';
import {getCurrentRelativeTeamUrl, getCurrentTeam, getCurrentTeamId} from 'mattermost-redux/selectors/entities/teams';
import {getCurrentUserId, getUserByUsername} from 'mattermost-redux/selectors/entities/users';
import {getMyPreferences} from 'mattermost-redux/selectors/entities/preferences';
import {getChannelByName, isFavoriteChannel} from 'mattermost-redux/utils/channel_utils';
import EventEmitter from 'mattermost-redux/utils/event_emitter';
import {openDirectChannelToUserId} from 'actions/channel_actions.jsx';
import {getLastViewedChannelName} from 'selectors/local_storage';
import {getLastPostsApiTimeForChannel} from 'selectors/views/channel';
import {getSocketStatus} from 'selectors/views/websocket';
import {browserHistory} from 'utils/browser_history';
import {Constants, ActionTypes, EventTypes, PostRequestTypes} from 'utils/constants';
import {isMobile} from 'utils/utils.jsx';
import LocalStorageStore from 'stores/local_storage_store.jsx';
export function checkAndSetMobileView() {
return (dispatch) => {
dispatch({
type: ActionTypes.UPDATE_MOBILE_VIEW,
data: isMobile(),
});
};
}
export function goToLastViewedChannel() {
return async (dispatch, getState) => {
const state = getState();
const currentChannel = getCurrentChannel(state);
const channelsInTeam = getChannelsNameMapInCurrentTeam(state);
let channelToSwitchTo = getChannelByName(channelsInTeam, getLastViewedChannelName(state));
if (currentChannel.id === channelToSwitchTo.id) {
channelToSwitchTo = getChannelByName(channelsInTeam, getRedirectChannelNameForTeam(state, getCurrentTeamId(state)));
}
return dispatch(switchToChannel(channelToSwitchTo));
};
}
export function switchToChannelById(channelId) {
return async (dispatch, getState) => {
const state = getState();
const channel = getChannel(state, channelId);
return dispatch(switchToChannel(channel));
};
}
export function switchToChannel(channel) {
return async (dispatch, getState) => {
const state = getState();
const teamUrl = getCurrentRelativeTeamUrl(state);
if (channel.fake || channel.userId) {
const username = channel.userId ? channel.name : channel.display_name;
const user = getUserByUsername(state, username);
if (!user) {
return {error: true};
}
const direct = await dispatch(openDirectChannelToUserId(user.id));
if (direct.error) {
return {error: true};
}
browserHistory.push(`${teamUrl}/messages/@${channel.name}`);
} else if (channel.type === Constants.GM_CHANNEL) {
const gmChannel = getChannel(state, channel.id);
browserHistory.push(`${teamUrl}/channels/${gmChannel.name}`);
} else {
browserHistory.push(`${teamUrl}/channels/${channel.name}`);
}
return {data: true};
};
}
export function joinChannelById(channelId) {
return async (dispatch, getState) => {
const state = getState();
const currentUserId = getCurrentUserId(state);
const currentTeamId = getCurrentTeamId(state);
return dispatch(joinChannel(currentUserId, currentTeamId, channelId));
};
}
export function leaveChannel(channelId) {
return async (dispatch, getState) => {
const state = getState();
const myPreferences = getMyPreferences(state);
const currentUserId = getCurrentUserId(state);
const currentTeam = getCurrentTeam(state);
if (isFavoriteChannel(myPreferences, channelId)) {
dispatch(unfavoriteChannel(channelId));
}
const teamUrl = getCurrentRelativeTeamUrl(state);
LocalStorageStore.removePreviousChannelName(currentUserId, currentTeam.id);
const {error} = await dispatch(leaveChannelRedux(channelId));
if (error) {
return {error};
}
const prevChannelName = LocalStorageStore.getPreviousChannelName(currentUserId, currentTeam.id);
const channelsInTeam = getChannelsNameMapInCurrentTeam(state);
const prevChannel = getChannelByName(channelsInTeam, prevChannelName);
if (!prevChannel || !getMyChannelMemberships(getState())[prevChannel.id]) {
LocalStorageStore.removePreviousChannelName(currentUserId, currentTeam.id);
}
if (getMyChannels(getState()).filter((c) => c.type === Constants.OPEN_CHANNEL || c.type === Constants.PRIVATE_CHANNEL).length === 0) {
LocalStorageStore.removePreviousChannelName(currentUserId, currentTeam.id);
dispatch(selectTeam(''));
dispatch({type: TeamTypes.LEAVE_TEAM, data: currentTeam});
browserHistory.push('/');
} else {
browserHistory.push(teamUrl);
}
return {
data: true,
};
};
}
export function autocompleteUsersInChannel(prefix, channelId) {
return async (dispatch, getState) => {
const state = getState();
const currentTeamId = getCurrentTeamId(state);
return dispatch(autocompleteUsers(prefix, currentTeamId, channelId));
};
}
export function loadUnreads(channelId) {
return async (dispatch) => {
const time = Date.now();
const {data, error} = await dispatch(PostActions.getPostsUnread(channelId, false));
if (error) {
return {
error,
atLatestMessage: false,
atOldestmessage: false,
};
}
dispatch({
type: ActionTypes.INCREASE_POST_VISIBILITY,
data: channelId,
amount: data.order.length,
});
if (data.next_post_id === '') {
dispatch({
type: ActionTypes.RECEIVED_POSTS_FOR_CHANNEL_AT_TIME,
channelId,
time,
});
}
return {
atLatestMessage: data.next_post_id === '',
atOldestmessage: data.prev_post_id === '',
};
};
}
export function loadPostsAround(channelId, focusedPostId) {
return async (dispatch) => {
const {data, error} = await dispatch(PostActions.getPostsAround(channelId, focusedPostId, Posts.POST_CHUNK_SIZE / 2, false));
if (error) {
return {
error,
atLatestMessage: false,
atOldestmessage: false,
};
}
dispatch({
type: ActionTypes.INCREASE_POST_VISIBILITY,
data: channelId,
amount: data.order.length,
});
return {
atLatestMessage: data.next_post_id === '',
atOldestmessage: data.prev_post_id === '',
};
};
}
export function loadLatestPosts(channelId) {
return async (dispatch) => {
const time = Date.now();
const {data, error} = await dispatch(PostActions.getPosts(channelId, 0, Posts.POST_CHUNK_SIZE / 2, false));
if (error) {
return {
error,
atLatestMessage: false,
atOldestmessage: false,
};
}
dispatch({
type: ActionTypes.RECEIVED_POSTS_FOR_CHANNEL_AT_TIME,
channelId,
time,
});
return {
data,
atLatestMessage: data.next_post_id === '',
atOldestmessage: data.prev_post_id === '',
};
};
}
export function loadPosts({channelId, postId, type}) {
//type here can be BEFORE_ID or AFTER_ID
return async (dispatch) => {
const POST_INCREASE_AMOUNT = Constants.POST_CHUNK_SIZE / 2;
dispatch({
type: ActionTypes.LOADING_POSTS,
data: true,
channelId,
});
const page = 0;
let result;
if (type === PostRequestTypes.BEFORE_ID) {
result = await dispatch(PostActions.getPostsBefore(channelId, postId, page, POST_INCREASE_AMOUNT, false));
} else {
result = await dispatch(PostActions.getPostsAfter(channelId, postId, page, POST_INCREASE_AMOUNT, false));
}
const {data} = result;
const actions = [{
type: ActionTypes.LOADING_POSTS,
data: false,
channelId,
}];
if (result.error) {
return {
error: result.error,
moreToLoad: true,
};
}
actions.push({
type: ActionTypes.INCREASE_POST_VISIBILITY,
data: channelId,
amount: data.order.length,
});
dispatch(batchActions(actions));
return {
moreToLoad: type === PostRequestTypes.BEFORE_ID ? data.prev_post_id !== '' : data.next_post_id !== '',
};
};
}
export function syncPostsInChannel(channelId, since, fetchThreads = true) {
return async (dispatch, getState) => {
const time = Date.now();
const state = getState();
const socketStatus = getSocketStatus(state);
let sinceTimeToGetPosts = since;
const lastPostsApiCallForChannel = getLastPostsApiTimeForChannel(state, channelId);
if (lastPostsApiCallForChannel && lastPostsApiCallForChannel < socketStatus.lastDisconnectAt) {
sinceTimeToGetPosts = lastPostsApiCallForChannel;
}
const {data, error} = await dispatch(PostActions.getPostsSince(channelId, sinceTimeToGetPosts, fetchThreads));
if (data) {
dispatch({
type: ActionTypes.RECEIVED_POSTS_FOR_CHANNEL_AT_TIME,
channelId,
time,
});
}
return {data, error};
};
}
export function scrollPostListToBottom() {
return () => {
EventEmitter.emit(EventTypes.POST_LIST_SCROLL_TO_BOTTOM);
};
}
export function markChannelAsReadOnFocus(channelId) {
return (dispatch, getState) => {
if (isManuallyUnread(getState(), channelId)) {
return;
}
dispatch(markChannelAsRead(channelId));
};
}