2013-08-29 21:33:26 +02:00
|
|
|
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, "\\$&");
|
|
|
|
}
|
|
|
|
|
2014-03-04 22:55:36 +01:00
|
|
|
var find_href_backwards = /href=['"][\w:\/\.]+$/;
|
|
|
|
var find_title_backwards = /title=['"][\w:\/\.]+$/;
|
|
|
|
|
2013-08-29 21:33:26 +02:00
|
|
|
exports.process_message = function (message) {
|
2013-10-11 17:30:57 +02:00
|
|
|
if (!exports.notifies(message)) {
|
2013-08-29 21:33:26 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_.each(exports.words, function (word) {
|
|
|
|
var clean = escape_user_regex(word);
|
2014-02-12 23:36:09 +01:00
|
|
|
var before_punctuation = '\\s|^|>|[\\(\\".,\';\\[]';
|
|
|
|
var after_punctuation = '\\s|$|<|[\\)\\"\\?!:.,\';\\]!]';
|
2013-10-09 20:09:48 +02:00
|
|
|
|
2014-03-04 22:55:36 +01:00
|
|
|
|
2013-10-09 20:09:48 +02:00
|
|
|
var regex = new RegExp('(' + before_punctuation + ')' +
|
|
|
|
'(' + clean + ')' +
|
2014-03-04 23:51:49 +01:00
|
|
|
'(' + after_punctuation + ')' , 'ig');
|
2016-12-02 15:16:33 +01:00
|
|
|
message.content = message.content.replace(regex, function (match, before, word,
|
|
|
|
after, offset, content) {
|
2014-03-04 22:55:36 +01:00
|
|
|
// Don't munge URL hrefs
|
|
|
|
var pre_match = content.substring(0, offset);
|
|
|
|
if (find_href_backwards.exec(pre_match) || find_title_backwards.exec(pre_match)) {
|
|
|
|
return before + word + after;
|
|
|
|
}
|
2016-12-02 21:34:35 +01:00
|
|
|
return before + "<span class='alert-word'>" + word + "</span>" + after;
|
2013-10-09 20:09:48 +02:00
|
|
|
});
|
2013-08-29 21:33:26 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.notifies = function (message) {
|
2016-06-08 05:54:07 +02:00
|
|
|
return !util.is_current_user(message.sender_email) && message.alerted;
|
2013-08-29 21:33:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = alert_words;
|
|
|
|
}
|