Add a span around alert words to mark them visually

(imported from commit 9b8fbbd957086f1eeaa3409e5830aa6d7974fbe8)
This commit is contained in:
Leo Franchi 2013-08-29 15:33:26 -04:00
parent b2ddd670e4
commit 897dd87b94
7 changed files with 108 additions and 2 deletions

36
static/js/alert_words.js Normal file
View File

@ -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 = "<span class='alert-word'>$1</span>";
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;
}

View File

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

View File

@ -596,6 +596,7 @@ function add_message_metadata(message) {
}
});
alert_words.process_message(message);
msg_metadata_cache[message.id] = message;
return message;
}

View File

@ -2605,6 +2605,10 @@ div.edit_bot {
background-color: #c9fcc1;
}
.alert-word {
background-color: #c9fcc1;
}
#settings {
margin-top: 45px;
margin-left: 15px;

View File

@ -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'

View File

@ -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: '<p>a message</p>',
flags: [] };
var own_message = { sender_email: 'tester@zulip.com', content: '<p>hey this message alertone</p>',
flags: ['has_alert_word'] };
var other_message = { sender_email: 'another@zulip.com', content: '<p>another alertone message</p>',
flags: ['has_alert_word'] };
var caps_message = { sender_email: 'another@zulip.com', content: '<p>another ALERTtwo message</p>',
flags: ['has_alert_word'] };
var alertwordboundary_message = { sender_email: 'another@zulip.com',
content: '<p>another alertthreemessage</p>', flags: [] };
var multialert_message = { sender_email: 'another@zulip.com', content:
'<p>another alertthreemessage alertone and then alerttwo</p>',
flags: ['has_alert_word'] };
var unsafe_word_message = { sender_email: 'another@zulip.com', content: '<p>gotta al*rt.*s all</p>',
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, "<p>another <span class='alert-word'>alertone</span> message</p>");
alert_words.process_message(caps_message);
assert.equal(caps_message.content, "<p>another <span class='alert-word'>ALERTtwo</span> message</p>");
alert_words.process_message(multialert_message);
assert.equal(multialert_message.content, "<p>another alertthreemessage <span class='alert-word'>alertone</span> and then <span class='alert-word'>alerttwo</span></p>");
alert_words.process_message(unsafe_word_message);
assert.equal(unsafe_word_message.content, "<p>gotta <span class='alert-word'>al*rt.*s</span> all</p>");
}());

View File

@ -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'
},