2018-03-29 17:55:37 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-10-23 21:39:48 +00:00
|
|
|
import { View, Text, ScrollView } from 'react-native';
|
2020-02-14 00:25:42 +00:00
|
|
|
import { BorderlessButton } from 'react-native-gesture-handler';
|
2019-01-31 16:08:38 +00:00
|
|
|
import { connect } from 'react-redux';
|
2019-10-02 12:18:08 +00:00
|
|
|
import { SafeAreaView } from 'react-navigation';
|
2020-05-08 17:36:10 +00:00
|
|
|
import UAParser from 'ua-parser-js';
|
|
|
|
import _ from 'lodash';
|
|
|
|
|
|
|
|
import database from '../../lib/database';
|
2020-02-14 00:25:42 +00:00
|
|
|
import { CustomIcon } from '../../lib/Icons';
|
2019-03-01 16:49:11 +00:00
|
|
|
import Status from '../../containers/Status';
|
2018-03-29 17:55:37 +00:00
|
|
|
import Avatar from '../../containers/Avatar';
|
|
|
|
import styles from './styles';
|
|
|
|
import sharedStyles from '../Styles';
|
|
|
|
import RocketChat from '../../lib/rocketchat';
|
2018-05-18 16:41:47 +00:00
|
|
|
import RoomTypeIcon from '../../containers/RoomTypeIcon';
|
2018-06-01 17:38:13 +00:00
|
|
|
import I18n from '../../i18n';
|
2020-05-08 17:36:10 +00:00
|
|
|
import { CustomHeaderButtons } from '../../containers/HeaderButton';
|
2019-03-12 16:23:06 +00:00
|
|
|
import StatusBar from '../../containers/StatusBar';
|
2019-04-29 16:03:52 +00:00
|
|
|
import log from '../../utils/log';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { themes } from '../../constants/colors';
|
|
|
|
import { withTheme } from '../../theme';
|
|
|
|
import { themedHeader } from '../../utils/navigation';
|
2020-02-11 14:09:14 +00:00
|
|
|
import { getUserSelector } from '../../selectors/login';
|
2020-02-20 20:23:09 +00:00
|
|
|
import Markdown from '../../containers/markdown';
|
2018-03-29 17:55:37 +00:00
|
|
|
|
2020-05-08 17:36:10 +00:00
|
|
|
import Livechat from './Livechat';
|
|
|
|
import Channel from './Channel';
|
|
|
|
import Item from './Item';
|
|
|
|
import Direct from './Direct';
|
|
|
|
|
2018-03-29 17:55:37 +00:00
|
|
|
const PERMISSION_EDIT_ROOM = 'edit-room';
|
2020-03-30 20:19:01 +00:00
|
|
|
const getRoomTitle = (room, type, name, username, statusText, theme) => (type === 'd'
|
2020-02-14 00:25:42 +00:00
|
|
|
? (
|
|
|
|
<>
|
|
|
|
<Text testID='room-info-view-name' style={[styles.roomTitle, { color: themes[theme].titleText }]}>{ name }</Text>
|
|
|
|
{username && <Text testID='room-info-view-username' style={[styles.roomUsername, { color: themes[theme].auxiliaryText }]}>{`@${ username }`}</Text>}
|
2020-03-30 20:19:01 +00:00
|
|
|
{!!statusText && <View testID='room-info-view-custom-status'><Markdown msg={statusText} style={[styles.roomUsername, { color: themes[theme].auxiliaryText }]} preview theme={theme} /></View>}
|
2020-02-14 00:25:42 +00:00
|
|
|
</>
|
|
|
|
)
|
2018-09-25 19:28:42 +00:00
|
|
|
: (
|
2018-08-31 16:46:33 +00:00
|
|
|
<View style={styles.roomTitleRow}>
|
2020-05-08 17:36:10 +00:00
|
|
|
<RoomTypeIcon type={room.prid ? 'discussion' : room.t} key='room-info-type' status={room.visitor?.status} theme={theme} />
|
2020-04-30 16:24:49 +00:00
|
|
|
<Text testID='room-info-view-name' style={[styles.roomTitle, { color: themes[theme].titleText }]} key='room-info-name'>{RocketChat.getRoomTitle(room)}</Text>
|
2018-08-31 16:46:33 +00:00
|
|
|
</View>
|
2018-09-25 19:28:42 +00:00
|
|
|
)
|
2018-05-23 13:39:18 +00:00
|
|
|
);
|
2018-07-10 13:40:32 +00:00
|
|
|
|
2019-08-07 13:51:34 +00:00
|
|
|
class RoomInfoView extends React.Component {
|
2019-12-04 16:39:53 +00:00
|
|
|
static navigationOptions = ({ navigation, screenProps }) => {
|
2020-01-28 13:28:25 +00:00
|
|
|
const t = navigation.getParam('t');
|
2020-05-08 17:36:10 +00:00
|
|
|
const rid = navigation.getParam('rid');
|
|
|
|
const room = navigation.getParam('room');
|
|
|
|
const roomUser = navigation.getParam('roomUser');
|
|
|
|
const showEdit = navigation.getParam('showEdit', t === 'l');
|
2018-10-23 21:39:48 +00:00
|
|
|
return {
|
2020-01-28 13:28:25 +00:00
|
|
|
title: t === 'd' ? I18n.t('User_Info') : I18n.t('Room_Info'),
|
2019-12-04 16:39:53 +00:00
|
|
|
...themedHeader(screenProps.theme),
|
2019-03-12 16:23:06 +00:00
|
|
|
headerRight: showEdit
|
|
|
|
? (
|
|
|
|
<CustomHeaderButtons>
|
2020-05-08 17:36:10 +00:00
|
|
|
<Item
|
|
|
|
iconName='edit'
|
|
|
|
onPress={() => navigation.navigate(t === 'l' ? 'LivechatEditView' : 'RoomInfoEditView', { rid, room, roomUser })}
|
|
|
|
testID='room-info-view-edit-button'
|
|
|
|
/>
|
2019-03-12 16:23:06 +00:00
|
|
|
</CustomHeaderButtons>
|
|
|
|
)
|
|
|
|
: null
|
2018-10-23 21:39:48 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-03-29 17:55:37 +00:00
|
|
|
static propTypes = {
|
2019-03-12 16:23:06 +00:00
|
|
|
navigation: PropTypes.object,
|
2019-02-07 19:58:20 +00:00
|
|
|
user: PropTypes.shape({
|
|
|
|
id: PropTypes.string,
|
|
|
|
token: PropTypes.string
|
|
|
|
}),
|
2018-09-11 16:32:52 +00:00
|
|
|
baseUrl: PropTypes.string,
|
2019-12-04 16:39:53 +00:00
|
|
|
theme: PropTypes.string
|
2018-03-29 17:55:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
2019-05-28 13:03:08 +00:00
|
|
|
super(props);
|
2019-09-16 20:26:32 +00:00
|
|
|
const room = props.navigation.getParam('room');
|
2020-03-30 20:19:01 +00:00
|
|
|
const roomUser = props.navigation.getParam('member');
|
2019-04-29 16:03:52 +00:00
|
|
|
this.rid = props.navigation.getParam('rid');
|
|
|
|
this.t = props.navigation.getParam('t');
|
2018-03-29 17:55:37 +00:00
|
|
|
this.state = {
|
2020-04-01 12:28:54 +00:00
|
|
|
room: room || { rid: this.rid, t: this.t },
|
2020-05-08 17:36:10 +00:00
|
|
|
roomUser: roomUser || {}
|
2018-03-29 17:55:37 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:36:10 +00:00
|
|
|
componentDidMount() {
|
|
|
|
if (this.isDirect) {
|
|
|
|
this.loadUser();
|
|
|
|
} else {
|
|
|
|
this.loadRoom();
|
|
|
|
}
|
|
|
|
|
|
|
|
const { navigation } = this.props;
|
|
|
|
this.willFocusListener = navigation.addListener('willFocus', () => {
|
|
|
|
if (this.isLivechat) {
|
|
|
|
this.loadVisitor();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.subscription && this.subscription.unsubscribe) {
|
|
|
|
this.subscription.unsubscribe();
|
|
|
|
}
|
|
|
|
if (this.willFocusListener && this.willFocusListener.remove) {
|
|
|
|
this.willFocusListener.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get isDirect() {
|
|
|
|
const { room } = this.state;
|
|
|
|
return room.t === 'd';
|
|
|
|
}
|
|
|
|
|
|
|
|
get isLivechat() {
|
|
|
|
const { room } = this.state;
|
|
|
|
return room.t === 'l';
|
|
|
|
}
|
|
|
|
|
|
|
|
getRoleDescription = async(id) => {
|
|
|
|
const db = database.active;
|
|
|
|
try {
|
|
|
|
const rolesCollection = db.collections.get('roles');
|
|
|
|
const role = await rolesCollection.find(id);
|
|
|
|
if (role) {
|
|
|
|
return role.description;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
} catch (e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
loadVisitor = async() => {
|
|
|
|
const { room } = this.state;
|
|
|
|
const { navigation } = this.props;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const result = await RocketChat.getVisitorInfo(room?.visitor?._id);
|
|
|
|
if (result.success) {
|
|
|
|
const { visitor } = result;
|
|
|
|
if (visitor.userAgent) {
|
|
|
|
const ua = new UAParser();
|
|
|
|
ua.setUA(visitor.userAgent);
|
|
|
|
visitor.os = `${ ua.getOS().name } ${ ua.getOS().version }`;
|
|
|
|
visitor.browser = `${ ua.getBrowser().name } ${ ua.getBrowser().version }`;
|
|
|
|
}
|
|
|
|
this.setState({ roomUser: visitor });
|
|
|
|
navigation.setParams({ roomUser: visitor });
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
// Do nothing
|
2020-03-30 20:19:01 +00:00
|
|
|
}
|
2020-05-08 17:36:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
loadUser = async() => {
|
|
|
|
const { room: roomState, roomUser } = this.state;
|
2020-03-30 20:19:01 +00:00
|
|
|
|
2020-05-08 17:36:10 +00:00
|
|
|
if (_.isEmpty(roomUser)) {
|
2019-04-29 16:03:52 +00:00
|
|
|
try {
|
2020-04-01 12:28:54 +00:00
|
|
|
const roomUserId = RocketChat.getUidDirectMessage(roomState);
|
2019-04-29 16:03:52 +00:00
|
|
|
const result = await RocketChat.getUserInfo(roomUserId);
|
|
|
|
if (result.success) {
|
2020-05-08 17:36:10 +00:00
|
|
|
const { user } = result;
|
|
|
|
const { roles } = user;
|
2019-09-16 20:26:32 +00:00
|
|
|
if (roles && roles.length) {
|
2020-05-08 17:36:10 +00:00
|
|
|
user.parsedRoles = await Promise.all(roles.map(async(role) => {
|
2019-09-16 20:26:32 +00:00
|
|
|
const description = await this.getRoleDescription(role);
|
|
|
|
return description;
|
|
|
|
}));
|
|
|
|
}
|
2020-05-08 17:36:10 +00:00
|
|
|
|
|
|
|
const room = await this.getDirect(user.username);
|
|
|
|
|
|
|
|
this.setState({ roomUser: user, room: { ...roomState, rid: room.rid } });
|
2018-12-21 10:55:35 +00:00
|
|
|
}
|
2020-05-08 17:36:10 +00:00
|
|
|
} catch {
|
|
|
|
// do nothing
|
2018-12-21 10:55:35 +00:00
|
|
|
}
|
2019-08-22 18:08:07 +00:00
|
|
|
}
|
2020-05-08 17:36:10 +00:00
|
|
|
}
|
2020-04-01 12:28:54 +00:00
|
|
|
|
2020-05-08 17:36:10 +00:00
|
|
|
loadRoom = async() => {
|
2019-09-16 20:26:32 +00:00
|
|
|
const { navigation } = this.props;
|
|
|
|
let room = navigation.getParam('room');
|
|
|
|
if (room && room.observe) {
|
|
|
|
this.roomObservable = room.observe();
|
|
|
|
this.subscription = this.roomObservable
|
|
|
|
.subscribe((changes) => {
|
|
|
|
this.setState({ room: changes });
|
2020-05-08 17:36:10 +00:00
|
|
|
navigation.setParams({ room: changes });
|
2019-09-16 20:26:32 +00:00
|
|
|
});
|
2019-08-22 18:08:07 +00:00
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
const result = await RocketChat.getRoomInfo(this.rid);
|
|
|
|
if (result.success) {
|
2020-05-08 17:36:10 +00:00
|
|
|
({ room } = result);
|
2019-08-22 18:08:07 +00:00
|
|
|
this.setState({ room });
|
|
|
|
}
|
2019-08-30 12:43:23 +00:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2019-08-22 18:08:07 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-08 17:36:10 +00:00
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
const permissions = await RocketChat.hasPermission([PERMISSION_EDIT_ROOM], room.rid);
|
2020-05-08 17:36:10 +00:00
|
|
|
if (permissions[PERMISSION_EDIT_ROOM] && !room.prid) {
|
2019-08-22 18:08:07 +00:00
|
|
|
navigation.setParams({ showEdit: true });
|
2018-12-21 10:55:35 +00:00
|
|
|
}
|
2018-03-29 17:55:37 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 17:36:10 +00:00
|
|
|
getDirect = async(username) => {
|
2019-09-16 20:26:32 +00:00
|
|
|
try {
|
2020-05-08 17:36:10 +00:00
|
|
|
const result = await RocketChat.createDirectMessage(username);
|
|
|
|
if (result.success) {
|
|
|
|
return result.room;
|
2019-09-16 20:26:32 +00:00
|
|
|
}
|
2020-05-08 17:36:10 +00:00
|
|
|
} catch {
|
|
|
|
// do nothing
|
2018-09-25 19:28:42 +00:00
|
|
|
}
|
2018-03-29 17:55:37 +00:00
|
|
|
}
|
2018-06-01 17:38:13 +00:00
|
|
|
|
2020-02-14 00:25:42 +00:00
|
|
|
goRoom = async() => {
|
2020-05-08 17:36:10 +00:00
|
|
|
const { roomUser, room } = this.state;
|
2020-02-14 00:25:42 +00:00
|
|
|
const { navigation } = this.props;
|
|
|
|
try {
|
2020-05-08 17:36:10 +00:00
|
|
|
if (room.rid) {
|
2020-02-14 00:25:42 +00:00
|
|
|
await navigation.navigate('RoomsListView');
|
2020-05-08 17:36:10 +00:00
|
|
|
navigation.navigate('RoomView', { rid: room.rid, name: RocketChat.getRoomTitle(roomUser), t: 'd' });
|
2020-02-14 00:25:42 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:36:10 +00:00
|
|
|
videoCall = () => {
|
|
|
|
const { room } = this.state;
|
|
|
|
RocketChat.callJitsi(room.rid);
|
2018-03-29 17:55:37 +00:00
|
|
|
}
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
renderAvatar = (room, roomUser) => {
|
2019-12-17 14:08:06 +00:00
|
|
|
const { baseUrl, user, theme } = this.props;
|
2018-09-25 19:28:42 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Avatar
|
2019-08-22 18:08:07 +00:00
|
|
|
text={room.name || roomUser.username}
|
2018-09-25 19:28:42 +00:00
|
|
|
size={100}
|
|
|
|
style={styles.avatar}
|
2019-08-22 18:08:07 +00:00
|
|
|
type={this.t}
|
2018-09-25 19:28:42 +00:00
|
|
|
baseUrl={baseUrl}
|
2019-04-18 20:57:35 +00:00
|
|
|
userId={user.id}
|
|
|
|
token={user.token}
|
2018-09-25 19:28:42 +00:00
|
|
|
>
|
2019-12-17 14:08:06 +00:00
|
|
|
{this.t === 'd' && roomUser._id ? <Status style={[sharedStyles.status, styles.status]} theme={theme} size={24} id={roomUser._id} /> : null}
|
2018-09-25 19:28:42 +00:00
|
|
|
</Avatar>
|
|
|
|
);
|
|
|
|
}
|
2018-05-18 17:55:08 +00:00
|
|
|
|
2020-02-14 00:25:42 +00:00
|
|
|
renderButton = (onPress, iconName, text) => {
|
|
|
|
const { theme } = this.props;
|
|
|
|
return (
|
|
|
|
<BorderlessButton
|
|
|
|
onPress={onPress}
|
|
|
|
style={styles.roomButton}
|
|
|
|
>
|
|
|
|
<CustomIcon
|
|
|
|
name={iconName}
|
|
|
|
size={30}
|
|
|
|
color={themes[theme].actionTintColor}
|
|
|
|
/>
|
|
|
|
<Text style={[styles.roomButtonText, { color: themes[theme].actionTintColor }]}>{text}</Text>
|
|
|
|
</BorderlessButton>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderButtons = () => (
|
|
|
|
<View style={styles.roomButtonsContainer}>
|
|
|
|
{this.renderButton(this.goRoom, 'message', I18n.t('Message'))}
|
|
|
|
{this.renderButton(this.videoCall, 'video', I18n.t('Video_call'))}
|
|
|
|
</View>
|
|
|
|
)
|
|
|
|
|
2020-05-08 17:36:10 +00:00
|
|
|
renderContent = () => {
|
|
|
|
const { room, roomUser } = this.state;
|
|
|
|
const { theme } = this.props;
|
2019-08-22 18:08:07 +00:00
|
|
|
|
2020-05-08 17:36:10 +00:00
|
|
|
if (this.isDirect) {
|
|
|
|
return <Direct roomUser={roomUser} theme={theme} />;
|
|
|
|
} else if (this.t === 'l') {
|
|
|
|
return <Livechat room={room} roomUser={roomUser} theme={theme} />;
|
|
|
|
}
|
|
|
|
return <Channel room={room} theme={theme} />;
|
2019-08-22 18:08:07 +00:00
|
|
|
}
|
|
|
|
|
2018-03-29 17:55:37 +00:00
|
|
|
render() {
|
|
|
|
const { room, roomUser } = this.state;
|
2019-12-04 16:39:53 +00:00
|
|
|
const { theme } = this.props;
|
2018-03-29 17:55:37 +00:00
|
|
|
return (
|
2019-12-04 16:39:53 +00:00
|
|
|
<ScrollView style={[styles.scroll, { backgroundColor: themes[theme].backgroundColor }]}>
|
|
|
|
<StatusBar theme={theme} />
|
|
|
|
<SafeAreaView
|
|
|
|
style={[styles.container, { backgroundColor: themes[theme].backgroundColor }]}
|
|
|
|
forceInset={{ vertical: 'never' }}
|
|
|
|
testID='room-info-view'
|
|
|
|
>
|
2020-05-08 17:36:10 +00:00
|
|
|
<View style={[styles.avatarContainer, this.isDirect && styles.avatarContainerDirectRoom, { backgroundColor: themes[theme].auxiliaryBackground }]}>
|
2018-08-01 19:35:06 +00:00
|
|
|
{this.renderAvatar(room, roomUser)}
|
2020-05-08 17:36:10 +00:00
|
|
|
<View style={styles.roomTitleContainer}>{ getRoomTitle(room, this.t, roomUser?.name, roomUser?.username, roomUser?.statusText, theme) }</View>
|
|
|
|
{this.isDirect ? this.renderButtons() : null}
|
2018-08-01 19:35:06 +00:00
|
|
|
</View>
|
2020-05-08 17:36:10 +00:00
|
|
|
{this.renderContent()}
|
2018-08-01 19:35:06 +00:00
|
|
|
</SafeAreaView>
|
2018-03-29 17:55:37 +00:00
|
|
|
</ScrollView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 13:51:34 +00:00
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
2020-02-11 14:09:14 +00:00
|
|
|
baseUrl: state.server.server,
|
2020-05-08 17:36:10 +00:00
|
|
|
user: getUserSelector(state)
|
2019-08-07 13:51:34 +00:00
|
|
|
});
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
export default connect(mapStateToProps)(withTheme(RoomInfoView));
|