* Makes the ModeratorsQuery not return an exception on invalid role The current implementation of the query would yield an exception when recieving an invalid state parameter. As per the [GH issue discussion](https://github.com/forem/forem/issues/10060#issuecomment-692295217), Zhao recommended to change its behaviour and not trigger an exception in this condition. This commit does just that. If the state argument is invalid, the query now returns an empty result set. * Adds a warning when there are no matching mods * Re-trigger the build
37 lines
1 KiB
Ruby
37 lines
1 KiB
Ruby
module Admin
|
|
class ModeratorsQuery
|
|
DEFAULT_OPTIONS = {
|
|
state: :trusted
|
|
}.with_indifferent_access.freeze
|
|
|
|
def self.call(relation: User.all, options: {})
|
|
options = DEFAULT_OPTIONS.merge(options)
|
|
state, search = options.values_at(:state, :search)
|
|
|
|
relation = if state.to_s == "potential"
|
|
relation.where(
|
|
"id NOT IN (SELECT user_id FROM users_roles WHERE role_id = ?)",
|
|
role_id_for(:trusted),
|
|
).order("users.comments_count" => :desc)
|
|
else
|
|
relation.joins(:roles)
|
|
.where(users_roles: { role_id: role_id_for(state) })
|
|
end
|
|
|
|
relation = search_relation(relation, search) if search.presence
|
|
|
|
relation
|
|
end
|
|
|
|
def self.role_id_for(role)
|
|
Role.find_by(name: role)&.id
|
|
end
|
|
|
|
def self.search_relation(relation, search)
|
|
relation.where(
|
|
"users.username ILIKE :search OR users.name ILIKE :search",
|
|
search: "%#{search}%",
|
|
)
|
|
end
|
|
end
|
|
end
|