2024-02-25 12:15:55 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-02-28 14:24:04 -05:00
|
|
|
class Blip < ApplicationRecord
|
2021-04-05 01:06:06 -04:00
|
|
|
include UserWarnable
|
2019-04-27 14:28:02 -04:00
|
|
|
simple_versioning
|
2019-02-28 14:24:04 -05:00
|
|
|
belongs_to_creator
|
2023-12-04 14:56:14 -05:00
|
|
|
belongs_to_updater optional: true
|
2024-11-10 23:22:40 -05:00
|
|
|
normalizes :body, with: ->(body) { body.gsub("\r\n", "\n") }
|
2019-09-05 08:59:51 -04:00
|
|
|
validates :body, presence: true
|
2023-12-04 14:19:49 -05:00
|
|
|
validates :body, length: { minimum: 5, maximum: Danbooru.config.blip_max_size }
|
|
|
|
validate :validate_parent_exists, on: :create
|
|
|
|
validate :validate_creator_is_not_limited, on: :create
|
|
|
|
|
2023-12-04 14:56:14 -05:00
|
|
|
after_update(if: ->(rec) { !rec.saved_change_to_is_hidden? && CurrentUser.id != rec.creator_id }) do |rec|
|
|
|
|
ModAction.log(:blip_update, { blip_id: rec.id, user_id: rec.creator_id })
|
|
|
|
end
|
|
|
|
after_destroy do |rec|
|
|
|
|
ModAction.log(:blip_delete, { blip_id: rec.id, user_id: rec.creator_id })
|
|
|
|
end
|
|
|
|
after_save(if: ->(rec) { rec.saved_change_to_is_hidden? && CurrentUser.id != rec.creator_id }) do |rec|
|
|
|
|
action = rec.is_hidden? ? :blip_hide : :blip_unhide
|
|
|
|
ModAction.log(action, { blip_id: rec.id, user_id: rec.creator_id })
|
|
|
|
end
|
|
|
|
|
2023-12-04 14:19:49 -05:00
|
|
|
user_status_counter :blip_count
|
2019-02-28 14:24:04 -05:00
|
|
|
belongs_to :parent, class_name: "Blip", foreign_key: "response_to", optional: true
|
2023-12-04 14:19:49 -05:00
|
|
|
belongs_to :warning_user, class_name: "User", optional: true
|
2019-02-28 14:24:04 -05:00
|
|
|
has_many :responses, class_name: "Blip", foreign_key: "response_to"
|
|
|
|
|
|
|
|
def response?
|
|
|
|
parent.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_responses?
|
|
|
|
responses.any?
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_creator_is_not_limited
|
2019-06-27 11:33:02 -04:00
|
|
|
allowed = creator.can_blip_with_reason
|
|
|
|
if allowed != true
|
|
|
|
errors.add(:creator, User.throttle_reason(allowed))
|
2019-09-04 00:56:41 -04:00
|
|
|
return false
|
2019-02-28 14:24:04 -05:00
|
|
|
end
|
2019-06-27 11:33:02 -04:00
|
|
|
true
|
2019-02-28 14:24:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def validate_parent_exists
|
|
|
|
if response_to.present?
|
|
|
|
errors.add(:response_to, "must exist") unless Blip.exists?(response_to)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module ApiMethods
|
|
|
|
def method_attributes
|
|
|
|
super + [:creator_name]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module PermissionsMethods
|
2022-12-23 10:35:37 -05:00
|
|
|
def can_edit?(user)
|
|
|
|
return true if user.is_admin?
|
|
|
|
return false if was_warned?
|
|
|
|
creator_id == user.id && created_at > 5.minutes.ago
|
2019-02-28 14:24:04 -05:00
|
|
|
end
|
|
|
|
|
2022-12-23 10:35:37 -05:00
|
|
|
def can_hide?(user)
|
|
|
|
return true if user.is_moderator?
|
|
|
|
return false if was_warned?
|
|
|
|
user.id == creator_id
|
2019-02-28 14:24:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def visible_to?(user)
|
|
|
|
return true unless is_hidden
|
|
|
|
user.is_moderator? || user.id == creator_id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module SearchMethods
|
2019-03-13 17:04:59 -04:00
|
|
|
def visible(user = CurrentUser)
|
|
|
|
if user.is_moderator?
|
2019-11-13 22:55:12 -05:00
|
|
|
all
|
2019-03-13 17:04:59 -04:00
|
|
|
else
|
2019-11-13 22:55:12 -05:00
|
|
|
where('is_hidden = ?', false)
|
2019-03-13 17:04:59 -04:00
|
|
|
end
|
2019-03-01 12:04:50 -05:00
|
|
|
end
|
|
|
|
|
2019-02-28 14:24:04 -05:00
|
|
|
def for_creator(user_id)
|
|
|
|
user_id.present? ? where("creator_id = ?", user_id) : none
|
|
|
|
end
|
|
|
|
|
|
|
|
def search(params)
|
|
|
|
q = super
|
|
|
|
|
|
|
|
q = q.includes(:creator).includes(:responses).includes(:parent)
|
|
|
|
|
2023-02-19 14:01:54 -05:00
|
|
|
q = q.attribute_matches(:body, params[:body_matches])
|
2019-02-28 14:24:04 -05:00
|
|
|
|
|
|
|
if params[:response_to].present?
|
|
|
|
q = q.where('response_to = ?', params[:response_to].to_i)
|
|
|
|
end
|
|
|
|
|
2023-08-03 16:01:53 -04:00
|
|
|
q = q.where_user(:creator_id, :creator, params)
|
2019-02-28 14:24:04 -05:00
|
|
|
|
2021-10-30 13:46:55 -04:00
|
|
|
if params[:ip_addr].present?
|
|
|
|
q = q.where("creator_ip_addr <<= ?", params[:ip_addr])
|
|
|
|
end
|
|
|
|
|
2019-02-28 14:24:04 -05:00
|
|
|
case params[:order]
|
|
|
|
when "updated_at", "updated_at_desc"
|
|
|
|
q = q.order("blips.updated_at DESC")
|
|
|
|
else
|
2023-07-07 08:32:57 -04:00
|
|
|
q = q.apply_basic_order(params)
|
2019-02-28 14:24:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
q
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
include PermissionsMethods
|
|
|
|
extend SearchMethods
|
|
|
|
include ApiMethods
|
2023-12-04 14:56:14 -05:00
|
|
|
|
|
|
|
def hide!
|
|
|
|
update(is_hidden: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
def unhide!
|
|
|
|
update(is_hidden: false)
|
|
|
|
end
|
2019-02-28 14:24:04 -05:00
|
|
|
end
|