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
/
post_utils.jsx
256 lines (202 loc) · 7.86 KB
/
post_utils.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
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {Client4} from 'mattermost-redux/client';
import {getLicense, getConfig} from 'mattermost-redux/selectors/entities/general';
import {haveIChannelPermission} from 'mattermost-redux/selectors/entities/roles';
import {getCurrentUserId} from 'mattermost-redux/selectors/entities/users';
import {getChannel} from 'mattermost-redux/selectors/entities/channels';
import {Permissions} from 'mattermost-redux/constants';
import * as PostListUtils from 'mattermost-redux/utils/post_list';
import {canEditPost as canEditPostRedux} from 'mattermost-redux/utils/post_utils';
import store from 'stores/redux_store.jsx';
import Constants, {PostListRowListIds} from 'utils/constants.jsx';
import {formatWithRenderer} from 'utils/markdown';
import MentionableRenderer from 'utils/markdown/mentionable_renderer';
import * as Utils from 'utils/utils.jsx';
import {isMobile} from 'utils/user_agent.jsx';
const CHANNEL_SWITCH_IGNORE_ENTER_THRESHOLD_MS = 500;
export function isSystemMessage(post) {
return Boolean(post.type && (post.type.lastIndexOf(Constants.SYSTEM_MESSAGE_PREFIX) === 0));
}
export function fromAutoResponder(post) {
return Boolean(post.type && (post.type === Constants.AUTO_RESPONDER));
}
export function isFromWebhook(post) {
return post.props && post.props.from_webhook === 'true';
}
export function isPostOwner(post) {
return getCurrentUserId(store.getState()) === post.user_id;
}
export function isComment(post) {
if ('root_id' in post) {
return post.root_id !== '' && post.root_id != null;
}
return false;
}
export function isEdited(post) {
return post.edit_at > 0;
}
export function getImageSrc(src, hasImageProxy) {
if (!src) {
return src;
}
const imageAPI = Client4.getBaseRoute() + '/image?url=';
if (hasImageProxy && !src.startsWith(imageAPI)) {
return imageAPI + encodeURIComponent(src);
}
return src;
}
export function canDeletePost(post) {
if (post.type === Constants.PostTypes.FAKE_PARENT_DELETED) {
return false;
}
const channel = getChannel(store.getState(), post.channel_id);
if (channel && channel.delete_at !== 0) {
return false;
}
if (isPostOwner(post)) {
return haveIChannelPermission(store.getState(), {channel: post.channel_id, team: channel && channel.team_id, permission: Permissions.DELETE_POST});
}
return haveIChannelPermission(store.getState(), {channel: post.channel_id, team: channel && channel.team_id, permission: Permissions.DELETE_OTHERS_POSTS});
}
export function canEditPost(post) {
const state = store.getState();
const license = getLicense(state);
const config = getConfig(state);
const channel = getChannel(state, post.channel_id);
const userId = getCurrentUserId(state);
return canEditPostRedux(state, config, license, channel && channel.team_id, channel && channel.id, userId, post);
}
export function shouldShowDotMenu(post) {
if (Utils.isMobile()) {
return true;
}
if (!isSystemMessage(post)) {
return true;
}
if (canDeletePost(post)) {
return true;
}
if (canEditPost(post)) {
return true;
}
return false;
}
export function containsAtChannel(text) {
// Don't warn for slash commands
if (!text || text.startsWith('/')) {
return false;
}
const mentionableText = formatWithRenderer(text, new MentionableRenderer());
return (/\B@(all|channel)\b/i).test(mentionableText);
}
export function shouldFocusMainTextbox(e, activeElement) {
if (!e) {
return false;
}
// Do not focus if we're currently focused on a textarea or input
const keepFocusTags = ['TEXTAREA', 'INPUT'];
if (!activeElement || keepFocusTags.includes(activeElement.tagName)) {
return false;
}
// Focus if it is an attempted paste
if (Utils.cmdOrCtrlPressed(e) && Utils.isKeyPressed(e, Constants.KeyCodes.V)) {
return true;
}
// Do not focus if a modifier key is pressed
if (e.ctrlKey || e.metaKey || e.altKey) {
return false;
}
// Do not focus if the key is undefined or null
if (e.key == null) {
return false;
}
// Do not focus for non-character or non-number keys
if (e.key.length !== 1 || !e.key.match(/./)) {
return false;
}
return true;
}
function canAutomaticallyCloseBackticks(message) {
const splitMessage = message.split('\n').filter((line) => line.trim() !== '');
const lastPart = splitMessage[splitMessage.length - 1];
if (splitMessage.length > 1 && !lastPart.includes('```')) {
return {
allowSending: true,
message: message.endsWith('\n') ? message.concat('```') : message.concat('\n```'),
withClosedCodeBlock: true,
};
}
return {allowSending: true};
}
function sendOnCtrlEnter(message, ctrlOrMetaKeyPressed, isSendMessageOnCtrlEnter) {
const match = message.match(Constants.TRIPLE_BACK_TICKS);
if (isSendMessageOnCtrlEnter && ctrlOrMetaKeyPressed && (!match || match.length % 2 === 0)) {
return {allowSending: true};
} else if (!isSendMessageOnCtrlEnter && (!match || match.length % 2 === 0)) {
return {allowSending: true};
} else if (ctrlOrMetaKeyPressed && match && match.length % 2 !== 0) {
return canAutomaticallyCloseBackticks(message);
}
return {allowSending: false};
}
export function postMessageOnKeyPress(event, message, sendMessageOnCtrlEnter, sendCodeBlockOnCtrlEnter, now = 0, lastChannelSwitchAt = 0) {
if (!event) {
return {allowSending: false};
}
// Typing enter on mobile never sends.
if (isMobile()) {
return {allowSending: false};
}
// Only ENTER sends, unless shift or alt key pressed.
if (!Utils.isKeyPressed(event, Constants.KeyCodes.ENTER) || event.shiftKey || event.altKey) {
return {allowSending: false};
}
// Don't send if we just switched channels within a threshold.
if (lastChannelSwitchAt > 0 && now > 0 && now - lastChannelSwitchAt <= CHANNEL_SWITCH_IGNORE_ENTER_THRESHOLD_MS) {
return {allowSending: false, ignoreKeyPress: true};
}
if (
message.trim() === '' ||
!(sendMessageOnCtrlEnter || sendCodeBlockOnCtrlEnter)
) {
return {allowSending: true};
}
const ctrlOrMetaKeyPressed = event.ctrlKey || event.metaKey;
if (sendMessageOnCtrlEnter) {
return sendOnCtrlEnter(message, ctrlOrMetaKeyPressed, true);
} else if (sendCodeBlockOnCtrlEnter) {
return sendOnCtrlEnter(message, ctrlOrMetaKeyPressed, false);
}
return {allowSending: false};
}
export function isErrorInvalidSlashCommand(error) {
if (error && error.server_error_id) {
return error.server_error_id === 'api.command.execute_command.not_found.app_error';
}
return false;
}
// getLastPostId returns the most recent post ID in the given list of post IDs. This function is copied from
// mattermost-redux, except it also includes additional special IDs that are only used in the web app.
export function getLastPostId(postIds) {
for (let i = postIds.length - 1; i >= 0; i--) {
const item = postIds[i];
if (
PostListUtils.isStartOfNewMessages(item) ||
PostListUtils.isDateLine(item) ||
item === PostListRowListIds.CHANNEL_INTRO_MESSAGE ||
item === PostListRowListIds.MORE_MESSAGES_LOADER
) {
// This is not a post at all
continue;
}
if (PostListUtils.isCombinedUserActivityPost(item)) {
// This is a combined post, so find the first post ID from it
const combinedIds = PostListUtils.getPostIdsForCombinedUserActivityPost(item);
return combinedIds[combinedIds.length - 1];
}
// This is a post ID
return item;
}
return '';
}