From 199331f8f6bec7079781eaa3b0732aad1f1b25d3 Mon Sep 17 00:00:00 2001 From: Reinaldo Neto <47038980+reinaldonetof@users.noreply.github.com> Date: Mon, 16 Oct 2023 17:55:38 -0300 Subject: [PATCH] fix: check the permissions properly to create a discussion and reply in dm (#5217) * fix: check the permissions properly to create a discussion * add the permission start-discussion-other-user and fix the permission at messagebox --------- Co-authored-by: Gleidson Daniel Silva --- app/containers/MessageActions/index.tsx | 31 ++++++++++++------ app/containers/MessageBox/index.tsx | 43 ++++++++++++++++++------- app/lib/methods/getPermissions.ts | 1 + 3 files changed, 53 insertions(+), 22 deletions(-) diff --git a/app/containers/MessageActions/index.tsx b/app/containers/MessageActions/index.tsx index 9ee4be937..2f9864659 100644 --- a/app/containers/MessageActions/index.tsx +++ b/app/containers/MessageActions/index.tsx @@ -44,6 +44,7 @@ export interface IMessageActionsProps { deleteOwnMessagePermission?: string[]; pinMessagePermission?: string[]; createDirectMessagePermission?: string[]; + createDiscussionOtherUserPermission?: string[]; } export interface IMessageActions { @@ -76,6 +77,7 @@ const MessageActions = React.memo( deleteOwnMessagePermission, pinMessagePermission, createDirectMessagePermission, + createDiscussionOtherUserPermission, serverVersion }, ref @@ -85,7 +87,9 @@ const MessageActions = React.memo( hasDeletePermission: false, hasForceDeletePermission: false, hasPinPermission: false, - hasDeleteOwnPermission: false + hasDeleteOwnPermission: false, + hasCreateDirectMessagePermission: false, + hasCreateDiscussionOtherUserPermission: false }; const { showActionSheet, hideActionSheet } = useActionSheet(); @@ -96,7 +100,9 @@ const MessageActions = React.memo( deleteMessagePermission, forceDeleteMessagePermission, pinMessagePermission, - deleteOwnMessagePermission + deleteOwnMessagePermission, + createDirectMessagePermission, + createDiscussionOtherUserPermission ]; const result = await hasPermission(permission, room.rid); permissions = { @@ -104,7 +110,9 @@ const MessageActions = React.memo( hasDeletePermission: result[1], hasForceDeletePermission: result[2], hasPinPermission: result[3], - hasDeleteOwnPermission: result[4] + hasDeleteOwnPermission: result[4], + hasCreateDirectMessagePermission: result[5], + hasCreateDiscussionOtherUserPermission: result[6] }; } catch { // Do nothing @@ -385,7 +393,7 @@ const MessageActions = React.memo( } // Reply in DM - if (room.t !== 'd' && room.t !== 'l' && createDirectMessagePermission && !videoConfBlock) { + if (room.t !== 'd' && room.t !== 'l' && permissions.hasCreateDirectMessagePermission && !videoConfBlock) { options.push({ title: I18n.t('Reply_in_direct_message'), icon: 'arrow-back', @@ -394,11 +402,13 @@ const MessageActions = React.memo( } // Create Discussion - options.push({ - title: I18n.t('Start_a_Discussion'), - icon: 'discussions', - onPress: () => handleCreateDiscussion(message) - }); + if (permissions.hasCreateDiscussionOtherUserPermission) { + options.push({ + title: I18n.t('Start_a_Discussion'), + icon: 'discussions', + onPress: () => handleCreateDiscussion(message) + }); + } if (compareServerVersion(serverVersion, 'greaterThanOrEqualTo', '6.2.0') && !videoConfBlock) { options.push({ @@ -541,7 +551,8 @@ const mapStateToProps = (state: IApplicationState) => ({ deleteOwnMessagePermission: state.permissions['delete-own-message'], forceDeleteMessagePermission: state.permissions['force-delete-message'], pinMessagePermission: state.permissions['pin-message'], - createDirectMessagePermission: state.permissions['create-d'] + createDirectMessagePermission: state.permissions['create-d'], + createDiscussionOtherUserPermission: state.permissions['start-discussion-other-user'] }); export default connect(mapStateToProps, null, null, { forwardRef: true })(MessageActions); diff --git a/app/containers/MessageBox/index.tsx b/app/containers/MessageBox/index.tsx index cbf4dfd94..695d8572d 100644 --- a/app/containers/MessageBox/index.tsx +++ b/app/containers/MessageBox/index.tsx @@ -112,6 +112,7 @@ export interface IMessageBoxProps extends IBaseScreen void | null; serverVersion: string; } @@ -130,6 +131,7 @@ interface IMessageBoxState { tshow: boolean; mentionLoading: boolean; permissionToUpload: boolean; + hasCreateDiscussionPermission: boolean; showEmojiSearchbar: boolean; } @@ -184,7 +186,8 @@ class MessageBox extends Component { tshow: this.sendThreadToChannel, mentionLoading: false, permissionToUpload: true, - showEmojiSearchbar: false + showEmojiSearchbar: false, + hasCreateDiscussionPermission: false }; this.text = ''; this.selection = { start: 0, end: 0 }; @@ -325,6 +328,7 @@ class MessageBox extends Component { mentionLoading, trackingType, permissionToUpload, + hasCreateDiscussionPermission, showEmojiSearchbar } = this.state; @@ -337,6 +341,7 @@ class MessageBox extends Component { theme, usedCannedResponse, uploadFilePermission, + createDiscussionPermission, goToCannedResponses } = this.props; if (nextProps.theme !== theme) { @@ -378,6 +383,9 @@ class MessageBox extends Component { if (nextState.permissionToUpload !== permissionToUpload) { return true; } + if (nextState.hasCreateDiscussionPermission !== hasCreateDiscussionPermission) { + return true; + } if (!dequal(nextState.mentions, mentions)) { return true; } @@ -390,6 +398,9 @@ class MessageBox extends Component { if (!dequal(nextProps.uploadFilePermission, uploadFilePermission)) { return true; } + if (!dequal(nextProps.createDiscussionPermission, createDiscussionPermission)) { + return true; + } if (nextProps.usedCannedResponse !== usedCannedResponse) { return true; } @@ -400,13 +411,18 @@ class MessageBox extends Component { } componentDidUpdate(prevProps: IMessageBoxProps) { - const { uploadFilePermission, goToCannedResponses, replyWithMention, threadsEnabled } = this.props; + const { uploadFilePermission, goToCannedResponses, replyWithMention, threadsEnabled, createDiscussionPermission } = + this.props; if (prevProps.replyWithMention !== replyWithMention) { if (threadsEnabled && replyWithMention) { this.setState({ tshow: this.sendThreadToChannel }); } } - if (!dequal(prevProps.uploadFilePermission, uploadFilePermission) || prevProps.goToCannedResponses !== goToCannedResponses) { + if ( + !dequal(prevProps.uploadFilePermission, uploadFilePermission) || + !dequal(prevProps.createDiscussionPermission, createDiscussionPermission) || + prevProps.goToCannedResponses !== goToCannedResponses + ) { this.setOptions(); } } @@ -441,7 +457,7 @@ class MessageBox extends Component { } setOptions = async () => { - const { uploadFilePermission, rid } = this.props; + const { uploadFilePermission, rid, createDiscussionPermission } = this.props; // Servers older than 4.2 if (!uploadFilePermission) { @@ -449,8 +465,8 @@ class MessageBox extends Component { return; } - const permissionToUpload = await hasPermission([uploadFilePermission], rid); - this.setState({ permissionToUpload: permissionToUpload[0] }); + const permissions = await hasPermission([uploadFilePermission, createDiscussionPermission], rid); + this.setState({ permissionToUpload: permissions[0], hasCreateDiscussionPermission: permissions[1] }); }; onChangeText: any = (text: string): void => { @@ -877,7 +893,7 @@ class MessageBox extends Component { showMessageBoxActions = () => { logEvent(events.ROOM_SHOW_BOX_ACTIONS); - const { permissionToUpload } = this.state; + const { permissionToUpload, hasCreateDiscussionPermission } = this.state; const { showActionSheet, goToCannedResponses } = this.props; const options: TActionSheetOptionsItem[] = []; @@ -913,11 +929,13 @@ class MessageBox extends Component { ); } - options.push({ - title: I18n.t('Create_Discussion'), - icon: 'discussions', - onPress: this.createDiscussion - }); + if (hasCreateDiscussionPermission) { + options.push({ + title: I18n.t('Create_Discussion'), + icon: 'discussions', + onPress: this.createDiscussion + }); + } this.closeEmojiAndAction(showActionSheet, { options }); }; @@ -1316,6 +1334,7 @@ const mapStateToProps = (state: IApplicationState) => ({ FileUpload_MaxFileSize: state.settings.FileUpload_MaxFileSize, Message_AudioRecorderEnabled: state.settings.Message_AudioRecorderEnabled, uploadFilePermission: state.permissions['mobile-upload-file'], + createDiscussionPermission: state.permissions['start-discussion'], serverVersion: state.server.version }); diff --git a/app/lib/methods/getPermissions.ts b/app/lib/methods/getPermissions.ts index aab7a432c..cd741ee62 100644 --- a/app/lib/methods/getPermissions.ts +++ b/app/lib/methods/getPermissions.ts @@ -23,6 +23,7 @@ export const SUPPORTED_PERMISSIONS = [ 'create-p', 'create-d', 'start-discussion', + 'start-discussion-other-user', 'create-team', 'delete-c', 'delete-message',