forked from mattermost/mattermost-webapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
post_actions.jsx
265 lines (218 loc) · 8.16 KB
/
post_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
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {SearchTypes} from 'mattermost-redux/action_types';
import {getMyChannelMember} from 'mattermost-redux/actions/channels';
import {getChannel, getMyChannelMember as getMyChannelMemberSelector} from 'mattermost-redux/selectors/entities/channels';
import * as PostActions from 'mattermost-redux/actions/posts';
import * as PostSelectors from 'mattermost-redux/selectors/entities/posts';
import {getCurrentUserId} from 'mattermost-redux/selectors/entities/users';
import {canEditPost, comparePosts} from 'mattermost-redux/utils/post_utils';
import {addRecentEmoji} from 'actions/emoji_actions';
import * as StorageActions from 'actions/storage';
import {loadNewDMIfNeeded, loadNewGMIfNeeded} from 'actions/user_actions.jsx';
import * as RhsActions from 'actions/views/rhs';
import {isEmbedVisible} from 'selectors/posts';
import {getSelectedPostId, getRhsState} from 'selectors/rhs';
import {
ActionTypes,
Constants,
RHSStates,
StoragePrefixes,
} from 'utils/constants';
import {EMOJI_PATTERN} from 'utils/emoticons.jsx';
import * as UserAgent from 'utils/user_agent';
import {completePostReceive} from './post_utils';
export function handleNewPost(post, msg) {
return async (dispatch, getState) => {
let websocketMessageProps = {};
if (msg) {
websocketMessageProps = msg.data;
}
const myChannelMember = getMyChannelMemberSelector(getState(), post.channel_id);
if (myChannelMember && Object.keys(myChannelMember).length === 0 && myChannelMember.constructor === 'Object') {
await dispatch(getMyChannelMember(post.channel_id));
}
dispatch(completePostReceive(post, websocketMessageProps));
if (msg && msg.data) {
if (msg.data.channel_type === Constants.DM_CHANNEL) {
loadNewDMIfNeeded(post.channel_id);
} else if (msg.data.channel_type === Constants.GM_CHANNEL) {
loadNewGMIfNeeded(post.channel_id);
}
}
};
}
const getPostsForIds = PostSelectors.makeGetPostsForIds();
export function flagPost(postId) {
return async (dispatch, getState) => {
await dispatch(PostActions.flagPost(postId));
const state = getState();
const rhsState = getRhsState(state);
if (rhsState === RHSStates.FLAG) {
addPostToSearchResults(postId, state, dispatch);
}
return {data: true};
};
}
export function unflagPost(postId) {
return async (dispatch, getState) => {
await dispatch(PostActions.unflagPost(postId));
const state = getState();
const rhsState = getRhsState(state);
if (rhsState === RHSStates.FLAG) {
removePostFromSearchResults(postId, state, dispatch);
}
return {data: true};
};
}
export function createPost(post, files) {
return async (dispatch) => {
// parse message and emit emoji event
const emojis = post.message.match(EMOJI_PATTERN);
if (emojis) {
for (const emoji of emojis) {
const trimmed = emoji.substring(1, emoji.length - 1);
dispatch(addRecentEmoji(trimmed));
}
}
let result;
if (UserAgent.isIosClassic()) {
result = await dispatch(PostActions.createPostImmediately(post, files));
} else {
result = await dispatch(PostActions.createPost(post, files));
}
if (post.root_id) {
dispatch(storeCommentDraft(post.root_id, null));
} else {
dispatch(storeDraft(post.channel_id, null));
}
return result;
};
}
export function storeDraft(channelId, draft) {
return (dispatch) => {
dispatch(StorageActions.setGlobalItem('draft_' + channelId, draft));
};
}
export function storeCommentDraft(rootPostId, draft) {
return (dispatch) => {
dispatch(StorageActions.setGlobalItem('comment_draft_' + rootPostId, draft));
};
}
export function addReaction(postId, emojiName) {
return (dispatch) => {
dispatch(PostActions.addReaction(postId, emojiName));
dispatch(addRecentEmoji(emojiName));
};
}
export function searchForTerm(term) {
return (dispatch) => {
dispatch(RhsActions.updateSearchTerms(term));
dispatch(RhsActions.showSearchResults());
};
}
function addPostToSearchResults(postId, state, dispatch) {
const results = state.entities.search.results;
const index = results.indexOf(postId);
if (index === -1) {
const newPost = PostSelectors.getPost(state, postId);
const posts = getPostsForIds(state, results).reduce((acc, post) => {
acc[post.id] = post;
return acc;
}, {});
posts[newPost.id] = newPost;
const newResults = [...results, postId];
newResults.sort((a, b) => comparePosts(posts[a], posts[b]));
dispatch({
type: SearchTypes.RECEIVED_SEARCH_POSTS,
data: {posts, order: newResults},
});
}
}
function removePostFromSearchResults(postId, state, dispatch) {
let results = state.entities.search.results;
const index = results.indexOf(postId);
if (index > -1) {
results = [...results];
results.splice(index, 1);
const posts = getPostsForIds(state, results);
dispatch({
type: SearchTypes.RECEIVED_SEARCH_POSTS,
data: {posts, order: results},
});
}
}
export function pinPost(postId) {
return async (dispatch, getState) => {
await dispatch(PostActions.pinPost(postId));
const state = getState();
const rhsState = getRhsState(state);
if (rhsState === RHSStates.PIN) {
addPostToSearchResults(postId, state, dispatch);
}
};
}
export function unpinPost(postId) {
return async (dispatch, getState) => {
await dispatch(PostActions.unpinPost(postId));
const state = getState();
const rhsState = getRhsState(state);
if (rhsState === RHSStates.PIN) {
removePostFromSearchResults(postId, state, dispatch);
}
};
}
export function setEditingPost(postId = '', commentCount = 0, refocusId = '', title = '', isRHS = false) {
return async (dispatch, getState) => {
const state = getState();
const post = PostSelectors.getPost(state, postId);
if (!post || post.pending_post_id === postId) {
return {data: false};
}
const config = state.entities.general.config;
const license = state.entities.general.license;
const userId = getCurrentUserId(state);
const channel = getChannel(state, post.channel_id);
const teamId = channel.team_id || '';
const canEditNow = canEditPost(state, config, license, teamId, post.channel_id, userId, post);
// Only show the modal if we can edit the post now, but allow it to be hidden at any time
if (canEditNow) {
dispatch({
type: ActionTypes.SHOW_EDIT_POST_MODAL,
data: {postId, commentCount, refocusId, title, isRHS},
});
}
return {data: canEditNow};
};
}
export function hideEditPostModal() {
return {
type: ActionTypes.HIDE_EDIT_POST_MODAL,
};
}
export function deleteAndRemovePost(post) {
return async (dispatch, getState) => {
const {error} = await dispatch(PostActions.deletePost(post));
if (error) {
return {error};
}
if (post.id === getSelectedPostId(getState())) {
dispatch({
type: ActionTypes.SELECT_POST,
postId: '',
channelId: '',
});
}
dispatch(PostActions.removePost(post));
return {data: true};
};
}
export function toggleEmbedVisibility(postId) {
return (dispatch, getState) => {
const visible = isEmbedVisible(getState(), postId);
dispatch(StorageActions.setGlobalItem(StoragePrefixes.EMBED_VISIBLE + postId, !visible));
};
}
export function resetEmbedVisibility() {
return StorageActions.actionOnGlobalItemsWithPrefix(StoragePrefixes.EMBED_VISIBLE, () => null);
}