From 2439a42bc144b91a6fda280f38ad5eaee73a46e8 Mon Sep 17 00:00:00 2001 From: moisseev Date: Sun, 7 Jun 2020 17:24:44 +0300 Subject: [PATCH] [WebUI] Add "Test selectors" tab --- interface/css/rspamd.css | 8 +++ interface/index.html | 45 +++++++++++++++++ interface/js/app/rspamd.js | 8 ++- interface/js/app/selectors.js | 94 +++++++++++++++++++++++++++++++++++ 4 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 interface/js/app/selectors.js diff --git a/interface/css/rspamd.css b/interface/css/rspamd.css index a23c5d135..eb5f2af1a 100644 --- a/interface/css/rspamd.css +++ b/interface/css/rspamd.css @@ -561,3 +561,11 @@ input.radio { #nprogress .bar { height: 1px; } + +/* Form element background for validation states */ +.has-error .form-control { + background-color: #fbe9e5; +} +.has-success .form-control { + background-color: #eef9e7; +} diff --git a/interface/index.html b/interface/index.html index 8e703649a..12f7ab2ba 100644 --- a/interface/index.html +++ b/interface/index.html @@ -39,6 +39,7 @@
  • Configuration
  • Symbols
  • Scan
  • +
  • History
  • +
    diff --git a/interface/js/app/rspamd.js b/interface/js/app/rspamd.js index 972d3c247..dc343a694 100644 --- a/interface/js/app/rspamd.js +++ b/interface/js/app/rspamd.js @@ -26,10 +26,10 @@ /* global jQuery:false, FooTable:false, Visibility:false */ define(["jquery", "d3pie", "visibility", "nprogress", "stickytabs", "app/stats", "app/graph", "app/config", - "app/symbols", "app/history", "app/upload"], + "app/symbols", "app/history", "app/upload", "app/selectors"], // eslint-disable-next-line max-params function ($, D3pie, visibility, NProgress, stickyTabs, tab_stat, tab_graph, tab_config, - tab_symbols, tab_history, tab_upload) { + tab_symbols, tab_history, tab_upload, tab_selectors) { "use strict"; var ui = { page_size: { @@ -260,6 +260,7 @@ function ($, D3pie, visibility, NProgress, stickyTabs, tab_stat, tab_graph, tab_ $('#selSrv [value="' + e.name + '"]').prop("disabled", true); } }); + tab_selectors.checkSelectors(ui); }, errorMessage: "Cannot get server status", server: "All SERVERS" @@ -272,10 +273,12 @@ function ($, D3pie, visibility, NProgress, stickyTabs, tab_stat, tab_graph, tab_ $(".learn").hide(); $("#resetHistory").attr("disabled", true); $("#errors-history").hide(); + $("#selectors_nav").hide(); } else { $(".learn").show(); $("#resetHistory").removeAttr("disabled", true); $("#errors-history").show(); + $("#selectors_nav").show(); } var buttons = $("#navBar form.navbar-right"); @@ -427,6 +430,7 @@ function ($, D3pie, visibility, NProgress, stickyTabs, tab_stat, tab_graph, tab_ }); tab_config.setup(ui); tab_history.setup(ui, tables); + tab_selectors.setup(ui); tab_symbols.setup(ui, tables); tab_upload.setup(ui, tables); selData = tab_graph.setup(ui); diff --git a/interface/js/app/selectors.js b/interface/js/app/selectors.js new file mode 100644 index 000000000..69fcd3ed4 --- /dev/null +++ b/interface/js/app/selectors.js @@ -0,0 +1,94 @@ +define(["jquery"], + function ($) { + "use strict"; + var ui = {}; + + function enable_disable_check_btn() { + $("#selectorsChkMsgBtn").prop("disabled", ( + $.trim($("#selectorsMsgArea").val()).length === 0 || + !$("#selectorsSelArea").parent().hasClass("has-success") + )); + } + + function get_server(rspamd) { + var checked_server = rspamd.getSelector("selSrv"); + return (checked_server === "All SERVERS") ? "local" : checked_server; + } + + function checkMsg(rspamd, data) { + var selector = $("#selectorsSelArea").val(); + rspamd.query("plugins/selectors/check_message?selector=" + encodeURIComponent(selector), { + data: data, + method: "POST", + success: function (neighbours_status) { + var json = neighbours_status[0].data; + if (json.success) { + rspamd.alertMessage("alert-success", "Message successfully processed"); + $("#selectorsResArea") + .val(Object.prototype.hasOwnProperty.call(json, "data") ? json.data.toString() : ""); + } else { + rspamd.alertMessage("alert-error", "Unexpected error processing message"); + } + }, + server: get_server(rspamd) + }); + } + + ui.checkSelectors = function (rspamd) { + function toggle_form_group_class(remove, add) { + var icon = { + error: "remove", + success: "ok" + }; + $("#selectorsSelArea").parent().removeClass("has-" + remove).addClass("has-" + add); + $("#selector-feedback-icon") + .removeClass("glyphicon-" + icon[remove]).addClass("glyphicon-" + icon[add]).show(); + enable_disable_check_btn(); + } + var selector = $("#selectorsSelArea").val(); + if (selector.length) { + rspamd.query("plugins/selectors/check_selector?selector=" + encodeURIComponent(selector), { + method: "GET", + success: function (json) { + if (json[0].data.success) { + toggle_form_group_class("error", "success"); + } else { + toggle_form_group_class("success", "error"); + } + }, + server: get_server(rspamd) + }); + } else { + $("#selectorsSelArea").parent().removeClass("has-error has-success"); + $("#selector-feedback-icon").hide(); + enable_disable_check_btn(); + } + }; + + ui.setup = function (rspamd) { + $("#selectorsMsgClean").on("click", function () { + $("#selectorsChkMsgBtn").attr("disabled", true); + $("#selectorsMsgArea").val(""); + return false; + }); + $("#selectorsClean").on("click", function () { + $("#selectorsSelArea").val(""); + ui.checkSelectors(rspamd); + return false; + }); + $("#selectorsChkMsgBtn").on("click", function () { + $("#selectorsResArea").val(""); + checkMsg(rspamd, $("#selectorsMsgArea").val()); + return false; + }); + + $("#selectorsMsgArea").on("input", function () { + enable_disable_check_btn(); + }); + $("#selectorsSelArea").on("input", function () { + ui.checkSelectors(rspamd); + }); + }; + + return ui; + }); -- 2.39.5