From c6401a2d392a5963967e808e4ed303cbe4565d23 Mon Sep 17 00:00:00 2001 From: Djorkaeff Alexandre Date: Fri, 17 Jul 2020 14:08:15 -0300 Subject: [PATCH] [FIX] Room Info actions doesn't check permissions/settings enabled (#2292) * [FIX] Show Call Button only when Jitsi Enabled (RoomInfoView) * [FIX] Show user info * [FIX] Show message button only if it's possible * [FIX] Create direct only when needed Co-authored-by: Diego Mello --- app/views/RoomInfoView/index.js | 61 ++++++++++++++++++++++++--------- 1 file changed, 44 insertions(+), 17 deletions(-) diff --git a/app/views/RoomInfoView/index.js b/app/views/RoomInfoView/index.js index b3711b0f4..8794995ac 100644 --- a/app/views/RoomInfoView/index.js +++ b/app/views/RoomInfoView/index.js @@ -22,6 +22,8 @@ import { themes } from '../../constants/colors'; import { withTheme } from '../../theme'; import { getUserSelector } from '../../selectors/login'; import Markdown from '../../containers/markdown'; +import { LISTENER } from '../../containers/Toast'; +import EventEmitter from '../../utils/events'; import Livechat from './Livechat'; import Channel from './Channel'; @@ -59,7 +61,8 @@ class RoomInfoView extends React.Component { baseUrl: PropTypes.string, rooms: PropTypes.array, theme: PropTypes.string, - isMasterDetail: PropTypes.bool + isMasterDetail: PropTypes.bool, + jitsiEnabled: PropTypes.bool } constructor(props) { @@ -167,11 +170,11 @@ class RoomInfoView extends React.Component { } loadUser = async() => { - const { room: roomState, roomUser } = this.state; + const { room, roomUser } = this.state; if (_.isEmpty(roomUser)) { try { - const roomUserId = RocketChat.getUidDirectMessage(roomState); + const roomUserId = RocketChat.getUidDirectMessage(room); const result = await RocketChat.getUserInfo(roomUserId); if (result.success) { const { user } = result; @@ -183,9 +186,7 @@ class RoomInfoView extends React.Component { })); } - const room = await this.getDirect(user.username); - - this.setState({ roomUser: user, room: { ...roomState, rid: room.rid } }); + this.setState({ roomUser: user }); } } catch { // do nothing @@ -220,16 +221,28 @@ class RoomInfoView extends React.Component { } } - getDirect = async(username) => { + createDirect = () => new Promise(async(resolve, reject) => { + const { route } = this.props; + + // We don't need to create a direct + const member = route.params?.member; + if (!_.isEmpty(member)) { + return resolve(); + } + + // TODO: Check if some direct with the user already exists on database try { + const { roomUser: { username } } = this.state; const result = await RocketChat.createDirectMessage(username); if (result.success) { - return result.room; + const { room: { rid } } = result; + return this.setState(({ room }) => ({ room: { ...room, rid } }), resolve); } } catch { // do nothing } - } + reject(); + }) goRoom = () => { const { roomUser, room } = this.state; @@ -287,9 +300,19 @@ class RoomInfoView extends React.Component { renderButton = (onPress, iconName, text) => { const { theme } = this.props; + + const onActionPress = async() => { + try { + await this.createDirect(); + onPress(); + } catch { + EventEmitter.emit(LISTENER, { message: I18n.t('error-action-not-allowed', { action: I18n.t('Create_Direct_Messages') }) }); + } + }; + return ( ( - - {this.renderButton(this.goRoom, 'message', I18n.t('Message'))} - {this.renderButton(this.videoCall, 'video-1', I18n.t('Video_call'))} - - ) + renderButtons = () => { + const { jitsiEnabled } = this.props; + return ( + + {this.renderButton(this.goRoom, 'message', I18n.t('Message'))} + {jitsiEnabled ? this.renderButton(this.videoCall, 'video-1', I18n.t('Video_call')) : null} + + ); + } renderContent = () => { const { room, roomUser } = this.state; @@ -348,7 +374,8 @@ const mapStateToProps = state => ({ baseUrl: state.server.server, user: getUserSelector(state), rooms: state.room.rooms, - isMasterDetail: state.app.isMasterDetail + isMasterDetail: state.app.isMasterDetail, + jitsiEnabled: state.settings.Jitsi_Enabled || false }); export default connect(mapStateToProps)(withTheme(RoomInfoView));