forked from mattermost/mattermost-webapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
switch_channel_provider.jsx
435 lines (367 loc) · 15.4 KB
/
switch_channel_provider.jsx
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React from 'react';
import PropTypes from 'prop-types';
import {connect} from 'react-redux';
import {UserTypes} from 'mattermost-redux/action_types';
import {Client4} from 'mattermost-redux/client';
import {Preferences} from 'mattermost-redux/constants';
import {
getChannelsInCurrentTeam,
getDirectChannels,
getSortedUnreadChannelIds,
makeGetChannel,
} from 'mattermost-redux/selectors/entities/channels';
import {getMyChannelMemberships} from 'mattermost-redux/selectors/entities/common';
import {getBool} from 'mattermost-redux/selectors/entities/preferences';
import {getConfig} from 'mattermost-redux/selectors/entities/general';
import {getCurrentTeamId} from 'mattermost-redux/selectors/entities/teams';
import {searchProfiles, getUserIdsInChannels, getUser} from 'mattermost-redux/selectors/entities/users';
import GlobeIcon from 'components/svg/globe_icon';
import LockIcon from 'components/svg/lock_icon';
import AppDispatcher from 'dispatcher/app_dispatcher.jsx';
import store from 'stores/redux_store.jsx';
import {getChannelDisplayName, sortChannelsByDisplayName} from 'utils/channel_utils.jsx';
import {ActionTypes, Constants} from 'utils/constants.jsx';
import * as Utils from 'utils/utils.jsx';
import Provider from './provider.jsx';
import Suggestion from './suggestion.jsx';
const getState = store.getState;
class SwitchChannelSuggestion extends Suggestion {
static get propTypes() {
return {
...super.propTypes,
channelMember: PropTypes.object,
};
}
render() {
const {item, isSelection} = this.props;
const channel = item.channel;
const member = this.props.channelMember;
let badge = null;
if (member) {
if (member.notify_props && member.mention_count > 0) {
badge = <span className='badge'>{member.mention_count}</span>;
}
}
let className = 'mentions__name';
if (isSelection) {
className += ' suggestion--selected';
}
let displayName = channel.display_name;
let icon = null;
if (channel.type === Constants.OPEN_CHANNEL) {
icon = (
<GlobeIcon className='icon icon__globe icon--body'/>
);
} else if (channel.type === Constants.PRIVATE_CHANNEL) {
icon = (
<LockIcon className='icon icon__lock icon--body'/>
);
} else if (channel.type === Constants.GM_CHANNEL) {
displayName = getChannelDisplayName(channel);
icon = <div className='status status--group'>{'G'}</div>;
} else {
icon = (
<div className='pull-left'>
<img
className='mention__image'
src={Utils.imageURLForUser(channel.userId)}
/>
</div>
);
}
return (
<div
onClick={this.handleClick}
className={className}
>
{icon}
{displayName}
{badge}
</div>
);
}
}
function mapStateToPropsForSwitchChannelSuggestion(state, ownProps) {
const channelId = ownProps.item && ownProps.item.channel ? ownProps.item.channel.id : '';
return {
channelMember: getMyChannelMemberships(state)[channelId],
};
}
const ConnectedSwitchChannelSuggestion = connect(mapStateToPropsForSwitchChannelSuggestion)(SwitchChannelSuggestion);
let prefix = '';
function quickSwitchSorter(wrappedA, wrappedB) {
if (wrappedA.type === Constants.MENTION_CHANNELS && wrappedB.type === Constants.MENTION_MORE_CHANNELS) {
return -1;
} else if (wrappedB.type === Constants.MENTION_CHANNELS && wrappedA.type === Constants.MENTION_MORE_CHANNELS) {
return 1;
}
if (wrappedA.deactivated && !wrappedB.deactivated) {
return 1;
} else if (wrappedB.deactivated && !wrappedA.deactivated) {
return -1;
}
const a = wrappedA.channel;
const b = wrappedB.channel;
let aDisplayName = getChannelDisplayName(a).toLowerCase();
let bDisplayName = getChannelDisplayName(b).toLowerCase();
if (a.type === Constants.DM_CHANNEL) {
aDisplayName = aDisplayName.substring(1);
}
if (b.type === Constants.DM_CHANNEL) {
bDisplayName = bDisplayName.substring(1);
}
const aStartsWith = aDisplayName.startsWith(prefix);
const bStartsWith = bDisplayName.startsWith(prefix);
if (aStartsWith && bStartsWith) {
return sortChannelsByDisplayName(a, b);
} else if (!aStartsWith && !bStartsWith) {
return sortChannelsByDisplayName(a, b);
} else if (aStartsWith) {
return -1;
}
return 1;
}
function makeChannelSearchFilter(channelPrefix) {
const channelPrefixLower = channelPrefix.toLowerCase();
const curState = getState();
const usersInChannels = getUserIdsInChannels(curState);
const userSearchStrings = {};
return (channel) => {
let searchString = channel.display_name;
if (channel.type === Constants.GM_CHANNEL || channel.type === Constants.DM_CHANNEL) {
const usersInChannel = usersInChannels[channel.id] || new Set([]);
// In case the channel is a DM and the profilesInChannel is not populated
if (!usersInChannel.length && channel.type === Constants.DM_CHANNEL) {
const userId = Utils.getUserIdFromChannelId(channel.name);
const user = getUser(curState, userId);
if (user) {
usersInChannel.add(userId);
}
}
for (const userId of usersInChannel) {
let userString = userSearchStrings[userId];
if (!userString) {
const user = getUser(curState, userId);
if (!user) {
continue;
}
const {nickname, username} = user;
userString = `${nickname}${username}${Utils.getFullName(user)}`;
userSearchStrings[userId] = userString;
}
searchString += userString;
}
}
return searchString.toLowerCase().includes(channelPrefixLower);
};
}
export default class SwitchChannelProvider extends Provider {
handlePretextChanged(suggestionId, channelPrefix) {
if (channelPrefix) {
prefix = channelPrefix;
this.startNewRequest(suggestionId, channelPrefix);
// Dispatch suggestions for local data
const channels = getChannelsInCurrentTeam(getState()).concat(getDirectChannels(getState()));
const users = Object.assign([], searchProfiles(getState(), channelPrefix, false));
this.formatChannelsAndDispatch(channelPrefix, suggestionId, channels, users, true);
// Fetch data from the server and dispatch
this.fetchUsersAndChannels(channelPrefix, suggestionId);
} else {
this.formatUnreadChannelsAndDispatch(suggestionId);
}
return true;
}
async fetchUsersAndChannels(channelPrefix, suggestionId) {
const state = getState();
const teamId = getCurrentTeamId(state);
if (!teamId) {
return;
}
const config = getConfig(state);
let usersAsync;
if (config.RestrictDirectMessage === 'team') {
usersAsync = Client4.autocompleteUsers(channelPrefix, teamId, '');
} else {
usersAsync = Client4.autocompleteUsers(channelPrefix, '', '');
}
const channelsAsync = Client4.searchChannels(teamId, channelPrefix);
let usersFromServer = [];
let channelsFromServer = [];
try {
usersFromServer = await usersAsync;
channelsFromServer = await channelsAsync;
} catch (err) {
AppDispatcher.handleServerAction({
type: ActionTypes.RECEIVED_ERROR,
err,
});
}
if (this.shouldCancelDispatch(channelPrefix)) {
return;
}
const users = Object.assign([], searchProfiles(state, channelPrefix, false)).concat(usersFromServer.users);
store.dispatch({
type: UserTypes.RECEIVED_PROFILES_LIST,
data: users,
});
const channels = getChannelsInCurrentTeam(state).concat(getDirectChannels(state)).concat(channelsFromServer);
this.formatChannelsAndDispatch(channelPrefix, suggestionId, channels, users);
}
userWrappedChannel(user, channel) {
let displayName = `@${user.username}`;
if ((user.first_name || user.last_name) && user.nickname) {
displayName += ` - ${Utils.getFullName(user)} (${user.nickname})`;
} else if (user.nickname) {
displayName += ` - (${user.nickname})`;
} else if (user.first_name || user.last_name) {
displayName += ` - ${Utils.getFullName(user)}`;
}
if (user.delete_at) {
displayName += ' - ' + Utils.localizeMessage('channel_switch_modal.deactivated', 'Deactivated');
}
return {
channel: {
display_name: displayName,
name: user.username,
id: channel ? channel.id : user.id,
userId: user.id,
update_at: user.update_at,
type: Constants.DM_CHANNEL,
last_picture_update: user.last_picture_update || 0,
},
name: user.username,
deactivated: user.delete_at,
};
}
formatChannelsAndDispatch(channelPrefix, suggestionId, allChannels, users, skipNotInChannel = false) {
const channels = [];
const members = getMyChannelMemberships(getState());
if (this.shouldCancelDispatch(channelPrefix)) {
return;
}
const completedChannels = {};
const channelFilter = makeChannelSearchFilter(channelPrefix);
for (const id of Object.keys(allChannels)) {
const channel = allChannels[id];
if (completedChannels[channel.id]) {
continue;
}
if (channelFilter(channel)) {
const newChannel = Object.assign({}, channel);
let wrappedChannel = {channel: newChannel, name: newChannel.name, deactivated: false};
if (newChannel.type === Constants.GM_CHANNEL) {
newChannel.name = getChannelDisplayName(newChannel);
wrappedChannel.name = newChannel.name;
const isGMVisible = getBool(getState(), Preferences.CATEGORY_GROUP_CHANNEL_SHOW, newChannel.id, false);
if (isGMVisible) {
wrappedChannel.type = Constants.MENTION_CHANNELS;
} else {
wrappedChannel.type = Constants.MENTION_MORE_CHANNELS;
if (skipNotInChannel) {
continue;
}
}
} else if (newChannel.type === Constants.DM_CHANNEL) {
const userId = Utils.getUserIdFromChannelId(newChannel.name);
const user = users.find((u) => u.id === userId);
if (user) {
completedChannels[user.id] = true;
wrappedChannel = this.userWrappedChannel(
user,
newChannel
);
const isDMVisible = getBool(getState(), Preferences.CATEGORY_DIRECT_CHANNEL_SHOW, user.id, false);
if (isDMVisible) {
wrappedChannel.type = Constants.MENTION_CHANNELS;
} else {
wrappedChannel.type = Constants.MENTION_MORE_CHANNELS;
if (skipNotInChannel) {
continue;
}
}
} else {
continue;
}
} else if (members[channel.id]) {
wrappedChannel.type = Constants.MENTION_CHANNELS;
} else {
wrappedChannel.type = Constants.MENTION_MORE_CHANNELS;
if (skipNotInChannel || !newChannel.display_name.toLowerCase().startsWith(channelPrefix)) {
continue;
}
}
completedChannels[channel.id] = true;
channels.push(wrappedChannel);
}
}
for (let i = 0; i < users.length; i++) {
const user = users[i];
if (completedChannels[user.id]) {
continue;
}
const isDMVisible = getBool(getState(), Preferences.CATEGORY_DIRECT_CHANNEL_SHOW, user.id, false);
const wrappedChannel = this.userWrappedChannel(user);
if (isDMVisible) {
wrappedChannel.type = Constants.MENTION_CHANNELS;
} else {
wrappedChannel.type = Constants.MENTION_MORE_CHANNELS;
if (skipNotInChannel) {
continue;
}
}
completedChannels[user.id] = true;
channels.push(wrappedChannel);
}
const channelNames = channels.
sort(quickSwitchSorter).
map((wrappedChannel) => wrappedChannel.channel.name);
if (skipNotInChannel) {
channels.push({
type: Constants.MENTION_MORE_CHANNELS,
loading: true,
});
}
setTimeout(() => {
AppDispatcher.handleServerAction({
type: ActionTypes.SUGGESTION_RECEIVED_SUGGESTIONS,
id: suggestionId,
matchedPretext: channelPrefix,
terms: channelNames,
items: channels,
component: ConnectedSwitchChannelSuggestion,
});
}, 0);
}
formatUnreadChannelsAndDispatch(suggestionId) {
const getChannel = makeGetChannel();
const unreadChannelIds = getSortedUnreadChannelIds(getState(), false);
const channels = [];
for (let i = 0; i < unreadChannelIds.length; i++) {
const channel = getChannel(getState(), {id: unreadChannelIds[i]}) || {};
let wrappedChannel = {channel, name: channel.name, deactivated: false};
if (channel.type === Constants.GM_CHANNEL) {
wrappedChannel.name = getChannelDisplayName(channel);
} else if (channel.type === Constants.DM_CHANNEL) {
wrappedChannel = this.userWrappedChannel(
getUser(getState(), Utils.getUserIdFromChannelId(channel.name)),
channel
);
}
wrappedChannel.type = Constants.MENTION_UNREAD_CHANNELS;
channels.push(wrappedChannel);
}
const channelNames = channels.map((wrappedChannel) => wrappedChannel.channel.name);
setTimeout(() => {
AppDispatcher.handleServerAction({
type: ActionTypes.SUGGESTION_RECEIVED_SUGGESTIONS,
id: suggestionId,
matchedPretext: '',
terms: channelNames,
items: channels,
component: ConnectedSwitchChannelSuggestion,
});
}, 0);
}
}