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_actions.jsx
171 lines (143 loc) · 5.75 KB
/
channel_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
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {batchActions} from 'redux-batched-actions';
import {PreferenceTypes} from 'mattermost-redux/action_types';
import * as ChannelActions from 'mattermost-redux/actions/channels';
import {savePreferences} from 'mattermost-redux/actions/preferences';
import {getMyChannelMemberships} from 'mattermost-redux/selectors/entities/common';
import {getChannelByName, getUnreadChannelIds, getChannel} from 'mattermost-redux/selectors/entities/channels';
import {getCurrentTeamUrl, getCurrentTeamId} from 'mattermost-redux/selectors/entities/teams';
import {getCurrentUserId} from 'mattermost-redux/selectors/entities/users';
import {trackEvent} from 'actions/diagnostics_actions.jsx';
import {loadNewDMIfNeeded, loadNewGMIfNeeded, loadProfilesForSidebar} from 'actions/user_actions.jsx';
import {browserHistory} from 'utils/browser_history';
import {Constants, Preferences, NotificationLevels} from 'utils/constants';
import {getDirectChannelName} from 'utils/utils';
export function openDirectChannelToUserId(userId) {
return async (dispatch, getState) => {
const state = getState();
const currentUserId = getCurrentUserId(state);
const channelName = getDirectChannelName(currentUserId, userId);
const channel = getChannelByName(state, channelName);
if (!channel) {
return dispatch(ChannelActions.createDirectChannel(currentUserId, userId));
}
trackEvent('api', 'api_channels_join_direct');
const now = Date.now();
const prefDirect = {
category: Preferences.CATEGORY_DIRECT_CHANNEL_SHOW,
name: userId,
value: 'true',
};
const prefOpenTime = {
category: Preferences.CATEGORY_CHANNEL_OPEN_TIME,
name: channel.id,
value: now.toString(),
};
const actions = [{
type: PreferenceTypes.RECEIVED_PREFERENCES,
data: [prefDirect],
}, {
type: PreferenceTypes.RECEIVED_PREFERENCES,
data: [prefOpenTime],
}];
dispatch(batchActions(actions));
dispatch(savePreferences(currentUserId, [
{user_id: currentUserId, ...prefDirect},
{user_id: currentUserId, ...prefOpenTime},
]));
return {data: channel};
};
}
export function openGroupChannelToUserIds(userIds) {
return async (dispatch, getState) => {
const result = await dispatch(ChannelActions.createGroupChannel(userIds));
if (result.error) {
browserHistory.push(getCurrentTeamUrl(getState()));
}
return result;
};
}
export function loadChannelsForCurrentUser() {
return async (dispatch, getState) => {
const state = getState();
const unreads = getUnreadChannelIds(state);
await dispatch(ChannelActions.fetchMyChannelsAndMembers(getCurrentTeamId(state)));
for (const id of unreads) {
const channel = getChannel(state, id);
if (channel && channel.type === Constants.DM_CHANNEL) {
dispatch(loadNewDMIfNeeded(channel.id));
} else if (channel && channel.type === Constants.GM_CHANNEL) {
dispatch(loadNewGMIfNeeded(channel.id));
}
}
loadProfilesForSidebar();
};
}
export function searchMoreChannels(term, showArchivedChannels) {
return async (dispatch, getState) => {
const state = getState();
const teamId = getCurrentTeamId(state);
if (!teamId) {
throw new Error('No team id');
}
const {data, error} = await dispatch(ChannelActions.searchChannels(teamId, term, showArchivedChannels));
if (data) {
const myMembers = getMyChannelMemberships(state);
// When searching public channels, only get channels user is not a member of
const channels = showArchivedChannels ? data : data.filter((c) => !myMembers[c.id]);
return {data: channels};
}
return {error};
};
}
export function autocompleteChannels(term, success, error) {
return async (dispatch, getState) => {
const state = getState();
const teamId = getCurrentTeamId(state);
if (!teamId) {
return;
}
const {data, error: err} = await dispatch(ChannelActions.autocompleteChannels(teamId, term));
if (data && success) {
success(data);
} else if (err && error) {
error({id: err.server_error_id, ...err});
}
};
}
export function autocompleteChannelsForSearch(term, success, error) {
return async (dispatch, getState) => {
const state = getState();
const teamId = getCurrentTeamId(state);
if (!teamId) {
return;
}
const {data, error: err} = await dispatch(ChannelActions.autocompleteChannelsForSearch(teamId, term));
if (data && success) {
success(data);
} else if (err && error) {
error({id: err.server_error_id, ...err});
}
};
}
export function addUsersToChannel(channelId, userIds) {
return async (dispatch) => {
try {
const requests = userIds.map((uId) => dispatch(ChannelActions.addChannelMember(channelId, uId)));
return await Promise.all(requests);
} catch (error) {
return {error};
}
};
}
export function unmuteChannel(userId, channelId) {
return ChannelActions.updateChannelNotifyProps(userId, channelId, {
mark_unread: NotificationLevels.ALL,
});
}
export function muteChannel(userId, channelId) {
return ChannelActions.updateChannelNotifyProps(userId, channelId, {
mark_unread: NotificationLevels.MENTION,
});
}