2017-08-04 00:34:37 +00:00
|
|
|
import React from 'react';
|
2017-08-05 18:16:32 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2018-08-01 19:35:06 +00:00
|
|
|
import { Text, View, LayoutAnimation, ActivityIndicator, SafeAreaView } from 'react-native';
|
2017-08-13 01:35:09 +00:00
|
|
|
import { connect } from 'react-redux';
|
2018-01-09 17:12:55 +00:00
|
|
|
import equal from 'deep-equal';
|
2017-08-13 01:35:09 +00:00
|
|
|
|
2018-04-03 16:24:59 +00:00
|
|
|
import LoggedView from '../View';
|
2018-01-30 19:48:26 +00:00
|
|
|
import { List } from './ListView';
|
2018-07-10 13:40:32 +00:00
|
|
|
import { openRoom, closeRoom, setLastOpen } from '../../actions/room';
|
2018-06-18 13:30:36 +00:00
|
|
|
import { toggleReactionPicker, actionsShow } from '../../actions/messages';
|
2017-12-27 15:22:06 +00:00
|
|
|
import database from '../../lib/realm';
|
2017-12-08 19:13:21 +00:00
|
|
|
import RocketChat from '../../lib/rocketchat';
|
|
|
|
import Message from '../../containers/message';
|
|
|
|
import MessageActions from '../../containers/MessageActions';
|
2017-12-13 15:00:26 +00:00
|
|
|
import MessageErrorActions from '../../containers/MessageErrorActions';
|
2017-12-08 19:13:21 +00:00
|
|
|
import MessageBox from '../../containers/MessageBox';
|
2018-01-30 19:48:26 +00:00
|
|
|
import ReactionPicker from './ReactionPicker';
|
2018-07-17 19:10:27 +00:00
|
|
|
import UploadProgress from './UploadProgress';
|
2017-12-08 19:13:21 +00:00
|
|
|
import styles from './styles';
|
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-06-18 13:30:36 +00:00
|
|
|
import debounce from '../../utils/debounce';
|
2018-07-10 13:40:32 +00:00
|
|
|
import { iconsMap } from '../../Icons';
|
2017-08-14 14:15:37 +00:00
|
|
|
|
2018-07-10 13:40:32 +00:00
|
|
|
@connect(state => ({
|
|
|
|
user: {
|
|
|
|
id: state.login.user && state.login.user.id,
|
|
|
|
username: state.login.user && state.login.user.username,
|
|
|
|
token: state.login.user && state.login.user.token
|
|
|
|
},
|
|
|
|
actionMessage: state.messages.actionMessage,
|
|
|
|
showActions: state.messages.showActions,
|
|
|
|
showErrorActions: state.messages.showErrorActions
|
|
|
|
}), dispatch => ({
|
|
|
|
openRoom: room => dispatch(openRoom(room)),
|
|
|
|
setLastOpen: date => dispatch(setLastOpen(date)),
|
|
|
|
toggleReactionPicker: message => dispatch(toggleReactionPicker(message)),
|
|
|
|
actionsShow: actionMessage => dispatch(actionsShow(actionMessage)),
|
|
|
|
close: () => dispatch(closeRoom())
|
|
|
|
}))
|
|
|
|
/** @extends React.Component */
|
2018-04-03 16:24:59 +00:00
|
|
|
export default class RoomView extends LoggedView {
|
2017-08-05 18:16:32 +00:00
|
|
|
static propTypes = {
|
2018-07-10 13:40:32 +00:00
|
|
|
navigator: PropTypes.object,
|
2017-11-20 22:18:00 +00:00
|
|
|
openRoom: PropTypes.func.isRequired,
|
2018-01-16 20:27:57 +00:00
|
|
|
setLastOpen: PropTypes.func.isRequired,
|
2018-07-10 13:40:32 +00:00
|
|
|
user: PropTypes.shape({
|
|
|
|
id: PropTypes.string.isRequired,
|
|
|
|
username: PropTypes.string.isRequired,
|
|
|
|
token: PropTypes.string.isRequired
|
|
|
|
}),
|
2017-08-12 20:52:55 +00:00
|
|
|
rid: PropTypes.string,
|
2018-07-10 13:40:32 +00:00
|
|
|
showActions: PropTypes.bool,
|
|
|
|
showErrorActions: PropTypes.bool,
|
2018-01-30 19:48:26 +00:00
|
|
|
actionMessage: PropTypes.object,
|
2018-02-19 21:19:39 +00:00
|
|
|
toggleReactionPicker: PropTypes.func.isRequired,
|
2018-07-10 13:40:32 +00:00
|
|
|
actionsShow: PropTypes.func,
|
|
|
|
close: PropTypes.func
|
2017-09-25 13:15:28 +00:00
|
|
|
};
|
2017-08-05 18:16:32 +00:00
|
|
|
|
2017-08-04 00:34:37 +00:00
|
|
|
constructor(props) {
|
2018-04-03 16:24:59 +00:00
|
|
|
super('RoomView', props);
|
2018-07-10 13:40:32 +00:00
|
|
|
this.rid = props.rid;
|
2018-01-17 16:42:30 +00:00
|
|
|
this.rooms = database.objects('subscriptions').filtered('rid = $0', this.rid);
|
2017-08-07 18:42:02 +00:00
|
|
|
this.state = {
|
2018-07-18 20:34:59 +00:00
|
|
|
loaded: false,
|
2018-01-17 16:42:30 +00:00
|
|
|
joined: typeof props.rid === 'undefined',
|
2018-06-01 17:56:59 +00:00
|
|
|
room: {},
|
|
|
|
end: false
|
2017-08-07 18:42:02 +00:00
|
|
|
};
|
2018-01-30 19:48:26 +00:00
|
|
|
this.onReactionPress = this.onReactionPress.bind(this);
|
2018-07-10 13:40:32 +00:00
|
|
|
props.navigator.setOnNavigatorEvent(this.onNavigatorEvent.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
async componentWillMount() {
|
|
|
|
this.props.navigator.setButtons({
|
|
|
|
rightButtons: [{
|
|
|
|
id: 'more',
|
|
|
|
testID: 'room-view-header-actions',
|
|
|
|
icon: iconsMap.more
|
|
|
|
}, {
|
|
|
|
id: 'star',
|
|
|
|
testID: 'room-view-header-star',
|
|
|
|
icon: iconsMap.starOutline
|
|
|
|
}]
|
|
|
|
});
|
2017-08-07 00:34:35 +00:00
|
|
|
}
|
2017-08-04 00:34:37 +00:00
|
|
|
|
2018-05-23 13:39:18 +00:00
|
|
|
componentDidMount() {
|
|
|
|
this.updateRoom();
|
2018-01-17 16:42:30 +00:00
|
|
|
this.rooms.addListener(this.updateRoom);
|
2018-07-10 13:40:32 +00:00
|
|
|
this.props.navigator.setDrawerEnabled({
|
|
|
|
side: 'left',
|
|
|
|
enabled: false
|
|
|
|
});
|
2018-07-18 20:34:59 +00:00
|
|
|
this.setState({ loaded: true });
|
2018-02-08 14:08:50 +00:00
|
|
|
}
|
2018-01-09 17:12:55 +00:00
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
2018-03-23 16:49:51 +00:00
|
|
|
return !(equal(this.props, nextProps) && equal(this.state, nextState) && this.state.room.ro === nextState.room.ro);
|
2017-09-21 17:08:00 +00:00
|
|
|
}
|
2018-07-10 13:40:32 +00:00
|
|
|
|
|
|
|
componentDidUpdate(prevProps, prevState) {
|
|
|
|
if (prevState.room.f !== this.state.room.f) {
|
|
|
|
this.props.navigator.setButtons({
|
|
|
|
rightButtons: [{
|
|
|
|
id: 'more',
|
|
|
|
testID: 'room-view-header-actions',
|
|
|
|
icon: iconsMap.more
|
|
|
|
}, {
|
|
|
|
id: 'star',
|
|
|
|
testID: 'room-view-header-star',
|
|
|
|
icon: this.state.room.f ? iconsMap.star : iconsMap.starOutline
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-07 00:34:35 +00:00
|
|
|
componentWillUnmount() {
|
2018-01-30 19:48:26 +00:00
|
|
|
this.rooms.removeAllListeners();
|
2018-06-18 13:30:36 +00:00
|
|
|
this.onEndReached.stop();
|
2018-07-10 13:40:32 +00:00
|
|
|
this.props.close();
|
2017-08-04 00:34:37 +00:00
|
|
|
}
|
|
|
|
|
2018-07-10 13:40:32 +00:00
|
|
|
onNavigatorEvent(event) {
|
|
|
|
if (event.type === 'NavBarButtonPress') {
|
|
|
|
if (event.id === 'more') {
|
|
|
|
this.props.navigator.push({
|
|
|
|
screen: 'RoomActionsView',
|
|
|
|
title: I18n.t('Actions'),
|
|
|
|
passProps: {
|
|
|
|
rid: this.state.room.rid
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else if (event.id === 'star') {
|
|
|
|
try {
|
|
|
|
RocketChat.toggleFavorite(this.state.room.rid, this.state.room.f);
|
|
|
|
} catch (e) {
|
|
|
|
log('toggleFavorite', e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onEndReached = debounce((lastRowData) => {
|
|
|
|
if (!lastRowData) {
|
2018-06-01 17:56:59 +00:00
|
|
|
this.setState({ end: true });
|
2018-01-30 19:48:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-11-21 16:55:32 +00:00
|
|
|
|
2018-06-01 17:56:59 +00:00
|
|
|
requestAnimationFrame(async() => {
|
2018-06-13 01:33:00 +00:00
|
|
|
try {
|
|
|
|
const result = await RocketChat.loadMessagesForRoom({ rid: this.rid, t: this.state.room.t, latest: lastRowData.ts });
|
|
|
|
this.setState({ end: result < 20 });
|
|
|
|
} catch (e) {
|
|
|
|
log('RoomView.onEndReached', e);
|
|
|
|
}
|
2017-08-09 01:40:55 +00:00
|
|
|
});
|
2018-06-18 13:30:36 +00:00
|
|
|
})
|
2018-01-30 19:48:26 +00:00
|
|
|
|
2018-02-19 21:19:39 +00:00
|
|
|
onMessageLongPress = (message) => {
|
|
|
|
this.props.actionsShow(message);
|
|
|
|
}
|
|
|
|
|
2018-01-30 19:48:26 +00:00
|
|
|
onReactionPress = (shortname, messageId) => {
|
2018-05-18 17:55:08 +00:00
|
|
|
try {
|
|
|
|
if (!messageId) {
|
|
|
|
RocketChat.setReaction(shortname, this.props.actionMessage._id);
|
|
|
|
return this.props.toggleReactionPicker();
|
|
|
|
}
|
|
|
|
RocketChat.setReaction(shortname, messageId);
|
|
|
|
} catch (e) {
|
|
|
|
log('RoomView.onReactionPress', e);
|
2018-01-30 19:48:26 +00:00
|
|
|
}
|
|
|
|
};
|
2017-08-04 00:34:37 +00:00
|
|
|
|
2018-05-07 20:43:26 +00:00
|
|
|
updateRoom = async() => {
|
|
|
|
if (this.rooms.length > 0) {
|
2018-05-18 17:55:08 +00:00
|
|
|
const { room: prevRoom } = this.state;
|
2018-07-18 20:34:59 +00:00
|
|
|
const room = JSON.parse(JSON.stringify(this.rooms[0]));
|
|
|
|
this.setState({ room });
|
|
|
|
|
2018-05-18 17:55:08 +00:00
|
|
|
if (!prevRoom.rid) {
|
2018-07-18 20:34:59 +00:00
|
|
|
this.props.navigator.setTitle({ title: room.name });
|
|
|
|
this.props.openRoom({
|
|
|
|
...room
|
2018-05-18 17:55:08 +00:00
|
|
|
});
|
2018-07-18 20:34:59 +00:00
|
|
|
if (room.alert || room.unread || room.userMentions) {
|
|
|
|
this.props.setLastOpen(room.ls);
|
2018-05-18 17:55:08 +00:00
|
|
|
} else {
|
|
|
|
this.props.setLastOpen(null);
|
|
|
|
}
|
|
|
|
}
|
2018-05-07 20:43:26 +00:00
|
|
|
}
|
2018-01-17 16:42:30 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 14:08:50 +00:00
|
|
|
sendMessage = (message) => {
|
2018-06-20 13:40:33 +00:00
|
|
|
LayoutAnimation.easeInEaseOut();
|
2018-02-08 14:08:50 +00:00
|
|
|
RocketChat.sendMessage(this.rid, message).then(() => {
|
|
|
|
this.props.setLastOpen(null);
|
|
|
|
});
|
|
|
|
};
|
2017-08-09 01:40:55 +00:00
|
|
|
|
2017-09-21 17:08:00 +00:00
|
|
|
joinRoom = async() => {
|
2018-05-18 17:55:08 +00:00
|
|
|
try {
|
|
|
|
await RocketChat.joinRoom(this.props.rid);
|
|
|
|
this.setState({
|
|
|
|
joined: true
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
log('joinRoom', e);
|
|
|
|
}
|
2017-08-10 16:16:32 +00:00
|
|
|
};
|
|
|
|
|
2018-05-24 20:17:45 +00:00
|
|
|
isOwner = () => this.state.room && this.state.room.roles && Array.from(Object.keys(this.state.room.roles), i => this.state.room.roles[i].value).includes('owner');
|
|
|
|
|
|
|
|
isMuted = () => this.state.room && this.state.room.muted && Array.from(Object.keys(this.state.room.muted), i => this.state.room.muted[i].value).includes(this.props.user.username);
|
|
|
|
|
|
|
|
isReadOnly = () => this.state.room.ro && this.isMuted() && !this.isOwner();
|
|
|
|
|
|
|
|
isBlocked = () => {
|
|
|
|
if (this.state.room) {
|
|
|
|
const { t, blocked, blocker } = this.state.room;
|
|
|
|
if (t === 'd' && (blocked || blocker)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
renderItem = (item, previousItem) => (
|
2017-08-09 01:40:55 +00:00
|
|
|
<Message
|
2017-12-13 15:00:26 +00:00
|
|
|
key={item._id}
|
2017-08-09 01:40:55 +00:00
|
|
|
item={item}
|
2018-03-02 21:31:44 +00:00
|
|
|
_updatedAt={item._updatedAt}
|
|
|
|
status={item.status}
|
2018-01-30 19:48:26 +00:00
|
|
|
reactions={JSON.parse(JSON.stringify(item.reactions))}
|
2017-12-02 13:19:58 +00:00
|
|
|
user={this.props.user}
|
2018-01-30 19:48:26 +00:00
|
|
|
onReactionPress={this.onReactionPress}
|
2018-02-19 21:19:39 +00:00
|
|
|
onLongPress={this.onMessageLongPress}
|
2018-03-29 17:55:37 +00:00
|
|
|
archived={this.state.room.archived}
|
2018-05-24 20:17:45 +00:00
|
|
|
broadcast={this.state.room.broadcast}
|
2018-04-24 19:34:03 +00:00
|
|
|
previousItem={previousItem}
|
2017-08-09 01:40:55 +00:00
|
|
|
/>
|
|
|
|
);
|
2017-08-07 00:34:35 +00:00
|
|
|
|
2017-08-10 16:16:32 +00:00
|
|
|
renderFooter = () => {
|
2018-07-10 13:40:32 +00:00
|
|
|
// TODO: fix it
|
|
|
|
// if (!this.state.joined) {
|
|
|
|
// return (
|
|
|
|
// <View>
|
|
|
|
// <Text>{I18n.t('You_are_in_preview_mode')}</Text>
|
|
|
|
// <Button title='Join' onPress={this.joinRoom} />
|
|
|
|
// </View>
|
|
|
|
// );
|
|
|
|
// }
|
2018-05-24 20:17:45 +00:00
|
|
|
if (this.state.room.archived || this.isReadOnly()) {
|
2018-01-17 16:42:30 +00:00
|
|
|
return (
|
|
|
|
<View style={styles.readOnly}>
|
2018-06-01 17:38:13 +00:00
|
|
|
<Text>{I18n.t('This_room_is_read_only')}</Text>
|
2018-01-17 16:42:30 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
2018-05-24 20:17:45 +00:00
|
|
|
if (this.isBlocked()) {
|
|
|
|
return (
|
|
|
|
<View style={styles.blockedOrBlocker}>
|
2018-06-01 17:38:13 +00:00
|
|
|
<Text>{I18n.t('This_room_is_blocked')}</Text>
|
2018-05-24 20:17:45 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
2018-04-10 13:03:54 +00:00
|
|
|
return <MessageBox onSubmit={this.sendMessage} rid={this.rid} />;
|
2017-09-25 13:15:28 +00:00
|
|
|
};
|
2017-08-09 20:08:50 +00:00
|
|
|
|
2017-08-10 23:21:46 +00:00
|
|
|
renderHeader = () => {
|
2018-08-10 13:17:45 +00:00
|
|
|
if (!this.state.end) {
|
|
|
|
return <Text style={styles.loadingMore}>{I18n.t('Loading_messages_ellipsis')}</Text>;
|
2017-08-10 23:21:46 +00:00
|
|
|
}
|
2018-08-10 13:17:45 +00:00
|
|
|
return null;
|
2017-11-21 16:55:32 +00:00
|
|
|
}
|
2018-07-18 20:34:59 +00:00
|
|
|
|
|
|
|
renderList = () => {
|
|
|
|
if (!this.state.loaded) {
|
|
|
|
return <ActivityIndicator style={styles.loading} />;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<List
|
|
|
|
key='room-view-messages'
|
|
|
|
end={this.state.end}
|
|
|
|
room={this.rid}
|
|
|
|
renderFooter={this.renderHeader}
|
|
|
|
onEndReached={this.onEndReached}
|
|
|
|
renderRow={this.renderItem}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-08-04 00:34:37 +00:00
|
|
|
render() {
|
|
|
|
return (
|
2018-08-01 19:35:06 +00:00
|
|
|
<SafeAreaView style={styles.container} testID='room-view'>
|
2018-07-18 20:34:59 +00:00
|
|
|
{this.renderList()}
|
2017-08-10 16:16:32 +00:00
|
|
|
{this.renderFooter()}
|
2018-07-10 13:40:32 +00:00
|
|
|
{this.state.room._id && this.props.showActions ?
|
|
|
|
<MessageActions room={this.state.room} user={this.props.user} /> :
|
|
|
|
null}
|
2018-06-18 13:30:36 +00:00
|
|
|
{this.props.showErrorActions ? <MessageErrorActions /> : null}
|
2018-01-30 19:48:26 +00:00
|
|
|
<ReactionPicker onEmojiSelected={this.onReactionPress} />
|
2018-07-17 19:10:27 +00:00
|
|
|
<UploadProgress rid={this.rid} />
|
2018-08-01 19:35:06 +00:00
|
|
|
</SafeAreaView>
|
2017-08-04 00:34:37 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|