2017-11-21 14:55:50 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-04-24 19:34:03 +00:00
|
|
|
import { View, Text, TouchableOpacity, Vibration, ViewPropTypes } from 'react-native';
|
2017-11-21 14:55:50 +00:00
|
|
|
import { connect } from 'react-redux';
|
2017-12-13 15:00:26 +00:00
|
|
|
import Icon from 'react-native-vector-icons/MaterialIcons';
|
2017-12-11 20:37:33 +00:00
|
|
|
import moment from 'moment';
|
2018-03-02 21:31:44 +00:00
|
|
|
import equal from 'deep-equal';
|
2018-02-08 14:08:50 +00:00
|
|
|
import { KeyboardUtils } from 'react-native-keyboard-input';
|
2017-11-21 14:55:50 +00:00
|
|
|
|
2017-12-02 13:19:58 +00:00
|
|
|
import Image from './Image';
|
2017-11-21 14:55:50 +00:00
|
|
|
import User from './User';
|
|
|
|
import Avatar from '../Avatar';
|
2017-12-02 13:19:58 +00:00
|
|
|
import Audio from './Audio';
|
|
|
|
import Video from './Video';
|
|
|
|
import Markdown from './Markdown';
|
|
|
|
import Url from './Url';
|
|
|
|
import Reply from './Reply';
|
2018-01-30 19:48:26 +00:00
|
|
|
import ReactionsModal from './ReactionsModal';
|
|
|
|
import Emoji from './Emoji';
|
2018-01-09 17:12:55 +00:00
|
|
|
import styles from './styles';
|
2018-05-24 20:17:45 +00:00
|
|
|
import { actionsShow, errorActionsShow, toggleReactionPicker, replyBroadcast } from '../../actions/messages';
|
2018-04-24 19:34:03 +00:00
|
|
|
import messagesStatus from '../../constants/messagesStatus';
|
|
|
|
import Touch from '../../utils/touch';
|
|
|
|
|
2018-05-24 20:17:45 +00:00
|
|
|
const SYSTEM_MESSAGES = [
|
|
|
|
'r',
|
|
|
|
'au',
|
|
|
|
'ru',
|
|
|
|
'ul',
|
|
|
|
'uj',
|
|
|
|
'rm',
|
|
|
|
'user-muted',
|
|
|
|
'user-unmuted',
|
|
|
|
'message_pinned',
|
|
|
|
'subscription-role-added',
|
|
|
|
'subscription-role-removed',
|
|
|
|
'room_changed_description',
|
|
|
|
'room_changed_announcement',
|
|
|
|
'room_changed_topic',
|
|
|
|
'room_changed_privacy'
|
|
|
|
];
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
const getInfoMessage = ({
|
|
|
|
t, role, msg, u
|
|
|
|
}) => {
|
|
|
|
if (t === 'rm') {
|
|
|
|
return 'Message removed';
|
|
|
|
} else if (t === 'uj') {
|
|
|
|
return 'Has joined the channel.';
|
|
|
|
} else if (t === 'r') {
|
|
|
|
return `Room name changed to: ${ msg } by ${ u.username }`;
|
|
|
|
} else if (t === 'message_pinned') {
|
|
|
|
return 'Message pinned';
|
|
|
|
} else if (t === 'ul') {
|
|
|
|
return 'Has left the channel.';
|
|
|
|
} else if (t === 'ru') {
|
|
|
|
return `User ${ msg } removed by ${ u.username }`;
|
|
|
|
} else if (t === 'au') {
|
|
|
|
return `User ${ msg } added by ${ u.username }`;
|
|
|
|
} else if (t === 'user-muted') {
|
|
|
|
return `User ${ msg } muted by ${ u.username }`;
|
|
|
|
} else if (t === 'user-unmuted') {
|
|
|
|
return `User ${ msg } unmuted by ${ u.username }`;
|
|
|
|
} else if (t === 'subscription-role-added') {
|
|
|
|
return `${ msg } was set ${ role } by ${ u.username }`;
|
|
|
|
} else if (t === 'subscription-role-removed') {
|
|
|
|
return `${ msg } is no longer ${ role } by ${ u.username }`;
|
|
|
|
} else if (t === 'room_changed_description') {
|
|
|
|
return `Room description changed to: ${ msg } by ${ u.username }`;
|
|
|
|
} else if (t === 'room_changed_announcement') {
|
|
|
|
return `Room announcement changed to: ${ msg } by ${ u.username }`;
|
|
|
|
} else if (t === 'room_changed_topic') {
|
|
|
|
return `Room topic changed to: ${ msg } by ${ u.username }`;
|
|
|
|
} else if (t === 'room_changed_privacy') {
|
|
|
|
return `Room type changed to: ${ msg } by ${ u.username }`;
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
};
|
2018-01-09 17:12:55 +00:00
|
|
|
|
2017-11-21 14:55:50 +00:00
|
|
|
@connect(state => ({
|
|
|
|
message: state.messages.message,
|
2018-01-16 18:48:05 +00:00
|
|
|
editing: state.messages.editing,
|
2018-04-24 19:34:03 +00:00
|
|
|
customEmojis: state.customEmojis,
|
|
|
|
Message_TimeFormat: state.settings.Message_TimeFormat,
|
|
|
|
Message_GroupingPeriod: state.settings.Message_GroupingPeriod
|
2017-11-21 14:55:50 +00:00
|
|
|
}), dispatch => ({
|
2017-12-13 15:00:26 +00:00
|
|
|
actionsShow: actionMessage => dispatch(actionsShow(actionMessage)),
|
2018-01-30 19:48:26 +00:00
|
|
|
errorActionsShow: actionMessage => dispatch(errorActionsShow(actionMessage)),
|
2018-05-24 20:17:45 +00:00
|
|
|
toggleReactionPicker: message => dispatch(toggleReactionPicker(message)),
|
|
|
|
replyBroadcast: message => dispatch(replyBroadcast(message))
|
2017-11-21 14:55:50 +00:00
|
|
|
}))
|
|
|
|
export default class Message extends React.Component {
|
|
|
|
static propTypes = {
|
2018-03-02 21:31:44 +00:00
|
|
|
status: PropTypes.any,
|
2017-11-21 14:55:50 +00:00
|
|
|
item: PropTypes.object.isRequired,
|
2018-02-19 21:19:39 +00:00
|
|
|
reactions: PropTypes.any.isRequired,
|
2017-11-21 14:55:50 +00:00
|
|
|
Message_TimeFormat: PropTypes.string.isRequired,
|
2018-04-24 19:34:03 +00:00
|
|
|
Message_GroupingPeriod: PropTypes.number.isRequired,
|
|
|
|
customTimeFormat: PropTypes.string,
|
2017-11-24 20:44:52 +00:00
|
|
|
message: PropTypes.object.isRequired,
|
2017-12-02 13:19:58 +00:00
|
|
|
user: PropTypes.object.isRequired,
|
2017-11-24 20:44:52 +00:00
|
|
|
editing: PropTypes.bool,
|
2018-01-09 17:12:55 +00:00
|
|
|
errorActionsShow: PropTypes.func,
|
2018-01-30 19:48:26 +00:00
|
|
|
toggleReactionPicker: PropTypes.func,
|
2018-05-24 20:17:45 +00:00
|
|
|
replyBroadcast: PropTypes.func,
|
2018-02-19 21:19:39 +00:00
|
|
|
onReactionPress: PropTypes.func,
|
|
|
|
style: ViewPropTypes.style,
|
2018-03-02 21:31:44 +00:00
|
|
|
onLongPress: PropTypes.func,
|
2018-03-29 17:55:37 +00:00
|
|
|
_updatedAt: PropTypes.instanceOf(Date),
|
2018-05-24 20:17:45 +00:00
|
|
|
archived: PropTypes.bool,
|
|
|
|
broadcast: PropTypes.bool
|
2018-01-30 19:48:26 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 16:49:51 +00:00
|
|
|
static defaultProps = {
|
|
|
|
onLongPress: () => {},
|
2018-03-29 17:55:37 +00:00
|
|
|
_updatedAt: new Date(),
|
2018-05-24 20:17:45 +00:00
|
|
|
archived: false,
|
|
|
|
broadcast: false
|
2018-03-23 16:49:51 +00:00
|
|
|
}
|
|
|
|
|
2018-01-30 19:48:26 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = { reactionsModal: false };
|
|
|
|
this.onClose = this.onClose.bind(this);
|
2018-01-09 17:12:55 +00:00
|
|
|
}
|
|
|
|
|
2018-03-02 21:31:44 +00:00
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
2018-04-24 19:34:03 +00:00
|
|
|
if (this.state.reactionsModal !== nextState.reactionsModal) {
|
2018-03-02 21:31:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
if (this.props.status !== nextProps.status) {
|
2018-03-02 21:31:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
// eslint-disable-next-line
|
|
|
|
if (!!this.props._updatedAt ^ !!nextProps._updatedAt) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!equal(this.props.reactions, nextProps.reactions)) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-05-24 20:17:45 +00:00
|
|
|
if (this.props.broadcast !== nextProps.broadcast) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
return this.props._updatedAt.toGMTString() !== nextProps._updatedAt.toGMTString();
|
2018-03-02 21:31:44 +00:00
|
|
|
}
|
2017-11-21 14:55:50 +00:00
|
|
|
|
2018-01-30 15:07:09 +00:00
|
|
|
onPress = () => {
|
2018-02-08 14:08:50 +00:00
|
|
|
KeyboardUtils.dismiss();
|
2018-01-30 15:07:09 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
onLongPress = () => {
|
2018-02-19 21:19:39 +00:00
|
|
|
this.props.onLongPress(this.parseMessage());
|
2017-11-21 14:55:50 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
onErrorPress = () => {
|
2018-01-30 19:48:26 +00:00
|
|
|
this.props.errorActionsShow(this.parseMessage());
|
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
onReactionPress = (emoji) => {
|
2018-01-30 19:48:26 +00:00
|
|
|
this.props.onReactionPress(emoji, this.props.item._id);
|
|
|
|
}
|
|
|
|
onClose() {
|
|
|
|
this.setState({ reactionsModal: false });
|
|
|
|
}
|
|
|
|
onReactionLongPress() {
|
|
|
|
this.setState({ reactionsModal: true });
|
|
|
|
Vibration.vibrate(50);
|
2017-12-13 15:00:26 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
get timeFormat() {
|
|
|
|
const { customTimeFormat, Message_TimeFormat } = this.props;
|
|
|
|
return customTimeFormat || Message_TimeFormat;
|
2017-12-02 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
2018-01-30 19:48:26 +00:00
|
|
|
parseMessage = () => JSON.parse(JSON.stringify(this.props.item));
|
|
|
|
|
2017-12-19 15:43:17 +00:00
|
|
|
isInfoMessage() {
|
2018-05-24 20:17:45 +00:00
|
|
|
return SYSTEM_MESSAGES.includes(this.props.item.t);
|
2017-12-19 15:43:17 +00:00
|
|
|
}
|
|
|
|
|
2018-05-24 20:17:45 +00:00
|
|
|
isOwn = () => this.props.item.u && this.props.item.u._id === this.props.user.id;
|
|
|
|
|
2017-12-19 15:43:17 +00:00
|
|
|
isDeleted() {
|
|
|
|
return this.props.item.t === 'rm';
|
2017-11-21 14:55:50 +00:00
|
|
|
}
|
|
|
|
|
2018-03-02 15:11:34 +00:00
|
|
|
isTemp() {
|
2018-04-24 19:34:03 +00:00
|
|
|
return this.props.item.status === messagesStatus.TEMP || this.props.item.status === messagesStatus.ERROR;
|
2018-03-02 15:11:34 +00:00
|
|
|
}
|
|
|
|
|
2017-12-13 15:00:26 +00:00
|
|
|
hasError() {
|
2018-04-24 19:34:03 +00:00
|
|
|
return this.props.item.status === messagesStatus.ERROR;
|
2017-12-13 15:00:26 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
renderHeader = (username) => {
|
|
|
|
const { item, previousItem } = this.props;
|
|
|
|
|
|
|
|
if (previousItem && (
|
|
|
|
(previousItem.ts.toDateString() === item.ts.toDateString()) &&
|
|
|
|
(previousItem.u.username === item.u.username) &&
|
2018-05-24 20:17:45 +00:00
|
|
|
!(previousItem.groupable === false || item.groupable === false || this.props.broadcast === true) &&
|
2018-04-24 19:34:03 +00:00
|
|
|
(previousItem.status === item.status) &&
|
|
|
|
(item.ts - previousItem.ts < this.props.Message_GroupingPeriod * 1000)
|
|
|
|
)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<View style={styles.flex}>
|
|
|
|
<Avatar
|
|
|
|
style={styles.avatar}
|
|
|
|
text={item.avatar ? '' : username}
|
|
|
|
size={20}
|
|
|
|
avatar={item.avatar}
|
|
|
|
/>
|
|
|
|
<User
|
|
|
|
onPress={this._onPress}
|
|
|
|
item={item}
|
|
|
|
Message_TimeFormat={this.timeFormat}
|
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderContent() {
|
|
|
|
if (this.isInfoMessage()) {
|
|
|
|
return <Text style={styles.textInfo}>{getInfoMessage(this.props.item)}</Text>;
|
|
|
|
}
|
|
|
|
const { item } = this.props;
|
|
|
|
return <Markdown msg={item.msg} />;
|
|
|
|
}
|
|
|
|
|
|
|
|
renderAttachment() {
|
2017-12-02 13:19:58 +00:00
|
|
|
if (this.props.item.attachments.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const file = this.props.item.attachments[0];
|
2018-04-24 19:34:03 +00:00
|
|
|
const { user } = this.props;
|
2018-04-26 17:33:43 +00:00
|
|
|
if (file.image_url) {
|
2018-04-24 19:34:03 +00:00
|
|
|
return <Image file={file} user={user} />;
|
2017-12-02 13:19:58 +00:00
|
|
|
}
|
2018-04-26 17:33:43 +00:00
|
|
|
if (file.audio_url) {
|
2018-04-24 19:34:03 +00:00
|
|
|
return <Audio file={file} user={user} />;
|
2017-11-21 14:55:50 +00:00
|
|
|
}
|
2018-04-26 17:33:43 +00:00
|
|
|
if (file.video_url) {
|
2018-04-24 19:34:03 +00:00
|
|
|
return <Video file={file} user={user} />;
|
|
|
|
}
|
|
|
|
|
|
|
|
return <Reply attachment={file} timeFormat={this.timeFormat} />;
|
2017-12-02 13:19:58 +00:00
|
|
|
}
|
2017-11-21 14:55:50 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
renderUrl = () => {
|
|
|
|
const { urls } = this.props.item;
|
|
|
|
if (urls.length === 0) {
|
2017-12-02 13:19:58 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
return urls.map(url => (
|
2018-02-23 20:29:06 +00:00
|
|
|
<Url url={url} key={url.url} />
|
2017-12-02 13:19:58 +00:00
|
|
|
));
|
2018-04-24 19:34:03 +00:00
|
|
|
};
|
2017-11-21 14:55:50 +00:00
|
|
|
|
2017-12-13 15:00:26 +00:00
|
|
|
renderError = () => {
|
|
|
|
if (!this.hasError()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (
|
2018-04-24 19:34:03 +00:00
|
|
|
<TouchableOpacity onPress={this.onErrorPress}>
|
|
|
|
<Icon name='error-outline' color='red' size={20} style={styles.errorIcon} />
|
2017-12-13 15:00:26 +00:00
|
|
|
</TouchableOpacity>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
renderReaction = (reaction) => {
|
2018-01-30 19:48:26 +00:00
|
|
|
const reacted = reaction.usernames.findIndex(item => item.value === this.props.user.username) !== -1;
|
2018-04-24 19:34:03 +00:00
|
|
|
const reactedContainerStyle = reacted && styles.reactedContainer;
|
|
|
|
const reactedCount = reacted && styles.reactedCountText;
|
2018-01-30 19:48:26 +00:00
|
|
|
return (
|
|
|
|
<TouchableOpacity
|
|
|
|
onPress={() => this.onReactionPress(reaction.emoji)}
|
|
|
|
onLongPress={() => this.onReactionLongPress()}
|
|
|
|
key={reaction.emoji}
|
2018-05-23 13:39:18 +00:00
|
|
|
testID={`message-reaction-${ reaction.emoji }`}
|
2018-01-30 19:48:26 +00:00
|
|
|
>
|
|
|
|
<View style={[styles.reactionContainer, reactedContainerStyle]}>
|
|
|
|
<Emoji
|
|
|
|
content={reaction.emoji}
|
2018-02-08 14:08:50 +00:00
|
|
|
standardEmojiStyle={styles.reactionEmoji}
|
|
|
|
customEmojiStyle={styles.reactionCustomEmoji}
|
2018-01-30 19:48:26 +00:00
|
|
|
/>
|
|
|
|
<Text style={[styles.reactionCount, reactedCount]}>{ reaction.usernames.length }</Text>
|
|
|
|
</View>
|
|
|
|
</TouchableOpacity>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderReactions() {
|
|
|
|
if (this.props.item.reactions.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<View style={styles.reactionsContainer}>
|
2018-04-24 19:34:03 +00:00
|
|
|
{this.props.item.reactions.map(this.renderReaction)}
|
2018-01-30 19:48:26 +00:00
|
|
|
<TouchableOpacity
|
|
|
|
onPress={() => this.props.toggleReactionPicker(this.parseMessage())}
|
2018-05-23 13:39:18 +00:00
|
|
|
key='message-add-reaction'
|
|
|
|
testID='message-add-reaction'
|
2018-01-30 19:48:26 +00:00
|
|
|
style={styles.reactionContainer}
|
|
|
|
>
|
|
|
|
<Icon name='insert-emoticon' color='#aaaaaa' size={15} />
|
|
|
|
</TouchableOpacity>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-05-24 20:17:45 +00:00
|
|
|
renderBroadcastReply() {
|
|
|
|
if (!this.props.broadcast || this.isOwn()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<TouchableOpacity
|
|
|
|
style={styles.broadcastButton}
|
|
|
|
onPress={() => this.props.replyBroadcast(this.parseMessage())}
|
|
|
|
>
|
|
|
|
<Text style={styles.broadcastButtonText}>Reply</Text>
|
|
|
|
</TouchableOpacity>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-11-21 14:55:50 +00:00
|
|
|
render() {
|
2017-11-24 20:44:52 +00:00
|
|
|
const {
|
2018-04-24 19:34:03 +00:00
|
|
|
item, message, editing, style, archived
|
2017-11-24 20:44:52 +00:00
|
|
|
} = this.props;
|
2017-11-21 14:55:50 +00:00
|
|
|
const username = item.alias || item.u.username;
|
2017-11-24 20:44:52 +00:00
|
|
|
const isEditing = message._id === item._id && editing;
|
2018-04-24 19:34:03 +00:00
|
|
|
const accessibilityLabel = `Message from ${ username } at ${ moment(item.ts).format(this.timeFormat) }, ${ this.props.item.msg }`;
|
2017-12-11 20:37:33 +00:00
|
|
|
|
2017-11-21 14:55:50 +00:00
|
|
|
return (
|
2018-04-24 19:34:03 +00:00
|
|
|
<Touch
|
|
|
|
onPress={this.onPress}
|
|
|
|
onLongPress={this.onLongPress}
|
|
|
|
disabled={this.isInfoMessage() || this.hasError() || archived}
|
2017-12-13 15:00:26 +00:00
|
|
|
underlayColor='#FFFFFF'
|
|
|
|
activeOpacity={0.3}
|
2017-12-11 20:37:33 +00:00
|
|
|
accessibilityLabel={accessibilityLabel}
|
2017-11-21 14:55:50 +00:00
|
|
|
>
|
2018-04-24 19:34:03 +00:00
|
|
|
<View style={[styles.message, isEditing && styles.editing, style]}>
|
|
|
|
{this.renderHeader(username)}
|
|
|
|
<View style={styles.flex}>
|
|
|
|
{this.renderError()}
|
|
|
|
<View style={[styles.messageContent, this.isTemp() && styles.temp]}>
|
|
|
|
{this.renderContent()}
|
|
|
|
{this.renderAttachment()}
|
2017-12-13 15:00:26 +00:00
|
|
|
{this.renderUrl()}
|
2018-01-30 19:48:26 +00:00
|
|
|
{this.renderReactions()}
|
2018-05-24 20:17:45 +00:00
|
|
|
{this.renderBroadcastReply()}
|
2017-12-13 15:00:26 +00:00
|
|
|
</View>
|
|
|
|
</View>
|
2018-04-24 19:34:03 +00:00
|
|
|
{this.state.reactionsModal &&
|
2018-01-30 19:48:26 +00:00
|
|
|
<ReactionsModal
|
|
|
|
isVisible={this.state.reactionsModal}
|
|
|
|
onClose={this.onClose}
|
|
|
|
reactions={item.reactions}
|
|
|
|
user={this.props.user}
|
|
|
|
/>
|
|
|
|
}
|
2018-02-08 14:08:50 +00:00
|
|
|
</View>
|
2018-04-24 19:34:03 +00:00
|
|
|
</Touch>
|
2017-11-21 14:55:50 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|