2018-12-21 10:55:35 +00:00
|
|
|
import React, { Component } from 'react';
|
2017-08-09 13:12:00 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2020-06-26 20:22:56 +00:00
|
|
|
import {
|
2021-01-25 17:14:45 +00:00
|
|
|
View, Alert, Keyboard, NativeModules, Text
|
2020-06-26 20:22:56 +00:00
|
|
|
} from 'react-native';
|
2017-11-21 16:55:32 +00:00
|
|
|
import { connect } from 'react-redux';
|
2020-12-01 11:58:51 +00:00
|
|
|
import { KeyboardAccessoryView } from 'react-native-ui-lib/keyboard';
|
2018-07-17 19:10:27 +00:00
|
|
|
import ImagePicker from 'react-native-image-crop-picker';
|
2018-12-21 10:55:35 +00:00
|
|
|
import equal from 'deep-equal';
|
2019-07-18 17:25:18 +00:00
|
|
|
import DocumentPicker from 'react-native-document-picker';
|
2019-09-16 20:26:32 +00:00
|
|
|
import { Q } from '@nozbe/watermelondb';
|
2020-10-30 17:35:07 +00:00
|
|
|
import { TouchableWithoutFeedback } from 'react-native-gesture-handler';
|
2018-03-07 00:17:20 +00:00
|
|
|
|
2020-02-11 14:01:35 +00:00
|
|
|
import { generateTriggerId } from '../../lib/methods/actions';
|
2019-12-04 16:39:53 +00:00
|
|
|
import TextInput from '../../presentation/TextInput';
|
2018-09-25 19:28:42 +00:00
|
|
|
import { userTyping as userTypingAction } from '../../actions/room';
|
2017-12-08 19:36:03 +00:00
|
|
|
import RocketChat from '../../lib/rocketchat';
|
2018-02-08 14:08:50 +00:00
|
|
|
import styles from './styles';
|
2019-09-16 20:26:32 +00:00
|
|
|
import database from '../../lib/database';
|
2018-01-30 19:48:26 +00:00
|
|
|
import { emojis } from '../../emojis';
|
2020-07-30 19:51:13 +00:00
|
|
|
import log, { logEvent, events } from '../../utils/log';
|
2020-07-06 19:23:46 +00:00
|
|
|
import RecordAudio from './RecordAudio';
|
2018-06-01 17:38:13 +00:00
|
|
|
import I18n from '../../i18n';
|
2018-07-20 19:54:46 +00:00
|
|
|
import ReplyPreview from './ReplyPreview';
|
2019-02-27 14:23:40 +00:00
|
|
|
import debounce from '../../utils/debounce';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { themes } from '../../constants/colors';
|
2019-05-27 16:19:39 +00:00
|
|
|
import LeftButtons from './LeftButtons';
|
|
|
|
import RightButtons from './RightButtons';
|
2019-11-25 20:01:17 +00:00
|
|
|
import { isAndroid, isTablet } from '../../utils/deviceInfo';
|
2019-09-24 20:16:59 +00:00
|
|
|
import { canUploadFile } from '../../utils/media';
|
2019-11-25 20:01:17 +00:00
|
|
|
import EventEmiter from '../../utils/events';
|
|
|
|
import {
|
|
|
|
KEY_COMMAND,
|
|
|
|
handleCommandTyping,
|
|
|
|
handleCommandSubmit,
|
|
|
|
handleCommandShowUpload
|
|
|
|
} from '../../commands';
|
2019-10-30 14:14:41 +00:00
|
|
|
import Mentions from './Mentions';
|
|
|
|
import MessageboxContext from './Context';
|
|
|
|
import {
|
|
|
|
MENTIONS_TRACKING_TYPE_EMOJIS,
|
|
|
|
MENTIONS_TRACKING_TYPE_COMMANDS,
|
|
|
|
MENTIONS_COUNT_TO_DISPLAY,
|
|
|
|
MENTIONS_TRACKING_TYPE_USERS
|
|
|
|
} from './constants';
|
|
|
|
import CommandsPreview from './CommandsPreview';
|
2020-02-11 14:09:14 +00:00
|
|
|
import { getUserSelector } from '../../selectors/login';
|
2020-03-30 19:50:27 +00:00
|
|
|
import Navigation from '../../lib/Navigation';
|
2020-06-15 19:35:45 +00:00
|
|
|
import { withActionSheet } from '../ActionSheet';
|
2020-09-15 13:01:43 +00:00
|
|
|
import { sanitizeLikeString } from '../../lib/database/utils';
|
2020-10-30 17:35:07 +00:00
|
|
|
import { CustomIcon } from '../../lib/Icons';
|
2017-12-20 20:14:07 +00:00
|
|
|
|
2020-12-01 11:58:51 +00:00
|
|
|
if (isAndroid) {
|
|
|
|
require('./EmojiKeyboard');
|
|
|
|
}
|
|
|
|
|
2018-07-17 19:10:27 +00:00
|
|
|
const imagePickerConfig = {
|
|
|
|
cropping: true,
|
|
|
|
compressImageQuality: 0.8,
|
2020-10-30 15:54:44 +00:00
|
|
|
avoidEmptySpaceAroundImage: false,
|
|
|
|
freeStyleCropEnabled: true
|
2018-07-17 19:10:27 +00:00
|
|
|
};
|
|
|
|
|
2019-07-18 17:07:37 +00:00
|
|
|
const libraryPickerConfig = {
|
2020-06-26 20:22:56 +00:00
|
|
|
multiple: true,
|
2019-07-18 17:07:37 +00:00
|
|
|
mediaType: 'any'
|
|
|
|
};
|
|
|
|
|
|
|
|
const videoPickerConfig = {
|
|
|
|
mediaType: 'video'
|
|
|
|
};
|
|
|
|
|
2019-04-08 12:35:28 +00:00
|
|
|
class MessageBox extends Component {
|
2017-08-09 13:12:00 +00:00
|
|
|
static propTypes = {
|
2017-11-21 14:55:50 +00:00
|
|
|
rid: PropTypes.string.isRequired,
|
2017-12-20 20:14:07 +00:00
|
|
|
baseUrl: PropTypes.string.isRequired,
|
2017-11-21 14:55:50 +00:00
|
|
|
message: PropTypes.object,
|
2018-07-20 19:54:46 +00:00
|
|
|
replying: PropTypes.bool,
|
2017-11-21 17:09:22 +00:00
|
|
|
editing: PropTypes.bool,
|
2019-04-17 17:01:03 +00:00
|
|
|
threadsEnabled: PropTypes.bool,
|
2019-10-28 20:51:46 +00:00
|
|
|
isFocused: PropTypes.func,
|
2019-02-07 19:58:20 +00:00
|
|
|
user: PropTypes.shape({
|
|
|
|
id: PropTypes.string,
|
|
|
|
username: PropTypes.string,
|
|
|
|
token: PropTypes.string
|
|
|
|
}),
|
2018-07-20 19:54:46 +00:00
|
|
|
roomType: PropTypes.string,
|
2019-04-24 18:36:29 +00:00
|
|
|
tmid: PropTypes.string,
|
2019-09-16 20:26:32 +00:00
|
|
|
replyWithMention: PropTypes.bool,
|
2019-09-24 20:16:59 +00:00
|
|
|
FileUpload_MediaTypeWhiteList: PropTypes.string,
|
|
|
|
FileUpload_MaxFileSize: PropTypes.number,
|
2020-02-20 21:02:09 +00:00
|
|
|
Message_AudioRecorderEnabled: PropTypes.bool,
|
2019-09-16 20:26:32 +00:00
|
|
|
getCustomEmoji: PropTypes.func,
|
2018-07-20 19:54:46 +00:00
|
|
|
editCancel: PropTypes.func.isRequired,
|
|
|
|
editRequest: PropTypes.func.isRequired,
|
|
|
|
onSubmit: PropTypes.func.isRequired,
|
2017-11-24 20:44:52 +00:00
|
|
|
typing: PropTypes.func,
|
2019-12-04 16:39:53 +00:00
|
|
|
theme: PropTypes.string,
|
2020-02-28 17:52:50 +00:00
|
|
|
replyCancel: PropTypes.func,
|
2020-06-26 20:22:56 +00:00
|
|
|
showSend: PropTypes.bool,
|
2020-06-15 19:35:45 +00:00
|
|
|
navigation: PropTypes.object,
|
2020-06-26 20:22:56 +00:00
|
|
|
children: PropTypes.node,
|
|
|
|
isMasterDetail: PropTypes.bool,
|
|
|
|
showActionSheet: PropTypes.func,
|
|
|
|
iOSScrollBehavior: PropTypes.number,
|
|
|
|
sharing: PropTypes.bool,
|
|
|
|
isActionsEnabled: PropTypes.bool
|
|
|
|
}
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
message: {
|
|
|
|
id: ''
|
|
|
|
},
|
|
|
|
sharing: false,
|
2020-12-01 11:58:51 +00:00
|
|
|
iOSScrollBehavior: NativeModules.KeyboardTrackingViewTempManager?.KeyboardTrackingScrollBehaviorFixedOffset,
|
2020-06-26 20:22:56 +00:00
|
|
|
isActionsEnabled: true,
|
|
|
|
getCustomEmoji: () => {}
|
2017-11-21 14:55:50 +00:00
|
|
|
}
|
|
|
|
|
2017-11-28 16:27:06 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
2017-12-20 20:14:07 +00:00
|
|
|
mentions: [],
|
2020-02-17 12:14:56 +00:00
|
|
|
showEmojiKeyboard: false,
|
2020-06-26 20:22:56 +00:00
|
|
|
showSend: props.showSend,
|
2018-07-17 19:10:27 +00:00
|
|
|
recording: false,
|
2019-02-27 14:23:40 +00:00
|
|
|
trackingType: '',
|
2019-09-25 21:32:13 +00:00
|
|
|
commandPreview: [],
|
2020-02-11 14:01:35 +00:00
|
|
|
showCommandPreview: false,
|
2020-10-30 17:35:07 +00:00
|
|
|
command: {},
|
|
|
|
tshow: false
|
2017-11-28 16:27:06 +00:00
|
|
|
};
|
2018-11-05 19:03:17 +00:00
|
|
|
this.text = '';
|
2020-07-20 16:35:17 +00:00
|
|
|
this.selection = { start: 0, end: 0 };
|
2019-11-25 20:01:17 +00:00
|
|
|
this.focused = false;
|
2020-06-15 19:35:45 +00:00
|
|
|
|
|
|
|
// MessageBox Actions
|
|
|
|
this.options = [
|
|
|
|
{
|
|
|
|
title: I18n.t('Take_a_photo'),
|
2020-08-21 18:11:10 +00:00
|
|
|
icon: 'camera-photo',
|
2020-06-15 19:35:45 +00:00
|
|
|
onPress: this.takePhoto
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: I18n.t('Take_a_video'),
|
2020-07-27 19:53:33 +00:00
|
|
|
icon: 'camera',
|
2020-06-15 19:35:45 +00:00
|
|
|
onPress: this.takeVideo
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: I18n.t('Choose_from_library'),
|
2020-08-21 18:11:10 +00:00
|
|
|
icon: 'image',
|
2020-06-15 19:35:45 +00:00
|
|
|
onPress: this.chooseFromLibrary
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: I18n.t('Choose_file'),
|
2020-07-29 21:02:51 +00:00
|
|
|
icon: 'attach',
|
2020-06-15 19:35:45 +00:00
|
|
|
onPress: this.chooseFile
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: I18n.t('Create_Discussion'),
|
2020-07-27 19:53:33 +00:00
|
|
|
icon: 'discussions',
|
2020-06-15 19:35:45 +00:00
|
|
|
onPress: this.createDiscussion
|
|
|
|
}
|
2019-07-16 14:30:29 +00:00
|
|
|
];
|
2020-06-15 19:35:45 +00:00
|
|
|
|
2019-07-18 17:07:37 +00:00
|
|
|
const libPickerLabels = {
|
|
|
|
cropperChooseText: I18n.t('Choose'),
|
|
|
|
cropperCancelText: I18n.t('Cancel'),
|
|
|
|
loadingLabelText: I18n.t('Processing')
|
|
|
|
};
|
2019-07-16 14:30:29 +00:00
|
|
|
this.imagePickerConfig = {
|
|
|
|
...imagePickerConfig,
|
2019-07-18 17:07:37 +00:00
|
|
|
...libPickerLabels
|
|
|
|
};
|
|
|
|
this.libraryPickerConfig = {
|
|
|
|
...libraryPickerConfig,
|
|
|
|
...libPickerLabels
|
|
|
|
};
|
|
|
|
this.videoPickerConfig = {
|
|
|
|
...videoPickerConfig,
|
|
|
|
...libPickerLabels
|
2019-07-16 14:30:29 +00:00
|
|
|
};
|
2017-11-28 16:27:06 +00:00
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
async componentDidMount() {
|
|
|
|
const db = database.active;
|
2020-06-26 20:22:56 +00:00
|
|
|
const {
|
|
|
|
rid, tmid, navigation, sharing
|
|
|
|
} = this.props;
|
2019-04-24 18:36:29 +00:00
|
|
|
let msg;
|
2019-09-16 20:26:32 +00:00
|
|
|
try {
|
|
|
|
const threadsCollection = db.collections.get('threads');
|
|
|
|
const subsCollection = db.collections.get('subscriptions');
|
2020-06-26 20:22:56 +00:00
|
|
|
try {
|
|
|
|
this.room = await subsCollection.find(rid);
|
|
|
|
} catch (error) {
|
|
|
|
console.log('Messagebox.didMount: Room not found');
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
if (tmid) {
|
|
|
|
try {
|
2020-06-26 20:22:56 +00:00
|
|
|
this.thread = await threadsCollection.find(tmid);
|
|
|
|
if (this.thread && !sharing) {
|
|
|
|
msg = this.thread.draftMessage;
|
2019-09-16 20:26:32 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.log('Messagebox.didMount: Thread not found');
|
|
|
|
}
|
2020-06-26 20:22:56 +00:00
|
|
|
} else if (!sharing) {
|
2020-07-02 17:10:11 +00:00
|
|
|
msg = this.room?.draftMessage;
|
2019-04-24 18:36:29 +00:00
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2019-04-24 18:36:29 +00:00
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
|
2019-04-24 18:36:29 +00:00
|
|
|
if (msg) {
|
|
|
|
this.setInput(msg);
|
2019-04-08 12:35:28 +00:00
|
|
|
this.setShowSend(true);
|
2019-04-01 14:45:17 +00:00
|
|
|
}
|
2019-05-27 16:19:39 +00:00
|
|
|
|
2019-11-25 20:01:17 +00:00
|
|
|
if (isTablet) {
|
|
|
|
EventEmiter.addEventListener(KEY_COMMAND, this.handleCommands);
|
|
|
|
}
|
2020-02-28 17:52:50 +00:00
|
|
|
|
2020-06-15 14:00:46 +00:00
|
|
|
this.unsubscribeFocus = navigation.addListener('focus', () => {
|
2020-12-14 16:30:25 +00:00
|
|
|
// didFocus
|
|
|
|
// We should wait pushed views be dismissed
|
2021-01-25 17:14:45 +00:00
|
|
|
this.trackingTimeout = setTimeout(() => {
|
2020-12-14 16:30:25 +00:00
|
|
|
if (this.tracking && this.tracking.resetTracking) {
|
|
|
|
// Reset messageBox keyboard tracking
|
|
|
|
this.tracking.resetTracking();
|
|
|
|
}
|
2021-01-25 17:14:45 +00:00
|
|
|
}, 500);
|
2020-02-28 17:52:50 +00:00
|
|
|
});
|
2020-06-15 14:00:46 +00:00
|
|
|
this.unsubscribeBlur = navigation.addListener('blur', () => {
|
|
|
|
this.component?.blur();
|
|
|
|
});
|
2019-04-01 14:45:17 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 16:37:49 +00:00
|
|
|
UNSAFE_componentWillReceiveProps(nextProps) {
|
2020-06-26 20:22:56 +00:00
|
|
|
const {
|
|
|
|
isFocused, editing, replying, sharing
|
|
|
|
} = this.props;
|
|
|
|
if (!isFocused?.()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (sharing) {
|
|
|
|
this.setInput(nextProps.message.msg ?? '');
|
2019-04-24 18:36:29 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
if (editing !== nextProps.editing && nextProps.editing) {
|
2018-11-05 19:03:17 +00:00
|
|
|
this.setInput(nextProps.message.msg);
|
2019-04-08 12:35:28 +00:00
|
|
|
if (this.text) {
|
|
|
|
this.setShowSend(true);
|
|
|
|
}
|
2020-06-15 19:35:45 +00:00
|
|
|
this.focus();
|
2019-09-16 20:26:32 +00:00
|
|
|
} else if (replying !== nextProps.replying && nextProps.replying) {
|
2019-03-06 13:27:40 +00:00
|
|
|
this.focus();
|
2017-11-24 20:44:52 +00:00
|
|
|
} else if (!nextProps.message) {
|
2018-11-05 19:03:17 +00:00
|
|
|
this.clearInput();
|
2017-11-21 14:55:50 +00:00
|
|
|
}
|
2021-01-25 17:14:45 +00:00
|
|
|
if (this.trackingTimeout) {
|
|
|
|
clearTimeout(this.trackingTimeout);
|
|
|
|
this.trackingTimeout = false;
|
|
|
|
}
|
2017-08-09 13:12:00 +00:00
|
|
|
}
|
2017-12-20 20:14:07 +00:00
|
|
|
|
2018-12-21 10:55:35 +00:00
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
|
|
|
const {
|
2020-10-30 17:35:07 +00:00
|
|
|
showEmojiKeyboard, showSend, recording, mentions, commandPreview, tshow
|
2018-12-21 10:55:35 +00:00
|
|
|
} = this.state;
|
2019-09-16 20:26:32 +00:00
|
|
|
|
2018-12-21 10:55:35 +00:00
|
|
|
const {
|
2020-06-26 20:22:56 +00:00
|
|
|
roomType, replying, editing, isFocused, message, theme, children
|
2018-12-21 10:55:35 +00:00
|
|
|
} = this.props;
|
2019-12-04 16:39:53 +00:00
|
|
|
if (nextProps.theme !== theme) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-10-28 20:51:46 +00:00
|
|
|
if (!isFocused()) {
|
2019-04-24 18:36:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-12-21 10:55:35 +00:00
|
|
|
if (nextProps.roomType !== roomType) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextProps.replying !== replying) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextProps.editing !== editing) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-02-17 12:14:56 +00:00
|
|
|
if (nextState.showEmojiKeyboard !== showEmojiKeyboard) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-21 10:55:35 +00:00
|
|
|
if (nextState.showSend !== showSend) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextState.recording !== recording) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-10-30 17:35:07 +00:00
|
|
|
if (nextState.tshow !== tshow) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-21 10:55:35 +00:00
|
|
|
if (!equal(nextState.mentions, mentions)) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-06-10 18:36:56 +00:00
|
|
|
if (!equal(nextState.commandPreview, commandPreview)) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-06-26 20:22:56 +00:00
|
|
|
if (!equal(nextProps.message, message)) {
|
2018-12-21 10:55:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-06-26 20:22:56 +00:00
|
|
|
if (!equal(nextProps.children, children)) {
|
2020-06-15 19:35:45 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-12-21 10:55:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
console.countReset(`${ this.constructor.name }.render calls`);
|
2019-09-27 19:17:29 +00:00
|
|
|
if (this.onChangeText && this.onChangeText.stop) {
|
|
|
|
this.onChangeText.stop();
|
|
|
|
}
|
|
|
|
if (this.getUsers && this.getUsers.stop) {
|
|
|
|
this.getUsers.stop();
|
|
|
|
}
|
|
|
|
if (this.getRooms && this.getRooms.stop) {
|
|
|
|
this.getRooms.stop();
|
|
|
|
}
|
|
|
|
if (this.getEmojis && this.getEmojis.stop) {
|
|
|
|
this.getEmojis.stop();
|
|
|
|
}
|
|
|
|
if (this.getSlashCommands && this.getSlashCommands.stop) {
|
|
|
|
this.getSlashCommands.stop();
|
|
|
|
}
|
2020-06-15 14:00:46 +00:00
|
|
|
if (this.unsubscribeFocus) {
|
|
|
|
this.unsubscribeFocus();
|
|
|
|
}
|
|
|
|
if (this.unsubscribeBlur) {
|
|
|
|
this.unsubscribeBlur();
|
2020-02-28 17:52:50 +00:00
|
|
|
}
|
2019-11-25 20:01:17 +00:00
|
|
|
if (isTablet) {
|
|
|
|
EventEmiter.removeListener(KEY_COMMAND, this.handleCommands);
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
}
|
|
|
|
|
2019-09-27 19:17:29 +00:00
|
|
|
onChangeText = (text) => {
|
2019-02-27 14:23:40 +00:00
|
|
|
const isTextEmpty = text.length === 0;
|
|
|
|
this.setShowSend(!isTextEmpty);
|
2019-09-27 19:17:29 +00:00
|
|
|
this.debouncedOnChangeText(text);
|
2019-11-25 20:01:17 +00:00
|
|
|
this.setInput(text);
|
2019-09-27 19:17:29 +00:00
|
|
|
}
|
|
|
|
|
2020-07-20 16:35:17 +00:00
|
|
|
onSelectionChange = (e) => {
|
|
|
|
this.selection = e.nativeEvent.selection;
|
|
|
|
}
|
|
|
|
|
2019-09-27 19:17:29 +00:00
|
|
|
// eslint-disable-next-line react/sort-comp
|
|
|
|
debouncedOnChangeText = debounce(async(text) => {
|
2020-06-26 20:22:56 +00:00
|
|
|
const { sharing } = this.props;
|
2019-09-27 19:17:29 +00:00
|
|
|
const db = database.active;
|
|
|
|
const isTextEmpty = text.length === 0;
|
|
|
|
// this.setShowSend(!isTextEmpty);
|
2019-02-27 14:23:40 +00:00
|
|
|
this.handleTyping(!isTextEmpty);
|
2020-06-26 20:22:56 +00:00
|
|
|
|
|
|
|
if (!sharing) {
|
|
|
|
// matches if their is text that stats with '/' and group the command and params so we can use it "/command params"
|
|
|
|
const slashCommand = text.match(/^\/([a-z0-9._-]+) (.+)/im);
|
|
|
|
if (slashCommand) {
|
|
|
|
const [, name, params] = slashCommand;
|
|
|
|
const commandsCollection = db.collections.get('slash_commands');
|
|
|
|
try {
|
|
|
|
const command = await commandsCollection.find(name);
|
|
|
|
if (command.providesPreview) {
|
|
|
|
return this.setCommandPreview(command, name, params);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.log('Slash command not found');
|
2019-09-16 20:26:32 +00:00
|
|
|
}
|
2019-06-10 18:36:56 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-25 14:04:20 +00:00
|
|
|
|
2019-05-30 17:19:26 +00:00
|
|
|
if (!isTextEmpty) {
|
2019-09-27 19:17:29 +00:00
|
|
|
try {
|
2020-07-20 16:35:17 +00:00
|
|
|
const { start, end } = this.selection;
|
2019-09-27 19:17:29 +00:00
|
|
|
const cursor = Math.max(start, end);
|
2020-07-20 16:35:17 +00:00
|
|
|
const lastNativeText = this.text;
|
2019-09-27 19:17:29 +00:00
|
|
|
// matches if text either starts with '/' or have (@,#,:) then it groups whatever comes next of mention type
|
2020-06-26 20:22:56 +00:00
|
|
|
let regexp = /(#|@|:|^\/)([a-z0-9._-]+)$/im;
|
|
|
|
|
|
|
|
// if sharing, track #|@|:
|
|
|
|
if (sharing) {
|
|
|
|
regexp = /(#|@|:)([a-z0-9._-]+)$/im;
|
|
|
|
}
|
|
|
|
|
2019-09-27 19:17:29 +00:00
|
|
|
const result = lastNativeText.substr(0, cursor).match(regexp);
|
|
|
|
if (!result) {
|
2020-06-26 20:22:56 +00:00
|
|
|
if (!sharing) {
|
|
|
|
const slash = lastNativeText.match(/^\/$/); // matches only '/' in input
|
|
|
|
if (slash) {
|
|
|
|
return this.identifyMentionKeyword('', MENTIONS_TRACKING_TYPE_COMMANDS);
|
|
|
|
}
|
2019-09-27 19:17:29 +00:00
|
|
|
}
|
|
|
|
return this.stopTrackingMention();
|
2019-06-10 18:36:56 +00:00
|
|
|
}
|
2019-09-27 19:17:29 +00:00
|
|
|
const [, lastChar, name] = result;
|
|
|
|
this.identifyMentionKeyword(name, lastChar);
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2019-05-27 16:19:39 +00:00
|
|
|
}
|
2019-06-03 16:56:16 +00:00
|
|
|
} else {
|
|
|
|
this.stopTrackingMention();
|
2019-03-06 13:27:40 +00:00
|
|
|
}
|
2019-09-27 19:17:29 +00:00
|
|
|
}, 100)
|
2017-12-20 20:14:07 +00:00
|
|
|
|
2020-02-17 12:14:56 +00:00
|
|
|
onKeyboardResigned = () => {
|
|
|
|
this.closeEmoji();
|
|
|
|
}
|
|
|
|
|
2019-02-27 14:23:40 +00:00
|
|
|
onPressMention = (item) => {
|
2019-03-06 13:27:40 +00:00
|
|
|
if (!this.component) {
|
|
|
|
return;
|
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
const { trackingType } = this.state;
|
2018-11-05 19:03:17 +00:00
|
|
|
const msg = this.text;
|
2020-07-20 16:35:17 +00:00
|
|
|
const { start, end } = this.selection;
|
2018-09-25 19:28:42 +00:00
|
|
|
const cursor = Math.max(start, end);
|
|
|
|
const regexp = /([a-z0-9._-]+)$/im;
|
|
|
|
const result = msg.substr(0, cursor).replace(regexp, '');
|
|
|
|
const mentionName = trackingType === MENTIONS_TRACKING_TYPE_EMOJIS
|
|
|
|
? `${ item.name || item }:`
|
2019-06-10 18:36:56 +00:00
|
|
|
: (item.username || item.name || item.command);
|
2018-09-25 19:28:42 +00:00
|
|
|
const text = `${ result }${ mentionName } ${ msg.slice(cursor) }`;
|
2019-06-10 18:36:56 +00:00
|
|
|
if ((trackingType === MENTIONS_TRACKING_TYPE_COMMANDS) && item.providesPreview) {
|
2019-09-25 21:32:13 +00:00
|
|
|
this.setState({ showCommandPreview: true });
|
2019-06-10 18:36:56 +00:00
|
|
|
}
|
2020-07-20 16:35:17 +00:00
|
|
|
const newCursor = cursor + mentionName.length;
|
|
|
|
this.setInput(text, { start: newCursor, end: newCursor });
|
2019-03-06 13:27:40 +00:00
|
|
|
this.focus();
|
2018-09-25 19:28:42 +00:00
|
|
|
requestAnimationFrame(() => this.stopTrackingMention());
|
|
|
|
}
|
|
|
|
|
2019-06-10 18:36:56 +00:00
|
|
|
onPressCommandPreview = (item) => {
|
2020-02-11 14:01:35 +00:00
|
|
|
const { command } = this.state;
|
2020-02-11 15:56:06 +00:00
|
|
|
const {
|
|
|
|
rid, tmid, message: { id: messageTmid }, replyCancel
|
|
|
|
} = this.props;
|
2019-06-10 18:36:56 +00:00
|
|
|
const { text } = this;
|
2020-02-11 14:01:35 +00:00
|
|
|
const name = text.substr(0, text.indexOf(' ')).slice(1);
|
2019-06-10 18:36:56 +00:00
|
|
|
const params = text.substr(text.indexOf(' ') + 1) || 'params';
|
2020-02-11 14:01:35 +00:00
|
|
|
this.setState({ commandPreview: [], showCommandPreview: false, command: {} });
|
2019-06-10 18:36:56 +00:00
|
|
|
this.stopTrackingMention();
|
|
|
|
this.clearInput();
|
2019-09-25 21:32:13 +00:00
|
|
|
this.handleTyping(false);
|
2019-06-10 18:36:56 +00:00
|
|
|
try {
|
2020-02-11 14:01:35 +00:00
|
|
|
const { appId } = command;
|
|
|
|
const triggerId = generateTriggerId(appId);
|
2020-02-11 15:56:06 +00:00
|
|
|
RocketChat.executeCommandPreview(name, params, rid, item, triggerId, tmid || messageTmid);
|
|
|
|
replyCancel();
|
2019-06-10 18:36:56 +00:00
|
|
|
} catch (e) {
|
2019-08-30 12:43:23 +00:00
|
|
|
log(e);
|
2019-06-10 18:36:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-17 12:14:56 +00:00
|
|
|
onEmojiSelected = (keyboardId, params) => {
|
|
|
|
const { text } = this;
|
|
|
|
const { emoji } = params;
|
|
|
|
let newText = '';
|
|
|
|
|
|
|
|
// if messagebox has an active cursor
|
2020-07-20 16:35:17 +00:00
|
|
|
const { start, end } = this.selection;
|
|
|
|
const cursor = Math.max(start, end);
|
|
|
|
newText = `${ text.substr(0, cursor) }${ emoji }${ text.substr(cursor) }`;
|
|
|
|
const newCursor = cursor + emoji.length;
|
|
|
|
this.setInput(newText, { start: newCursor, end: newCursor });
|
2020-02-17 12:14:56 +00:00
|
|
|
this.setShowSend(true);
|
|
|
|
}
|
|
|
|
|
2018-07-20 19:54:46 +00:00
|
|
|
getPermalink = async(message) => {
|
|
|
|
try {
|
2019-05-29 21:19:12 +00:00
|
|
|
return await RocketChat.getPermalinkMessage(message);
|
2018-07-20 19:54:46 +00:00
|
|
|
} catch (error) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-27 14:23:40 +00:00
|
|
|
getFixedMentions = (keyword) => {
|
2019-09-16 20:26:32 +00:00
|
|
|
let result = [];
|
2018-09-25 19:28:42 +00:00
|
|
|
if ('all'.indexOf(keyword) !== -1) {
|
2019-09-27 19:17:29 +00:00
|
|
|
result = [{ id: -1, username: 'all' }];
|
2018-09-25 19:28:42 +00:00
|
|
|
}
|
|
|
|
if ('here'.indexOf(keyword) !== -1) {
|
2019-09-27 19:17:29 +00:00
|
|
|
result = [{ id: -2, username: 'here' }, ...result];
|
2018-09-25 19:28:42 +00:00
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
return result;
|
2018-09-25 19:28:42 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
getUsers = debounce(async(keyword) => {
|
|
|
|
let res = await RocketChat.search({ text: keyword, filterRooms: false, filterUsers: true });
|
|
|
|
res = [...this.getFixedMentions(keyword), ...res];
|
|
|
|
this.setState({ mentions: res });
|
|
|
|
}, 300)
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
getRooms = debounce(async(keyword = '') => {
|
|
|
|
const res = await RocketChat.search({ text: keyword, filterRooms: true, filterUsers: false });
|
|
|
|
this.setState({ mentions: res });
|
|
|
|
}, 300)
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
getEmojis = debounce(async(keyword) => {
|
|
|
|
const db = database.active;
|
2018-09-25 19:28:42 +00:00
|
|
|
if (keyword) {
|
2019-09-16 20:26:32 +00:00
|
|
|
const customEmojisCollection = db.collections.get('custom_emojis');
|
2020-09-15 13:01:43 +00:00
|
|
|
const likeString = sanitizeLikeString(keyword);
|
2019-09-16 20:26:32 +00:00
|
|
|
let customEmojis = await customEmojisCollection.query(
|
2020-09-15 13:01:43 +00:00
|
|
|
Q.where('name', Q.like(`${ likeString }%`))
|
2019-09-16 20:26:32 +00:00
|
|
|
).fetch();
|
|
|
|
customEmojis = customEmojis.slice(0, MENTIONS_COUNT_TO_DISPLAY);
|
|
|
|
const filteredEmojis = emojis.filter(emoji => emoji.indexOf(keyword) !== -1).slice(0, MENTIONS_COUNT_TO_DISPLAY);
|
|
|
|
const mergedEmojis = [...customEmojis, ...filteredEmojis].slice(0, MENTIONS_COUNT_TO_DISPLAY);
|
|
|
|
this.setState({ mentions: mergedEmojis || [] });
|
|
|
|
}
|
|
|
|
}, 300)
|
|
|
|
|
|
|
|
getSlashCommands = debounce(async(keyword) => {
|
|
|
|
const db = database.active;
|
|
|
|
const commandsCollection = db.collections.get('slash_commands');
|
2020-09-15 13:01:43 +00:00
|
|
|
const likeString = sanitizeLikeString(keyword);
|
2019-09-16 20:26:32 +00:00
|
|
|
const commands = await commandsCollection.query(
|
2020-09-15 13:01:43 +00:00
|
|
|
Q.where('id', Q.like(`${ likeString }%`))
|
2019-09-16 20:26:32 +00:00
|
|
|
).fetch();
|
|
|
|
this.setState({ mentions: commands || [] });
|
|
|
|
}, 300)
|
2019-06-10 18:36:56 +00:00
|
|
|
|
2019-03-06 13:27:40 +00:00
|
|
|
focus = () => {
|
|
|
|
if (this.component && this.component.focus) {
|
|
|
|
this.component.focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-25 16:22:48 +00:00
|
|
|
handleTyping = (isTyping) => {
|
2020-06-26 20:22:56 +00:00
|
|
|
const { typing, rid, sharing } = this.props;
|
|
|
|
if (sharing) {
|
|
|
|
return;
|
|
|
|
}
|
2019-02-25 16:22:48 +00:00
|
|
|
if (!isTyping) {
|
|
|
|
if (this.typingTimeout) {
|
|
|
|
clearTimeout(this.typingTimeout);
|
|
|
|
this.typingTimeout = false;
|
|
|
|
}
|
2019-04-08 12:35:28 +00:00
|
|
|
typing(rid, false);
|
2019-02-25 16:22:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.typingTimeout) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.typingTimeout = setTimeout(() => {
|
2019-04-08 12:35:28 +00:00
|
|
|
typing(rid, true);
|
2019-02-25 16:22:48 +00:00
|
|
|
this.typingTimeout = false;
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
|
2020-02-11 14:01:35 +00:00
|
|
|
setCommandPreview = async(command, name, params) => {
|
2019-06-10 18:36:56 +00:00
|
|
|
const { rid } = this.props;
|
|
|
|
try {
|
2020-07-08 16:42:49 +00:00
|
|
|
const { success, preview } = await RocketChat.getCommandPreview(name, rid, params);
|
|
|
|
if (success) {
|
|
|
|
return this.setState({ commandPreview: preview?.items, showCommandPreview: true, command });
|
|
|
|
}
|
2019-06-10 18:36:56 +00:00
|
|
|
} catch (e) {
|
2019-08-30 12:43:23 +00:00
|
|
|
log(e);
|
2019-06-10 18:36:56 +00:00
|
|
|
}
|
2020-07-08 16:42:49 +00:00
|
|
|
this.setState({ commandPreview: [], showCommandPreview: true, command: {} });
|
2019-06-10 18:36:56 +00:00
|
|
|
}
|
|
|
|
|
2020-07-20 16:35:17 +00:00
|
|
|
setInput = (text, selection) => {
|
2020-05-20 12:47:41 +00:00
|
|
|
this.text = text;
|
2020-07-20 16:35:17 +00:00
|
|
|
if (selection) {
|
|
|
|
return this.component.setTextAndSelection(text, selection);
|
2019-03-06 13:27:40 +00:00
|
|
|
}
|
2020-07-20 16:35:17 +00:00
|
|
|
this.component.setNativeProps({ text });
|
2019-02-27 14:23:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setShowSend = (showSend) => {
|
2019-11-25 20:01:17 +00:00
|
|
|
const { showSend: prevShowSend } = this.state;
|
2020-06-26 20:22:56 +00:00
|
|
|
const { showSend: propShowSend } = this.props;
|
|
|
|
if (prevShowSend !== showSend && !propShowSend) {
|
2019-11-25 20:01:17 +00:00
|
|
|
this.setState({ showSend });
|
|
|
|
}
|
2018-11-05 19:03:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
clearInput = () => {
|
|
|
|
this.setInput('');
|
2019-02-27 14:23:40 +00:00
|
|
|
this.setShowSend(false);
|
2020-10-30 17:35:07 +00:00
|
|
|
this.setState({ tshow: false });
|
2018-11-05 19:03:17 +00:00
|
|
|
}
|
|
|
|
|
2019-09-24 20:16:59 +00:00
|
|
|
canUploadFile = (file) => {
|
|
|
|
const { FileUpload_MediaTypeWhiteList, FileUpload_MaxFileSize } = this.props;
|
2020-06-26 20:22:56 +00:00
|
|
|
const result = canUploadFile(file, FileUpload_MediaTypeWhiteList, FileUpload_MaxFileSize);
|
2019-09-24 20:16:59 +00:00
|
|
|
if (result.success) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Alert.alert(I18n.t('Error_uploading'), I18n.t(result.error));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-17 19:10:27 +00:00
|
|
|
takePhoto = async() => {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.ROOM_BOX_ACTION_PHOTO);
|
2018-07-17 19:10:27 +00:00
|
|
|
try {
|
2019-07-16 14:30:29 +00:00
|
|
|
const image = await ImagePicker.openCamera(this.imagePickerConfig);
|
2019-09-24 20:16:59 +00:00
|
|
|
if (this.canUploadFile(image)) {
|
2020-06-26 20:22:56 +00:00
|
|
|
this.openShareView([image]);
|
2019-09-24 20:16:59 +00:00
|
|
|
}
|
2018-07-17 19:10:27 +00:00
|
|
|
} catch (e) {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.ROOM_BOX_ACTION_PHOTO_F);
|
2018-07-17 19:10:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-18 17:07:37 +00:00
|
|
|
takeVideo = async() => {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.ROOM_BOX_ACTION_VIDEO);
|
2019-07-18 17:07:37 +00:00
|
|
|
try {
|
|
|
|
const video = await ImagePicker.openCamera(this.videoPickerConfig);
|
2019-09-24 20:16:59 +00:00
|
|
|
if (this.canUploadFile(video)) {
|
2020-06-26 20:22:56 +00:00
|
|
|
this.openShareView([video]);
|
2019-09-24 20:16:59 +00:00
|
|
|
}
|
2019-07-18 17:07:37 +00:00
|
|
|
} catch (e) {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.ROOM_BOX_ACTION_VIDEO_F);
|
2019-07-18 17:07:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-17 19:10:27 +00:00
|
|
|
chooseFromLibrary = async() => {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.ROOM_BOX_ACTION_LIBRARY);
|
2018-07-17 19:10:27 +00:00
|
|
|
try {
|
2020-06-26 20:22:56 +00:00
|
|
|
const attachments = await ImagePicker.openPicker(this.libraryPickerConfig);
|
|
|
|
this.openShareView(attachments);
|
2018-07-17 19:10:27 +00:00
|
|
|
} catch (e) {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.ROOM_BOX_ACTION_LIBRARY_F);
|
2018-07-17 19:10:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-18 17:25:18 +00:00
|
|
|
chooseFile = async() => {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.ROOM_BOX_ACTION_FILE);
|
2019-07-18 17:25:18 +00:00
|
|
|
try {
|
|
|
|
const res = await DocumentPicker.pick({
|
|
|
|
type: [DocumentPicker.types.allFiles]
|
|
|
|
});
|
2019-09-24 20:16:59 +00:00
|
|
|
const file = {
|
2019-07-18 17:25:18 +00:00
|
|
|
filename: res.name,
|
|
|
|
size: res.size,
|
|
|
|
mime: res.type,
|
|
|
|
path: res.uri
|
2019-09-24 20:16:59 +00:00
|
|
|
};
|
|
|
|
if (this.canUploadFile(file)) {
|
2020-06-26 20:22:56 +00:00
|
|
|
this.openShareView([file]);
|
2019-09-24 20:16:59 +00:00
|
|
|
}
|
2019-08-30 12:43:23 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (!DocumentPicker.isCancel(e)) {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.ROOM_BOX_ACTION_FILE_F);
|
2019-08-30 12:43:23 +00:00
|
|
|
log(e);
|
2019-07-18 17:25:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-26 20:22:56 +00:00
|
|
|
openShareView = (attachments) => {
|
2020-07-03 14:07:29 +00:00
|
|
|
const { message, replyCancel, replyWithMention } = this.props;
|
|
|
|
// Start a thread with an attachment
|
|
|
|
let { thread } = this;
|
|
|
|
if (replyWithMention) {
|
|
|
|
thread = message;
|
|
|
|
replyCancel();
|
|
|
|
}
|
|
|
|
Navigation.navigate('ShareView', { room: this.room, thread, attachments });
|
2020-06-26 20:22:56 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 19:50:27 +00:00
|
|
|
createDiscussion = () => {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.ROOM_BOX_ACTION_DISCUSSION);
|
2020-06-15 14:00:46 +00:00
|
|
|
const { isMasterDetail } = this.props;
|
|
|
|
const params = { channel: this.room, showCloseModal: true };
|
|
|
|
if (isMasterDetail) {
|
|
|
|
Navigation.navigate('ModalStackNavigator', { screen: 'CreateDiscussionView', params });
|
|
|
|
} else {
|
|
|
|
Navigation.navigate('NewMessageStackNavigator', { screen: 'CreateDiscussionView', params });
|
|
|
|
}
|
2020-03-30 19:50:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
showMessageBoxActions = () => {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.ROOM_SHOW_BOX_ACTIONS);
|
2020-06-15 19:35:45 +00:00
|
|
|
const { showActionSheet } = this.props;
|
|
|
|
showActionSheet({ options: this.options });
|
2019-05-27 16:19:39 +00:00
|
|
|
}
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
editCancel = () => {
|
|
|
|
const { editCancel } = this.props;
|
|
|
|
editCancel();
|
2018-11-05 19:03:17 +00:00
|
|
|
this.clearInput();
|
2017-11-24 20:44:52 +00:00
|
|
|
}
|
2018-07-17 19:10:27 +00:00
|
|
|
|
2020-07-30 19:51:13 +00:00
|
|
|
openEmoji = () => {
|
|
|
|
logEvent(events.ROOM_OPEN_EMOJI);
|
|
|
|
this.setState({ showEmojiKeyboard: true });
|
2020-02-17 12:14:56 +00:00
|
|
|
}
|
|
|
|
|
2020-07-06 19:23:46 +00:00
|
|
|
recordingCallback = (recording) => {
|
2018-03-07 00:17:20 +00:00
|
|
|
this.setState({ recording });
|
|
|
|
}
|
|
|
|
|
|
|
|
finishAudioMessage = async(fileInfo) => {
|
2019-07-29 16:33:28 +00:00
|
|
|
const {
|
|
|
|
rid, tmid, baseUrl: server, user
|
|
|
|
} = this.props;
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2018-05-29 17:10:40 +00:00
|
|
|
if (fileInfo) {
|
|
|
|
try {
|
2019-09-24 20:16:59 +00:00
|
|
|
if (this.canUploadFile(fileInfo)) {
|
|
|
|
await RocketChat.sendFileMessage(rid, fileInfo, tmid, server, user);
|
2018-05-29 17:10:40 +00:00
|
|
|
}
|
2019-09-24 20:16:59 +00:00
|
|
|
} catch (e) {
|
2019-08-23 13:18:47 +00:00
|
|
|
log(e);
|
2018-05-29 17:10:40 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-07 00:17:20 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 12:14:56 +00:00
|
|
|
closeEmoji = () => {
|
|
|
|
this.setState({ showEmojiKeyboard: false });
|
|
|
|
}
|
|
|
|
|
2018-11-05 19:03:17 +00:00
|
|
|
submit = async() => {
|
2020-10-30 17:35:07 +00:00
|
|
|
const { tshow } = this.state;
|
2018-09-25 19:28:42 +00:00
|
|
|
const {
|
2020-06-26 20:22:56 +00:00
|
|
|
onSubmit, rid: roomId, tmid, showSend, sharing
|
2018-09-25 19:28:42 +00:00
|
|
|
} = this.props;
|
2018-11-05 19:03:17 +00:00
|
|
|
const message = this.text;
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2020-06-26 20:22:56 +00:00
|
|
|
// if sharing, only execute onSubmit prop
|
|
|
|
if (sharing) {
|
|
|
|
onSubmit(message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-27 16:19:39 +00:00
|
|
|
this.clearInput();
|
2019-11-25 20:01:17 +00:00
|
|
|
this.debouncedOnChangeText.stop();
|
2020-02-17 12:14:56 +00:00
|
|
|
this.closeEmoji();
|
2017-12-20 20:14:07 +00:00
|
|
|
this.stopTrackingMention();
|
2019-02-25 16:22:48 +00:00
|
|
|
this.handleTyping(false);
|
2020-06-26 20:22:56 +00:00
|
|
|
if (message.trim() === '' && !showSend) {
|
2018-03-02 21:31:44 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-04-17 17:01:03 +00:00
|
|
|
|
2018-07-20 19:54:46 +00:00
|
|
|
const {
|
2020-02-11 15:56:06 +00:00
|
|
|
editing, replying, message: { id: messageTmid }, replyCancel
|
2018-07-20 19:54:46 +00:00
|
|
|
} = this.props;
|
|
|
|
|
2019-06-10 18:36:56 +00:00
|
|
|
// Slash command
|
|
|
|
if (message[0] === MENTIONS_TRACKING_TYPE_COMMANDS) {
|
2019-09-16 20:26:32 +00:00
|
|
|
const db = database.active;
|
|
|
|
const commandsCollection = db.collections.get('slash_commands');
|
2019-06-10 18:36:56 +00:00
|
|
|
const command = message.replace(/ .*/, '').slice(1);
|
2020-09-15 13:01:43 +00:00
|
|
|
const likeString = sanitizeLikeString(command);
|
2019-09-16 20:26:32 +00:00
|
|
|
const slashCommand = await commandsCollection.query(
|
2020-09-15 13:01:43 +00:00
|
|
|
Q.where('id', Q.like(`${ likeString }%`))
|
2019-09-16 20:26:32 +00:00
|
|
|
).fetch();
|
2019-06-10 18:36:56 +00:00
|
|
|
if (slashCommand.length > 0) {
|
2020-08-05 13:15:56 +00:00
|
|
|
logEvent(events.COMMAND_RUN);
|
2019-06-10 18:36:56 +00:00
|
|
|
try {
|
2019-09-25 22:14:20 +00:00
|
|
|
const messageWithoutCommand = message.replace(/([^\s]+)/, '').trim();
|
2020-02-11 14:01:35 +00:00
|
|
|
const [{ appId }] = slashCommand;
|
|
|
|
const triggerId = generateTriggerId(appId);
|
2020-02-11 15:56:06 +00:00
|
|
|
RocketChat.runSlashCommand(command, roomId, messageWithoutCommand, triggerId, tmid || messageTmid);
|
|
|
|
replyCancel();
|
2019-06-10 18:36:56 +00:00
|
|
|
} catch (e) {
|
2020-08-05 13:15:56 +00:00
|
|
|
logEvent(events.COMMAND_RUN_F);
|
2019-08-30 12:43:23 +00:00
|
|
|
log(e);
|
2019-06-10 18:36:56 +00:00
|
|
|
}
|
|
|
|
this.clearInput();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-04-17 17:01:03 +00:00
|
|
|
// Edit
|
2018-03-02 21:31:44 +00:00
|
|
|
if (editing) {
|
2019-09-16 20:26:32 +00:00
|
|
|
const { message: editingMessage, editRequest } = this.props;
|
|
|
|
const { id, subscription: { id: rid } } = editingMessage;
|
|
|
|
editRequest({ id, msg: message, rid });
|
2019-04-17 17:01:03 +00:00
|
|
|
|
|
|
|
// Reply
|
2018-07-20 19:54:46 +00:00
|
|
|
} else if (replying) {
|
2019-09-16 20:26:32 +00:00
|
|
|
const {
|
2020-02-11 15:56:06 +00:00
|
|
|
message: replyingMessage, threadsEnabled, replyWithMention
|
2019-09-16 20:26:32 +00:00
|
|
|
} = this.props;
|
2019-04-17 17:01:03 +00:00
|
|
|
|
|
|
|
// Thread
|
2019-09-16 20:26:32 +00:00
|
|
|
if (threadsEnabled && replyWithMention) {
|
2020-10-30 17:35:07 +00:00
|
|
|
onSubmit(message, replyingMessage.id, tshow);
|
2019-04-17 17:01:03 +00:00
|
|
|
|
2019-05-03 13:33:38 +00:00
|
|
|
// Legacy reply or quote (quote is a reply without mention)
|
2019-04-17 17:01:03 +00:00
|
|
|
} else {
|
|
|
|
const { user, roomType } = this.props;
|
2019-09-16 20:26:32 +00:00
|
|
|
const permalink = await this.getPermalink(replyingMessage);
|
2019-04-17 17:01:03 +00:00
|
|
|
let msg = `[ ](${ permalink }) `;
|
2018-07-20 19:54:46 +00:00
|
|
|
|
2019-04-17 17:01:03 +00:00
|
|
|
// if original message wasn't sent by current user and neither from a direct room
|
2019-09-16 20:26:32 +00:00
|
|
|
if (user.username !== replyingMessage.u.username && roomType !== 'd' && replyWithMention) {
|
|
|
|
msg += `@${ replyingMessage.u.username } `;
|
2019-04-17 17:01:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
msg = `${ msg } ${ message }`;
|
|
|
|
onSubmit(msg);
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
replyCancel();
|
2019-04-17 17:01:03 +00:00
|
|
|
|
|
|
|
// Normal message
|
2018-03-02 21:31:44 +00:00
|
|
|
} else {
|
2020-10-30 17:35:07 +00:00
|
|
|
onSubmit(message, undefined, tshow);
|
2018-03-02 21:31:44 +00:00
|
|
|
}
|
2017-11-24 20:44:52 +00:00
|
|
|
}
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
updateMentions = (keyword, type) => {
|
|
|
|
if (type === MENTIONS_TRACKING_TYPE_USERS) {
|
|
|
|
this.getUsers(keyword);
|
|
|
|
} else if (type === MENTIONS_TRACKING_TYPE_EMOJIS) {
|
|
|
|
this.getEmojis(keyword);
|
2019-06-10 18:36:56 +00:00
|
|
|
} else if (type === MENTIONS_TRACKING_TYPE_COMMANDS) {
|
|
|
|
this.getSlashCommands(keyword);
|
2018-09-25 19:28:42 +00:00
|
|
|
} else {
|
|
|
|
this.getRooms(keyword);
|
2017-12-20 20:14:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-27 14:23:40 +00:00
|
|
|
identifyMentionKeyword = (keyword, type) => {
|
2020-02-17 12:14:56 +00:00
|
|
|
this.setState({
|
|
|
|
showEmojiKeyboard: false,
|
|
|
|
trackingType: type
|
|
|
|
});
|
2018-09-25 19:28:42 +00:00
|
|
|
this.updateMentions(keyword, type);
|
2018-01-30 19:48:26 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 14:23:40 +00:00
|
|
|
stopTrackingMention = () => {
|
2019-09-25 21:32:13 +00:00
|
|
|
const { trackingType, showCommandPreview } = this.state;
|
|
|
|
if (!trackingType && !showCommandPreview) {
|
2018-11-05 19:03:17 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-12-20 20:14:07 +00:00
|
|
|
this.setState({
|
2018-01-30 19:48:26 +00:00
|
|
|
mentions: [],
|
2019-06-10 18:36:56 +00:00
|
|
|
trackingType: '',
|
2019-09-25 21:32:13 +00:00
|
|
|
commandPreview: [],
|
|
|
|
showCommandPreview: false
|
2017-12-20 20:14:07 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-25 20:01:17 +00:00
|
|
|
handleCommands = ({ event }) => {
|
|
|
|
if (handleCommandTyping(event)) {
|
|
|
|
if (this.focused) {
|
|
|
|
Keyboard.dismiss();
|
|
|
|
} else {
|
|
|
|
this.component.focus();
|
|
|
|
}
|
|
|
|
this.focused = !this.focused;
|
|
|
|
} else if (handleCommandSubmit(event)) {
|
|
|
|
this.submit();
|
|
|
|
} else if (handleCommandShowUpload(event)) {
|
2020-03-30 19:50:27 +00:00
|
|
|
this.showMessageBoxActions();
|
2019-11-25 20:01:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-30 17:35:07 +00:00
|
|
|
onPressSendToChannel = () => this.setState(({ tshow }) => ({ tshow: !tshow }))
|
|
|
|
|
|
|
|
renderSendToChannel = () => {
|
|
|
|
const { tshow } = this.state;
|
2020-11-04 13:39:53 +00:00
|
|
|
const { theme, tmid, replyWithMention } = this.props;
|
2020-10-30 17:35:07 +00:00
|
|
|
|
2020-11-04 13:39:53 +00:00
|
|
|
if (!tmid && !replyWithMention) {
|
2020-10-30 17:35:07 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<TouchableWithoutFeedback
|
|
|
|
style={[styles.sendToChannelButton, { backgroundColor: themes[theme].messageboxBackground }]}
|
|
|
|
onPress={this.onPressSendToChannel}
|
|
|
|
testID='messagebox-send-to-channel'
|
|
|
|
>
|
|
|
|
<CustomIcon name={tshow ? 'checkbox-checked' : 'checkbox-unchecked'} size={24} color={themes[theme].auxiliaryText} />
|
|
|
|
<Text style={[styles.sendToChannelText, { color: themes[theme].auxiliaryText }]}>{I18n.t('Messagebox_Send_to_channel')}</Text>
|
|
|
|
</TouchableWithoutFeedback>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-10-30 14:14:41 +00:00
|
|
|
renderContent = () => {
|
2018-09-11 16:32:52 +00:00
|
|
|
const {
|
2020-02-17 12:14:56 +00:00
|
|
|
recording, showEmojiKeyboard, showSend, mentions, trackingType, commandPreview, showCommandPreview
|
2019-10-30 14:14:41 +00:00
|
|
|
} = this.state;
|
|
|
|
const {
|
2020-06-26 20:22:56 +00:00
|
|
|
editing, message, replying, replyCancel, user, getCustomEmoji, theme, Message_AudioRecorderEnabled, children, isActionsEnabled
|
2018-09-11 16:32:52 +00:00
|
|
|
} = this.props;
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2019-11-25 20:01:17 +00:00
|
|
|
const isAndroidTablet = isTablet && isAndroid ? {
|
|
|
|
multiline: false,
|
|
|
|
onSubmitEditing: this.submit,
|
|
|
|
returnKeyType: 'send'
|
|
|
|
} : {};
|
|
|
|
|
2020-07-06 19:23:46 +00:00
|
|
|
const recordAudio = showSend || !Message_AudioRecorderEnabled ? null : (
|
|
|
|
<RecordAudio
|
|
|
|
theme={theme}
|
|
|
|
recordingCallback={this.recordingCallback}
|
|
|
|
onFinish={this.finishAudioMessage}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
const commandsPreviewAndMentions = !recording ? (
|
2019-09-16 20:26:32 +00:00
|
|
|
<>
|
2019-10-30 14:14:41 +00:00
|
|
|
<CommandsPreview commandPreview={commandPreview} showCommandPreview={showCommandPreview} />
|
2019-12-04 16:39:53 +00:00
|
|
|
<Mentions mentions={mentions} trackingType={trackingType} theme={theme} />
|
2020-07-06 19:23:46 +00:00
|
|
|
</>
|
|
|
|
) : null;
|
|
|
|
|
|
|
|
const replyPreview = !recording ? (
|
|
|
|
<ReplyPreview
|
|
|
|
message={message}
|
|
|
|
close={replyCancel}
|
|
|
|
username={user.username}
|
|
|
|
replying={replying}
|
|
|
|
getCustomEmoji={getCustomEmoji}
|
|
|
|
theme={theme}
|
|
|
|
/>
|
|
|
|
) : null;
|
|
|
|
|
|
|
|
const textInputAndButtons = !recording ? (
|
|
|
|
<>
|
|
|
|
<LeftButtons
|
|
|
|
theme={theme}
|
|
|
|
showEmojiKeyboard={showEmojiKeyboard}
|
|
|
|
editing={editing}
|
|
|
|
showMessageBoxActions={this.showMessageBoxActions}
|
|
|
|
editCancel={this.editCancel}
|
|
|
|
openEmoji={this.openEmoji}
|
|
|
|
closeEmoji={this.closeEmoji}
|
|
|
|
isActionsEnabled={isActionsEnabled}
|
|
|
|
/>
|
|
|
|
<TextInput
|
|
|
|
ref={component => this.component = component}
|
2021-01-25 17:14:45 +00:00
|
|
|
style={[styles.textBoxInput, { color: themes[theme].bodyText }]}
|
2020-07-06 19:23:46 +00:00
|
|
|
returnKeyType='default'
|
|
|
|
keyboardType='twitter'
|
|
|
|
blurOnSubmit={false}
|
|
|
|
placeholder={I18n.t('New_Message')}
|
2020-10-30 17:35:07 +00:00
|
|
|
placeholderTextColor={themes[theme].auxiliaryTintColor}
|
2020-07-06 19:23:46 +00:00
|
|
|
onChangeText={this.onChangeText}
|
2020-07-20 16:35:17 +00:00
|
|
|
onSelectionChange={this.onSelectionChange}
|
2020-07-06 19:23:46 +00:00
|
|
|
underlineColorAndroid='transparent'
|
|
|
|
defaultValue=''
|
|
|
|
multiline
|
|
|
|
testID='messagebox-input'
|
|
|
|
theme={theme}
|
|
|
|
{...isAndroidTablet}
|
|
|
|
/>
|
|
|
|
<RightButtons
|
|
|
|
theme={theme}
|
|
|
|
showSend={showSend}
|
|
|
|
submit={this.submit}
|
|
|
|
showMessageBoxActions={this.showMessageBoxActions}
|
|
|
|
isActionsEnabled={isActionsEnabled}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
) : null;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{commandsPreviewAndMentions}
|
2020-07-14 16:43:15 +00:00
|
|
|
<View style={[styles.composer, { borderTopColor: themes[theme].borderColor }]}>
|
2020-07-06 19:23:46 +00:00
|
|
|
{replyPreview}
|
2018-09-11 16:32:52 +00:00
|
|
|
<View
|
2019-12-04 16:39:53 +00:00
|
|
|
style={[
|
|
|
|
styles.textArea,
|
2020-07-06 19:23:46 +00:00
|
|
|
{ backgroundColor: themes[theme].messageboxBackground },
|
|
|
|
!recording && editing && { backgroundColor: themes[theme].chatComponentBackground }
|
2019-12-04 16:39:53 +00:00
|
|
|
]}
|
2018-09-11 16:32:52 +00:00
|
|
|
testID='messagebox'
|
|
|
|
>
|
2020-07-06 19:23:46 +00:00
|
|
|
{textInputAndButtons}
|
|
|
|
{recordAudio}
|
2018-09-11 16:32:52 +00:00
|
|
|
</View>
|
2020-10-30 17:35:07 +00:00
|
|
|
{this.renderSendToChannel()}
|
2018-04-10 13:03:54 +00:00
|
|
|
</View>
|
2020-06-26 20:22:56 +00:00
|
|
|
{children}
|
2019-09-16 20:26:32 +00:00
|
|
|
</>
|
2018-02-08 14:08:50 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2019-09-16 20:26:32 +00:00
|
|
|
console.count(`${ this.constructor.name }.render calls`);
|
2020-06-26 20:22:56 +00:00
|
|
|
const { showEmojiKeyboard } = this.state;
|
2020-06-15 14:00:46 +00:00
|
|
|
const {
|
2020-06-26 20:22:56 +00:00
|
|
|
user, baseUrl, theme, iOSScrollBehavior
|
2020-06-15 14:00:46 +00:00
|
|
|
} = this.props;
|
2018-02-08 14:08:50 +00:00
|
|
|
return (
|
2019-10-30 14:14:41 +00:00
|
|
|
<MessageboxContext.Provider
|
|
|
|
value={{
|
|
|
|
user,
|
|
|
|
baseUrl,
|
|
|
|
onPressMention: this.onPressMention,
|
|
|
|
onPressCommandPreview: this.onPressCommandPreview
|
|
|
|
}}
|
|
|
|
>
|
2020-02-17 12:14:56 +00:00
|
|
|
<KeyboardAccessoryView
|
2020-02-28 17:52:50 +00:00
|
|
|
ref={ref => this.tracking = ref}
|
2020-02-17 12:14:56 +00:00
|
|
|
renderContent={this.renderContent}
|
|
|
|
kbInputRef={this.component}
|
|
|
|
kbComponent={showEmojiKeyboard ? 'EmojiKeyboard' : null}
|
|
|
|
onKeyboardResigned={this.onKeyboardResigned}
|
|
|
|
onItemSelected={this.onEmojiSelected}
|
|
|
|
trackInteractive
|
|
|
|
// revealKeyboardInteractive
|
2020-02-13 20:44:57 +00:00
|
|
|
requiresSameParentToManageScrollView
|
2020-02-17 12:14:56 +00:00
|
|
|
addBottomView
|
2019-12-04 16:39:53 +00:00
|
|
|
bottomViewColor={themes[theme].messageboxBackground}
|
2020-06-26 20:22:56 +00:00
|
|
|
iOSScrollBehavior={iOSScrollBehavior}
|
2018-07-17 19:10:27 +00:00
|
|
|
/>
|
2019-10-30 14:14:41 +00:00
|
|
|
</MessageboxContext.Provider>
|
2017-08-09 13:12:00 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-04-08 12:35:28 +00:00
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
2020-06-15 14:00:46 +00:00
|
|
|
isMasterDetail: state.app.isMasterDetail,
|
2020-02-11 14:09:14 +00:00
|
|
|
baseUrl: state.server.server,
|
2019-04-17 17:01:03 +00:00
|
|
|
threadsEnabled: state.settings.Threads_enabled,
|
2020-02-11 14:09:14 +00:00
|
|
|
user: getUserSelector(state),
|
2019-09-24 20:16:59 +00:00
|
|
|
FileUpload_MediaTypeWhiteList: state.settings.FileUpload_MediaTypeWhiteList,
|
2020-02-20 21:02:09 +00:00
|
|
|
FileUpload_MaxFileSize: state.settings.FileUpload_MaxFileSize,
|
|
|
|
Message_AudioRecorderEnabled: state.settings.Message_AudioRecorderEnabled
|
2019-04-08 12:35:28 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const dispatchToProps = ({
|
2019-09-16 20:26:32 +00:00
|
|
|
typing: (rid, status) => userTypingAction(rid, status)
|
2019-04-08 12:35:28 +00:00
|
|
|
});
|
|
|
|
|
2020-06-15 19:35:45 +00:00
|
|
|
export default connect(mapStateToProps, dispatchToProps, null, { forwardRef: true })(withActionSheet(MessageBox));
|