FEATURE: Add a test facility to the watched words admin interface

This commit is contained in:
David Taylor 2019-08-02 10:53:03 +01:00
parent 39e0442de9
commit 06e757245f
9 changed files with 68 additions and 3 deletions

View File

@ -2,6 +2,7 @@ import computed from "ember-addons/ember-computed-decorators";
import WatchedWord from "admin/models/watched-word";
import { ajax } from "discourse/lib/ajax";
import { fmt } from "discourse/lib/computed";
import showModal from "discourse/lib/show-modal";
export default Ember.Controller.extend({
actionNameKey: null,
@ -101,6 +102,16 @@ export default Ember.Controller.extend({
}
}
);
},
test() {
WatchedWord.findAll().then(data => {
this.set("adminWatchedWords.model", data);
showModal("admin-watched-word-test", {
admin: true,
model: this.currentAction
});
});
}
}
});

View File

@ -0,0 +1,11 @@
import { default as computed } from "ember-addons/ember-computed-decorators";
import ModalFunctionality from "discourse/mixins/modal-functionality";
export default Ember.Controller.extend(ModalFunctionality, {
@computed("value", "model.compiledRegularExpression")
matches(value, regexpString) {
if (!value || !regexpString) return;
let censorRegexp = new RegExp(regexpString, "ig");
return value.match(censorRegexp);
}
});

View File

@ -42,7 +42,8 @@ WatchedWord.reopenClass({
name: I18n.t("admin.watched_words.actions." + n),
words: actions[n],
count: actions[n].length,
regularExpressions: list.regular_expressions
regularExpressions: list.regular_expressions,
compiledRegularExpression: list.compiled_regular_expressions[n]
});
});
});

View File

@ -0,0 +1,16 @@
{{#d-modal-body rawTitle=(i18n "admin.watched_words.test.modal_title" action=model.name) class="watched-words-test-modal"}}
<p>{{i18n "admin.watched_words.test.description"}}</p>
{{textarea name="test_value" value=value autofocus="autofocus"}}
{{#if matches}}
<p>
{{i18n "admin.watched_words.test.found_matches"}}
<ul>
{{#each matches as |match|}}
<li>{{match}}</li>
{{/each}}
</ul>
</p>
{{else}}
<p>{{i18n "admin.watched_words.test.no_matches"}}</p>
{{/if}}
{{/d-modal-body}}

View File

@ -38,6 +38,10 @@
</div>
<div class="clear-all-row">
{{d-button
label="admin.watched_words.test.button_label"
icon="far-eye"
action=(action "test")}}
{{d-button
class="btn-danger clear-all"
label="admin.watched_words.clear_all"

View File

@ -370,6 +370,9 @@ table.screened-ip-addresses {
display: flex;
margin-top: 10px;
justify-content: flex-end;
.clear-all {
margin-left: 5px;
}
}
}
@ -431,6 +434,10 @@ table.screened-ip-addresses {
}
}
.watched-words-test-modal p {
margin-top: 0;
}
// Search logs
table.search-logs-list {

View File

@ -1,7 +1,7 @@
# frozen_string_literal: true
class WatchedWordListSerializer < ApplicationSerializer
attributes :actions, :words, :regular_expressions
attributes :actions, :words, :regular_expressions, :compiled_regular_expressions
def actions
WatchedWord.actions.keys
@ -18,4 +18,12 @@ class WatchedWordListSerializer < ApplicationSerializer
def regular_expressions
SiteSetting.watched_words_regular_expressions?
end
def compiled_regular_expressions
expressions = {}
WatchedWord.actions.keys.each do |action|
expressions[action] = WordWatcher.word_matcher_regexp(action)&.source
end
expressions
end
end

View File

@ -3920,6 +3920,12 @@ en:
exists: "Already exists"
upload: "Add from file"
upload_successful: "Upload successful. Words have been added."
test:
button_label: "Test"
modal_title: "Test '%{action}' Watched Words"
description: "Enter text below to check for matches with watched words"
found_matches: "Found matches:"
no_matches: "No matches found"
impersonate:
title: "Impersonate"

View File

@ -8,6 +8,7 @@ export default {
{ id: 4, word: "scheme", action: "flag" },
{ id: 5, word: "coupon", action: "require_approval" },
{ id: 6, word: '<img src="x">', action: "block" }
]
],
compiled_regular_expressions: {}
}
};