autocomplete: optimize various types of bogus input.

Optimize autocomplete to ignore various types of bogus input that will
never match anything. It turns out it's not uncommon for people to do
things like paste random URLs into autocomplete, or hold down keys, or
enter long strings of gibberish text (sometimes in other languages).
Some things, like autocorrect and slash abbreviations, become
pathologically slow when fed certain types of bad input.

Autocomplete will abort and return nothing in the following situations:

* Searching for URLs (tags that start with http:// or https://).
* Overly long tags (strings longer than the 170 char tag name limit).
* Slash abbreviations longer than 10 chars (e.g. typing `/qwoijqoiqogirqewgoi`).
* Slash abbreviations that aren't alphanumeric (e.g. typing `/////////`).
* Autocorrect input that contains too much punctuation and not enough actual letters.
This commit is contained in:
evazion
2021-01-11 02:11:28 -06:00
parent fc5db679e4
commit be1251b6be
4 changed files with 35 additions and 13 deletions

View File

@@ -254,12 +254,13 @@ class Tag < ApplicationRecord
end
def abbreviation_matches(abbrev)
abbrev = abbrev.delete_prefix("/")
abbrev = abbrev.downcase.delete_prefix("/")
return none if abbrev !~ /\A[a-z0-9\*]*\z/
where("regexp_replace(tags.name, ?, '\\1', 'g') LIKE ?", ABBREVIATION_REGEXP.source, abbrev.to_escaped_for_sql_like)
end
def find_by_abbreviation(abbrev)
abbrev = abbrev.delete_prefix("/")
abbreviation_matches(abbrev.escape_wildcards).order(post_count: :desc).first
end