summaryrefslogtreecommitdiffstats
path: root/webapp
diff options
context:
space:
mode:
authorSaturnino Abril <saturnino.abril@gmail.com>2017-08-12 02:01:11 +0800
committerGitHub <noreply@github.com>2017-08-12 02:01:11 +0800
commit4cf316fcd39dbb654bb07d80b0dfa9f8194c571d (patch)
tree8d3ff2dff290b8dc793661e961a5fbf505f5d570 /webapp
parent638c38cc0d2296335a0fbd5bde8b6d2cbf9f9062 (diff)
downloadchat-4cf316fcd39dbb654bb07d80b0dfa9f8194c571d.tar.gz
chat-4cf316fcd39dbb654bb07d80b0dfa9f8194c571d.tar.bz2
chat-4cf316fcd39dbb654bb07d80b0dfa9f8194c571d.zip
[PLT-7342] Add function and tests to specifically determine @all & @channel (#7181)
* add function and tests to specifically determine @all & @channel * uodate per comments - regex and tests
Diffstat (limited to 'webapp')
-rw-r--r--webapp/components/create_post.jsx3
-rw-r--r--webapp/tests/utils/post_utils.test.jsx97
-rw-r--r--webapp/utils/post_utils.jsx9
3 files changed, 108 insertions, 1 deletions
diff --git a/webapp/components/create_post.jsx b/webapp/components/create_post.jsx
index 55d6884ac..b57eb2f17 100644
--- a/webapp/components/create_post.jsx
+++ b/webapp/components/create_post.jsx
@@ -14,6 +14,7 @@ import * as EmojiPicker from 'components/emoji_picker/emoji_picker.jsx';
import AppDispatcher from 'dispatcher/app_dispatcher.jsx';
import * as GlobalActions from 'actions/global_actions.jsx';
import * as Utils from 'utils/utils.jsx';
+import * as PostUtils from 'utils/post_utils.jsx';
import * as UserAgent from 'utils/user_agent.jsx';
import * as ChannelActions from 'actions/channel_actions.jsx';
import * as PostActions from 'actions/post_actions.jsx';
@@ -224,7 +225,7 @@ export default class CreatePost extends React.Component {
const members = stats.member_count - 1;
const updateChannel = ChannelStore.getCurrent();
- if ((this.state.message.includes('@all') || this.state.message.includes('@channel')) && members >= Constants.NOTIFY_ALL_MEMBERS) {
+ if ((PostUtils.containsAtMention(this.state.message, '@all') || PostUtils.containsAtMention(this.state.message, '@channel')) && members >= Constants.NOTIFY_ALL_MEMBERS) {
this.setState({totalMembers: members});
this.showNotifyAllModal();
return;
diff --git a/webapp/tests/utils/post_utils.test.jsx b/webapp/tests/utils/post_utils.test.jsx
new file mode 100644
index 000000000..0546d5bea
--- /dev/null
+++ b/webapp/tests/utils/post_utils.test.jsx
@@ -0,0 +1,97 @@
+import * as PostUtils from 'utils/post_utils.jsx';
+
+describe('PostUtils.containsAtMention', function() {
+ test('should return correct @all (same for @channel)', function() {
+ for (const data of [
+ {
+ text: undefined, //eslint-disable-line no-undefined
+ key: undefined, //eslint-disable-line no-undefined
+ result: false
+ },
+ {
+ text: '',
+ key: '',
+ result: false
+ },
+ {
+ text: 'all',
+ key: '@all',
+ result: false
+ },
+ {
+ text: '@allison',
+ key: '@all',
+ result: false
+ },
+ {
+ text: '@ALLISON',
+ key: '@all',
+ result: false
+ },
+ {
+ text: '@all123',
+ key: '@all',
+ result: false
+ },
+ {
+ text: '123@all',
+ key: '@all',
+ result: false
+ },
+ {
+ text: 'hey@all',
+ key: '@all',
+ result: false
+ },
+ {
+ text: 'hey@all.com',
+ key: '@all',
+ result: false
+ },
+ {
+ text: '@all',
+ key: '@all',
+ result: true
+ },
+ {
+ text: '@ALL',
+ key: '@all',
+ result: true
+ },
+ {
+ text: '@all hey',
+ key: '@all',
+ result: true
+ },
+ {
+ text: 'hey @all',
+ key: '@all',
+ result: true
+ },
+ {
+ text: 'HEY @ALL',
+ key: '@all',
+ result: true
+ },
+ {
+ text: 'hey @all!',
+ key: '@all',
+ result: true
+ },
+ {
+ text: 'hey @all:+1:',
+ key: '@all',
+ result: true
+ },
+ {
+ text: 'hey @ALL:+1:',
+ key: '@all',
+ result: true
+ }
+ ]) {
+ const containsAtMention = PostUtils.containsAtMention(data.text, data.key);
+
+ expect(containsAtMention).toEqual(data.result);
+ }
+ });
+});
diff --git a/webapp/utils/post_utils.jsx b/webapp/utils/post_utils.jsx
index 9309e1e49..83fb666af 100644
--- a/webapp/utils/post_utils.jsx
+++ b/webapp/utils/post_utils.jsx
@@ -107,3 +107,12 @@ export function shouldShowDotMenu(post) {
return false;
}
+
+export function containsAtMention(text, key) {
+ if (!text || !key) {
+ return false;
+ }
+
+ // This doesn't work for at mentions containing periods or hyphens
+ return new RegExp(`\\B${key}\\b`, 'i').test(text);
+}