Skip to content

Commit 4e8ff00

Browse files
Gargronhiyuki2578
authored andcommitted
Add exclude_unreviewed param to GET /api/v2/search REST API (mastodon#11977)
Make it so normal search returns even unreviewed matches, but autosuggestions do not. Fix mastodon#11960
1 parent efe417e commit 4e8ff00

6 files changed

Lines changed: 22 additions & 17 deletions

File tree

app/controllers/api/v2/search_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def search_results
2222
params[:q],
2323
current_account,
2424
limit_param(RESULTS_LIMIT),
25-
search_params.merge(resolve: truthy_param?(:resolve))
25+
search_params.merge(resolve: truthy_param?(:resolve), exclude_unreviewed: truthy_param?(:exclude_unreviewed))
2626
)
2727
end
2828

app/javascript/mastodon/actions/compose.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ const fetchComposeSuggestionsTags = throttle((dispatch, getState, token) => {
369369
q: token.slice(1),
370370
resolve: false,
371371
limit: 4,
372+
exclude_unreviewed: true,
372373
},
373374
}).then(({ data }) => {
374375
dispatch(readyComposeSuggestionsTags(token, data.hashtags));

app/models/tag.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,15 @@ def find_or_create_by_names(name_or_names)
124124
end
125125
end
126126

127-
def search_for(term, limit = 5, offset = 0)
127+
def search_for(term, limit = 5, offset = 0, options = {})
128128
normalized_term = normalize(term.strip).mb_chars.downcase.to_s
129129
pattern = sanitize_sql_like(normalized_term) + '%'
130+
query = Tag.listable.where(arel_table[:name].lower.matches(pattern))
131+
query = query.where(arel_table[:name].lower.eq(normalized_term).or(arel_table[:reviewed_at].not_eq(nil))) if options[:exclude_unreviewed]
130132

131-
Tag.listable
132-
.where(arel_table[:name].lower.matches(pattern))
133-
.where(arel_table[:name].lower.eq(normalized_term).or(arel_table[:reviewed_at].not_eq(nil)))
134-
.order(Arel.sql('length(name) ASC, name ASC'))
135-
.limit(limit)
136-
.offset(offset)
133+
query.order(Arel.sql('length(name) ASC, name ASC'))
134+
.limit(limit)
135+
.offset(offset)
137136
end
138137

139138
def find_normalized(name)

app/services/search_service.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ def perform_hashtags_search!
6060
TagSearchService.new.call(
6161
@query,
6262
limit: @limit,
63-
offset: @offset
63+
offset: @offset,
64+
exclude_unreviewed: @options[:exclude_unreviewed]
6465
)
6566
end
6667

app/services/tag_search_service.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
class TagSearchService < BaseService
44
def call(query, options = {})
5-
@query = query.strip.gsub(/\A#/, '')
6-
@offset = options[:offset].to_i
7-
@limit = options[:limit].to_i
5+
@query = query.strip.gsub(/\A#/, '')
6+
@offset = options.delete(:offset).to_i
7+
@limit = options.delete(:limit).to_i
8+
@options = options
89

9-
results = from_elasticsearch if Chewy.enabled?
10+
results = from_elasticsearch if Chewy.enabled?
1011
results ||= from_database
1112

1213
results
@@ -72,12 +73,15 @@ def from_elasticsearch
7273
},
7374
}
7475

75-
TagsIndex.query(query).filter(filter).limit(@limit).offset(@offset).objects.compact
76+
definition = TagsIndex.query(query)
77+
definition = definition.filter(filter) if @options[:exclude_unreviewed]
78+
79+
definition.limit(@limit).offset(@offset).objects.compact
7680
rescue Faraday::ConnectionFailed, Parslet::ParseFailed
7781
nil
7882
end
7983

8084
def from_database
81-
Tag.search_for(@query, @limit, @offset)
85+
Tag.search_for(@query, @limit, @offset, @options)
8286
end
8387
end

spec/services/search_service_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@
7777
it 'includes the tag in the results' do
7878
query = '#tag'
7979
tag = Tag.new
80-
allow(Tag).to receive(:search_for).with('tag', 10, 0).and_return([tag])
80+
allow(Tag).to receive(:search_for).with('tag', 10, 0, exclude_unreviewed: nil).and_return([tag])
8181

8282
results = subject.call(query, nil, 10)
83-
expect(Tag).to have_received(:search_for).with('tag', 10, 0)
83+
expect(Tag).to have_received(:search_for).with('tag', 10, 0, exclude_unreviewed: nil)
8484
expect(results).to eq empty_results.merge(hashtags: [tag])
8585
end
8686
it 'does not include tag when starts with @ character' do

0 commit comments

Comments
 (0)