Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent query spam by using the searchbar #39

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion asset/css/search-base.less
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
font-size: 80%;
}

.failure-message {
.failure-message,
.progress-indicator {
font-weight: bold;

em {
Expand Down
37 changes: 36 additions & 1 deletion asset/js/widget/Completer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ define(["../notjQuery"], function ($) {
this.completedValue = null;
this.completedData = null;
this._termSuggestions = null;
this.last_response_times = [];
}

get termSuggestions() {
Expand Down Expand Up @@ -177,6 +178,19 @@ define(["../notjQuery"], function ($) {
}

requestCompletion(input, data, trigger = 'user') {
let showProgress = false;
if (this.last_response_times.length === 3) {
let avgResponseTime = this.last_response_times.reduce((a, b) => a + b)
/ this.last_response_times.length;
if (avgResponseTime > 1000) {
if (this.activeSuggestion !== null) {
return;
}

showProgress = true;
}
}

this.abort();

this.nextSuggestion = setTimeout(() => {
Expand All @@ -194,8 +208,14 @@ define(["../notjQuery"], function ($) {
}
}

let now = Date.now();
req.addEventListener('loadend', () => {
let hide = false;
if (req.readyState > 0) {
if (this.last_response_times.push(Date.now() - now) > 3) {
this.last_response_times.shift();
}

if (req.responseText) {
let suggestions = this.renderSuggestions(req.responseText);
if (trigger === 'script') {
Expand All @@ -212,16 +232,31 @@ define(["../notjQuery"], function ($) {
this.showSuggestions(suggestions, input);
}
} else {
this.hideSuggestions();
hide = true;
}
}

this.activeSuggestion = null;
this.nextSuggestion = null;

if (hide) {
this.hideSuggestions();
}
});

req.send(JSON.stringify(data));

if (showProgress) {
this.showSuggestions(this.renderSuggestions(`
<ul>
<li class="progress-indicator">
<em>${this.input.dataset.completeActive}</em>
<i class="icon fa fa-spinner spinner active"></i>
</li>
</ul>
`), input);
}

this.activeSuggestion = req;
}, 200);
}
Expand Down
1 change: 1 addition & 0 deletions src/Control/SearchBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ protected function assemble()
'data-incomplete-group' => t('Please close or remove this group.'),
'data-choose-template' => t('Please type one of: %s', '..<comma separated list>'),
'data-choose-column' => t('Please enter a valid column.'),
'data-complete-active' => t('Searching...'),
'validators' => [
new CallbackValidator(function ($q, CallbackValidator $validator) {
try {
Expand Down