From 259dd6dab3e04f928b5f26e178695f050c71ffd0 Mon Sep 17 00:00:00 2001 From: TheJetOU <31071116+TheJetOU@users.noreply.github.com> Date: Mon, 22 Jul 2019 20:00:52 +0300 Subject: [PATCH] Support room highlights (#1334) --- js/client-chat.js | 63 +++++++++++++++++++++++++++++++---------------- 1 file changed, 42 insertions(+), 21 deletions(-) diff --git a/js/client-chat.js b/js/client-chat.js index fdf7277fe..53e8c5fb5 100644 --- a/js/client-chat.js +++ b/js/client-chat.js @@ -208,7 +208,15 @@ // highlight getHighlight: function (message) { - var highlights = Dex.prefs('highlights') || []; + var highlights = Dex.prefs('highlights') || {}; + if (Array.isArray(highlights)) { + highlights = {global: highlights}; + // Migrate from the old highlight system + Dex.prefs('highlights', highlights); + } + if (!Dex.prefs('noselfhighlight') && app.user.nameRegExp) { + if (app.user.nameRegExp.test(message)) return true; + } if (!app.highlightRegExp) { try { this.updateHighlightRegExp(highlights); @@ -220,17 +228,22 @@ return false; } } - if (!Dex.prefs('noselfhighlight') && app.user.nameRegExp) { - if (app.user.nameRegExp.test(message)) return true; - } - return ((highlights.length > 0) && app.highlightRegExp.test(message)); + var id = Config.server.id + '#' + this.id; + var globalHighlightsRegExp = app.highlightRegExp['global']; + var roomHighlightsRegExp = app.highlightRegExp[id]; + return (((globalHighlightsRegExp && globalHighlightsRegExp.test(message)) || (roomHighlightsRegExp && roomHighlightsRegExp.test(message)))); }, updateHighlightRegExp: function (highlights) { // Enforce boundary for match sides, if a letter on match side is // a word character. For example, regular expression "a" matches // "a", but not "abc", while regular expression "!" matches // "!" and "!abc". - app.highlightRegExp = new RegExp('(?:\\b|(?!\\w))(?:' + highlights.join('|') + ')(?:\\b|(?!\\w))', 'i'); + if (!app.highlightRegExp) { + app.highlightRegExp = {}; + } + for (var i in highlights) { + app.highlightRegExp[i] = new RegExp('(?:\\b|(?!\\w))(?:' + highlights[i].join('|') + ')(?:\\b|(?!\\w))', 'i'); + } }, // chat history @@ -705,7 +718,7 @@ case 'hl': case 'highlight': - var highlights = Dex.prefs('highlights') || []; + var highlights = Dex.prefs('highlights') || {}; if (target.indexOf(',') > -1) { var targets = target.match(/([^,]+?({\d*,\d*})?)+/g); // trim the targets to be safe @@ -713,7 +726,9 @@ targets[i] = targets[i].replace(/\n/g, '').trim(); } switch (targets[0]) { - case 'add': + case 'add': case 'roomadd': + var key = targets[0] === 'roomadd' ? (Config.server.id + '#' + this.id) : 'global'; + var highlightList = highlights[key] || []; for (var i = 1, len = targets.length; i < len; i++) { if (!targets[i]) continue; if (/[\\^$*+?()|{}[\]]/.test(targets[i])) { @@ -724,24 +739,26 @@ return this.add(e.message.substr(0, 28) === 'Invalid regular expression: ' ? e.message : 'Invalid regular expression: /' + targets[i] + '/: ' + e.message); } } - if (highlights.indexOf(targets[i]) > -1) { + if (highlightList.indexOf(targets[i]) > -1) { return this.add(targets[i] + ' is already on your highlights list.'); } } - highlights = highlights.concat(targets.slice(1)); - this.add("Now highlighting on: " + highlights.join(', ')); + highlights[key] = highlightList.concat(targets.slice(1)); + this.add("Now highlighting on " + (key === 'global' ? "(everywhere): " : "(in " + key + "): ") + highlights[key].join(', ')); // We update the regex this.updateHighlightRegExp(highlights); break; - case 'delete': + case 'delete': case 'roomdelete': + var key = targets[0] === 'roomdelete' ? (Config.server.id + '#' + this.id) : 'global'; + var highlightList = highlights[key] || []; var newHls = []; - for (var i = 0, len = highlights.length; i < len; i++) { - if (targets.indexOf(highlights[i]) === -1) { - newHls.push(highlights[i]); + for (var i = 0, len = highlightList.length; i < len; i++) { + if (targets.indexOf(highlightList[i]) === -1) { + newHls.push(highlightList[i]); } } - highlights = newHls; - this.add("Now highlighting on: " + highlights.join(', ')); + highlights[key] = newHls; + this.add("Now highlighting on " + (key === 'global' ? "(everywhere): " : "(in " + key + "): ") + highlights[key].join(', ')); // We update the regex this.updateHighlightRegExp(highlights); break; @@ -756,10 +773,11 @@ if (target === 'delete') { Dex.prefs('highlights', false); this.add("All highlights cleared"); - } else if (target === 'show' || target === 'list') { + } else if (['show', 'list', 'roomshow', 'roomlist'].includes(target)) { // Shows a list of the current highlighting words - if (highlights.length > 0) { - this.add("Current highlight list: " + highlights.join(", ")); + var key = target.startsWith('room') ? (Config.server.id + '#' + this.id) : 'global'; + if (highlights[key].length > 0) { + this.add("Current highlight list " + (key === 'global' ? "(everywhere): " : "(in " + key + "): ") + highlights[key].join(", ")); } else { this.add('Your highlight list is empty.'); } @@ -996,8 +1014,11 @@ case 'hl': this.add('Set up highlights:'); this.add('/highlight add, [word] - Add the word [word] to the highlight list.'); + this.add('/highlight roomadd, [word] - Add the word [word] to the highlight list of whichever room you used the command in.'); this.add('/highlight list - List all words that currently highlight you.'); - this.add('/highlight delete, [word] - Delete the word [word] from the highlight list.'); + this.add('/highlight roomlist - List all words that currently highlight you in whichever room you used the command in.'); + this.add('/highlight delete, [word] - Delete the word [word] from your entire highlight list.'); + this.add('/highlight roomdelete, [word] - Delete the word [word] from the highlight list of whichever room you used the command in.'); this.add('/highlight delete - Clear the highlight list.'); return false; case 'rank':