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
|
import Constants from 'utils/constants.jsx';
const Preferences = Constants.Preferences;
import * as Utils from 'utils/utils.jsx';
import UserStore from 'stores/user_store.jsx';
import ChannelStore from 'stores/channel_store.jsx';
import TeamStore from 'stores/team_store.jsx';
import PreferenceStore from 'stores/preference_store.jsx';
import LocalizationStore from 'stores/localization_store.jsx';
/**
* Returns list of sorted channels grouped by type. Favorites here is considered as separated type.
*
* Example: {
* publicChannels: [...],
* privateChannels: [...],
* directAndGroupChannels: [...],
* favoriteChannels: [...]
* }
*/
export function buildDisplayableChannelList(persistentChannels) {
const missingDirectChannels = createMissingDirectChannels(persistentChannels);
const channels = persistentChannels.
concat(missingDirectChannels).
map(completeDirectChannelInfo).
filter(isNotDeletedChannel).
sort(sortChannelsByDisplayName);
const favoriteChannels = channels.filter(isFavoriteChannel);
const notFavoriteChannels = channels.filter(not(isFavoriteChannel));
const directAndGroupChannels = notFavoriteChannels.filter(orX(andX(isGroupChannel, isGroupChannelVisible), andX(isDirectChannel, isDirectChannelVisible)));
return {
favoriteChannels,
publicChannels: notFavoriteChannels.filter(isOpenChannel),
privateChannels: notFavoriteChannels.filter(isPrivateChannel),
directAndGroupChannels
};
}
export function isFavoriteChannel(channel) {
return PreferenceStore.getBool(Preferences.CATEGORY_FAVORITE_CHANNEL, channel.id);
}
export function isFavoriteChannelId(channelId) {
return PreferenceStore.getBool(Preferences.CATEGORY_FAVORITE_CHANNEL, channelId);
}
export function isNotDeletedChannel(channel) {
return channel.delete_at === 0;
}
export function isOpenChannel(channel) {
return channel.type === Constants.OPEN_CHANNEL;
}
export function isPrivateChannel(channel) {
return channel.type === Constants.PRIVATE_CHANNEL;
}
export function isGroupChannel(channel) {
return channel.type === Constants.GM_CHANNEL;
}
export function isGroupChannelVisible(channel) {
return PreferenceStore.getBool(Preferences.CATEGORY_GROUP_CHANNEL_SHOW, channel.id);
}
export function isDirectChannel(channel) {
return channel.type === Constants.DM_CHANNEL;
}
export function isDirectChannelVisible(channel) {
const channelId = Utils.getUserIdFromChannelName(channel);
return PreferenceStore.getBool(Preferences.CATEGORY_DIRECT_CHANNEL_SHOW, channelId);
}
export function completeDirectChannelInfo(channel) {
if (!isDirectChannel(channel)) {
return channel;
}
const dmChannelClone = JSON.parse(JSON.stringify(channel));
const teammateId = Utils.getUserIdFromChannelName(channel);
return Object.assign(dmChannelClone, {
display_name: Utils.displayUsername(teammateId),
teammate_id: teammateId,
status: UserStore.getStatus(teammateId) || 'offline'
});
}
const defaultPrefix = 'D'; // fallback for future types
const typeToPrefixMap = {[Constants.OPEN_CHANNEL]: 'A', [Constants.PRIVATE_CHANNEL]: 'B', [Constants.DM_CHANNEL]: 'C', [Constants.GM_CHANNEL]: 'C'};
export function sortChannelsByDisplayName(a, b) {
const locale = LocalizationStore.getLocale();
if (a.type !== b.type && typeToPrefixMap[a.type] !== typeToPrefixMap[b.type]) {
return (typeToPrefixMap[a.type] || defaultPrefix).localeCompare((typeToPrefixMap[b.type] || defaultPrefix), locale);
}
const aDisplayName = getChannelDisplayName(a);
const bDisplayName = getChannelDisplayName(b);
if (aDisplayName !== bDisplayName) {
return aDisplayName.localeCompare(bDisplayName, locale, {numeric: true});
}
return a.name.localeCompare(b.name, locale, {numeric: true});
}
const MAX_CHANNEL_NAME_LENGTH = 64;
export function getChannelDisplayName(channel) {
if (channel.type !== Constants.GM_CHANNEL) {
return channel.display_name;
}
const currentUser = UserStore.getCurrentUser();
if (currentUser) {
let displayName = channel.display_name;
if (displayName.length >= MAX_CHANNEL_NAME_LENGTH) {
displayName += '...';
}
displayName = displayName.replace(currentUser.username + ', ', '').replace(currentUser.username, '').trim();
if (displayName[displayName.length - 1] === ',') {
return displayName.slice(0, -1);
}
return displayName;
}
return channel.display_name;
}
export function showCreateOption(channelType, isAdmin, isSystemAdmin) {
if (global.window.mm_license.IsLicensed !== 'true') {
return true;
}
if (channelType === Constants.OPEN_CHANNEL) {
if (global.window.mm_config.RestrictPublicChannelCreation === Constants.PERMISSIONS_SYSTEM_ADMIN && !isSystemAdmin) {
return false;
} else if (global.window.mm_config.RestrictPublicChannelCreation === Constants.PERMISSIONS_TEAM_ADMIN && !isAdmin) {
return false;
}
} else if (channelType === Constants.PRIVATE_CHANNEL) {
if (global.window.mm_config.RestrictPrivateChannelCreation === Constants.PERMISSIONS_SYSTEM_ADMIN && !isSystemAdmin) {
return false;
} else if (global.window.mm_config.RestrictPrivateChannelCreation === Constants.PERMISSIONS_TEAM_ADMIN && !isAdmin) {
return false;
}
}
return true;
}
export function showManagementOptions(channel, isAdmin, isSystemAdmin, isChannelAdmin) {
if (global.window.mm_license.IsLicensed !== 'true') {
return true;
}
if (channel.type === Constants.OPEN_CHANNEL) {
if (global.window.mm_config.RestrictPublicChannelManagement === Constants.PERMISSIONS_SYSTEM_ADMIN && !isSystemAdmin) {
return false;
}
if (global.window.mm_config.RestrictPublicChannelManagement === Constants.PERMISSIONS_TEAM_ADMIN && !isAdmin) {
return false;
}
if (global.window.mm_config.RestrictPublicChannelManagement === Constants.PERMISSIONS_CHANNEL_ADMIN && !isChannelAdmin && !isAdmin) {
return false;
}
} else if (channel.type === Constants.PRIVATE_CHANNEL) {
if (global.window.mm_config.RestrictPrivateChannelManagement === Constants.PERMISSIONS_SYSTEM_ADMIN && !isSystemAdmin) {
return false;
}
if (global.window.mm_config.RestrictPrivateChannelManagement === Constants.PERMISSIONS_TEAM_ADMIN && !isAdmin) {
return false;
}
if (global.window.mm_config.RestrictPrivateChannelManagement === Constants.PERMISSIONS_CHANNEL_ADMIN && !isChannelAdmin && !isAdmin) {
return false;
}
}
return true;
}
export function showDeleteOption(channel, isAdmin, isSystemAdmin, isChannelAdmin) {
if (global.window.mm_license.IsLicensed !== 'true') {
return true;
}
if (channel.type === Constants.OPEN_CHANNEL) {
if (global.window.mm_config.RestrictPublicChannelDeletion === Constants.PERMISSIONS_SYSTEM_ADMIN && !isSystemAdmin) {
return false;
}
if (global.window.mm_config.RestrictPublicChannelDeletion === Constants.PERMISSIONS_TEAM_ADMIN && !isAdmin) {
return false;
}
if (global.window.mm_config.RestrictPublicChannelDeletion === Constants.PERMISSIONS_CHANNEL_ADMIN && !isChannelAdmin && !isAdmin) {
return false;
}
} else if (channel.type === Constants.PRIVATE_CHANNEL) {
if (global.window.mm_config.RestrictPrivateChannelDeletion === Constants.PERMISSIONS_SYSTEM_ADMIN && !isSystemAdmin) {
return false;
}
if (global.window.mm_config.RestrictPrivateChannelDeletion === Constants.PERMISSIONS_TEAM_ADMIN && !isAdmin) {
return false;
}
if (global.window.mm_config.RestrictPrivateChannelDeletion === Constants.PERMISSIONS_CHANNEL_ADMIN && !isChannelAdmin && !isAdmin) {
return false;
}
}
return true;
}
export function canManageMembers(channel, isSystemAdmin, isTeamAdmin, isChannelAdmin) {
if (global.window.mm_license.IsLicensed !== 'true') {
return true;
}
if (channel.type === Constants.PRIVATE_CHANNEL) {
if (global.window.mm_config.RestrictPrivateChannelManageMembers === Constants.PERMISSIONS_SYSTEM_ADMIN && !isSystemAdmin) {
return false;
}
if (global.window.mm_config.RestrictPrivateChannelManageMembers === Constants.PERMISSIONS_TEAM_ADMIN && !isTeamAdmin && !isSystemAdmin) {
return false;
}
if (global.window.mm_config.RestrictPrivateChannelManageMembers === Constants.PERMISSIONS_CHANNEL_ADMIN && !isChannelAdmin && !isTeamAdmin && !isSystemAdmin) {
return false;
}
}
return true;
}
export function buildGroupChannelName(channelId) {
const profiles = UserStore.getProfileListInChannel(channelId, true);
let displayName = '';
for (let i = 0; i < profiles.length; i++) {
displayName += Utils.displayUsernameForUser(profiles[i]);
if (i !== profiles.length - 1) {
displayName += ', ';
}
}
return displayName;
}
export function getCountsStateFromStores(team = TeamStore.getCurrent(), teamMembers = TeamStore.getMyTeamMembers(), unreadCounts = ChannelStore.getUnreadCounts()) {
let mentionCount = 0;
let messageCount = 0;
teamMembers.forEach((member) => {
if (member.team_id !== TeamStore.getCurrentId()) {
mentionCount += (member.mention_count || 0);
messageCount += (member.msg_count || 0);
}
});
Object.keys(unreadCounts).forEach((chId) => {
const channel = ChannelStore.get(chId);
if (channel && (channel.type === Constants.DM_CHANNEL || channel.type === Constants.GM_CHANNEL || channel.team_id === team.id)) {
messageCount += unreadCounts[chId].msgs;
mentionCount += unreadCounts[chId].mentions;
}
});
return {mentionCount, messageCount};
}
/*
* not exported helpers
*/
function createMissingDirectChannels(channels) {
const directChannelsDisplayPreferences = PreferenceStore.getCategory(Preferences.CATEGORY_DIRECT_CHANNEL_SHOW);
return Array.
from(directChannelsDisplayPreferences).
filter((entry) => entry[1] === 'true').
map((entry) => entry[0]).
filter((teammateId) => !channels.some(Utils.isDirectChannelForUser.bind(null, teammateId))).
map(createFakeChannelCurried(UserStore.getCurrentId()));
}
function createFakeChannel(userId, otherUserId) {
return {
name: Utils.getDirectChannelName(userId, otherUserId),
last_post_at: 0,
total_msg_count: 0,
type: Constants.DM_CHANNEL,
fake: true
};
}
function createFakeChannelCurried(userId) {
return (otherUserId) => createFakeChannel(userId, otherUserId);
}
function not(f) {
return (...args) => !f(...args);
}
function orX(...fns) {
return (...args) => fns.some((f) => f(...args));
}
function andX(...fns) {
return (...args) => fns.every((f) => f(...args));
}
|