import React from 'react'; import PropTypes from 'prop-types'; import { View, Text, TouchableOpacity, Vibration, ViewPropTypes } from 'react-native'; import { connect } from 'react-redux'; import Icon from 'react-native-vector-icons/MaterialIcons'; import moment from 'moment'; import equal from 'deep-equal'; import { KeyboardUtils } from 'react-native-keyboard-input'; import Image from './Image'; import User from './User'; import Avatar from '../Avatar'; import Audio from './Audio'; import Video from './Video'; import Markdown from './Markdown'; import Url from './Url'; import Reply from './Reply'; import ReactionsModal from './ReactionsModal'; import Emoji from './Emoji'; import styles from './styles'; import { actionsShow, errorActionsShow, toggleReactionPicker } from '../../actions/messages'; import messagesStatus from '../../constants/messagesStatus'; import Touch from '../../utils/touch'; 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 ''; }; @connect(state => ({ message: state.messages.message, editing: state.messages.editing, customEmojis: state.customEmojis, Message_TimeFormat: state.settings.Message_TimeFormat, Message_GroupingPeriod: state.settings.Message_GroupingPeriod }), dispatch => ({ actionsShow: actionMessage => dispatch(actionsShow(actionMessage)), errorActionsShow: actionMessage => dispatch(errorActionsShow(actionMessage)), toggleReactionPicker: message => dispatch(toggleReactionPicker(message)) })) export default class Message extends React.Component { static propTypes = { status: PropTypes.any, item: PropTypes.object.isRequired, reactions: PropTypes.any.isRequired, Message_TimeFormat: PropTypes.string.isRequired, Message_GroupingPeriod: PropTypes.number.isRequired, customTimeFormat: PropTypes.string, message: PropTypes.object.isRequired, user: PropTypes.object.isRequired, editing: PropTypes.bool, errorActionsShow: PropTypes.func, toggleReactionPicker: PropTypes.func, onReactionPress: PropTypes.func, style: ViewPropTypes.style, onLongPress: PropTypes.func, _updatedAt: PropTypes.instanceOf(Date), archived: PropTypes.bool } static defaultProps = { onLongPress: () => {}, _updatedAt: new Date(), archived: false } constructor(props) { super(props); this.state = { reactionsModal: false }; this.onClose = this.onClose.bind(this); } shouldComponentUpdate(nextProps, nextState) { if (this.state.reactionsModal !== nextState.reactionsModal) { return true; } if (this.props.status !== nextProps.status) { return true; } // eslint-disable-next-line if (!!this.props._updatedAt ^ !!nextProps._updatedAt) { return true; } if (!equal(this.props.reactions, nextProps.reactions)) { return true; } return this.props._updatedAt.toGMTString() !== nextProps._updatedAt.toGMTString(); } onPress = () => { KeyboardUtils.dismiss(); } onLongPress = () => { this.props.onLongPress(this.parseMessage()); } onErrorPress = () => { this.props.errorActionsShow(this.parseMessage()); } onReactionPress = (emoji) => { this.props.onReactionPress(emoji, this.props.item._id); } onClose() { this.setState({ reactionsModal: false }); } onReactionLongPress() { this.setState({ reactionsModal: true }); Vibration.vibrate(50); } get timeFormat() { const { customTimeFormat, Message_TimeFormat } = this.props; return customTimeFormat || Message_TimeFormat; } parseMessage = () => JSON.parse(JSON.stringify(this.props.item)); isInfoMessage() { return [ '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' ].includes(this.props.item.t); } isDeleted() { return this.props.item.t === 'rm'; } isTemp() { return this.props.item.status === messagesStatus.TEMP || this.props.item.status === messagesStatus.ERROR; } hasError() { return this.props.item.status === messagesStatus.ERROR; } renderHeader = (username) => { const { item, previousItem } = this.props; if (previousItem && ( (previousItem.ts.toDateString() === item.ts.toDateString()) && (previousItem.u.username === item.u.username) && !(previousItem.groupable === false || item.groupable === false) && (previousItem.status === item.status) && (item.ts - previousItem.ts < this.props.Message_GroupingPeriod * 1000) )) { return null; } return ( ); } renderContent() { if (this.isInfoMessage()) { return {getInfoMessage(this.props.item)}; } const { item } = this.props; return ; } renderAttachment() { if (this.props.item.attachments.length === 0) { return null; } const file = this.props.item.attachments[0]; const { user } = this.props; if (file.image_url) { return ; } if (file.audio_url) { return