2013-08-29 21:33:26 +02:00
|
|
|
var alert_words = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
|
|
|
exports.words = page_params.alert_words;
|
2018-08-04 08:25:43 +02:00
|
|
|
exports.set_words = function (value) {
|
|
|
|
exports.words = value;
|
|
|
|
};
|
2013-08-29 21:33:26 +02:00
|
|
|
|
2017-06-11 15:21:11 +02:00
|
|
|
// Delete the `page_params.alert_words` since we are its sole user.
|
|
|
|
delete page_params.alert_words;
|
|
|
|
|
2013-08-29 21:33:26 +02:00
|
|
|
// 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) {
|
2017-10-12 22:11:43 +02:00
|
|
|
// Parsing for alert words is expensive, so we rely on the host
|
|
|
|
// to tell us there any alert words to even look for.
|
|
|
|
if (!message.alerted) {
|
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 + ')' +
|
2018-12-07 21:21:39 +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) {
|
2017-07-29 21:17:12 +02:00
|
|
|
// Logic for ensuring that we don't muck up rendered HTML.
|
2014-03-04 22:55:36 +01:00
|
|
|
var pre_match = content.substring(0, offset);
|
2017-07-29 21:17:12 +02:00
|
|
|
// We want to find the position of the `<` and `>` only in the
|
|
|
|
// match and the string before it. So, don't include the last
|
|
|
|
// character of match in `check_string`. This covers the corner
|
|
|
|
// case when there is an alert word just before `<` or `>`.
|
|
|
|
var check_string = pre_match + match.substring(0, match.length - 1);
|
|
|
|
var in_tag = check_string.lastIndexOf('<') > check_string.lastIndexOf('>');
|
|
|
|
// Matched word is inside a HTML tag so don't perform any highlighting.
|
|
|
|
if (in_tag === true) {
|
2014-03-04 22:55:36 +01:00
|
|
|
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) {
|
2017-10-12 22:11:43 +02:00
|
|
|
// We exclude ourselves from notifications when we type one of our own
|
|
|
|
// alert words into a message, just because that can be annoying for
|
|
|
|
// certain types of workflows where everybody on your team, including
|
|
|
|
// yourself, sets up an alert word to effectively mention the team.
|
2017-01-19 20:18:03 +01:00
|
|
|
return !people.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;
|
|
|
|
}
|
2018-05-28 08:04:36 +02:00
|
|
|
window.alert_words = alert_words;
|