2017-11-21 14:55:50 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2019-02-27 14:47:15 +00:00
|
|
|
import { ViewPropTypes } from 'react-native';
|
2017-11-21 14:55:50 +00:00
|
|
|
import { connect } from 'react-redux';
|
2018-03-02 21:31:44 +00:00
|
|
|
import equal from 'deep-equal';
|
2017-11-21 14:55:50 +00:00
|
|
|
|
2018-09-11 16:32:52 +00:00
|
|
|
import Message from './Message';
|
2018-09-25 19:28:42 +00:00
|
|
|
import {
|
|
|
|
errorActionsShow as errorActionsShowAction,
|
|
|
|
toggleReactionPicker as toggleReactionPickerAction,
|
|
|
|
replyBroadcast as replyBroadcastAction
|
|
|
|
} from '../../actions/messages';
|
2019-02-27 14:47:15 +00:00
|
|
|
import { vibrate } from '../../utils/vibration';
|
2019-04-17 17:01:03 +00:00
|
|
|
import debounce from '../../utils/debounce';
|
2018-01-09 17:12:55 +00:00
|
|
|
|
2017-11-21 14:55:50 +00:00
|
|
|
@connect(state => ({
|
2018-09-11 16:32:52 +00:00
|
|
|
baseUrl: state.settings.Site_Url || state.server ? state.server.server : '',
|
2018-04-24 19:34:03 +00:00
|
|
|
customEmojis: state.customEmojis,
|
2018-09-11 16:32:52 +00:00
|
|
|
Message_GroupingPeriod: state.settings.Message_GroupingPeriod,
|
2018-04-24 19:34:03 +00:00
|
|
|
Message_TimeFormat: state.settings.Message_TimeFormat,
|
2018-09-26 19:38:06 +00:00
|
|
|
editingMessage: state.messages.message,
|
2018-09-11 16:32:52 +00:00
|
|
|
useRealName: state.settings.UI_Use_Real_Name
|
2017-11-21 14:55:50 +00:00
|
|
|
}), dispatch => ({
|
2018-09-25 19:28:42 +00:00
|
|
|
errorActionsShow: actionMessage => dispatch(errorActionsShowAction(actionMessage)),
|
|
|
|
replyBroadcast: message => dispatch(replyBroadcastAction(message)),
|
|
|
|
toggleReactionPicker: message => dispatch(toggleReactionPickerAction(message))
|
2017-11-21 14:55:50 +00:00
|
|
|
}))
|
2018-09-11 16:32:52 +00:00
|
|
|
export default class MessageContainer extends React.Component {
|
2017-11-21 14:55:50 +00:00
|
|
|
static propTypes = {
|
|
|
|
item: PropTypes.object.isRequired,
|
2018-07-10 13:40:32 +00:00
|
|
|
user: PropTypes.shape({
|
|
|
|
id: PropTypes.string.isRequired,
|
|
|
|
username: PropTypes.string.isRequired,
|
|
|
|
token: PropTypes.string.isRequired
|
|
|
|
}),
|
2018-09-11 16:32:52 +00:00
|
|
|
customTimeFormat: PropTypes.string,
|
2019-04-17 17:01:03 +00:00
|
|
|
customThreadTimeFormat: PropTypes.string,
|
2018-02-19 21:19:39 +00:00
|
|
|
style: ViewPropTypes.style,
|
2018-05-24 20:17:45 +00:00
|
|
|
archived: PropTypes.bool,
|
2018-07-10 13:40:32 +00:00
|
|
|
broadcast: PropTypes.bool,
|
2018-09-11 16:32:52 +00:00
|
|
|
previousItem: PropTypes.object,
|
|
|
|
_updatedAt: PropTypes.instanceOf(Date),
|
|
|
|
// redux
|
|
|
|
baseUrl: PropTypes.string,
|
|
|
|
customEmojis: PropTypes.object,
|
|
|
|
Message_GroupingPeriod: PropTypes.number,
|
|
|
|
Message_TimeFormat: PropTypes.string,
|
2018-09-26 19:38:06 +00:00
|
|
|
editingMessage: PropTypes.object,
|
2018-09-11 16:32:52 +00:00
|
|
|
useRealName: PropTypes.bool,
|
2019-04-17 17:01:03 +00:00
|
|
|
status: PropTypes.number,
|
|
|
|
navigation: PropTypes.object,
|
2018-09-11 16:32:52 +00:00
|
|
|
// methods - props
|
|
|
|
onLongPress: PropTypes.func,
|
|
|
|
onReactionPress: PropTypes.func,
|
2019-04-08 12:35:28 +00:00
|
|
|
onDiscussionPress: PropTypes.func,
|
2018-09-11 16:32:52 +00:00
|
|
|
// methods - redux
|
|
|
|
errorActionsShow: PropTypes.func,
|
|
|
|
replyBroadcast: PropTypes.func,
|
2019-04-17 17:01:03 +00:00
|
|
|
toggleReactionPicker: PropTypes.func,
|
|
|
|
fetchThreadName: PropTypes.func
|
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 };
|
2018-09-11 16:32:52 +00:00
|
|
|
this.closeReactions = this.closeReactions.bind(this);
|
2018-01-09 17:12:55 +00:00
|
|
|
}
|
|
|
|
|
2018-03-02 21:31:44 +00:00
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { reactionsModal } = this.state;
|
|
|
|
const {
|
2019-04-17 17:01:03 +00:00
|
|
|
status, editingMessage, item, _updatedAt
|
2018-09-25 19:28:42 +00:00
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
if (reactionsModal !== nextState.reactionsModal) {
|
2018-03-02 21:31:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
if (status !== nextProps.status) {
|
2018-03-02 21:31:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-04-17 17:01:03 +00:00
|
|
|
if (item.tmsg !== nextProps.item.tmsg) {
|
2018-05-24 20:17:45 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-04-17 17:01:03 +00:00
|
|
|
|
2018-09-26 19:38:06 +00:00
|
|
|
if (!equal(editingMessage, nextProps.editingMessage)) {
|
|
|
|
if (nextProps.editingMessage && nextProps.editingMessage._id === item._id) {
|
|
|
|
return true;
|
|
|
|
} else if (!nextProps.editingMessage._id !== item._id && editingMessage._id === item._id) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-09-11 16:32:52 +00:00
|
|
|
}
|
2019-04-17 17:01:03 +00:00
|
|
|
return _updatedAt.toISOString() !== nextProps._updatedAt.toISOString();
|
2018-03-02 21:31:44 +00:00
|
|
|
}
|
2017-11-21 14:55:50 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
onLongPress = () => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { onLongPress } = this.props;
|
|
|
|
onLongPress(this.parseMessage());
|
2017-11-21 14:55:50 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
onErrorPress = () => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { errorActionsShow } = this.props;
|
|
|
|
errorActionsShow(this.parseMessage());
|
2018-01-30 19:48:26 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
onReactionPress = (emoji) => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { onReactionPress, item } = this.props;
|
|
|
|
onReactionPress(emoji, item._id);
|
2018-01-30 19:48:26 +00:00
|
|
|
}
|
2018-09-11 16:32:52 +00:00
|
|
|
|
|
|
|
onReactionLongPress = () => {
|
2018-01-30 19:48:26 +00:00
|
|
|
this.setState({ reactionsModal: true });
|
2019-02-27 14:47:15 +00:00
|
|
|
vibrate();
|
2017-12-13 15:00:26 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 12:35:28 +00:00
|
|
|
onDiscussionPress = () => {
|
|
|
|
const { onDiscussionPress, item } = this.props;
|
|
|
|
onDiscussionPress(item);
|
|
|
|
}
|
|
|
|
|
2019-04-17 17:01:03 +00:00
|
|
|
onThreadPress = debounce(() => {
|
|
|
|
const { navigation, item } = this.props;
|
|
|
|
if (item.tmid) {
|
|
|
|
navigation.push('RoomView', {
|
|
|
|
rid: item.rid, tmid: item.tmid, name: item.tmsg, t: 'thread'
|
|
|
|
});
|
|
|
|
} else if (item.tlm) {
|
|
|
|
const title = item.msg || (item.attachments && item.attachments.length && item.attachments[0].title);
|
|
|
|
navigation.push('RoomView', {
|
|
|
|
rid: item.rid, tmid: item._id, name: title, t: 'thread'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, 1000, true)
|
|
|
|
|
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-09-11 16:32:52 +00:00
|
|
|
closeReactions = () => {
|
|
|
|
this.setState({ reactionsModal: false });
|
2017-12-13 15:00:26 +00:00
|
|
|
}
|
|
|
|
|
2018-09-11 16:32:52 +00:00
|
|
|
isHeader = () => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const {
|
|
|
|
item, previousItem, broadcast, Message_GroupingPeriod
|
|
|
|
} = this.props;
|
2018-04-24 19:34:03 +00:00
|
|
|
if (previousItem && (
|
2018-09-25 19:28:42 +00:00
|
|
|
(previousItem.ts.toDateString() === item.ts.toDateString())
|
|
|
|
&& (previousItem.u.username === item.u.username)
|
|
|
|
&& !(previousItem.groupable === false || item.groupable === false || broadcast === true)
|
|
|
|
&& (item.ts - previousItem.ts < Message_GroupingPeriod * 1000)
|
2019-04-17 17:01:03 +00:00
|
|
|
&& (previousItem.tmid === item.tmid)
|
2018-04-24 19:34:03 +00:00
|
|
|
)) {
|
2018-09-11 16:32:52 +00:00
|
|
|
return false;
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
2018-09-11 16:32:52 +00:00
|
|
|
return true;
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
parseMessage = () => {
|
|
|
|
const { item } = this.props;
|
|
|
|
return JSON.parse(JSON.stringify(item));
|
|
|
|
}
|
2017-12-13 15:00:26 +00:00
|
|
|
|
2018-09-11 16:32:52 +00:00
|
|
|
toggleReactionPicker = () => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { toggleReactionPicker } = this.props;
|
|
|
|
toggleReactionPicker(this.parseMessage());
|
2018-01-30 19:48:26 +00:00
|
|
|
}
|
|
|
|
|
2018-09-11 16:32:52 +00:00
|
|
|
replyBroadcast = () => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { replyBroadcast } = this.props;
|
|
|
|
replyBroadcast(this.parseMessage());
|
2018-05-24 20:17:45 +00:00
|
|
|
}
|
|
|
|
|
2017-11-21 14:55:50 +00:00
|
|
|
render() {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { reactionsModal } = this.state;
|
2017-11-24 20:44:52 +00:00
|
|
|
const {
|
2019-04-17 17:01:03 +00:00
|
|
|
item, editingMessage, user, style, archived, baseUrl, customEmojis, useRealName, broadcast, fetchThreadName, customThreadTimeFormat
|
2017-11-24 20:44:52 +00:00
|
|
|
} = this.props;
|
2018-09-11 16:32:52 +00:00
|
|
|
const {
|
2019-04-17 17:01:03 +00:00
|
|
|
_id, msg, ts, attachments, urls, reactions, t, status, avatar, u, alias, editedBy, role, drid, dcount, dlm, tmid, tcount, tlm, tmsg
|
2018-09-11 16:32:52 +00:00
|
|
|
} = item;
|
2018-09-26 19:38:06 +00:00
|
|
|
const isEditing = editingMessage._id === item._id;
|
2017-11-21 14:55:50 +00:00
|
|
|
return (
|
2018-09-11 16:32:52 +00:00
|
|
|
<Message
|
2019-04-17 17:01:03 +00:00
|
|
|
id={_id}
|
2018-09-11 16:32:52 +00:00
|
|
|
msg={msg}
|
|
|
|
author={u}
|
|
|
|
ts={ts}
|
|
|
|
type={t}
|
|
|
|
status={status}
|
|
|
|
attachments={attachments}
|
|
|
|
urls={urls}
|
|
|
|
reactions={reactions}
|
|
|
|
alias={alias}
|
|
|
|
editing={isEditing}
|
|
|
|
header={this.isHeader()}
|
|
|
|
avatar={avatar}
|
|
|
|
user={user}
|
|
|
|
edited={editedBy && !!editedBy.username}
|
|
|
|
timeFormat={this.timeFormat}
|
2019-04-17 17:01:03 +00:00
|
|
|
customThreadTimeFormat={customThreadTimeFormat}
|
2018-09-11 16:32:52 +00:00
|
|
|
style={style}
|
|
|
|
archived={archived}
|
|
|
|
broadcast={broadcast}
|
|
|
|
baseUrl={baseUrl}
|
|
|
|
customEmojis={customEmojis}
|
2018-09-25 19:28:42 +00:00
|
|
|
reactionsModal={reactionsModal}
|
2018-09-11 16:32:52 +00:00
|
|
|
useRealName={useRealName}
|
2018-09-19 14:18:32 +00:00
|
|
|
role={role}
|
2019-04-08 12:35:28 +00:00
|
|
|
drid={drid}
|
|
|
|
dcount={dcount}
|
|
|
|
dlm={dlm}
|
2019-04-17 17:01:03 +00:00
|
|
|
tmid={tmid}
|
|
|
|
tcount={tcount}
|
|
|
|
tlm={tlm}
|
|
|
|
tmsg={tmsg}
|
|
|
|
fetchThreadName={fetchThreadName}
|
2018-09-11 16:32:52 +00:00
|
|
|
closeReactions={this.closeReactions}
|
|
|
|
onErrorPress={this.onErrorPress}
|
2018-04-24 19:34:03 +00:00
|
|
|
onLongPress={this.onLongPress}
|
2018-09-11 16:32:52 +00:00
|
|
|
onReactionLongPress={this.onReactionLongPress}
|
|
|
|
onReactionPress={this.onReactionPress}
|
|
|
|
replyBroadcast={this.replyBroadcast}
|
|
|
|
toggleReactionPicker={this.toggleReactionPicker}
|
2019-04-08 12:35:28 +00:00
|
|
|
onDiscussionPress={this.onDiscussionPress}
|
2019-04-17 17:01:03 +00:00
|
|
|
onThreadPress={this.onThreadPress}
|
2018-09-11 16:32:52 +00:00
|
|
|
/>
|
2017-11-21 14:55:50 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|