Files
danbooru/app/models/post_disapproval.rb
evazion 3114ef3daf searchable: standardize the <field>_matches operator for text fields.
Standardize it so that all fields of type `text` are searchable with
`search[<field>_matches]`.

Before, the `<field>_matches` param was handled manually and some fields
were left out or handled inconsistently. Now it applies to all columns
of type `text`.

This does a full-text search on the field, so for example, searching
`/artist_commentaries?search[translated_description_matches]=smiling`
will match translated commentaries containing either the word "smiling",
"smiles", "smiled", or "smile".

Note that this only applies to columns defined as type `text`, not to
columns defined as `character varying`. The difference is that `text` is
used for fields containing free-form natural language, such as comments,
notes, forum posts, wiki pages, pool descriptions, etc, while `character
varying` is used for short strings not containing free-form language,
such as tag names, wiki page titles, urls, status fields, etc.

API changes:

* Add the `search[original_title_matches]`, `search[original_description_matches]`,
  `search[translated_title_matches]`, `search[translated_description_matches]` params
  to /artist_commentaries and /artist_commentary_versions.
* Remove the `search[name_matches]` and `search[group_name_matches]` params from /artist_versions.
* Remove the `search[title_matches]` param from /wiki_page_versions.
* Change the `search[name_matches]` param on /pools, /favorite_groups, and /pool_versions
  to do a full-text search instead of a substring match.
2022-09-22 01:52:13 -05:00

78 lines
2.2 KiB
Ruby

# frozen_string_literal: true
class PostDisapproval < ApplicationRecord
DELETION_THRESHOLD = 1.month
REASONS = %w[breaks_rules poor_quality disinterest]
belongs_to :post
belongs_to :user
validates :user, uniqueness: { scope: :post, message: "have already hidden this post" }
validates :reason, inclusion: { in: REASONS }
validate :validate_disapproval
scope :with_message, -> { where.not(message: nil) }
scope :without_message, -> { where(message: nil) }
scope :breaks_rules, -> {where(:reason => "breaks_rules")}
scope :poor_quality, -> {where(:reason => "poor_quality")}
scope :disinterest, -> {where(:reason => "disinterest")}
def self.prune!
PostDisapproval.where("post_id in (select _.post_id from post_disapprovals _ where _.created_at < ?)", DELETION_THRESHOLD.ago).delete_all
end
concerning :SearchMethods do
class_methods do
def creator_matches(creator, searcher)
return none if creator.nil?
policy = Pundit.policy!(searcher, PostDisapproval.new(user: creator))
if policy.can_view_creator?
where(user: creator)
else
none
end
end
def search(params)
q = search_attributes(params, :id, :created_at, :updated_at, :message, :reason, :post)
q = q.with_message if params[:has_message].to_s.truthy?
q = q.without_message if params[:has_message].to_s.falsy?
if params[:user_id].present?
user = User.find(params[:user_id])
q = q.creator_matches(user, CurrentUser.user)
elsif params[:user_name].present?
user = User.find_by_name(params[:user_name])
q = q.creator_matches(user, CurrentUser.user)
end
case params[:order]
when "post_id", "post_id_desc"
q = q.order(post_id: :desc, id: :desc)
else
q = q.apply_default_order(params)
end
q
end
end
end
def self.available_includes
[:user, :post]
end
def validate_disapproval
if post.is_active?
errors.add(:post, "is already active and cannot be disapproved")
end
end
def message=(message)
message = nil if message.blank?
super(message)
end
end