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';
|
2019-01-31 16:08:38 +00:00
|
|
|
import { connect } from 'react-redux';
|
2018-03-29 17:55:37 +00:00
|
|
|
import moment from 'moment';
|
2019-03-12 16:23:06 +00:00
|
|
|
import { SafeAreaView } from 'react-navigation';
|
2018-03-29 17:55:37 +00:00
|
|
|
|
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';
|
2019-08-23 16:24:15 +00:00
|
|
|
import database, { safeAddListener } from '../../lib/realm';
|
2018-03-29 17:55:37 +00:00
|
|
|
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';
|
2019-03-12 16:23:06 +00:00
|
|
|
import { CustomHeaderButtons, Item } from '../../containers/HeaderButton';
|
|
|
|
import StatusBar from '../../containers/StatusBar';
|
2019-04-29 16:03:52 +00:00
|
|
|
import log from '../../utils/log';
|
2018-03-29 17:55:37 +00:00
|
|
|
|
|
|
|
const PERMISSION_EDIT_ROOM = 'edit-room';
|
|
|
|
|
|
|
|
const camelize = str => str.replace(/^(.)/, (match, chr) => chr.toUpperCase());
|
2019-08-22 18:08:07 +00:00
|
|
|
const getRoomTitle = (room, type, name) => (type === 'd'
|
|
|
|
? <Text testID='room-info-view-name' style={styles.roomTitle}>{name}</Text>
|
2018-09-25 19:28:42 +00:00
|
|
|
: (
|
2018-08-31 16:46:33 +00:00
|
|
|
<View style={styles.roomTitleRow}>
|
2019-04-08 12:35:28 +00:00
|
|
|
<RoomTypeIcon type={room.prid ? 'discussion' : room.t} key='room-info-type' />
|
|
|
|
<Text testID='room-info-view-name' style={styles.roomTitle} key='room-info-name'>{room.prid ? room.fname : room.name}</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-03-12 16:23:06 +00:00
|
|
|
static navigationOptions = ({ navigation }) => {
|
|
|
|
const showEdit = navigation.getParam('showEdit');
|
|
|
|
const rid = navigation.getParam('rid');
|
2018-10-23 21:39:48 +00:00
|
|
|
return {
|
2019-03-12 16:23:06 +00:00
|
|
|
title: I18n.t('Room_Info'),
|
|
|
|
headerRight: showEdit
|
|
|
|
? (
|
|
|
|
<CustomHeaderButtons>
|
|
|
|
<Item iconName='edit' onPress={() => navigation.navigate('RoomInfoEditView', { rid })} testID='room-info-view-edit-button' />
|
|
|
|
</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-04-26 21:15:25 +00:00
|
|
|
Message_TimeFormat: PropTypes.string
|
2018-03-29 17:55:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
2019-05-28 13:03:08 +00:00
|
|
|
super(props);
|
2019-04-29 16:03:52 +00:00
|
|
|
this.rid = props.navigation.getParam('rid');
|
|
|
|
this.t = props.navigation.getParam('t');
|
|
|
|
this.roles = database.objects('roles');
|
2018-03-29 17:55:37 +00:00
|
|
|
this.sub = {
|
|
|
|
unsubscribe: () => {}
|
|
|
|
};
|
|
|
|
this.state = {
|
2019-08-22 18:08:07 +00:00
|
|
|
room: {},
|
2019-04-29 16:03:52 +00:00
|
|
|
roomUser: {}
|
2018-03-29 17:55:37 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-12-21 10:55:35 +00:00
|
|
|
async componentDidMount() {
|
2019-04-29 16:03:52 +00:00
|
|
|
if (this.t === 'd') {
|
|
|
|
const { user } = this.props;
|
|
|
|
const roomUserId = RocketChat.getRoomMemberId(this.rid, user.id);
|
|
|
|
try {
|
|
|
|
const result = await RocketChat.getUserInfo(roomUserId);
|
|
|
|
if (result.success) {
|
|
|
|
this.setState({ roomUser: result.user });
|
2018-12-21 10:55:35 +00:00
|
|
|
}
|
2019-08-23 13:18:47 +00:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2018-12-21 10:55:35 +00:00
|
|
|
}
|
2019-08-22 18:08:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-08-23 16:24:15 +00:00
|
|
|
this.rooms = database.objects('subscriptions').filtered('rid = $0', this.rid);
|
|
|
|
safeAddListener(this.rooms, this.updateRoom);
|
2019-08-22 18:08:07 +00:00
|
|
|
let room = {};
|
2019-08-23 16:24:15 +00:00
|
|
|
if (this.rooms.length > 0) {
|
|
|
|
this.setState({ room: this.rooms[0] });
|
|
|
|
[room] = this.rooms;
|
2019-08-22 18:08:07 +00:00
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
const result = await RocketChat.getRoomInfo(this.rid);
|
|
|
|
if (result.success) {
|
|
|
|
// eslint-disable-next-line prefer-destructuring
|
|
|
|
room = result.room;
|
|
|
|
this.setState({ room });
|
|
|
|
}
|
2019-08-30 12:43:23 +00:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2019-08-22 18:08:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
const permissions = RocketChat.hasPermission([PERMISSION_EDIT_ROOM], room.rid);
|
|
|
|
if (permissions[PERMISSION_EDIT_ROOM] && !room.prid) {
|
|
|
|
const { navigation } = this.props;
|
|
|
|
navigation.setParams({ showEdit: true });
|
2018-12-21 10:55:35 +00:00
|
|
|
}
|
2018-03-29 17:55:37 +00:00
|
|
|
}
|
|
|
|
|
2019-04-26 21:15:25 +00:00
|
|
|
getRoleDescription = (id) => {
|
|
|
|
const role = database.objectForPrimaryKey('roles', id);
|
|
|
|
if (role) {
|
|
|
|
return role.description;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-08-22 18:08:07 +00:00
|
|
|
isDirect = () => this.t === 'd'
|
2018-03-29 17:55:37 +00:00
|
|
|
|
2018-12-21 10:55:35 +00:00
|
|
|
updateRoom = () => {
|
|
|
|
if (this.rooms.length > 0) {
|
|
|
|
this.setState({ room: JSON.parse(JSON.stringify(this.rooms[0])) });
|
2018-09-25 19:28:42 +00:00
|
|
|
}
|
2018-03-29 17:55:37 +00:00
|
|
|
}
|
2018-06-01 17:38:13 +00:00
|
|
|
|
2018-03-29 17:55:37 +00:00
|
|
|
renderItem = (key, room) => (
|
|
|
|
<View style={styles.item}>
|
2018-06-01 17:38:13 +00:00
|
|
|
<Text style={styles.itemLabel}>{I18n.t(camelize(key))}</Text>
|
2018-05-23 13:39:18 +00:00
|
|
|
<Text
|
|
|
|
style={[styles.itemContent, !room[key] && styles.itemContent__empty]}
|
|
|
|
testID={`room-info-view-${ key }`}
|
2018-06-01 17:38:13 +00:00
|
|
|
>{ room[key] ? room[key] : I18n.t(`No_${ key }_provided`) }
|
2018-05-23 13:39:18 +00:00
|
|
|
</Text>
|
2018-03-29 17:55:37 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
|
2019-04-29 16:03:52 +00:00
|
|
|
renderRole = (role) => {
|
|
|
|
const description = this.getRoleDescription(role);
|
|
|
|
if (description) {
|
|
|
|
return (
|
|
|
|
<View style={styles.roleBadge} key={role}>
|
|
|
|
<Text style={styles.role}>{ this.getRoleDescription(role) }</Text>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
renderRoles = () => {
|
|
|
|
const { roomUser } = this.state;
|
|
|
|
if (roomUser && roomUser.roles && roomUser.roles.length) {
|
|
|
|
return (
|
|
|
|
<View style={styles.item}>
|
|
|
|
<Text style={styles.itemLabel}>{I18n.t('Roles')}</Text>
|
|
|
|
<View style={styles.rolesContainer}>
|
|
|
|
{roomUser.roles.map(role => this.renderRole(role))}
|
2018-09-25 19:28:42 +00:00
|
|
|
</View>
|
2019-04-29 16:03:52 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
2018-09-25 19:28:42 +00:00
|
|
|
}
|
2018-03-29 17:55:37 +00:00
|
|
|
|
2019-04-29 16:03:52 +00:00
|
|
|
renderTimezone = () => {
|
|
|
|
const { roomUser } = this.state;
|
|
|
|
const { Message_TimeFormat } = this.props;
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2019-04-29 16:03:52 +00:00
|
|
|
if (roomUser) {
|
|
|
|
const { utcOffset } = roomUser;
|
2018-03-29 17:55:37 +00:00
|
|
|
|
|
|
|
if (!utcOffset) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<View style={styles.item}>
|
2018-06-01 17:38:13 +00:00
|
|
|
<Text style={styles.itemLabel}>{I18n.t('Timezone')}</Text>
|
2018-09-25 19:28:42 +00:00
|
|
|
<Text style={styles.itemContent}>{moment().utcOffset(utcOffset).format(Message_TimeFormat)} (UTC { utcOffset })</Text>
|
2018-03-29 17:55:37 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
renderAvatar = (room, roomUser) => {
|
2019-02-07 19:58:20 +00:00
|
|
|
const { baseUrl, user } = 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-08-22 18:08:07 +00:00
|
|
|
{this.t === 'd' && roomUser._id ? <Status style={[sharedStyles.status, styles.status]} size={24} id={roomUser._id} /> : null}
|
2018-09-25 19:28:42 +00:00
|
|
|
</Avatar>
|
|
|
|
);
|
|
|
|
}
|
2018-05-18 17:55:08 +00:00
|
|
|
|
2018-05-24 20:17:45 +00:00
|
|
|
renderBroadcast = () => (
|
|
|
|
<View style={styles.item}>
|
2018-06-01 17:38:13 +00:00
|
|
|
<Text style={styles.itemLabel}>{I18n.t('Broadcast_Channel')}</Text>
|
2018-05-24 20:17:45 +00:00
|
|
|
<Text
|
|
|
|
style={styles.itemContent}
|
|
|
|
testID='room-info-view-broadcast'
|
2018-06-01 17:38:13 +00:00
|
|
|
>{I18n.t('Broadcast_channel_Description')}
|
2018-05-24 20:17:45 +00:00
|
|
|
</Text>
|
|
|
|
</View>
|
|
|
|
)
|
|
|
|
|
2019-04-29 16:03:52 +00:00
|
|
|
renderCustomFields = () => {
|
|
|
|
const { roomUser } = this.state;
|
|
|
|
if (roomUser) {
|
|
|
|
const { customFields } = roomUser;
|
2018-10-15 19:41:39 +00:00
|
|
|
|
2019-04-29 16:03:52 +00:00
|
|
|
if (!roomUser.customFields) {
|
2018-10-15 19:41:39 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
Object.keys(customFields).map((title) => {
|
|
|
|
if (!customFields[title]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<View style={styles.item} key={title}>
|
|
|
|
<Text style={styles.itemLabel}>{title}</Text>
|
|
|
|
<Text style={styles.itemContent}>{customFields[title]}</Text>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-08-22 18:08:07 +00:00
|
|
|
renderChannel = () => {
|
|
|
|
const { room } = this.state;
|
|
|
|
return (
|
|
|
|
<React.Fragment>
|
|
|
|
{this.renderItem('description', room)}
|
|
|
|
{this.renderItem('topic', room)}
|
|
|
|
{this.renderItem('announcement', room)}
|
|
|
|
{room.broadcast ? this.renderBroadcast() : null}
|
|
|
|
</React.Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderDirect = () => {
|
|
|
|
const { roomUser } = this.state;
|
|
|
|
return (
|
|
|
|
<React.Fragment>
|
|
|
|
{this.renderRoles()}
|
|
|
|
{this.renderTimezone()}
|
|
|
|
{this.renderCustomFields(roomUser._id)}
|
|
|
|
</React.Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-03-29 17:55:37 +00:00
|
|
|
render() {
|
|
|
|
const { room, roomUser } = this.state;
|
2018-05-18 17:55:08 +00:00
|
|
|
if (!room) {
|
|
|
|
return <View />;
|
|
|
|
}
|
2018-03-29 17:55:37 +00:00
|
|
|
return (
|
2018-08-01 19:35:06 +00:00
|
|
|
<ScrollView style={styles.scroll}>
|
2019-03-12 16:23:06 +00:00
|
|
|
<StatusBar />
|
2019-08-07 13:51:34 +00:00
|
|
|
<SafeAreaView style={styles.container} testID='room-info-view' forceInset={{ vertical: 'never' }}>
|
2018-08-01 19:35:06 +00:00
|
|
|
<View style={styles.avatarContainer}>
|
|
|
|
{this.renderAvatar(room, roomUser)}
|
2019-08-22 18:08:07 +00:00
|
|
|
<View style={styles.roomTitleContainer}>{ getRoomTitle(room, this.t, roomUser && roomUser.name) }</View>
|
2018-08-01 19:35:06 +00:00
|
|
|
</View>
|
2019-08-22 18:08:07 +00:00
|
|
|
{this.isDirect() ? this.renderDirect() : this.renderChannel()}
|
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 => ({
|
|
|
|
baseUrl: state.settings.Site_Url || state.server ? state.server.server : '',
|
|
|
|
user: {
|
|
|
|
id: state.login.user && state.login.user.id,
|
|
|
|
token: state.login.user && state.login.user.token
|
|
|
|
},
|
|
|
|
Message_TimeFormat: state.settings.Message_TimeFormat
|
|
|
|
});
|
|
|
|
|
|
|
|
export default connect(mapStateToProps)(RoomInfoView);
|