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 <gleidson10daniel@hotmail.com>
This commit is contained in:
Reinaldo Neto 2023-10-16 17:55:38 -03:00 committed by GitHub
parent 231057af10
commit 199331f8f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 22 deletions

View File

@ -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);

View File

@ -112,6 +112,7 @@ export interface IMessageBoxProps extends IBaseScreen<ChatsStackParamList & Mast
isActionsEnabled: boolean;
usedCannedResponse: string;
uploadFilePermission: string[];
createDiscussionPermission: string[];
goToCannedResponses: () => 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<IMessageBoxProps, IMessageBoxState> {
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<IMessageBoxProps, IMessageBoxState> {
mentionLoading,
trackingType,
permissionToUpload,
hasCreateDiscussionPermission,
showEmojiSearchbar
} = this.state;
@ -337,6 +341,7 @@ class MessageBox extends Component<IMessageBoxProps, IMessageBoxState> {
theme,
usedCannedResponse,
uploadFilePermission,
createDiscussionPermission,
goToCannedResponses
} = this.props;
if (nextProps.theme !== theme) {
@ -378,6 +383,9 @@ class MessageBox extends Component<IMessageBoxProps, IMessageBoxState> {
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<IMessageBoxProps, IMessageBoxState> {
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<IMessageBoxProps, IMessageBoxState> {
}
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<IMessageBoxProps, IMessageBoxState> {
}
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<IMessageBoxProps, IMessageBoxState> {
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<IMessageBoxProps, IMessageBoxState> {
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<IMessageBoxProps, IMessageBoxState> {
);
}
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
});

View File

@ -23,6 +23,7 @@ export const SUPPORTED_PERMISSIONS = [
'create-p',
'create-d',
'start-discussion',
'start-discussion-other-user',
'create-team',
'delete-c',
'delete-message',