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';
|
2018-09-25 19:28:42 +00:00
|
|
|
import {
|
2018-09-27 11:43:19 +00:00
|
|
|
View, TextInput, FlatList, Text, TouchableOpacity, Alert, Image
|
2018-09-25 19:28:42 +00:00
|
|
|
} 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-09-27 11:43:19 +00:00
|
|
|
import { BorderlessButton } from 'react-native-gesture-handler';
|
2018-12-21 10:55:35 +00:00
|
|
|
import equal from 'deep-equal';
|
2018-03-07 00:17:20 +00:00
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
import { userTyping as userTypingAction } from '../../actions/room';
|
|
|
|
import {
|
|
|
|
editRequest as editRequestAction,
|
|
|
|
editCancel as editCancelAction,
|
|
|
|
replyCancel as replyCancelAction
|
|
|
|
} from '../../actions/messages';
|
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';
|
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,
|
2018-11-19 18:18:15 +00:00
|
|
|
avoidEmptySpaceAroundImage: false,
|
2018-07-17 19:10:27 +00:00
|
|
|
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 => ({
|
2018-09-25 19:28:42 +00:00
|
|
|
editCancel: () => dispatch(editCancelAction()),
|
|
|
|
editRequest: message => dispatch(editRequestAction(message)),
|
|
|
|
typing: status => dispatch(userTypingAction(status)),
|
|
|
|
closeReply: () => dispatch(replyCancelAction())
|
2017-11-21 14:55:50 +00:00
|
|
|
}))
|
2018-12-21 10:55:35 +00:00
|
|
|
export default 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
|
|
|
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
|
|
|
mentions: [],
|
2018-02-08 14:08:50 +00:00
|
|
|
showEmojiKeyboard: false,
|
2018-07-17 19:10:27 +00:00
|
|
|
showFilesAction: false,
|
2018-11-05 19:03:17 +00:00
|
|
|
showSend: false,
|
2018-07-17 19:10:27 +00:00
|
|
|
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-09-25 19:28:42 +00:00
|
|
|
this.onEmojiSelected = this.onEmojiSelected.bind(this);
|
2018-11-05 19:03:17 +00:00
|
|
|
this.text = '';
|
2017-11-28 16:27:06 +00:00
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2017-11-21 14:55:50 +00:00
|
|
|
componentWillReceiveProps(nextProps) {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { message, replyMessage } = this.props;
|
|
|
|
if (message !== nextProps.message && nextProps.message.msg) {
|
2018-11-05 19:03:17 +00:00
|
|
|
this.setInput(nextProps.message.msg);
|
2017-11-21 14:55:50 +00:00
|
|
|
this.component.focus();
|
2018-09-25 19:28:42 +00:00
|
|
|
} else if (replyMessage !== nextProps.replyMessage && nextProps.replyMessage.msg) {
|
2018-07-20 19:54:46 +00:00
|
|
|
this.component.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
|
|
|
}
|
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 {
|
|
|
|
showEmojiKeyboard, showFilesAction, showSend, recording, mentions, file
|
|
|
|
} = this.state;
|
|
|
|
const {
|
|
|
|
roomType, replying, editing
|
|
|
|
} = this.props;
|
|
|
|
if (nextProps.roomType !== roomType) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextProps.replying !== replying) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextProps.editing !== editing) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextState.showEmojiKeyboard !== showEmojiKeyboard) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextState.showFilesAction !== showFilesAction) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextState.showSend !== showSend) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextState.recording !== recording) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!equal(nextState.mentions, mentions)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!equal(nextState.file, file)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-25 14:04:20 +00:00
|
|
|
onChangeText(text) {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { typing } = this.props;
|
|
|
|
|
2018-11-05 19:03:17 +00:00
|
|
|
this.setInput(text);
|
2018-09-25 19:28:42 +00:00
|
|
|
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;
|
2018-01-30 19:48:26 +00:00
|
|
|
const regexp = /(#|@|:)([a-z0-9._-]+)$/im;
|
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();
|
|
|
|
}
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
onPressMention(item) {
|
|
|
|
const { trackingType } = this.state;
|
2018-11-05 19:03:17 +00:00
|
|
|
const msg = this.text;
|
2018-09-25 19:28:42 +00:00
|
|
|
const { start, end } = this.component._lastNativeSelection;
|
|
|
|
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 }:`
|
|
|
|
: (item.username || item.name);
|
|
|
|
const text = `${ result }${ mentionName } ${ msg.slice(cursor) }`;
|
2018-11-05 19:03:17 +00:00
|
|
|
this.setInput(text);
|
2018-09-25 19:28:42 +00:00
|
|
|
this.component.focus();
|
|
|
|
requestAnimationFrame(() => this.stopTrackingMention());
|
|
|
|
}
|
|
|
|
|
|
|
|
onEmojiSelected(keyboardId, params) {
|
2018-11-05 19:03:17 +00:00
|
|
|
const { text } = this;
|
2018-09-25 19:28:42 +00:00
|
|
|
const { emoji } = params;
|
|
|
|
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 }`;
|
|
|
|
}
|
2018-11-05 19:03:17 +00:00
|
|
|
this.setInput(newText);
|
2018-09-25 19:28:42 +00:00
|
|
|
}
|
|
|
|
|
2017-12-08 19:36:03 +00:00
|
|
|
get leftButtons() {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { showEmojiKeyboard } = this.state;
|
2017-11-24 17:21:21 +00:00
|
|
|
const { editing } = this.props;
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2017-11-21 14:55:50 +00:00
|
|
|
if (editing) {
|
2018-09-25 19:28:42 +00:00
|
|
|
return (
|
2018-09-27 11:43:19 +00:00
|
|
|
<BorderlessButton
|
|
|
|
onPress={this.editCancel}
|
2018-09-25 19:28:42 +00:00
|
|
|
accessibilityLabel={I18n.t('Cancel_editing')}
|
|
|
|
accessibilityTraits='button'
|
2018-09-27 11:43:19 +00:00
|
|
|
style={styles.actionButton}
|
2018-09-25 19:28:42 +00:00
|
|
|
testID='messagebox-cancel-editing'
|
2018-09-27 11:43:19 +00:00
|
|
|
>
|
|
|
|
<Icon
|
|
|
|
size={22}
|
|
|
|
color='#1d74f5'
|
|
|
|
name='close'
|
|
|
|
/>
|
|
|
|
</BorderlessButton>
|
2018-09-25 19:28:42 +00:00
|
|
|
);
|
2017-08-22 01:24:41 +00:00
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
return !showEmojiKeyboard
|
|
|
|
? (
|
2018-09-27 11:43:19 +00:00
|
|
|
<BorderlessButton
|
|
|
|
onPress={this.openEmoji}
|
2018-09-25 19:28:42 +00:00
|
|
|
accessibilityLabel={I18n.t('Open_emoji_selector')}
|
|
|
|
accessibilityTraits='button'
|
2018-09-27 11:43:19 +00:00
|
|
|
style={styles.actionButton}
|
2018-09-25 19:28:42 +00:00
|
|
|
testID='messagebox-open-emoji'
|
2018-09-27 11:43:19 +00:00
|
|
|
>
|
|
|
|
<Icon
|
|
|
|
size={22}
|
|
|
|
color='#1d74f5'
|
|
|
|
name='mood'
|
|
|
|
/>
|
|
|
|
</BorderlessButton>
|
|
|
|
)
|
2018-09-25 19:28:42 +00:00
|
|
|
: (
|
2018-09-27 11:43:19 +00:00
|
|
|
<BorderlessButton
|
|
|
|
onPress={this.closeEmoji}
|
2018-09-25 19:28:42 +00:00
|
|
|
accessibilityLabel={I18n.t('Close_emoji_selector')}
|
|
|
|
accessibilityTraits='button'
|
2018-09-27 11:43:19 +00:00
|
|
|
style={styles.actionButton}
|
2018-09-25 19:28:42 +00:00
|
|
|
testID='messagebox-close-emoji'
|
2018-09-27 11:43:19 +00:00
|
|
|
>
|
|
|
|
<Icon
|
|
|
|
size={22}
|
|
|
|
color='#1d74f5'
|
|
|
|
name='keyboard'
|
|
|
|
/>
|
|
|
|
</BorderlessButton>
|
|
|
|
);
|
2017-12-08 19:36:03 +00:00
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2017-12-08 19:36:03 +00:00
|
|
|
get rightButtons() {
|
2018-11-05 19:03:17 +00:00
|
|
|
const { showSend } = this.state;
|
2017-12-08 19:36:03 +00:00
|
|
|
const icons = [];
|
|
|
|
|
2018-11-05 19:03:17 +00:00
|
|
|
if (showSend) {
|
2018-09-27 11:43:19 +00:00
|
|
|
icons.push(
|
|
|
|
<BorderlessButton
|
|
|
|
key='send-message'
|
2018-11-05 19:03:17 +00:00
|
|
|
onPress={() => this.submit()}
|
2018-09-27 11:43:19 +00:00
|
|
|
style={styles.actionButton}
|
|
|
|
testID='messagebox-send-message'
|
|
|
|
accessibilityLabel={I18n.t('Send message')}
|
|
|
|
accessibilityTraits='button'
|
|
|
|
>
|
|
|
|
<Image source={{ uri: 'composer_send' }} style={{ width: 23, height: 23 }} />
|
|
|
|
</BorderlessButton>
|
|
|
|
);
|
2018-03-07 00:17:20 +00:00
|
|
|
return icons;
|
2017-12-08 19:36:03 +00:00
|
|
|
}
|
2018-09-27 11:43:19 +00:00
|
|
|
icons.push(
|
|
|
|
<BorderlessButton
|
|
|
|
key='audio-message'
|
|
|
|
onPress={this.recordAudioMessage}
|
|
|
|
style={styles.actionButton}
|
|
|
|
testID='messagebox-send-audio'
|
|
|
|
accessibilityLabel={I18n.t('Send audio message')}
|
|
|
|
accessibilityTraits='button'
|
|
|
|
>
|
|
|
|
<Image source={{ uri: 'composer_mic' }} style={{ width: 16, height: 23 }} />
|
|
|
|
</BorderlessButton>
|
|
|
|
);
|
|
|
|
icons.push(
|
|
|
|
<BorderlessButton
|
|
|
|
key='file-message'
|
|
|
|
onPress={this.toggleFilesActions}
|
|
|
|
style={styles.actionButton}
|
|
|
|
testID='messagebox-actions'
|
|
|
|
accessibilityLabel={I18n.t('Message actions')}
|
|
|
|
accessibilityTraits='button'
|
|
|
|
>
|
|
|
|
<Image source={{ uri: 'composer_plus' }} style={{ width: 18, height: 18 }} />
|
|
|
|
</BorderlessButton>
|
|
|
|
);
|
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-09-25 19:28:42 +00:00
|
|
|
getFixedMentions(keyword) {
|
|
|
|
if ('all'.indexOf(keyword) !== -1) {
|
|
|
|
this.users = [{ _id: -1, username: 'all' }, ...this.users];
|
|
|
|
}
|
|
|
|
if ('here'.indexOf(keyword) !== -1) {
|
|
|
|
this.users = [{ _id: -2, username: 'here' }, ...this.users];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async getUsers(keyword) {
|
|
|
|
this.users = database.objects('users');
|
|
|
|
if (keyword) {
|
|
|
|
this.users = this.users.filtered('username CONTAINS[c] $0', keyword);
|
|
|
|
}
|
|
|
|
this.getFixedMentions(keyword);
|
|
|
|
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))
|
|
|
|
]);
|
|
|
|
if (results.users && results.users.length) {
|
|
|
|
database.write(() => {
|
|
|
|
results.users.forEach((user) => {
|
|
|
|
database.create('users', user, true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.warn('spotlight canceled');
|
|
|
|
} finally {
|
|
|
|
delete this.oldPromise;
|
|
|
|
this.users = database.objects('users').filtered('username CONTAINS[c] $0', keyword).slice();
|
|
|
|
this.getFixedMentions(keyword);
|
|
|
|
this.setState({ mentions: this.users });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async getRooms(keyword = '') {
|
|
|
|
this.roomsCache = this.roomsCache || [];
|
|
|
|
this.rooms = database.objects('subscriptions')
|
|
|
|
.filtered('t != $0', 'd');
|
|
|
|
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))
|
|
|
|
]);
|
|
|
|
if (results.rooms && results.rooms.length) {
|
|
|
|
this.roomsCache = [...this.roomsCache, ...results.rooms].filter(onlyUnique);
|
|
|
|
}
|
|
|
|
this.setState({ mentions: [...rooms.slice(), ...results.rooms] });
|
|
|
|
} catch (e) {
|
|
|
|
console.warn('spotlight canceled');
|
|
|
|
} finally {
|
|
|
|
delete this.oldPromise;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-05 19:03:17 +00:00
|
|
|
setInput = (text) => {
|
|
|
|
this.text = text;
|
|
|
|
this.component.setNativeProps({ text });
|
|
|
|
this.setState({ showSend: text.length > 0 });
|
|
|
|
}
|
|
|
|
|
|
|
|
clearInput = () => {
|
|
|
|
this.setInput('');
|
|
|
|
}
|
|
|
|
|
2018-07-17 19:10:27 +00:00
|
|
|
toggleFilesActions = () => {
|
|
|
|
this.setState(prevState => ({ showFilesAction: !prevState.showFilesAction }));
|
|
|
|
}
|
|
|
|
|
|
|
|
sendImageMessage = async(file) => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { rid } = this.props;
|
|
|
|
|
2018-07-17 19:10:27 +00:00
|
|
|
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 {
|
2018-09-25 19:28:42 +00:00
|
|
|
await RocketChat.sendFileMessage(rid, fileInfo);
|
2018-07-17 19:10:27 +00:00
|
|
|
} 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 } });
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
openEmoji = async() => {
|
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
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
recordAudioMessage = async() => {
|
2018-03-07 00:17:20 +00:00
|
|
|
const recording = await Recording.permission();
|
|
|
|
this.setState({ recording });
|
|
|
|
}
|
|
|
|
|
|
|
|
finishAudioMessage = async(fileInfo) => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { rid } = this.props;
|
|
|
|
|
2018-03-07 00:17:20 +00:00
|
|
|
this.setState({
|
|
|
|
recording: false
|
|
|
|
});
|
2018-05-29 17:10:40 +00:00
|
|
|
if (fileInfo) {
|
|
|
|
try {
|
2018-09-25 19:28:42 +00:00
|
|
|
await RocketChat.sendFileMessage(rid, fileInfo);
|
2018-05-29 17:10:40 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (e && e.error === 'error-file-too-large') {
|
2018-09-19 14:18:32 +00:00
|
|
|
return Alert.alert(I18n.t(e.error));
|
2018-05-29 17:10:40 +00:00
|
|
|
}
|
|
|
|
log('finishAudioMessage', e);
|
|
|
|
}
|
|
|
|
}
|
2018-03-07 00:17:20 +00:00
|
|
|
}
|
|
|
|
|
2018-09-25 19:28:42 +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-11-05 19:03:17 +00:00
|
|
|
submit = async() => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const {
|
|
|
|
typing, message: editingMessage, editRequest, onSubmit
|
|
|
|
} = this.props;
|
2018-11-05 19:03:17 +00:00
|
|
|
const message = this.text;
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2018-11-05 19:03:17 +00:00
|
|
|
this.clearInput();
|
2018-01-16 18:48:05 +00:00
|
|
|
this.closeEmoji();
|
2017-12-20 20:14:07 +00:00
|
|
|
this.stopTrackingMention();
|
2018-09-25 19:28:42 +00:00
|
|
|
typing(false);
|
2018-03-02 21:31:44 +00:00
|
|
|
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) {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { _id, rid } = editingMessage;
|
|
|
|
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 }`;
|
2018-09-25 19:28:42 +00:00
|
|
|
onSubmit(msg);
|
2018-07-20 19:54:46 +00:00
|
|
|
closeReply();
|
2018-03-02 21:31:44 +00:00
|
|
|
} else {
|
|
|
|
// if is submiting a new message
|
2018-09-25 19:28:42 +00:00
|
|
|
onSubmit(message);
|
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);
|
|
|
|
} else {
|
|
|
|
this.getRooms(keyword);
|
2017-12-20 20:14:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
identifyMentionKeyword(keyword, type) {
|
|
|
|
this.setState({
|
|
|
|
showEmojiKeyboard: false,
|
|
|
|
trackingType: type
|
2017-12-20 20:14:07 +00:00
|
|
|
});
|
2018-09-25 19:28:42 +00:00
|
|
|
this.updateMentions(keyword, type);
|
2018-01-30 19:48:26 +00:00
|
|
|
}
|
|
|
|
|
2017-12-20 20:14:07 +00:00
|
|
|
stopTrackingMention() {
|
2018-11-05 19:03:17 +00:00
|
|
|
const { trackingType } = this.state;
|
|
|
|
if (!trackingType) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-20 20:14:07 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-01-19 12:38:14 +00:00
|
|
|
renderFixedMentionItem = item => (
|
2017-12-20 20:14:07 +00:00
|
|
|
<TouchableOpacity
|
|
|
|
style={styles.mentionItem}
|
2018-09-25 19:28:42 +00:00
|
|
|
onPress={() => this.onPressMention(item)}
|
2017-12-20 20:14:07 +00:00
|
|
|
>
|
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-09-25 19:28:42 +00:00
|
|
|
|
2018-01-30 19:48:26 +00:00
|
|
|
renderMentionEmoji = (item) => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { baseUrl } = this.props;
|
|
|
|
|
2018-01-30 19:48:26 +00:00
|
|
|
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}
|
2018-09-25 19:28:42 +00:00
|
|
|
baseUrl={baseUrl}
|
2018-01-30 19:48:26 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
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-09-25 19:28:42 +00:00
|
|
|
|
2018-01-19 12:38:14 +00:00
|
|
|
renderMentionItem = (item) => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { trackingType } = this.state;
|
|
|
|
const { baseUrl } = this.props;
|
|
|
|
|
2018-01-19 12:38:14 +00:00
|
|
|
if (item.username === 'all' || item.username === 'here') {
|
|
|
|
return this.renderFixedMentionItem(item);
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<TouchableOpacity
|
|
|
|
style={styles.mentionItem}
|
2018-09-25 19:28:42 +00:00
|
|
|
onPress={() => this.onPressMention(item)}
|
|
|
|
testID={`mention-item-${ trackingType === MENTIONS_TRACKING_TYPE_EMOJIS ? item.name || item : item.username || item.name }`}
|
2018-01-19 12:38:14 +00:00
|
|
|
>
|
2018-09-25 19:28:42 +00:00
|
|
|
{trackingType === MENTIONS_TRACKING_TYPE_EMOJIS
|
|
|
|
? [
|
2018-01-30 19:48:26 +00:00
|
|
|
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-09-25 19:28:42 +00:00
|
|
|
baseUrl={baseUrl}
|
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-09-25 19:28:42 +00:00
|
|
|
|
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 = () => {
|
2018-09-11 16:32:52 +00:00
|
|
|
const {
|
|
|
|
replyMessage, replying, closeReply, username
|
|
|
|
} = this.props;
|
2018-07-20 19:54:46 +00:00
|
|
|
if (!replying) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-09-11 16:32:52 +00:00
|
|
|
return <ReplyPreview key='reply-preview' message={replyMessage} close={closeReply} username={username} />;
|
2018-07-20 19:54:46 +00:00
|
|
|
};
|
|
|
|
|
2018-07-17 19:10:27 +00:00
|
|
|
renderFilesActions = () => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { showFilesAction } = this.state;
|
|
|
|
|
|
|
|
if (!showFilesAction) {
|
2018-07-17 19:10:27 +00:00
|
|
|
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-11-05 19:03:17 +00:00
|
|
|
const { recording } = this.state;
|
2018-09-25 19:28:42 +00:00
|
|
|
const { editing } = this.props;
|
|
|
|
|
|
|
|
if (recording) {
|
2018-03-07 00:17:20 +00:00
|
|
|
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-09-11 16:32:52 +00:00
|
|
|
<View style={styles.composer} key='messagebox'>
|
|
|
|
{this.renderReplyPreview()}
|
|
|
|
<View
|
2018-09-25 19:28:42 +00:00
|
|
|
style={[styles.textArea, editing && styles.editing]}
|
2018-09-11 16:32:52 +00:00
|
|
|
testID='messagebox'
|
|
|
|
>
|
|
|
|
{this.leftButtons}
|
|
|
|
<TextInput
|
|
|
|
ref={component => this.component = component}
|
|
|
|
style={styles.textBoxInput}
|
|
|
|
returnKeyType='default'
|
|
|
|
keyboardType='twitter'
|
|
|
|
blurOnSubmit={false}
|
|
|
|
placeholder={I18n.t('New_Message')}
|
2018-09-25 19:28:42 +00:00
|
|
|
onChangeText={t => this.onChangeText(t)}
|
2018-09-11 16:32:52 +00:00
|
|
|
underlineColorAndroid='transparent'
|
|
|
|
defaultValue=''
|
|
|
|
multiline
|
2018-09-27 11:43:19 +00:00
|
|
|
placeholderTextColor='#9ea2a8'
|
2018-09-11 16:32:52 +00:00
|
|
|
testID='messagebox-input'
|
|
|
|
/>
|
|
|
|
{this.rightButtons}
|
|
|
|
</View>
|
2018-04-10 13:03:54 +00:00
|
|
|
</View>
|
2018-02-08 14:08:50 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { showEmojiKeyboard, file } = this.state;
|
2018-02-08 14:08:50 +00:00
|
|
|
return (
|
2018-07-17 19:10:27 +00:00
|
|
|
[
|
|
|
|
<KeyboardAccessoryView
|
|
|
|
key='input'
|
|
|
|
renderContent={() => this.renderContent()}
|
|
|
|
kbInputRef={this.component}
|
2018-09-25 19:28:42 +00:00
|
|
|
kbComponent={showEmojiKeyboard ? 'EmojiKeyboard' : null}
|
2018-07-17 19:10:27 +00:00
|
|
|
onKeyboardResigned={() => this.onKeyboardResigned()}
|
2018-09-25 19:28:42 +00:00
|
|
|
onItemSelected={this.onEmojiSelected}
|
2018-07-17 19:10:27 +00:00
|
|
|
trackInteractive
|
|
|
|
// revealKeyboardInteractive
|
|
|
|
requiresSameParentToManageScrollView
|
|
|
|
addBottomView
|
|
|
|
/>,
|
|
|
|
this.renderFilesActions(),
|
|
|
|
<UploadModal
|
|
|
|
key='upload-modal'
|
2018-09-25 19:28:42 +00:00
|
|
|
isVisible={(file && file.isVisible)}
|
|
|
|
file={file}
|
2018-07-17 19:10:27 +00:00
|
|
|
close={() => this.setState({ file: {} })}
|
|
|
|
submit={this.sendImageMessage}
|
|
|
|
/>
|
|
|
|
]
|
2017-08-09 13:12:00 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|