From 897dd87b941046833f188b235f821dba4b9af3ed Mon Sep 17 00:00:00 2001 From: Leo Franchi Date: Thu, 29 Aug 2013 15:33:26 -0400 Subject: [PATCH] Add a span around alert words to mark them visually (imported from commit 9b8fbbd957086f1eeaa3409e5830aa6d7974fbe8) --- static/js/alert_words.js | 36 ++++++++++++++ static/js/notifications.js | 3 +- static/js/zulip.js | 1 + static/styles/zulip.css | 4 ++ tools/jslint/check-all.js | 3 ++ zerver/tests/frontend/node/alert_words.js | 60 +++++++++++++++++++++++ zproject/settings.py | 3 +- 7 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 static/js/alert_words.js create mode 100644 zerver/tests/frontend/node/alert_words.js diff --git a/static/js/alert_words.js b/static/js/alert_words.js new file mode 100644 index 0000000000..085b99e2df --- /dev/null +++ b/static/js/alert_words.js @@ -0,0 +1,36 @@ +var alert_words = (function () { + +var exports = {}; + +exports.words = page_params.alert_words; + +// escape_user_regex taken from jquery-ui/autocomplete.js, +// licensed under MIT license. +function escape_user_regex(value) { + return value.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&"); +} + +exports.process_message = function (message) { + if (!exports.notifies(message)) { + return; + } + + _.each(exports.words, function (word) { + var clean = escape_user_regex(word); + var regex = new RegExp('(\\b' + clean + '\\b)', 'i'); + var replaced = "$1"; + message.content = message.content.replace(regex, replaced); + }); +}; + +exports.notifies = function (message) { + return ((message.sender_email !== page_params.email) && + (message.flags.indexOf('has_alert_word') > -1)); +}; + +return exports; + +}()); +if (typeof module !== 'undefined') { + module.exports = alert_words; +} diff --git a/static/js/notifications.js b/static/js/notifications.js index ae309963a4..3e11284d0b 100644 --- a/static/js/notifications.js +++ b/static/js/notifications.js @@ -302,7 +302,8 @@ function message_is_notifiable(message) { (message.type === "private" || exports.speaking_at_me(message) || (message.type === "stream" && - subs.receives_notifications(message.stream)))); + subs.receives_notifications(message.stream)) || + alert_words.notifies(message))); } exports.received_messages = function (messages) { diff --git a/static/js/zulip.js b/static/js/zulip.js index 4aa848414f..9a2b1ef141 100644 --- a/static/js/zulip.js +++ b/static/js/zulip.js @@ -596,6 +596,7 @@ function add_message_metadata(message) { } }); + alert_words.process_message(message); msg_metadata_cache[message.id] = message; return message; } diff --git a/static/styles/zulip.css b/static/styles/zulip.css index bbf48262cb..75d7aca6d5 100644 --- a/static/styles/zulip.css +++ b/static/styles/zulip.css @@ -2605,6 +2605,10 @@ div.edit_bot { background-color: #c9fcc1; } +.alert-word { + background-color: #c9fcc1; +} + #settings { margin-top: 45px; margin-left: 15px; diff --git a/tools/jslint/check-all.js b/tools/jslint/check-all.js index 5689f19ec9..9e2c75f763 100644 --- a/tools/jslint/check-all.js +++ b/tools/jslint/check-all.js @@ -38,6 +38,9 @@ var globals = // templates.js + ' templates' + // alert_words.js + + ' alert_words' + // zulip.js + ' all_msg_list home_msg_list narrowed_msg_list current_msg_list get_updates_params' + ' add_messages' diff --git a/zerver/tests/frontend/node/alert_words.js b/zerver/tests/frontend/node/alert_words.js new file mode 100644 index 0000000000..3399af312d --- /dev/null +++ b/zerver/tests/frontend/node/alert_words.js @@ -0,0 +1,60 @@ +var assert = require('assert'); + +add_dependencies({ + _: 'third/underscore/underscore.js' +}); + +set_global('page_params', { + alert_words: ['alertone', 'alerttwo', 'alertthree', 'al*rt.*s', '.+'], + email: 'tester@zulip.com' +}); + +var alert_words = require('js/alert_words.js'); + +var regular_message = { sender_email: 'another@zulip.com', content: '

a message

', + flags: [] }; +var own_message = { sender_email: 'tester@zulip.com', content: '

hey this message alertone

', + flags: ['has_alert_word'] }; +var other_message = { sender_email: 'another@zulip.com', content: '

another alertone message

', + flags: ['has_alert_word'] }; +var caps_message = { sender_email: 'another@zulip.com', content: '

another ALERTtwo message

', + flags: ['has_alert_word'] }; +var alertwordboundary_message = { sender_email: 'another@zulip.com', + content: '

another alertthreemessage

', flags: [] }; +var multialert_message = { sender_email: 'another@zulip.com', content: + '

another alertthreemessage alertone and then alerttwo

', + flags: ['has_alert_word'] }; +var unsafe_word_message = { sender_email: 'another@zulip.com', content: '

gotta al*rt.*s all

', + flags: ['has_alert_word'] }; + +(function test_notifications() { + assert.equal(alert_words.notifies(regular_message), false); + assert.equal(alert_words.notifies(own_message), false); + assert.equal(alert_words.notifies(other_message), true); + assert.equal(alert_words.notifies(caps_message), true); + assert.equal(alert_words.notifies(alertwordboundary_message), false); + assert.equal(alert_words.notifies(multialert_message), true); + assert.equal(alert_words.notifies(unsafe_word_message), true); +}()); + +(function test_munging() { + var saved_content = regular_message.content; + alert_words.process_message(regular_message); + assert.equal(saved_content, regular_message.content); + + saved_content = alertwordboundary_message.content; + alert_words.process_message(alertwordboundary_message); + assert.equal(alertwordboundary_message.content, saved_content); + + alert_words.process_message(other_message); + assert.equal(other_message.content, "

another alertone message

"); + alert_words.process_message(caps_message); + assert.equal(caps_message.content, "

another ALERTtwo message

"); + + alert_words.process_message(multialert_message); + assert.equal(multialert_message.content, "

another alertthreemessage alertone and then alerttwo

"); + + + alert_words.process_message(unsafe_word_message); + assert.equal(unsafe_word_message.content, "

gotta al*rt.*s all

"); +}()); \ No newline at end of file diff --git a/zproject/settings.py b/zproject/settings.py index fb98cbe922..46f63af9a4 100644 --- a/zproject/settings.py +++ b/zproject/settings.py @@ -410,7 +410,8 @@ JS_SPECS = { 'js/tab_bar.js', 'js/metrics.js', 'js/emoji.js', - 'js/referral.js' + 'js/referral.js', + 'js/alert_words.js' ], 'output_filename': 'min/app.js' },