2013-08-29 21:33:26 +02:00
|
|
|
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) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const clean = escape_user_regex(word);
|
|
|
|
const before_punctuation = '\\s|^|>|[\\(\\".,\';\\[]';
|
|
|
|
const after_punctuation = '\\s|$|<|[\\)\\"\\?!:.,\';\\]!]';
|
2013-10-09 20:09:48 +02:00
|
|
|
|
2014-03-04 22:55:36 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const regex = new RegExp('(' + before_punctuation + ')' +
|
2013-10-09 20:09:48 +02:00
|
|
|
'(' + 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.
|
2019-11-02 00:06:25 +01:00
|
|
|
const 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 `>`.
|
2019-11-02 00:06:25 +01:00
|
|
|
const check_string = pre_match + match.substring(0, match.length - 1);
|
|
|
|
const in_tag = check_string.lastIndexOf('<') > check_string.lastIndexOf('>');
|
2017-07-29 21:17:12 +02:00
|
|
|
// 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
|
|
|
};
|
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.alert_words = exports;
|