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
/
user_actions.jsx
335 lines (279 loc) · 11.7 KB
/
user_actions.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
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {getChannelAndMyMember, getChannelMembersByIds} from 'mattermost-redux/actions/channels';
import {savePreferences as savePreferencesRedux} from 'mattermost-redux/actions/preferences';
import {getTeamMembersByIds} from 'mattermost-redux/actions/teams';
import * as UserActions from 'mattermost-redux/actions/users';
import {Preferences as PreferencesRedux, General} from 'mattermost-redux/constants';
import {
getChannel,
getCurrentChannelId,
getMyChannels,
getMyChannelMember,
getChannelMembersInChannels,
} from 'mattermost-redux/selectors/entities/channels';
import {getBool} from 'mattermost-redux/selectors/entities/preferences';
import {getCurrentTeamId, getTeamMember} from 'mattermost-redux/selectors/entities/teams';
import * as Selectors from 'mattermost-redux/selectors/entities/users';
import {loadStatusesForProfilesList, loadStatusesForProfilesMap} from 'actions/status_actions.jsx';
import store from 'stores/redux_store.jsx';
import * as Utils from 'utils/utils.jsx';
import {Constants, Preferences, UserStatuses} from 'utils/constants';
const dispatch = store.dispatch;
const getState = store.getState;
export function loadProfilesAndStatusesInChannel(channelId, page = 0, perPage = General.PROFILE_CHUNK_SIZE, sort = '') {
return async (doDispatch) => {
const {data} = await doDispatch(UserActions.getProfilesInChannel(channelId, page, perPage, sort));
if (data) {
doDispatch(loadStatusesForProfilesList(data));
}
return {data: true};
};
}
export function loadProfilesAndTeamMembers(page, perPage, teamId, options) {
return async (doDispatch, doGetState) => {
const newTeamId = teamId || getCurrentTeamId(doGetState());
const {data} = await doDispatch(UserActions.getProfilesInTeam(newTeamId, page, perPage, '', options));
if (data) {
doDispatch(loadTeamMembersForProfilesList(data, newTeamId));
doDispatch(loadStatusesForProfilesList(data));
}
return {data: true};
};
}
export function loadProfilesAndTeamMembersAndChannelMembers(page, perPage, teamId, channelId) {
return async (doDispatch, doGetState) => {
const state = doGetState();
const teamIdParam = teamId || getCurrentTeamId(state);
const channelIdParam = channelId || getCurrentChannelId(state);
const {data} = await doDispatch(UserActions.getProfilesInChannel(channelIdParam, page, perPage));
if (data) {
const {data: listData} = await doDispatch(loadTeamMembersForProfilesList(data, teamIdParam));
if (listData) {
doDispatch(loadChannelMembersForProfilesList(data, channelIdParam));
doDispatch(loadStatusesForProfilesList(data));
}
}
return {data: true};
};
}
export function loadTeamMembersForProfilesList(profiles, teamId) {
return async (doDispatch, doGetState) => {
const state = doGetState();
const teamIdParam = teamId || getCurrentTeamId(state);
const membersToLoad = {};
for (let i = 0; i < profiles.length; i++) {
const pid = profiles[i].id;
if (!getTeamMember(state, teamIdParam, pid)) {
membersToLoad[pid] = true;
}
}
const userIdsToLoad = Object.keys(membersToLoad);
if (userIdsToLoad.length === 0) {
return {data: true};
}
await doDispatch(getTeamMembersByIds(teamIdParam, userIdsToLoad));
return {data: true};
};
}
export function loadProfilesWithoutTeam(page, perPage, options) {
return async (doDispatch) => {
const {data} = await doDispatch(UserActions.getProfilesWithoutTeam(page, perPage, options));
doDispatch(loadStatusesForProfilesMap(data));
return data;
};
}
export function loadTeamMembersAndChannelMembersForProfilesList(profiles, teamId, channelId) {
return async (doDispatch, doGetState) => {
const state = doGetState();
const teamIdParam = teamId || getCurrentTeamId(state);
const channelIdParam = channelId || getCurrentChannelId(state);
const {data} = await doDispatch(loadTeamMembersForProfilesList(profiles, teamIdParam));
if (data) {
doDispatch(loadChannelMembersForProfilesList(profiles, channelIdParam));
}
return {data: true};
};
}
export function loadChannelMembersForProfilesList(profiles, channelId) {
return async (doDispatch, doGetState) => {
const state = doGetState();
const channelIdParam = channelId || getCurrentChannelId(state);
const membersToLoad = {};
for (let i = 0; i < profiles.length; i++) {
const pid = profiles[i].id;
const members = getChannelMembersInChannels(state)[channelIdParam];
if (!members || !members[pid]) {
membersToLoad[pid] = true;
}
}
const list = Object.keys(membersToLoad);
if (list.length === 0) {
return {data: true};
}
await doDispatch(getChannelMembersByIds(channelIdParam, list));
return {data: true};
};
}
export async function loadNewDMIfNeeded(channelId) {
function checkPreference(channel) {
const userId = Utils.getUserIdFromChannelName(channel);
if (!userId) {
return;
}
const pref = getBool(getState(), Preferences.CATEGORY_DIRECT_CHANNEL_SHOW, userId, false);
if (pref === false) {
const now = Utils.getTimestamp();
const currentUserId = Selectors.getCurrentUserId(getState());
savePreferencesRedux(currentUserId, [
{user_id: currentUserId, category: Preferences.CATEGORY_DIRECT_CHANNEL_SHOW, name: userId, value: 'true'},
{user_id: currentUserId, category: Preferences.CATEGORY_CHANNEL_OPEN_TIME, name: channelId, value: now.toString()},
])(dispatch, getState);
loadProfilesForDM();
}
}
const channel = getChannel(getState(), channelId);
if (channel) {
checkPreference(channel);
} else {
const {data} = await getChannelAndMyMember(channelId)(dispatch, getState);
if (data) {
checkPreference(data.channel);
}
}
}
export async function loadNewGMIfNeeded(channelId) {
function checkPreference() {
const pref = getBool(getState(), Preferences.CATEGORY_GROUP_CHANNEL_SHOW, channelId, false);
if (pref === false) {
const currentUserId = Selectors.getCurrentUserId(getState());
savePreferencesRedux(currentUserId, [{user_id: currentUserId, category: Preferences.CATEGORY_GROUP_CHANNEL_SHOW, name: channelId, value: 'true'}])(dispatch, getState);
loadProfilesForGM();
}
}
const channel = getChannel(getState(), channelId);
if (!channel) {
await getChannelAndMyMember(channelId)(dispatch, getState);
}
checkPreference();
}
export function loadProfilesForGroupChannels(groupChannels) {
return (doDispatch, doGetState) => {
const state = doGetState();
const userIdsInChannels = Selectors.getUserIdsInChannels(state);
const groupChannelsToFetch = groupChannels.reduce((acc, {id}) => {
const userIdsInGroupChannel = (userIdsInChannels[id] || new Set());
if (userIdsInGroupChannel.size === 0) {
acc.push(id);
}
return acc;
}, []);
if (groupChannelsToFetch.length > 0) {
doDispatch(UserActions.getProfilesInGroupChannels(groupChannelsToFetch));
}
};
}
export function loadProfilesForSidebar() {
loadProfilesForDM();
loadProfilesForGM();
}
export async function loadProfilesForGM() {
const state = getState();
const channels = getMyChannels(state);
const newPreferences = [];
const userIdsInChannels = Selectors.getUserIdsInChannels(state);
const currentUserId = Selectors.getCurrentUserId(state);
for (let i = 0; i < channels.length; i++) {
const channel = channels[i];
if (channel.type !== Constants.GM_CHANNEL) {
continue;
}
const userIds = userIdsInChannels[channel.id] || new Set();
if (userIds.size >= Constants.MIN_USERS_IN_GM) {
continue;
}
const isVisible = getBool(state, Preferences.CATEGORY_GROUP_CHANNEL_SHOW, channel.id);
if (!isVisible) {
const member = getMyChannelMember(state, channel.id);
if (!member || (member.mention_count === 0 && member.msg_count >= channel.total_msg_count)) {
continue;
}
newPreferences.push({
user_id: currentUserId,
category: Preferences.CATEGORY_GROUP_CHANNEL_SHOW,
name: channel.id,
value: 'true',
});
}
await dispatch(UserActions.getProfilesInChannel(channel.id, 0, Constants.MAX_USERS_IN_GM)); //eslint-disable-line no-await-in-loop
}
if (newPreferences.length > 0) {
savePreferencesRedux(currentUserId, newPreferences)(dispatch, getState);
}
}
export async function loadProfilesForDM() {
const state = getState();
const channels = getMyChannels(state);
const newPreferences = [];
const profilesToLoad = [];
const profileIds = [];
const currentUserId = Selectors.getCurrentUserId(state);
for (let i = 0; i < channels.length; i++) {
const channel = channels[i];
if (channel.type !== Constants.DM_CHANNEL) {
continue;
}
const teammateId = channel.name.replace(currentUserId, '').replace('__', '');
const isVisible = getBool(state, Preferences.CATEGORY_DIRECT_CHANNEL_SHOW, teammateId);
if (!isVisible) {
const member = getMyChannelMember(state, channel.id);
if (!member || member.mention_count === 0) {
continue;
}
newPreferences.push({
user_id: currentUserId,
category: Preferences.CATEGORY_DIRECT_CHANNEL_SHOW,
name: teammateId,
value: 'true',
});
}
if (!Selectors.getUser(state, teammateId)) {
profilesToLoad.push(teammateId);
}
profileIds.push(teammateId);
}
if (newPreferences.length > 0) {
savePreferencesRedux(currentUserId, newPreferences)(dispatch, getState);
}
if (profilesToLoad.length > 0) {
await UserActions.getProfilesByIds(profilesToLoad)(dispatch, getState);
}
}
export function autocompleteUsersInTeam(username) {
return async (doDispatch, doGetState) => {
const currentTeamId = getCurrentTeamId(doGetState());
const {data} = await doDispatch(UserActions.autocompleteUsers(username, currentTeamId));
return data;
};
}
export function autocompleteUsers(username) {
return async (doDispatch) => {
const {data} = await doDispatch(UserActions.autocompleteUsers(username));
return data;
};
}
export function autoResetStatus() {
return async (doDispatch, doGetState) => {
const {currentUserId} = getState().entities.users;
const {data: userStatus} = await UserActions.getStatus(currentUserId)(doDispatch, doGetState);
if (userStatus.status === UserStatuses.OUT_OF_OFFICE || !userStatus.manual) {
return userStatus;
}
const autoReset = getBool(getState(), PreferencesRedux.CATEGORY_AUTO_RESET_MANUAL_STATUS, currentUserId, false);
if (autoReset) {
UserActions.setStatus({user_id: currentUserId, status: 'online'})(doDispatch, doGetState);
return userStatus;
}
return userStatus;
};
}