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';
|
2018-10-23 21:39:48 +00:00
|
|
|
import SafeAreaView from 'react-native-safe-area-view';
|
2018-12-21 10:55:35 +00:00
|
|
|
import equal from 'deep-equal';
|
2018-03-29 17:55:37 +00:00
|
|
|
|
2019-01-31 16:08:38 +00:00
|
|
|
import Navigation from '../../lib/Navigation';
|
2018-04-03 16:24:59 +00:00
|
|
|
import LoggedView from '../View';
|
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 database from '../../lib/realm';
|
|
|
|
import RocketChat from '../../lib/rocketchat';
|
2018-05-18 17:55:08 +00:00
|
|
|
import log from '../../utils/log';
|
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-02-07 16:04:41 +00:00
|
|
|
import Icons from '../../lib/Icons';
|
2018-03-29 17:55:37 +00:00
|
|
|
|
|
|
|
const PERMISSION_EDIT_ROOM = 'edit-room';
|
|
|
|
|
|
|
|
const camelize = str => str.replace(/^(.)/, (match, chr) => chr.toUpperCase());
|
2018-09-25 19:28:42 +00:00
|
|
|
const getRoomTitle = room => (room.t === 'd'
|
|
|
|
? <Text testID='room-info-view-name' style={styles.roomTitle}>{room.fname}</Text>
|
|
|
|
: (
|
2018-08-31 16:46:33 +00:00
|
|
|
<View style={styles.roomTitleRow}>
|
|
|
|
<RoomTypeIcon type={room.t} key='room-info-type' />
|
|
|
|
<Text testID='room-info-view-name' style={styles.roomTitle} key='room-info-name'>{room.name}</Text>
|
|
|
|
</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
|
|
|
|
2018-03-29 17:55:37 +00:00
|
|
|
@connect(state => ({
|
|
|
|
baseUrl: state.settings.Site_Url || state.server ? state.server.server : '',
|
2019-02-07 19:58:20 +00:00
|
|
|
user: {
|
|
|
|
id: state.login.user && state.login.user.id,
|
|
|
|
token: state.login.user && state.login.user.token
|
|
|
|
},
|
2018-12-21 10:55:35 +00:00
|
|
|
activeUsers: state.activeUsers, // TODO: remove it
|
2018-03-29 17:55:37 +00:00
|
|
|
Message_TimeFormat: state.settings.Message_TimeFormat,
|
2018-12-21 10:55:35 +00:00
|
|
|
allRoles: state.roles,
|
|
|
|
room: state.room
|
2018-03-29 17:55:37 +00:00
|
|
|
}))
|
2018-07-10 13:40:32 +00:00
|
|
|
/** @extends React.Component */
|
2018-04-03 16:24:59 +00:00
|
|
|
export default class RoomInfoView extends LoggedView {
|
2018-10-23 21:39:48 +00:00
|
|
|
static options() {
|
|
|
|
return {
|
|
|
|
topBar: {
|
|
|
|
title: {
|
|
|
|
text: I18n.t('Room_Info')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-03-29 17:55:37 +00:00
|
|
|
static propTypes = {
|
2018-10-23 21:39:48 +00:00
|
|
|
componentId: PropTypes.string,
|
2018-07-10 13:40:32 +00:00
|
|
|
rid: PropTypes.string,
|
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,
|
2018-03-29 17:55:37 +00:00
|
|
|
activeUsers: PropTypes.object,
|
|
|
|
Message_TimeFormat: PropTypes.string,
|
2018-12-21 10:55:35 +00:00
|
|
|
allRoles: PropTypes.object,
|
|
|
|
room: PropTypes.object
|
2018-03-29 17:55:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
2018-04-03 16:24:59 +00:00
|
|
|
super('RoomInfoView', props);
|
2018-12-21 10:55:35 +00:00
|
|
|
const { rid, room } = props;
|
2018-03-29 17:55:37 +00:00
|
|
|
this.rooms = database.objects('subscriptions').filtered('rid = $0', rid);
|
|
|
|
this.sub = {
|
|
|
|
unsubscribe: () => {}
|
|
|
|
};
|
|
|
|
this.state = {
|
2018-12-21 10:55:35 +00:00
|
|
|
room,
|
2018-03-29 17:55:37 +00:00
|
|
|
roomUser: {},
|
|
|
|
roles: []
|
|
|
|
};
|
2018-10-23 21:39:48 +00:00
|
|
|
Navigation.events().bindComponent(this);
|
2018-03-29 17:55:37 +00:00
|
|
|
}
|
|
|
|
|
2018-12-21 10:55:35 +00:00
|
|
|
async componentDidMount() {
|
2018-03-29 17:55:37 +00:00
|
|
|
this.rooms.addListener(this.updateRoom);
|
2018-10-23 21:39:48 +00:00
|
|
|
|
2018-12-21 10:55:35 +00:00
|
|
|
let room = {};
|
|
|
|
if (this.rooms.length > 0) {
|
|
|
|
room = this.rooms[0]; // eslint-disable-line prefer-destructuring
|
|
|
|
} else {
|
|
|
|
room = this.state.room; // eslint-disable-line
|
|
|
|
}
|
2018-10-23 21:39:48 +00:00
|
|
|
const { componentId } = this.props;
|
|
|
|
const permissions = RocketChat.hasPermission([PERMISSION_EDIT_ROOM], room.rid);
|
|
|
|
if (permissions[PERMISSION_EDIT_ROOM]) {
|
|
|
|
Navigation.mergeOptions(componentId, {
|
|
|
|
topBar: {
|
|
|
|
rightButtons: [{
|
|
|
|
id: 'edit',
|
2019-03-01 16:49:11 +00:00
|
|
|
icon: Icons.getSource('edit'),
|
2018-10-23 21:39:48 +00:00
|
|
|
testID: 'room-info-view-edit-button'
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-12-21 10:55:35 +00:00
|
|
|
|
|
|
|
// get user of room
|
|
|
|
if (room) {
|
|
|
|
if (room.t === 'd') {
|
|
|
|
try {
|
2019-02-07 19:58:20 +00:00
|
|
|
const { user, activeUsers } = this.props;
|
|
|
|
const roomUser = await RocketChat.getRoomMember(room.rid, user.id);
|
2018-12-21 10:55:35 +00:00
|
|
|
this.setState({ roomUser: roomUser || {} });
|
|
|
|
const username = room.name;
|
|
|
|
|
|
|
|
const activeUser = activeUsers[roomUser._id];
|
|
|
|
if (!activeUser || !activeUser.utcOffset) {
|
|
|
|
// get full user data looking for utcOffset
|
|
|
|
// will be catched by .on('users) and saved on activeUsers reducer
|
|
|
|
this.getFullUserData(username);
|
|
|
|
}
|
|
|
|
|
|
|
|
// get all users roles
|
|
|
|
// needs to be changed by a better method
|
|
|
|
const allUsersRoles = await RocketChat.getUserRoles();
|
2019-02-07 19:58:20 +00:00
|
|
|
const userRoles = allUsersRoles.find(u => u.username === username);
|
2018-12-21 10:55:35 +00:00
|
|
|
if (userRoles) {
|
|
|
|
this.setState({ roles: userRoles.roles || [] });
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
log('RoomInfoView.componentDidMount', e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
|
|
|
const {
|
|
|
|
room, roomUser, roles
|
|
|
|
} = this.state;
|
|
|
|
const { activeUsers } = this.props;
|
|
|
|
if (!equal(nextState.room, room)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!equal(nextState.roomUser, roomUser)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!equal(nextState.roles, roles)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (roomUser._id) {
|
|
|
|
if (nextProps.activeUsers[roomUser._id] !== activeUsers[roomUser._id]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2018-03-29 17:55:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.rooms.removeAllListeners();
|
|
|
|
this.sub.unsubscribe();
|
|
|
|
}
|
|
|
|
|
2018-10-23 21:39:48 +00:00
|
|
|
navigationButtonPressed = ({ buttonId }) => {
|
|
|
|
const { rid, componentId } = this.props;
|
|
|
|
if (buttonId === 'edit') {
|
|
|
|
Navigation.push(componentId, {
|
|
|
|
component: {
|
|
|
|
id: 'RoomInfoEditView',
|
|
|
|
name: 'RoomInfoEditView',
|
2018-07-10 13:40:32 +00:00
|
|
|
passProps: {
|
2018-09-25 19:28:42 +00:00
|
|
|
rid
|
2018-07-10 13:40:32 +00:00
|
|
|
}
|
2018-10-23 21:39:48 +00:00
|
|
|
}
|
|
|
|
});
|
2018-07-10 13:40:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-29 17:55:37 +00:00
|
|
|
getFullUserData = async(username) => {
|
2018-06-18 13:30:36 +00:00
|
|
|
try {
|
|
|
|
const result = await RocketChat.subscribe('fullUserData', username);
|
|
|
|
this.sub = result;
|
|
|
|
} catch (e) {
|
|
|
|
log('getFullUserData', e);
|
|
|
|
}
|
2018-03-29 17:55:37 +00:00
|
|
|
}
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
isDirect = () => {
|
|
|
|
const { room: { t } } = this.state;
|
|
|
|
return 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>
|
|
|
|
);
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
renderRoles = () => {
|
|
|
|
const { roles } = this.state;
|
|
|
|
const { allRoles } = this.props;
|
|
|
|
|
|
|
|
return (
|
|
|
|
roles.length > 0
|
|
|
|
? (
|
|
|
|
<View style={styles.item}>
|
|
|
|
<Text style={styles.itemLabel}>{I18n.t('Roles')}</Text>
|
|
|
|
<View style={styles.rolesContainer}>
|
|
|
|
{roles.map(role => (
|
|
|
|
<View style={styles.roleBadge} key={role}>
|
|
|
|
<Text>{ allRoles[role] }</Text>
|
|
|
|
</View>
|
|
|
|
))}
|
2018-06-13 01:33:00 +00:00
|
|
|
</View>
|
2018-09-25 19:28:42 +00:00
|
|
|
</View>
|
|
|
|
)
|
|
|
|
: null
|
|
|
|
);
|
|
|
|
}
|
2018-03-29 17:55:37 +00:00
|
|
|
|
|
|
|
renderTimezone = (userId) => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { activeUsers, Message_TimeFormat } = this.props;
|
|
|
|
|
|
|
|
if (activeUsers[userId]) {
|
|
|
|
const { utcOffset } = activeUsers[userId];
|
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
|
|
|
|
text={room.name}
|
|
|
|
size={100}
|
|
|
|
style={styles.avatar}
|
|
|
|
type={room.t}
|
|
|
|
baseUrl={baseUrl}
|
2019-02-07 19:58:20 +00:00
|
|
|
user={user}
|
2018-09-25 19:28:42 +00:00
|
|
|
>
|
2019-03-01 16:49:11 +00:00
|
|
|
{room.t === 'd' ? <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>
|
|
|
|
)
|
|
|
|
|
2018-10-15 19:41:39 +00:00
|
|
|
renderCustomFields = (userId) => {
|
|
|
|
const { activeUsers } = this.props;
|
|
|
|
if (activeUsers[userId]) {
|
|
|
|
const { customFields } = activeUsers[userId];
|
|
|
|
|
|
|
|
if (!customFields) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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}>
|
2018-10-23 21:39:48 +00:00
|
|
|
<SafeAreaView style={styles.container} testID='room-info-view' forceInset={{ bottom: 'never' }}>
|
2018-08-01 19:35:06 +00:00
|
|
|
<View style={styles.avatarContainer}>
|
|
|
|
{this.renderAvatar(room, roomUser)}
|
|
|
|
<View style={styles.roomTitleContainer}>{ getRoomTitle(room) }</View>
|
|
|
|
</View>
|
|
|
|
{!this.isDirect() ? this.renderItem('description', room) : null}
|
|
|
|
{!this.isDirect() ? this.renderItem('topic', room) : null}
|
|
|
|
{!this.isDirect() ? this.renderItem('announcement', room) : null}
|
|
|
|
{this.isDirect() ? this.renderRoles() : null}
|
|
|
|
{this.isDirect() ? this.renderTimezone(roomUser._id) : null}
|
2018-10-15 19:41:39 +00:00
|
|
|
{this.isDirect() ? this.renderCustomFields(roomUser._id) : null}
|
2018-08-01 19:35:06 +00:00
|
|
|
{room.broadcast ? this.renderBroadcast() : null}
|
|
|
|
</SafeAreaView>
|
2018-03-29 17:55:37 +00:00
|
|
|
</ScrollView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|