2017-08-09 13:12:00 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-05-29 17:10:40 +00:00
|
|
|
import { View, TextInput, FlatList, Text, TouchableOpacity, Alert } from 'react-native';
|
2018-01-16 18:48:05 +00:00
|
|
|
import Icon from 'react-native-vector-icons/MaterialIcons';
|
2017-11-21 16:55:32 +00:00
|
|
|
import { connect } from 'react-redux';
|
2018-01-30 19:48:26 +00:00
|
|
|
import { emojify } from 'react-emojione';
|
2018-02-08 14:08:50 +00:00
|
|
|
import { KeyboardAccessoryView } from 'react-native-keyboard-input';
|
2018-07-17 19:10:27 +00:00
|
|
|
import ImagePicker from 'react-native-image-crop-picker';
|
2018-03-07 00:17:20 +00:00
|
|
|
|
2018-05-18 17:55:08 +00:00
|
|
|
import { userTyping } from '../../actions/room';
|
2017-12-08 19:36:03 +00:00
|
|
|
import RocketChat from '../../lib/rocketchat';
|
2018-07-20 19:54:46 +00:00
|
|
|
import { editRequest, editCancel, replyCancel } from '../../actions/messages';
|
2018-02-08 14:08:50 +00:00
|
|
|
import styles from './styles';
|
2017-12-08 19:36:03 +00:00
|
|
|
import MyIcon from '../icons';
|
2017-12-27 15:22:06 +00:00
|
|
|
import database from '../../lib/realm';
|
2017-12-20 20:14:07 +00:00
|
|
|
import Avatar from '../Avatar';
|
2018-01-30 19:48:26 +00:00
|
|
|
import CustomEmoji from '../EmojiPicker/CustomEmoji';
|
|
|
|
import { emojis } from '../../emojis';
|
2018-03-07 00:17:20 +00:00
|
|
|
import Recording from './Recording';
|
2018-07-17 19:10:27 +00:00
|
|
|
import FilesActions from './FilesActions';
|
|
|
|
import UploadModal from './UploadModal';
|
2018-02-08 14:08:50 +00:00
|
|
|
import './EmojiKeyboard';
|
2018-05-18 17:55:08 +00:00
|
|
|
import log from '../../utils/log';
|
2018-06-01 17:38:13 +00:00
|
|
|
import I18n from '../../i18n';
|
2018-07-20 19:54:46 +00:00
|
|
|
import ReplyPreview from './ReplyPreview';
|
2018-03-07 00:17:20 +00:00
|
|
|
|
2017-12-20 20:14:07 +00:00
|
|
|
const MENTIONS_TRACKING_TYPE_USERS = '@';
|
2018-01-30 19:48:26 +00:00
|
|
|
const MENTIONS_TRACKING_TYPE_EMOJIS = ':';
|
2017-12-20 20:14:07 +00:00
|
|
|
|
|
|
|
const onlyUnique = function onlyUnique(value, index, self) {
|
|
|
|
return self.indexOf(({ _id }) => value._id === _id) === index;
|
|
|
|
};
|
|
|
|
|
2018-07-17 19:10:27 +00:00
|
|
|
const imagePickerConfig = {
|
|
|
|
cropping: true,
|
|
|
|
compressImageQuality: 0.8,
|
|
|
|
cropperAvoidEmptySpaceAroundImage: false,
|
|
|
|
cropperChooseText: I18n.t('Choose'),
|
|
|
|
cropperCancelText: I18n.t('Cancel')
|
|
|
|
};
|
|
|
|
|
2017-11-21 14:55:50 +00:00
|
|
|
@connect(state => ({
|
2018-07-20 19:54:46 +00:00
|
|
|
roomType: state.room.t,
|
2017-11-21 14:55:50 +00:00
|
|
|
message: state.messages.message,
|
2018-07-20 19:54:46 +00:00
|
|
|
replyMessage: state.messages.replyMessage,
|
|
|
|
replying: state.messages.replyMessage && !!state.messages.replyMessage.msg,
|
2017-12-20 20:14:07 +00:00
|
|
|
editing: state.messages.editing,
|
2018-07-20 19:54:46 +00:00
|
|
|
baseUrl: state.settings.Site_Url || state.server ? state.server.server : '',
|
|
|
|
username: state.login.user && state.login.user.username
|
2017-11-21 14:55:50 +00:00
|
|
|
}), dispatch => ({
|
2017-11-24 20:44:52 +00:00
|
|
|
editCancel: () => dispatch(editCancel()),
|
2017-11-21 17:05:14 +00:00
|
|
|
editRequest: message => dispatch(editRequest(message)),
|
2017-11-24 20:44:52 +00:00
|
|
|
typing: status => dispatch(userTyping(status)),
|
2018-07-20 19:54:46 +00:00
|
|
|
closeReply: () => dispatch(replyCancel())
|
2017-11-21 14:55:50 +00:00
|
|
|
}))
|
2018-01-09 17:12:55 +00:00
|
|
|
export default class MessageBox extends React.PureComponent {
|
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
|
|
|
replyMessage: PropTypes.object,
|
|
|
|
replying: PropTypes.bool,
|
2017-11-21 17:09:22 +00:00
|
|
|
editing: PropTypes.bool,
|
2018-07-20 19:54:46 +00:00
|
|
|
username: PropTypes.string,
|
|
|
|
roomType: PropTypes.string,
|
|
|
|
editCancel: PropTypes.func.isRequired,
|
|
|
|
editRequest: PropTypes.func.isRequired,
|
|
|
|
onSubmit: PropTypes.func.isRequired,
|
2017-11-24 20:44:52 +00:00
|
|
|
typing: PropTypes.func,
|
2018-07-20 19:54:46 +00:00
|
|
|
closeReply: PropTypes.func
|
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
|
|
|
text: '',
|
|
|
|
mentions: [],
|
2018-02-08 14:08:50 +00:00
|
|
|
showEmojiKeyboard: false,
|
2018-07-17 19:10:27 +00:00
|
|
|
showFilesAction: false,
|
|
|
|
recording: false,
|
|
|
|
file: {
|
|
|
|
isVisible: false
|
|
|
|
}
|
2017-11-28 16:27:06 +00:00
|
|
|
};
|
2017-12-20 20:14:07 +00:00
|
|
|
this.users = [];
|
|
|
|
this.rooms = [];
|
2018-01-30 19:48:26 +00:00
|
|
|
this.emojis = [];
|
|
|
|
this.customEmojis = [];
|
2018-02-08 14:08:50 +00:00
|
|
|
this._onEmojiSelected = this._onEmojiSelected.bind(this);
|
2017-11-28 16:27:06 +00:00
|
|
|
}
|
2017-11-21 14:55:50 +00:00
|
|
|
componentWillReceiveProps(nextProps) {
|
2017-12-20 20:14:07 +00:00
|
|
|
if (this.props.message !== nextProps.message && nextProps.message.msg) {
|
|
|
|
this.setState({ text: nextProps.message.msg });
|
2017-11-21 14:55:50 +00:00
|
|
|
this.component.focus();
|
2018-07-20 19:54:46 +00:00
|
|
|
} else if (this.props.replyMessage !== nextProps.replyMessage && nextProps.replyMessage.msg) {
|
|
|
|
this.component.focus();
|
2017-11-24 20:44:52 +00:00
|
|
|
} else if (!nextProps.message) {
|
2017-12-20 20:14:07 +00:00
|
|
|
this.setState({ text: '' });
|
2017-11-21 14:55:50 +00:00
|
|
|
}
|
2017-08-09 13:12:00 +00:00
|
|
|
}
|
2017-12-20 20:14:07 +00:00
|
|
|
|
2018-01-25 14:04:20 +00:00
|
|
|
onChangeText(text) {
|
|
|
|
this.setState({ text });
|
2018-04-24 19:34:03 +00:00
|
|
|
this.props.typing(text.length > 0);
|
2018-01-25 14:04:20 +00:00
|
|
|
|
2017-12-20 20:14:07 +00:00
|
|
|
requestAnimationFrame(() => {
|
|
|
|
const { start, end } = this.component._lastNativeSelection;
|
|
|
|
|
|
|
|
const cursor = Math.max(start, end);
|
|
|
|
|
2018-01-25 14:04:20 +00:00
|
|
|
const lastNativeText = this.component._lastNativeText;
|
2017-12-20 20:14:07 +00:00
|
|
|
|
2018-01-30 19:48:26 +00:00
|
|
|
const regexp = /(#|@|:)([a-z0-9._-]+)$/im;
|
2017-12-20 20:14:07 +00:00
|
|
|
|
2018-01-25 14:04:20 +00:00
|
|
|
const result = lastNativeText.substr(0, cursor).match(regexp);
|
2017-12-20 20:14:07 +00:00
|
|
|
if (!result) {
|
|
|
|
return this.stopTrackingMention();
|
|
|
|
}
|
|
|
|
const [, lastChar, name] = result;
|
|
|
|
|
|
|
|
this.identifyMentionKeyword(name, lastChar);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-02-08 14:08:50 +00:00
|
|
|
onKeyboardResigned() {
|
|
|
|
this.closeEmoji();
|
|
|
|
}
|
|
|
|
|
2017-12-08 19:36:03 +00:00
|
|
|
get leftButtons() {
|
2017-11-24 17:21:21 +00:00
|
|
|
const { editing } = this.props;
|
2017-11-21 14:55:50 +00:00
|
|
|
if (editing) {
|
2017-12-11 20:37:33 +00:00
|
|
|
return (<Icon
|
|
|
|
style={styles.actionButtons}
|
2018-01-16 18:48:05 +00:00
|
|
|
name='close'
|
2018-06-01 17:38:13 +00:00
|
|
|
accessibilityLabel={I18n.t('Cancel_editing')}
|
2017-12-11 20:37:33 +00:00
|
|
|
accessibilityTraits='button'
|
|
|
|
onPress={() => this.editCancel()}
|
2018-05-23 13:39:18 +00:00
|
|
|
testID='messagebox-cancel-editing'
|
2017-12-11 20:37:33 +00:00
|
|
|
/>);
|
2017-08-22 01:24:41 +00:00
|
|
|
}
|
2018-02-08 14:08:50 +00:00
|
|
|
return !this.state.showEmojiKeyboard ? (<Icon
|
2017-12-11 20:37:33 +00:00
|
|
|
style={styles.actionButtons}
|
|
|
|
onPress={() => this.openEmoji()}
|
2018-06-01 17:38:13 +00:00
|
|
|
accessibilityLabel={I18n.t('Open_emoji_selector')}
|
2017-12-11 20:37:33 +00:00
|
|
|
accessibilityTraits='button'
|
2018-01-16 18:48:05 +00:00
|
|
|
name='mood'
|
2018-05-23 13:39:18 +00:00
|
|
|
testID='messagebox-open-emoji'
|
2017-12-11 20:37:33 +00:00
|
|
|
/>) : (<Icon
|
2018-01-16 18:48:05 +00:00
|
|
|
onPress={() => this.closeEmoji()}
|
2017-12-11 20:37:33 +00:00
|
|
|
style={styles.actionButtons}
|
2018-06-01 17:38:13 +00:00
|
|
|
accessibilityLabel={I18n.t('Close_emoji_selector')}
|
2017-12-11 20:37:33 +00:00
|
|
|
accessibilityTraits='button'
|
2018-01-16 18:48:05 +00:00
|
|
|
name='keyboard'
|
2018-05-23 13:39:18 +00:00
|
|
|
testID='messagebox-close-emoji'
|
2017-12-11 20:37:33 +00:00
|
|
|
/>);
|
2017-12-08 19:36:03 +00:00
|
|
|
}
|
|
|
|
get rightButtons() {
|
|
|
|
const icons = [];
|
|
|
|
|
2017-12-20 20:14:07 +00:00
|
|
|
if (this.state.text) {
|
2017-12-08 19:36:03 +00:00
|
|
|
icons.push(<MyIcon
|
|
|
|
style={[styles.actionButtons, { color: '#1D74F5' }]}
|
|
|
|
name='send'
|
|
|
|
key='sendIcon'
|
2018-06-01 17:38:13 +00:00
|
|
|
accessibilityLabel={I18n.t('Send message')}
|
2017-12-11 20:37:33 +00:00
|
|
|
accessibilityTraits='button'
|
2017-12-20 20:14:07 +00:00
|
|
|
onPress={() => this.submit(this.state.text)}
|
2018-05-23 13:39:18 +00:00
|
|
|
testID='messagebox-send-message'
|
2017-12-08 19:36:03 +00:00
|
|
|
/>);
|
2018-03-07 00:17:20 +00:00
|
|
|
return icons;
|
2017-12-08 19:36:03 +00:00
|
|
|
}
|
2018-03-07 00:17:20 +00:00
|
|
|
icons.push(<Icon
|
|
|
|
style={[styles.actionButtons, { color: '#1D74F5', paddingHorizontal: 10 }]}
|
|
|
|
name='mic'
|
2018-03-23 16:55:40 +00:00
|
|
|
key='micIcon'
|
2018-06-01 17:38:13 +00:00
|
|
|
accessibilityLabel={I18n.t('Send audio message')}
|
2018-03-07 00:17:20 +00:00
|
|
|
accessibilityTraits='button'
|
|
|
|
onPress={() => this.recordAudioMessage()}
|
2018-05-23 13:39:18 +00:00
|
|
|
testID='messagebox-send-audio'
|
2018-03-07 00:17:20 +00:00
|
|
|
/>);
|
|
|
|
icons.push(<MyIcon
|
|
|
|
style={[styles.actionButtons, { color: '#2F343D', fontSize: 16 }]}
|
|
|
|
name='plus'
|
|
|
|
key='fileIcon'
|
2018-06-01 17:38:13 +00:00
|
|
|
accessibilityLabel={I18n.t('Message actions')}
|
2018-03-07 00:17:20 +00:00
|
|
|
accessibilityTraits='button'
|
2018-07-17 19:10:27 +00:00
|
|
|
onPress={this.toggleFilesActions}
|
2018-05-23 13:39:18 +00:00
|
|
|
testID='messagebox-actions'
|
2018-03-07 00:17:20 +00:00
|
|
|
/>);
|
2017-12-08 19:36:03 +00:00
|
|
|
return icons;
|
2017-08-11 18:18:09 +00:00
|
|
|
}
|
2017-08-09 13:12:00 +00:00
|
|
|
|
2018-07-20 19:54:46 +00:00
|
|
|
getPermalink = async(message) => {
|
|
|
|
try {
|
|
|
|
return await RocketChat.getPermalink(message);
|
|
|
|
} catch (error) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-17 19:10:27 +00:00
|
|
|
toggleFilesActions = () => {
|
|
|
|
this.setState(prevState => ({ showFilesAction: !prevState.showFilesAction }));
|
|
|
|
}
|
|
|
|
|
|
|
|
sendImageMessage = async(file) => {
|
|
|
|
this.setState({ file: { isVisible: false } });
|
|
|
|
const fileInfo = {
|
|
|
|
name: file.name,
|
|
|
|
description: file.description,
|
|
|
|
size: file.size,
|
|
|
|
type: file.mime,
|
|
|
|
store: 'Uploads',
|
|
|
|
path: file.path
|
2017-08-10 20:09:54 +00:00
|
|
|
};
|
2018-07-17 19:10:27 +00:00
|
|
|
try {
|
|
|
|
await RocketChat.sendFileMessage(this.props.rid, fileInfo);
|
|
|
|
} catch (e) {
|
|
|
|
log('sendImageMessage', e);
|
|
|
|
}
|
2017-08-10 20:09:54 +00:00
|
|
|
}
|
2018-07-17 19:10:27 +00:00
|
|
|
|
|
|
|
takePhoto = async() => {
|
|
|
|
try {
|
|
|
|
const image = await ImagePicker.openCamera(imagePickerConfig);
|
|
|
|
this.showUploadModal(image);
|
|
|
|
} catch (e) {
|
|
|
|
log('takePhoto', e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
chooseFromLibrary = async() => {
|
|
|
|
try {
|
|
|
|
const image = await ImagePicker.openPicker(imagePickerConfig);
|
|
|
|
this.showUploadModal(image);
|
|
|
|
} catch (e) {
|
|
|
|
log('chooseFromLibrary', e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
showUploadModal = (file) => {
|
|
|
|
this.setState({ file: { ...file, isVisible: true } });
|
|
|
|
}
|
|
|
|
|
2017-11-24 20:44:52 +00:00
|
|
|
editCancel() {
|
|
|
|
this.props.editCancel();
|
2017-12-20 20:14:07 +00:00
|
|
|
this.setState({ text: '' });
|
2017-11-24 20:44:52 +00:00
|
|
|
}
|
2018-07-17 19:10:27 +00:00
|
|
|
|
2018-01-16 18:48:05 +00:00
|
|
|
async openEmoji() {
|
2018-01-30 19:48:26 +00:00
|
|
|
await this.setState({
|
2018-02-08 14:08:50 +00:00
|
|
|
showEmojiKeyboard: true
|
2018-01-30 19:48:26 +00:00
|
|
|
});
|
2018-01-16 18:48:05 +00:00
|
|
|
}
|
2018-03-07 00:17:20 +00:00
|
|
|
|
|
|
|
async recordAudioMessage() {
|
|
|
|
const recording = await Recording.permission();
|
|
|
|
this.setState({ recording });
|
|
|
|
}
|
|
|
|
|
|
|
|
finishAudioMessage = async(fileInfo) => {
|
|
|
|
this.setState({
|
|
|
|
recording: false
|
|
|
|
});
|
2018-05-29 17:10:40 +00:00
|
|
|
if (fileInfo) {
|
|
|
|
try {
|
|
|
|
await RocketChat.sendFileMessage(this.props.rid, fileInfo);
|
|
|
|
} catch (e) {
|
|
|
|
if (e && e.error === 'error-file-too-large') {
|
|
|
|
return Alert.alert('File is too large!');
|
|
|
|
}
|
|
|
|
log('finishAudioMessage', e);
|
|
|
|
}
|
|
|
|
}
|
2018-03-07 00:17:20 +00:00
|
|
|
}
|
|
|
|
|
2018-01-16 18:48:05 +00:00
|
|
|
closeEmoji() {
|
2018-02-08 14:08:50 +00:00
|
|
|
this.setState({ showEmojiKeyboard: false });
|
2017-12-08 19:36:03 +00:00
|
|
|
}
|
2018-03-07 00:17:20 +00:00
|
|
|
|
2018-07-20 19:54:46 +00:00
|
|
|
async submit(message) {
|
2017-12-08 19:36:03 +00:00
|
|
|
this.setState({ text: '' });
|
2018-01-16 18:48:05 +00:00
|
|
|
this.closeEmoji();
|
2017-12-20 20:14:07 +00:00
|
|
|
this.stopTrackingMention();
|
2018-03-02 21:31:44 +00:00
|
|
|
this.props.typing(false);
|
|
|
|
if (message.trim() === '') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// if is editing a message
|
2018-07-20 19:54:46 +00:00
|
|
|
const {
|
|
|
|
editing, replying
|
|
|
|
} = this.props;
|
|
|
|
|
2018-03-02 21:31:44 +00:00
|
|
|
if (editing) {
|
|
|
|
const { _id, rid } = this.props.message;
|
|
|
|
this.props.editRequest({ _id, msg: message, rid });
|
2018-07-20 19:54:46 +00:00
|
|
|
} else if (replying) {
|
|
|
|
const {
|
|
|
|
username, replyMessage, roomType, closeReply
|
|
|
|
} = this.props;
|
|
|
|
const permalink = await this.getPermalink(replyMessage);
|
|
|
|
let msg = `[ ](${ permalink }) `;
|
|
|
|
|
|
|
|
// if original message wasn't sent by current user and neither from a direct room
|
|
|
|
if (username !== replyMessage.u.username && roomType !== 'd' && replyMessage.mention) {
|
|
|
|
msg += `@${ replyMessage.u.username } `;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg = `${ msg } ${ message }`;
|
|
|
|
this.props.onSubmit(msg);
|
|
|
|
closeReply();
|
2018-03-02 21:31:44 +00:00
|
|
|
} else {
|
|
|
|
// if is submiting a new message
|
|
|
|
this.props.onSubmit(message);
|
|
|
|
}
|
2017-11-24 20:44:52 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 12:38:14 +00:00
|
|
|
_getFixedMentions(keyword) {
|
|
|
|
if ('all'.indexOf(keyword) !== -1) {
|
2018-06-01 17:38:13 +00:00
|
|
|
this.users = [{ _id: -1, username: 'all' }, ...this.users];
|
2018-01-19 12:38:14 +00:00
|
|
|
}
|
|
|
|
if ('here'.indexOf(keyword) !== -1) {
|
2018-06-01 17:38:13 +00:00
|
|
|
this.users = [{ _id: -2, username: 'here' }, ...this.users];
|
2018-01-19 12:38:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-20 20:14:07 +00:00
|
|
|
async _getUsers(keyword) {
|
2017-12-27 15:22:06 +00:00
|
|
|
this.users = database.objects('users');
|
2017-12-20 20:14:07 +00:00
|
|
|
if (keyword) {
|
|
|
|
this.users = this.users.filtered('username CONTAINS[c] $0', keyword);
|
|
|
|
}
|
2018-01-19 12:38:14 +00:00
|
|
|
this._getFixedMentions(keyword);
|
2017-12-20 20:14:07 +00:00
|
|
|
this.setState({ mentions: this.users.slice() });
|
|
|
|
|
|
|
|
const usernames = [];
|
|
|
|
|
|
|
|
if (keyword && this.users.length > 7) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.users.forEach(user => usernames.push(user.username));
|
|
|
|
|
|
|
|
if (this.oldPromise) {
|
|
|
|
this.oldPromise();
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const results = await Promise.race([
|
|
|
|
RocketChat.spotlight(keyword, usernames, { users: true }),
|
|
|
|
new Promise((resolve, reject) => (this.oldPromise = reject))
|
|
|
|
]);
|
2018-05-18 17:55:08 +00:00
|
|
|
if (results.users && results.users.length) {
|
|
|
|
database.write(() => {
|
|
|
|
results.users.forEach((user) => {
|
|
|
|
database.create('users', user, true);
|
|
|
|
});
|
2017-12-20 20:14:07 +00:00
|
|
|
});
|
2018-05-18 17:55:08 +00:00
|
|
|
}
|
2017-12-20 20:14:07 +00:00
|
|
|
} catch (e) {
|
2018-04-24 19:34:03 +00:00
|
|
|
console.warn('spotlight canceled');
|
2017-12-20 20:14:07 +00:00
|
|
|
} finally {
|
|
|
|
delete this.oldPromise;
|
2018-01-19 12:38:14 +00:00
|
|
|
this.users = database.objects('users').filtered('username CONTAINS[c] $0', keyword).slice();
|
|
|
|
this._getFixedMentions(keyword);
|
|
|
|
this.setState({ mentions: this.users });
|
2017-12-20 20:14:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async _getRooms(keyword = '') {
|
|
|
|
this.roomsCache = this.roomsCache || [];
|
2017-12-27 15:22:06 +00:00
|
|
|
this.rooms = database.objects('subscriptions')
|
|
|
|
.filtered('t != $0', 'd');
|
2017-12-20 20:14:07 +00:00
|
|
|
if (keyword) {
|
|
|
|
this.rooms = this.rooms.filtered('name CONTAINS[c] $0', keyword);
|
|
|
|
}
|
|
|
|
|
|
|
|
const rooms = [];
|
|
|
|
this.rooms.forEach(room => rooms.push(room));
|
|
|
|
|
|
|
|
this.roomsCache.forEach((room) => {
|
|
|
|
if (room.name && room.name.toUpperCase().indexOf(keyword.toUpperCase()) !== -1) {
|
|
|
|
rooms.push(room);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (rooms.length > 3) {
|
|
|
|
this.setState({ mentions: rooms });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.oldPromise) {
|
|
|
|
this.oldPromise();
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const results = await Promise.race([
|
|
|
|
RocketChat.spotlight(keyword, [...rooms, ...this.roomsCache].map(r => r.name), { rooms: true }),
|
|
|
|
new Promise((resolve, reject) => (this.oldPromise = reject))
|
|
|
|
]);
|
2018-05-18 17:55:08 +00:00
|
|
|
if (results.rooms && results.rooms.length) {
|
|
|
|
this.roomsCache = [...this.roomsCache, ...results.rooms].filter(onlyUnique);
|
|
|
|
}
|
2017-12-20 20:14:07 +00:00
|
|
|
this.setState({ mentions: [...rooms.slice(), ...results.rooms] });
|
|
|
|
} catch (e) {
|
2018-04-24 19:34:03 +00:00
|
|
|
console.warn('spotlight canceled');
|
2017-12-20 20:14:07 +00:00
|
|
|
} finally {
|
|
|
|
delete this.oldPromise;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-30 19:48:26 +00:00
|
|
|
_getEmojis(keyword) {
|
|
|
|
if (keyword) {
|
|
|
|
this.customEmojis = database.objects('customEmojis').filtered('name CONTAINS[c] $0', keyword).slice(0, 4);
|
|
|
|
this.emojis = emojis.filter(emoji => emoji.indexOf(keyword) !== -1).slice(0, 4);
|
|
|
|
const mergedEmojis = [...this.customEmojis, ...this.emojis];
|
|
|
|
this.setState({ mentions: mergedEmojis });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-20 20:14:07 +00:00
|
|
|
stopTrackingMention() {
|
|
|
|
this.setState({
|
2018-01-30 19:48:26 +00:00
|
|
|
mentions: [],
|
|
|
|
trackingType: ''
|
2017-12-20 20:14:07 +00:00
|
|
|
});
|
|
|
|
this.users = [];
|
|
|
|
this.rooms = [];
|
2018-01-30 19:48:26 +00:00
|
|
|
this.customEmojis = [];
|
|
|
|
this.emojis = [];
|
2017-12-20 20:14:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
identifyMentionKeyword(keyword, type) {
|
|
|
|
this.setState({
|
2018-02-08 14:08:50 +00:00
|
|
|
showEmojiKeyboard: false,
|
2018-01-30 19:48:26 +00:00
|
|
|
trackingType: type
|
2017-12-20 20:14:07 +00:00
|
|
|
});
|
2018-01-30 19:48:26 +00:00
|
|
|
this.updateMentions(keyword, type);
|
2017-12-20 20:14:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
updateMentions = (keyword, type) => {
|
|
|
|
if (type === MENTIONS_TRACKING_TYPE_USERS) {
|
|
|
|
this._getUsers(keyword);
|
2018-01-30 19:48:26 +00:00
|
|
|
} else if (type === MENTIONS_TRACKING_TYPE_EMOJIS) {
|
|
|
|
this._getEmojis(keyword);
|
2017-12-20 20:14:07 +00:00
|
|
|
} else {
|
|
|
|
this._getRooms(keyword);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_onPressMention(item) {
|
|
|
|
const msg = this.component._lastNativeText;
|
|
|
|
|
|
|
|
const { start, end } = this.component._lastNativeSelection;
|
|
|
|
|
|
|
|
const cursor = Math.max(start, end);
|
|
|
|
|
2018-01-30 19:48:26 +00:00
|
|
|
const regexp = /([a-z0-9._-]+)$/im;
|
2017-12-20 20:14:07 +00:00
|
|
|
|
|
|
|
const result = msg.substr(0, cursor).replace(regexp, '');
|
2018-01-30 19:48:26 +00:00
|
|
|
const mentionName = this.state.trackingType === MENTIONS_TRACKING_TYPE_EMOJIS ?
|
|
|
|
`${ item.name || item }:` : (item.username || item.name);
|
|
|
|
const text = `${ result }${ mentionName } ${ msg.slice(cursor) }`;
|
2017-12-20 20:14:07 +00:00
|
|
|
this.component.setNativeProps({ text });
|
|
|
|
this.setState({ text });
|
|
|
|
this.component.focus();
|
|
|
|
requestAnimationFrame(() => this.stopTrackingMention());
|
|
|
|
}
|
2018-02-08 14:08:50 +00:00
|
|
|
_onEmojiSelected(keyboardId, params) {
|
2018-01-16 18:48:05 +00:00
|
|
|
const { text } = this.state;
|
2018-02-08 14:08:50 +00:00
|
|
|
const { emoji } = params;
|
2018-01-16 18:48:05 +00:00
|
|
|
let newText = '';
|
|
|
|
|
|
|
|
// if messagebox has an active cursor
|
|
|
|
if (this.component._lastNativeSelection) {
|
|
|
|
const { start, end } = this.component._lastNativeSelection;
|
|
|
|
const cursor = Math.max(start, end);
|
|
|
|
newText = `${ text.substr(0, cursor) }${ emoji }${ text.substr(cursor) }`;
|
|
|
|
} else {
|
|
|
|
// if messagebox doesn't have a cursor, just append selected emoji
|
|
|
|
newText = `${ text }${ emoji }`;
|
|
|
|
}
|
|
|
|
this.component.setNativeProps({ text: newText });
|
|
|
|
this.setState({ text: newText });
|
|
|
|
}
|
2018-01-19 12:38:14 +00:00
|
|
|
renderFixedMentionItem = item => (
|
2017-12-20 20:14:07 +00:00
|
|
|
<TouchableOpacity
|
|
|
|
style={styles.mentionItem}
|
|
|
|
onPress={() => this._onPressMention(item)}
|
|
|
|
>
|
2018-01-19 12:38:14 +00:00
|
|
|
<Text style={styles.fixedMentionAvatar}>{item.username}</Text>
|
2018-06-01 17:38:13 +00:00
|
|
|
<Text>{item.username === 'here' ? I18n.t('Notify_active_in_this_room') : I18n.t('Notify_all_in_this_room')}</Text>
|
2017-12-20 20:14:07 +00:00
|
|
|
</TouchableOpacity>
|
|
|
|
)
|
2018-01-30 19:48:26 +00:00
|
|
|
renderMentionEmoji = (item) => {
|
|
|
|
if (item.name) {
|
|
|
|
return (
|
|
|
|
<CustomEmoji
|
|
|
|
key='mention-item-avatar'
|
2018-02-08 14:08:50 +00:00
|
|
|
style={styles.mentionItemCustomEmoji}
|
2018-01-30 19:48:26 +00:00
|
|
|
emoji={item}
|
|
|
|
baseUrl={this.props.baseUrl}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<Text
|
|
|
|
key='mention-item-avatar'
|
2018-02-08 14:08:50 +00:00
|
|
|
style={styles.mentionItemEmoji}
|
2018-01-30 19:48:26 +00:00
|
|
|
>
|
|
|
|
{emojify(`:${ item }:`, { output: 'unicode' })}
|
|
|
|
</Text>
|
|
|
|
);
|
|
|
|
}
|
2018-01-19 12:38:14 +00:00
|
|
|
renderMentionItem = (item) => {
|
|
|
|
if (item.username === 'all' || item.username === 'here') {
|
|
|
|
return this.renderFixedMentionItem(item);
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<TouchableOpacity
|
|
|
|
style={styles.mentionItem}
|
|
|
|
onPress={() => this._onPressMention(item)}
|
2018-05-23 13:39:18 +00:00
|
|
|
testID={`mention-item-${ this.state.trackingType === MENTIONS_TRACKING_TYPE_EMOJIS ? item.name || item : item.username || item.name }`}
|
2018-01-19 12:38:14 +00:00
|
|
|
>
|
2018-01-30 19:48:26 +00:00
|
|
|
{this.state.trackingType === MENTIONS_TRACKING_TYPE_EMOJIS ?
|
|
|
|
[
|
|
|
|
this.renderMentionEmoji(item),
|
|
|
|
<Text key='mention-item-name'>:{ item.name || item }:</Text>
|
|
|
|
]
|
|
|
|
: [
|
|
|
|
<Avatar
|
|
|
|
key='mention-item-avatar'
|
|
|
|
style={{ margin: 8 }}
|
|
|
|
text={item.username || item.name}
|
|
|
|
size={30}
|
2018-06-13 01:33:00 +00:00
|
|
|
type={item.username ? 'd' : 'c'}
|
2018-01-30 19:48:26 +00:00
|
|
|
/>,
|
|
|
|
<Text key='mention-item-name'>{ item.username || item.name }</Text>
|
|
|
|
]
|
|
|
|
}
|
2018-01-19 12:38:14 +00:00
|
|
|
</TouchableOpacity>
|
|
|
|
);
|
|
|
|
}
|
2018-05-18 17:55:08 +00:00
|
|
|
renderMentions = () => {
|
|
|
|
const { mentions, trackingType } = this.state;
|
|
|
|
if (!trackingType) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (
|
2018-05-23 13:39:18 +00:00
|
|
|
<View key='messagebox-container' testID='messagebox-container'>
|
|
|
|
<FlatList
|
|
|
|
style={styles.mentionList}
|
|
|
|
data={mentions}
|
|
|
|
renderItem={({ item }) => this.renderMentionItem(item)}
|
2018-06-13 01:33:00 +00:00
|
|
|
keyExtractor={item => item._id || item.username || item}
|
2018-05-23 13:39:18 +00:00
|
|
|
keyboardShouldPersistTaps='always'
|
|
|
|
/>
|
|
|
|
</View>
|
2018-05-18 17:55:08 +00:00
|
|
|
);
|
|
|
|
};
|
2018-02-08 14:08:50 +00:00
|
|
|
|
2018-07-20 19:54:46 +00:00
|
|
|
renderReplyPreview = () => {
|
|
|
|
const { replyMessage, replying, closeReply } = this.props;
|
|
|
|
if (!replying) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return <ReplyPreview key='reply-preview' message={replyMessage} close={closeReply} />;
|
|
|
|
};
|
|
|
|
|
2018-07-17 19:10:27 +00:00
|
|
|
renderFilesActions = () => {
|
|
|
|
if (!this.state.showFilesAction) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<FilesActions
|
|
|
|
key='files-actions'
|
|
|
|
hideActions={this.toggleFilesActions}
|
|
|
|
takePhoto={this.takePhoto}
|
|
|
|
chooseFromLibrary={this.chooseFromLibrary}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-02-08 14:08:50 +00:00
|
|
|
renderContent() {
|
2018-03-07 00:17:20 +00:00
|
|
|
if (this.state.recording) {
|
|
|
|
return (<Recording onFinish={this.finishAudioMessage} />);
|
|
|
|
}
|
2017-08-09 13:12:00 +00:00
|
|
|
return (
|
2018-02-08 14:08:50 +00:00
|
|
|
[
|
|
|
|
this.renderMentions(),
|
2018-07-20 19:54:46 +00:00
|
|
|
this.renderReplyPreview(),
|
2018-05-23 13:39:18 +00:00
|
|
|
<View
|
|
|
|
key='messagebox'
|
|
|
|
style={[styles.textArea, this.props.editing && styles.editing]}
|
|
|
|
testID='messagebox'
|
|
|
|
>
|
2018-04-10 13:03:54 +00:00
|
|
|
{this.leftButtons}
|
|
|
|
<TextInput
|
|
|
|
ref={component => this.component = component}
|
|
|
|
style={styles.textBoxInput}
|
|
|
|
returnKeyType='default'
|
|
|
|
keyboardType='twitter'
|
|
|
|
blurOnSubmit={false}
|
2018-06-01 17:38:13 +00:00
|
|
|
placeholder={I18n.t('New_Message')}
|
2018-04-10 13:03:54 +00:00
|
|
|
onChangeText={text => this.onChangeText(text)}
|
|
|
|
value={this.state.text}
|
|
|
|
underlineColorAndroid='transparent'
|
|
|
|
defaultValue=''
|
|
|
|
multiline
|
|
|
|
placeholderTextColor='#9EA2A8'
|
2018-05-23 13:39:18 +00:00
|
|
|
testID='messagebox-input'
|
2018-04-10 13:03:54 +00:00
|
|
|
/>
|
|
|
|
{this.rightButtons}
|
|
|
|
</View>
|
2018-02-08 14:08:50 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2018-07-17 19:10:27 +00:00
|
|
|
[
|
|
|
|
<KeyboardAccessoryView
|
|
|
|
key='input'
|
|
|
|
renderContent={() => this.renderContent()}
|
|
|
|
kbInputRef={this.component}
|
|
|
|
kbComponent={this.state.showEmojiKeyboard ? 'EmojiKeyboard' : null}
|
|
|
|
onKeyboardResigned={() => this.onKeyboardResigned()}
|
|
|
|
onItemSelected={this._onEmojiSelected}
|
|
|
|
trackInteractive
|
|
|
|
// revealKeyboardInteractive
|
|
|
|
requiresSameParentToManageScrollView
|
|
|
|
addBottomView
|
|
|
|
/>,
|
|
|
|
this.renderFilesActions(),
|
|
|
|
<UploadModal
|
|
|
|
key='upload-modal'
|
|
|
|
isVisible={(this.state.file && this.state.file.isVisible)}
|
|
|
|
file={this.state.file}
|
|
|
|
close={() => this.setState({ file: {} })}
|
|
|
|
submit={this.sendImageMessage}
|
|
|
|
/>
|
|
|
|
]
|
2017-08-09 13:12:00 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|