* Rename banned and comment_banned roles * Add data update script to rename roles containing 'ban' * Add named error for Suspended users * Update unidiomatic method names * Rename misc banned to suspended * Apply suggestions from code review Co-authored-by: Michael Kohl <me@citizen428.net> * Add unit tests for suspended methods This commit also adds TODO comments for removing banned and comment_banned from the codebase after data update scripts have successfully run on all of our Forems. Co-authored-by: Michael Kohl <me@citizen428.net>
52 lines
1.4 KiB
Ruby
52 lines
1.4 KiB
Ruby
module Search
|
|
module QueryBuilders
|
|
class User < QueryBase
|
|
QUERY_KEYS = %i[
|
|
search_fields
|
|
].freeze
|
|
|
|
# In the event we want to search for documents that do NOT contain certain values
|
|
EXCLUDED_TERM_KEYS = {
|
|
exclude_roles: "roles"
|
|
}.freeze
|
|
|
|
DEFAULT_PARAMS = {
|
|
sort_by: "hotness_score",
|
|
sort_direction: "desc",
|
|
size: 0
|
|
}.freeze
|
|
|
|
def initialize(params:)
|
|
super()
|
|
|
|
@params = params.deep_symbolize_keys
|
|
|
|
# default to excluding users who are suspended
|
|
# TODO: [@jacobherrington] banned can be removed once the data scripts have succesfully run on all Forems
|
|
@params[:exclude_roles] = %w[suspended banned]
|
|
|
|
build_body
|
|
end
|
|
|
|
private
|
|
|
|
def build_queries
|
|
@body[:query] = { bool: {} }
|
|
@body[:query][:bool][:must] = query_conditions if query_keys_present?
|
|
@body[:query][:bool][:must_not] = excluded_term_keys if excluded_term_keys_present?
|
|
end
|
|
|
|
def excluded_term_keys_present?
|
|
self.class::EXCLUDED_TERM_KEYS.detect { |key, _| @params[key].present? }
|
|
end
|
|
|
|
def excluded_term_keys
|
|
EXCLUDED_TERM_KEYS.filter_map do |term_key, search_key|
|
|
next unless @params.key? term_key
|
|
|
|
{ terms: { search_key => Array.wrap(@params[term_key]) } }
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|