Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
[MM-23997] Ensure channel mentions permission is set on edit post (#5296
Browse files Browse the repository at this point in the history
)

* MM-23997 Ensure channel mentions permission is set on edit post

* Fix linting
  • Loading branch information
fmunshi committed Apr 9, 2020
1 parent fc4e532 commit 875a63c
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 5 deletions.
170 changes: 170 additions & 0 deletions components/edit_post_modal/__snapshots__/edit_post_modal.test.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ exports[`components/EditPostModal should disable the button on not canDeletePost
suggestionListStyle="bottom"
supportsCommands={false}
tabIndex="0"
useChannelMentions={true}
value=""
/>
<div
Expand Down Expand Up @@ -242,6 +243,7 @@ exports[`components/EditPostModal should disable the button on not canEditPost a
suggestionListStyle="bottom"
supportsCommands={false}
tabIndex="0"
useChannelMentions={true}
value="new message"
/>
<div
Expand Down Expand Up @@ -320,6 +322,167 @@ exports[`components/EditPostModal should disable the button on not canEditPost a
</Modal>
`;

exports[`components/EditPostModal should match snapshot with useChannelMentions set to false 1`] = `
<Modal
animation={true}
aria-labelledby="editPostModalLabel"
autoFocus={true}
backdrop={true}
bsClass="modal"
dialogClassName="a11y__modal edit-modal"
dialogComponentClass={[Function]}
enforceFocus={true}
id="editPostModal"
keyboard={false}
manager={
ModalManager {
"add": [Function],
"containers": Array [],
"data": Array [],
"handleContainerOverflow": true,
"hideSiblingNodes": true,
"isTopModal": [Function],
"modals": Array [],
"remove": [Function],
}
}
onEntered={[Function]}
onExit={[Function]}
onExited={[Function]}
onHide={[Function]}
onKeyDown={[Function]}
renderBackdrop={[Function]}
restoreFocus={true}
role="dialog"
show={true}
>
<ModalHeader
bsClass="modal-header"
closeButton={true}
closeLabel="Close"
onHide={[Function]}
>
<ModalTitle
bsClass="modal-title"
componentClass="h1"
id="editPostModalLabel"
>
<FormattedMessage
defaultMessage="Edit {title}"
id="edit_post.edit"
values={
Object {
"title": "test",
}
}
/>
</ModalTitle>
</ModalHeader>
<ModalBody
bsClass="modal-body edit-modal-body"
componentClass="div"
>
<div
className="post-create__container"
>
<div
className="textarea-wrapper"
>
<Connect(Textbox)
channelId="5"
characterLimit={4000}
createMessage="Edit the post..."
emojiEnabled={true}
handlePostError={[Function]}
id="edit_textbox"
onChange={[Function]}
onKeyDown={[Function]}
onKeyPress={[Function]}
onKeyUp={[Function]}
onMouseUp={[Function]}
preview={false}
suggestionListStyle="bottom"
supportsCommands={false}
tabIndex="0"
useChannelMentions={false}
value=""
/>
<div
className="post-body__actions"
>
<div>
<EmojiPickerOverlay
container={[Function]}
enableGifPicker={false}
onEmojiClick={[Function]}
onGifClick={[Function]}
onHide={[Function]}
show={false}
spaceRequiredAbove={476}
spaceRequiredBelow={497}
target={[Function]}
topOffset={-20}
/>
<button
aria-label="emoji picker"
className="style--none post-action"
id="editPostEmoji"
onClick={[Function]}
>
<EmojiIcon
className="icon icon--emoji"
/>
</button>
</div>
</div>
</div>
<div
className="post-create-footer"
>
<Connect(TextboxLinks)
characterLimit={4000}
message=""
showPreview={false}
updatePreview={[Function]}
/>
<div
className="edit-post-footer"
/>
</div>
</div>
</ModalBody>
<ModalFooter
bsClass="modal-footer"
componentClass="div"
>
<button
className="btn btn-link"
onClick={[Function]}
type="button"
>
<FormattedMessage
defaultMessage="Cancel"
id="edit_post.cancel"
values={Object {}}
/>
</button>
<button
className="btn btn-primary"
disabled={false}
id="editButton"
onClick={[Function]}
type="button"
>
<FormattedMessage
defaultMessage="Save"
id="edit_post.save"
values={Object {}}
/>
</button>
</ModalFooter>
</Modal>
`;

exports[`components/EditPostModal should match with default config 1`] = `
<Modal
animation={true}
Expand Down Expand Up @@ -402,6 +565,7 @@ exports[`components/EditPostModal should match with default config 1`] = `
suggestionListStyle="bottom"
supportsCommands={false}
tabIndex="0"
useChannelMentions={true}
value=""
/>
<div
Expand Down Expand Up @@ -562,6 +726,7 @@ exports[`components/EditPostModal should match without emoji picker 1`] = `
suggestionListStyle="bottom"
supportsCommands={false}
tabIndex="0"
useChannelMentions={true}
value=""
/>
<div
Expand Down Expand Up @@ -697,6 +862,7 @@ exports[`components/EditPostModal should not disable the button on not canDelete
suggestionListStyle="bottom"
supportsCommands={false}
tabIndex="0"
useChannelMentions={true}
value="new message"
/>
<div
Expand Down Expand Up @@ -857,6 +1023,7 @@ exports[`components/EditPostModal should not disable the button on not canEditPo
suggestionListStyle="bottom"
supportsCommands={false}
tabIndex="0"
useChannelMentions={true}
value=""
/>
<div
Expand Down Expand Up @@ -1017,6 +1184,7 @@ exports[`components/EditPostModal should not disable the save button on not canD
suggestionListStyle="bottom"
supportsCommands={false}
tabIndex="0"
useChannelMentions={true}
value=""
/>
<div
Expand Down Expand Up @@ -1177,6 +1345,7 @@ exports[`components/EditPostModal should show emojis on emojis click 1`] = `
suggestionListStyle="bottom"
supportsCommands={false}
tabIndex="0"
useChannelMentions={true}
value=""
/>
<div
Expand Down Expand Up @@ -1337,6 +1506,7 @@ exports[`components/EditPostModal should show errors when it is set in the state
suggestionListStyle="bottom"
supportsCommands={false}
tabIndex="0"
useChannelMentions={true}
value=""
/>
<div
Expand Down
3 changes: 3 additions & 0 deletions components/edit_post_modal/edit_post_modal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class EditPostModal extends React.PureComponent {
intl: intlShape.isRequired,
maxPostSize: PropTypes.number.isRequired,
shouldShowPreview: PropTypes.bool.isRequired,
useChannelMentions: PropTypes.bool.isRequired,

editingPost: PropTypes.shape({
post: PropTypes.object,
postId: PropTypes.string,
Expand Down Expand Up @@ -406,6 +408,7 @@ class EditPostModal extends React.PureComponent {
ref={this.setEditboxRef}
characterLimit={this.props.maxPostSize}
preview={this.props.shouldShowPreview}
useChannelMentions={this.props.useChannelMentions}
/>
<div className='post-body__actions'>
{emojiPicker}
Expand Down
9 changes: 8 additions & 1 deletion components/edit_post_modal/edit_post_modal.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ jest.mock('utils/user_agent', () => ({
isMobile: jest.fn().mockReturnValue(false),
}));

function createEditPost({canEditPost, canDeletePost, ctrlSend, config, license, editingPost, actions} = {canEditPost: true, canDeletePost: true}) { //eslint-disable-line react/prop-types
function createEditPost({canEditPost, canDeletePost, useChannelMentions, ctrlSend, config, license, editingPost, actions} = {canEditPost: true, canDeletePost: true}) { //eslint-disable-line react/prop-types
const canEditPostProp = canEditPost === undefined ? true : canEditPost;
const canDeletePostProp = canDeletePost === undefined ? true : canDeletePost;
const useChannelMentionsProp = useChannelMentions === undefined ? true : useChannelMentions;
const ctrlSendProp = ctrlSend || false;
const configProp = config || {
PostEditTimeLimit: 300,
Expand Down Expand Up @@ -60,6 +61,7 @@ function createEditPost({canEditPost, canDeletePost, ctrlSend, config, license,
editingPost={editingPostProp}
actions={actionsProp}
maxPostSize={Constants.DEFAULT_CHARACTER_LIMIT}
useChannelMentions={useChannelMentionsProp}
/>
);
}
Expand Down Expand Up @@ -545,4 +547,9 @@ describe('components/EditPostModal', () => {
wrapper.setState({editText: ''});
expect(wrapper).toMatchSnapshot();
});

it('should match snapshot with useChannelMentions set to false', () => {
var wrapper = shallowWithIntl(createEditPost({useChannelMentions: false}));
expect(wrapper).toMatchSnapshot();
});
});
17 changes: 13 additions & 4 deletions components/edit_post_modal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,24 @@ function mapStateToProps(state) {
const config = getConfig(state);
const editingPost = getEditingPost(state);
const currentUserId = getCurrentUserId(state);
const channelId = getCurrentChannelId(state);
const teamId = getCurrentTeamId(state);
let canDeletePost = false;
let canEditPost = false;
if (editingPost && editingPost.post && editingPost.post.user_id === currentUserId) {
canDeletePost = haveIChannelPermission(state, {channel: getCurrentChannelId(state), team: getCurrentTeamId(state), permission: Permissions.DELETE_POST});
canEditPost = haveIChannelPermission(state, {channel: getCurrentChannelId(state), team: getCurrentTeamId(state), permission: Permissions.EDIT_POST});
canDeletePost = haveIChannelPermission(state, {channel: channelId, team: teamId, permission: Permissions.DELETE_POST});
canEditPost = haveIChannelPermission(state, {channel: channelId, team: teamId, permission: Permissions.EDIT_POST});
} else {
canDeletePost = haveIChannelPermission(state, {channel: getCurrentChannelId(state), team: getCurrentTeamId(state), permission: Permissions.DELETE_OTHERS_POSTS});
canEditPost = haveIChannelPermission(state, {channel: getCurrentChannelId(state), team: getCurrentTeamId(state), permission: Permissions.EDIT_OTHERS_POSTS});
canDeletePost = haveIChannelPermission(state, {channel: channelId, team: teamId, permission: Permissions.DELETE_OTHERS_POSTS});
canEditPost = haveIChannelPermission(state, {channel: channelId, team: teamId, permission: Permissions.EDIT_OTHERS_POSTS});
}

const useChannelMentions = haveIChannelPermission(state, {
channel: channelId,
team: teamId,
permission: Permissions.USE_CHANNEL_MENTIONS,
});

return {
canEditPost,
canDeletePost,
Expand All @@ -45,6 +53,7 @@ function mapStateToProps(state) {
editingPost,
shouldShowPreview: showPreviewOnEditPostModal(state),
maxPostSize: parseInt(config.MaxPostSize, 10) || Constants.DEFAULT_CHARACTER_LIMIT,
useChannelMentions,
};
}

Expand Down

0 comments on commit 875a63c

Please sign in to comment.