2017-11-21 14:55:50 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2020-02-17 12:14:56 +00:00
|
|
|
import { KeyboardUtils } from 'react-native-keyboard-input';
|
2017-11-21 14:55:50 +00:00
|
|
|
|
2018-09-11 16:32:52 +00:00
|
|
|
import Message from './Message';
|
2020-04-30 20:05:59 +00:00
|
|
|
import MessageContext from './Context';
|
2019-04-17 17:01:03 +00:00
|
|
|
import debounce from '../../utils/debounce';
|
2019-09-16 20:26:32 +00:00
|
|
|
import { SYSTEM_MESSAGES, getMessageTranslation } from './utils';
|
2020-10-30 13:12:02 +00:00
|
|
|
import { E2E_MESSAGE_TYPE, E2E_STATUS } from '../../lib/encryption/constants';
|
2019-05-20 20:43:50 +00:00
|
|
|
import messagesStatus from '../../constants/messagesStatus';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { withTheme } from '../../theme';
|
2018-01-09 17:12:55 +00:00
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
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
|
|
|
|
}),
|
2020-02-11 14:01:35 +00:00
|
|
|
rid: PropTypes.string,
|
2019-05-20 20:43:50 +00:00
|
|
|
timeFormat: PropTypes.string,
|
2019-05-22 20:15:35 +00:00
|
|
|
style: PropTypes.any,
|
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,
|
|
|
|
baseUrl: PropTypes.string,
|
|
|
|
Message_GroupingPeriod: PropTypes.number,
|
2019-06-10 18:36:31 +00:00
|
|
|
isReadReceiptEnabled: PropTypes.bool,
|
2019-09-16 20:26:32 +00:00
|
|
|
isThreadRoom: PropTypes.bool,
|
2018-09-11 16:32:52 +00:00
|
|
|
useRealName: PropTypes.bool,
|
2019-06-28 17:02:30 +00:00
|
|
|
autoTranslateRoom: PropTypes.bool,
|
|
|
|
autoTranslateLanguage: PropTypes.string,
|
2019-04-17 17:01:03 +00:00
|
|
|
status: PropTypes.number,
|
2020-11-30 20:00:31 +00:00
|
|
|
isIgnored: PropTypes.bool,
|
2019-09-16 20:26:32 +00:00
|
|
|
getCustomEmoji: PropTypes.func,
|
2018-09-11 16:32:52 +00:00
|
|
|
onLongPress: PropTypes.func,
|
|
|
|
onReactionPress: PropTypes.func,
|
2020-09-11 14:31:38 +00:00
|
|
|
onEncryptedPress: PropTypes.func,
|
2019-04-08 12:35:28 +00:00
|
|
|
onDiscussionPress: PropTypes.func,
|
2019-05-20 20:43:50 +00:00
|
|
|
onThreadPress: PropTypes.func,
|
2018-09-11 16:32:52 +00:00
|
|
|
errorActionsShow: PropTypes.func,
|
|
|
|
replyBroadcast: PropTypes.func,
|
2019-09-16 20:26:32 +00:00
|
|
|
reactionInit: PropTypes.func,
|
2019-05-20 20:43:50 +00:00
|
|
|
fetchThreadName: PropTypes.func,
|
2019-12-18 21:13:11 +00:00
|
|
|
showAttachment: PropTypes.func,
|
2019-08-22 18:08:07 +00:00
|
|
|
onReactionLongPress: PropTypes.func,
|
2019-09-18 17:32:12 +00:00
|
|
|
navToRoomInfo: PropTypes.func,
|
2019-12-04 16:39:53 +00:00
|
|
|
callJitsi: PropTypes.func,
|
2020-02-11 14:01:35 +00:00
|
|
|
blockAction: PropTypes.func,
|
2020-10-30 17:35:07 +00:00
|
|
|
theme: PropTypes.string,
|
2020-11-12 14:17:32 +00:00
|
|
|
threadBadgeColor: PropTypes.string,
|
2020-10-30 17:35:07 +00:00
|
|
|
toggleFollowThread: PropTypes.func
|
2018-01-30 19:48:26 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 16:49:51 +00:00
|
|
|
static defaultProps = {
|
2020-02-17 16:06:46 +00:00
|
|
|
getCustomEmoji: () => {},
|
2018-03-23 16:49:51 +00:00
|
|
|
onLongPress: () => {},
|
2020-02-17 16:06:46 +00:00
|
|
|
onReactionPress: () => {},
|
2020-09-11 14:31:38 +00:00
|
|
|
onEncryptedPress: () => {},
|
2020-02-17 16:06:46 +00:00
|
|
|
onDiscussionPress: () => {},
|
|
|
|
onThreadPress: () => {},
|
|
|
|
errorActionsShow: () => {},
|
|
|
|
replyBroadcast: () => {},
|
|
|
|
reactionInit: () => {},
|
|
|
|
fetchThreadName: () => {},
|
|
|
|
showAttachment: () => {},
|
|
|
|
onReactionLongPress: () => {},
|
|
|
|
navToRoomInfo: () => {},
|
|
|
|
callJitsi: () => {},
|
|
|
|
blockAction: () => {},
|
2018-05-24 20:17:45 +00:00
|
|
|
archived: false,
|
2019-12-04 16:39:53 +00:00
|
|
|
broadcast: false,
|
2020-11-30 20:00:31 +00:00
|
|
|
isIgnored: false,
|
2019-12-04 16:39:53 +00:00
|
|
|
theme: 'light'
|
2018-03-23 16:49:51 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 20:00:31 +00:00
|
|
|
state = { isManualUnignored: false };
|
|
|
|
|
2020-11-04 16:53:44 +00:00
|
|
|
componentDidMount() {
|
2019-09-16 20:26:32 +00:00
|
|
|
const { item } = this.props;
|
|
|
|
if (item && item.observe) {
|
|
|
|
const observable = item.observe();
|
|
|
|
this.subscription = observable.subscribe(() => {
|
|
|
|
this.forceUpdate();
|
|
|
|
});
|
2019-06-10 18:36:31 +00:00
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
}
|
2019-04-17 17:01:03 +00:00
|
|
|
|
2020-11-30 20:00:31 +00:00
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
|
|
|
const { isManualUnignored } = this.state;
|
|
|
|
const { theme, threadBadgeColor, isIgnored } = this.props;
|
2019-12-04 16:39:53 +00:00
|
|
|
if (nextProps.theme !== theme) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-11-12 14:17:32 +00:00
|
|
|
if (nextProps.threadBadgeColor !== threadBadgeColor) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-11-30 20:00:31 +00:00
|
|
|
if (nextProps.isIgnored !== isIgnored) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextState.isManualUnignored !== isManualUnignored) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.subscription && this.subscription.unsubscribe) {
|
|
|
|
this.subscription.unsubscribe();
|
|
|
|
}
|
2018-03-02 21:31:44 +00:00
|
|
|
}
|
2017-11-21 14:55:50 +00:00
|
|
|
|
2019-05-20 20:43:50 +00:00
|
|
|
onPress = debounce(() => {
|
2020-11-30 20:00:31 +00:00
|
|
|
if (this.isIgnored) {
|
|
|
|
return this.onIgnoredMessagePress();
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
const { item, isThreadRoom } = this.props;
|
2020-02-17 12:14:56 +00:00
|
|
|
KeyboardUtils.dismiss();
|
2019-05-20 20:43:50 +00:00
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
if (((item.tlm || item.tmid) && !isThreadRoom)) {
|
2019-05-20 20:43:50 +00:00
|
|
|
this.onThreadPress();
|
|
|
|
}
|
|
|
|
}, 300, true);
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
onLongPress = () => {
|
2019-09-16 20:26:32 +00:00
|
|
|
const { archived, onLongPress, item } = this.props;
|
2020-09-11 14:31:38 +00:00
|
|
|
if (this.isInfo || this.hasError || this.isEncrypted || archived) {
|
2019-05-20 20:43:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (onLongPress) {
|
2019-09-16 20:26:32 +00:00
|
|
|
onLongPress(item);
|
2019-05-20 20:43:50 +00:00
|
|
|
}
|
2017-11-21 14:55:50 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
onErrorPress = () => {
|
2019-09-16 20:26:32 +00:00
|
|
|
const { errorActionsShow, item } = this.props;
|
2019-05-20 20:43:50 +00:00
|
|
|
if (errorActionsShow) {
|
2019-09-16 20:26:32 +00:00
|
|
|
errorActionsShow(item);
|
2019-05-20 20:43:50 +00:00
|
|
|
}
|
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;
|
2019-05-20 20:43:50 +00:00
|
|
|
if (onReactionPress) {
|
2019-10-01 12:54:59 +00:00
|
|
|
onReactionPress(emoji, item.id);
|
2019-05-20 20:43:50 +00:00
|
|
|
}
|
2018-01-30 19:48:26 +00:00
|
|
|
}
|
2018-09-11 16:32:52 +00:00
|
|
|
|
|
|
|
onReactionLongPress = () => {
|
2019-05-20 20:43:50 +00:00
|
|
|
const { onReactionLongPress, item } = this.props;
|
|
|
|
if (onReactionLongPress) {
|
|
|
|
onReactionLongPress(item);
|
|
|
|
}
|
2017-12-13 15:00:26 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 14:31:38 +00:00
|
|
|
onEncryptedPress = () => {
|
|
|
|
const { onEncryptedPress } = this.props;
|
|
|
|
if (onEncryptedPress) {
|
|
|
|
onEncryptedPress();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 12:35:28 +00:00
|
|
|
onDiscussionPress = () => {
|
|
|
|
const { onDiscussionPress, item } = this.props;
|
2019-05-20 20:43:50 +00:00
|
|
|
if (onDiscussionPress) {
|
|
|
|
onDiscussionPress(item);
|
2019-04-17 17:01:03 +00:00
|
|
|
}
|
2017-12-02 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
2019-05-20 20:43:50 +00:00
|
|
|
onThreadPress = () => {
|
|
|
|
const { onThreadPress, item } = this.props;
|
|
|
|
if (onThreadPress) {
|
|
|
|
onThreadPress(item);
|
|
|
|
}
|
2017-12-13 15:00:26 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 20:00:31 +00:00
|
|
|
onIgnoredMessagePress = () => {
|
|
|
|
this.setState({ isManualUnignored: true });
|
|
|
|
}
|
|
|
|
|
2019-05-20 20:43:50 +00:00
|
|
|
get isHeader() {
|
2018-09-25 19:28:42 +00:00
|
|
|
const {
|
|
|
|
item, previousItem, broadcast, Message_GroupingPeriod
|
|
|
|
} = this.props;
|
2019-07-17 14:06:39 +00:00
|
|
|
if (this.hasError || (previousItem && previousItem.status === messagesStatus.ERROR)) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
try {
|
|
|
|
if (previousItem && (
|
|
|
|
(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)
|
|
|
|
&& (previousItem.tmid === item.tmid)
|
|
|
|
)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} catch (error) {
|
|
|
|
return true;
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-20 20:43:50 +00:00
|
|
|
get isThreadReply() {
|
2019-05-03 13:33:38 +00:00
|
|
|
const {
|
2019-09-16 20:26:32 +00:00
|
|
|
item, previousItem, isThreadRoom
|
2019-05-03 13:33:38 +00:00
|
|
|
} = this.props;
|
2019-09-16 20:26:32 +00:00
|
|
|
if (isThreadRoom) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (previousItem && item.tmid && (previousItem.tmid !== item.tmid) && (previousItem.id !== item.tmid)) {
|
2019-05-03 13:33:38 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-20 20:43:50 +00:00
|
|
|
get isThreadSequential() {
|
2019-05-03 13:33:38 +00:00
|
|
|
const {
|
2019-09-16 20:26:32 +00:00
|
|
|
item, previousItem, isThreadRoom
|
2019-05-03 13:33:38 +00:00
|
|
|
} = this.props;
|
2019-09-16 20:26:32 +00:00
|
|
|
if (isThreadRoom) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (previousItem && item.tmid && ((previousItem.tmid === item.tmid) || (previousItem.id === item.tmid))) {
|
2019-05-03 13:33:38 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-11 14:31:38 +00:00
|
|
|
get isEncrypted() {
|
|
|
|
const { item } = this.props;
|
|
|
|
const { t, e2e } = item;
|
|
|
|
return t === E2E_MESSAGE_TYPE && e2e !== E2E_STATUS.DONE;
|
|
|
|
}
|
|
|
|
|
2019-05-20 20:43:50 +00:00
|
|
|
get isInfo() {
|
|
|
|
const { item } = this.props;
|
|
|
|
return SYSTEM_MESSAGES.includes(item.t);
|
|
|
|
}
|
|
|
|
|
|
|
|
get isTemp() {
|
|
|
|
const { item } = this.props;
|
|
|
|
return item.status === messagesStatus.TEMP || item.status === messagesStatus.ERROR;
|
|
|
|
}
|
|
|
|
|
2020-11-30 20:00:31 +00:00
|
|
|
get isIgnored() {
|
|
|
|
const { isManualUnignored } = this.state;
|
|
|
|
const { isIgnored } = this.props;
|
|
|
|
return isManualUnignored ? false : isIgnored;
|
|
|
|
}
|
|
|
|
|
2019-05-20 20:43:50 +00:00
|
|
|
get hasError() {
|
|
|
|
const { item } = this.props;
|
|
|
|
return item.status === messagesStatus.ERROR;
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
reactionInit = () => {
|
|
|
|
const { reactionInit, item } = this.props;
|
|
|
|
if (reactionInit) {
|
|
|
|
reactionInit(item);
|
2019-05-20 20:43:50 +00:00
|
|
|
}
|
2018-01-30 19:48:26 +00:00
|
|
|
}
|
|
|
|
|
2018-09-11 16:32:52 +00:00
|
|
|
replyBroadcast = () => {
|
2019-09-16 20:26:32 +00:00
|
|
|
const { replyBroadcast, item } = this.props;
|
2019-05-20 20:43:50 +00:00
|
|
|
if (replyBroadcast) {
|
2019-09-16 20:26:32 +00:00
|
|
|
replyBroadcast(item);
|
2019-05-20 20:43:50 +00:00
|
|
|
}
|
2018-05-24 20:17:45 +00:00
|
|
|
}
|
|
|
|
|
2017-11-21 14:55:50 +00:00
|
|
|
render() {
|
2017-11-24 20:44:52 +00:00
|
|
|
const {
|
2020-11-12 14:17:32 +00:00
|
|
|
item, user, style, archived, baseUrl, useRealName, broadcast, fetchThreadName, showAttachment, timeFormat, isReadReceiptEnabled, autoTranslateRoom, autoTranslateLanguage, navToRoomInfo, getCustomEmoji, isThreadRoom, callJitsi, blockAction, rid, theme, threadBadgeColor, toggleFollowThread
|
2017-11-24 20:44:52 +00:00
|
|
|
} = this.props;
|
2018-09-11 16:32:52 +00:00
|
|
|
const {
|
2020-10-30 17:35:07 +00:00
|
|
|
id, msg, ts, attachments, urls, reactions, t, avatar, emoji, u, alias, editedBy, role, drid, dcount, dlm, tmid, tcount, tlm, tmsg, mentions, channels, unread, blocks, autoTranslate: autoTranslateMessage, replies
|
2018-09-11 16:32:52 +00:00
|
|
|
} = item;
|
2019-05-20 20:43:50 +00:00
|
|
|
|
2019-06-28 17:02:30 +00:00
|
|
|
let message = msg;
|
|
|
|
// "autoTranslateRoom" and "autoTranslateLanguage" are properties from the subscription
|
|
|
|
// "autoTranslateMessage" is a toggle between "View Original" and "Translate" state
|
|
|
|
if (autoTranslateRoom && autoTranslateMessage) {
|
|
|
|
message = getMessageTranslation(item, autoTranslateLanguage) || message;
|
|
|
|
}
|
|
|
|
|
2017-11-21 14:55:50 +00:00
|
|
|
return (
|
2020-04-30 20:05:59 +00:00
|
|
|
<MessageContext.Provider
|
|
|
|
value={{
|
|
|
|
user,
|
|
|
|
baseUrl,
|
|
|
|
onPress: this.onPress,
|
|
|
|
onLongPress: this.onLongPress,
|
|
|
|
reactionInit: this.reactionInit,
|
|
|
|
onErrorPress: this.onErrorPress,
|
|
|
|
replyBroadcast: this.replyBroadcast,
|
|
|
|
onReactionPress: this.onReactionPress,
|
2020-09-11 14:31:38 +00:00
|
|
|
onEncryptedPress: this.onEncryptedPress,
|
2020-04-30 20:05:59 +00:00
|
|
|
onDiscussionPress: this.onDiscussionPress,
|
2020-10-30 17:35:07 +00:00
|
|
|
onReactionLongPress: this.onReactionLongPress,
|
2020-11-12 14:17:32 +00:00
|
|
|
threadBadgeColor,
|
2020-10-30 17:35:07 +00:00
|
|
|
toggleFollowThread,
|
|
|
|
replies
|
2020-04-30 20:05:59 +00:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Message
|
|
|
|
id={id}
|
|
|
|
msg={message}
|
|
|
|
rid={rid}
|
2020-11-04 16:53:44 +00:00
|
|
|
author={u}
|
2020-04-30 20:05:59 +00:00
|
|
|
ts={ts}
|
|
|
|
type={t}
|
|
|
|
attachments={attachments}
|
|
|
|
blocks={blocks}
|
|
|
|
urls={urls}
|
|
|
|
reactions={reactions}
|
|
|
|
alias={alias}
|
|
|
|
avatar={avatar}
|
2020-05-08 17:16:22 +00:00
|
|
|
emoji={emoji}
|
2020-04-30 20:05:59 +00:00
|
|
|
timeFormat={timeFormat}
|
|
|
|
style={style}
|
|
|
|
archived={archived}
|
|
|
|
broadcast={broadcast}
|
|
|
|
useRealName={useRealName}
|
|
|
|
isReadReceiptEnabled={isReadReceiptEnabled}
|
|
|
|
unread={unread}
|
|
|
|
role={role}
|
|
|
|
drid={drid}
|
|
|
|
dcount={dcount}
|
|
|
|
dlm={dlm}
|
|
|
|
tmid={tmid}
|
|
|
|
tcount={tcount}
|
|
|
|
tlm={tlm}
|
|
|
|
tmsg={tmsg}
|
|
|
|
fetchThreadName={fetchThreadName}
|
|
|
|
mentions={mentions}
|
|
|
|
channels={channels}
|
2020-11-30 20:00:31 +00:00
|
|
|
isIgnored={this.isIgnored}
|
2020-04-30 20:05:59 +00:00
|
|
|
isEdited={editedBy && !!editedBy.username}
|
|
|
|
isHeader={this.isHeader}
|
|
|
|
isThreadReply={this.isThreadReply}
|
|
|
|
isThreadSequential={this.isThreadSequential}
|
|
|
|
isThreadRoom={isThreadRoom}
|
|
|
|
isInfo={this.isInfo}
|
|
|
|
isTemp={this.isTemp}
|
2020-09-11 14:31:38 +00:00
|
|
|
isEncrypted={this.isEncrypted}
|
2020-04-30 20:05:59 +00:00
|
|
|
hasError={this.hasError}
|
|
|
|
showAttachment={showAttachment}
|
|
|
|
getCustomEmoji={getCustomEmoji}
|
|
|
|
navToRoomInfo={navToRoomInfo}
|
|
|
|
callJitsi={callJitsi}
|
|
|
|
blockAction={blockAction}
|
|
|
|
theme={theme}
|
|
|
|
/>
|
|
|
|
</MessageContext.Provider>
|
2017-11-21 14:55:50 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-12-04 16:39:53 +00:00
|
|
|
|
|
|
|
export default withTheme(MessageContainer);
|