Skip to content

Commit 7eb9b98

Browse files
Gargronhiyuki2578
authored andcommitted
Change hashtag search to only return results that have trended in the past (mastodon#11448)
* Change hashtag search to only return results that have trended in the past A way to eliminate typos and other one-off "junk" results * Fix excluding exact matches that don't have a score * Fix tests
1 parent 0d71136 commit 7eb9b98

2 files changed

Lines changed: 6 additions & 4 deletions

File tree

app/models/tag.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,11 @@ def find_or_create_by_names(name_or_names)
7676
end
7777

7878
def search_for(term, limit = 5, offset = 0)
79-
pattern = sanitize_sql_like(normalize(term.strip)) + '%'
79+
normalized_term = normalize(term.strip).mb_chars.downcase.to_s
80+
pattern = sanitize_sql_like(normalized_term) + '%'
8081

81-
Tag.where(arel_table[:name].lower.matches(pattern.mb_chars.downcase.to_s))
82+
Tag.where(arel_table[:name].lower.matches(pattern))
83+
.where(arel_table[:score].gt(0).or(arel_table[:name].lower.eq(normalized_term)))
8284
.order(Arel.sql('length(name) ASC, score DESC, name ASC'))
8385
.limit(limit)
8486
.offset(offset)

spec/models/tag_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@
136136
end
137137

138138
it 'finds the exact matching tag as the first item' do
139-
similar_tag = Fabricate(:tag, name: "matchlater")
140-
tag = Fabricate(:tag, name: "match")
139+
similar_tag = Fabricate(:tag, name: "matchlater", score: 1)
140+
tag = Fabricate(:tag, name: "match", score: 1)
141141

142142
results = Tag.search_for("match")
143143

0 commit comments

Comments
 (0)