2024-02-25 12:15:55 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-14 22:27:53 -04:00
|
|
|
class UserFeedback < ApplicationRecord
|
2012-01-27 14:22:47 -05:00
|
|
|
self.table_name = "user_feedback"
|
2010-02-19 17:30:11 -05:00
|
|
|
belongs_to :user
|
2018-04-02 13:51:26 -04:00
|
|
|
belongs_to_creator
|
2024-01-04 15:31:54 -05:00
|
|
|
belongs_to_updater
|
2024-11-10 23:22:40 -05:00
|
|
|
normalizes :body, with: ->(body) { body.gsub("\r\n", "\n") }
|
2023-04-10 06:51:03 -04:00
|
|
|
validates :body, :category, presence: true
|
|
|
|
validates :category, inclusion: { in: %w[positive negative neutral] }
|
2023-09-12 13:09:17 -04:00
|
|
|
validates :body, length: { minimum: 1, maximum: Danbooru.config.user_feedback_max_size }
|
2020-04-25 02:56:06 -04:00
|
|
|
validate :creator_is_moderator, on: :create
|
2013-03-22 20:36:34 -04:00
|
|
|
validate :user_is_not_creator
|
2024-07-21 13:58:40 -04:00
|
|
|
after_create :log_create
|
|
|
|
after_update :log_update
|
|
|
|
after_destroy :log_destroy
|
2023-04-10 06:51:03 -04:00
|
|
|
after_save :create_dmail
|
2013-01-10 17:45:52 -05:00
|
|
|
|
2023-04-10 06:51:03 -04:00
|
|
|
attr_accessor :send_update_dmail
|
|
|
|
|
2024-07-21 13:58:40 -04:00
|
|
|
scope :active, -> { where(is_deleted: false) }
|
|
|
|
scope :deleted, -> { where(is_deleted: true) }
|
|
|
|
|
|
|
|
module LogMethods
|
|
|
|
def log_create
|
|
|
|
ModAction.log(:user_feedback_create, { user_id: user_id, reason: body, type: category, record_id: id })
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_update
|
|
|
|
details = { user_id: user_id, reason: body, reason_was: body_before_last_save, type: category, type_was: category_before_last_save, record_id: id }
|
|
|
|
if saved_change_to_is_deleted?
|
|
|
|
action = is_deleted? ? :user_feedback_delete : :user_feedback_undelete
|
|
|
|
ModAction.log(action, details)
|
|
|
|
return unless saved_change_to_category? || saved_change_to_body?
|
|
|
|
end
|
|
|
|
ModAction.log(:user_feedback_update, details)
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_destroy
|
2024-07-21 18:20:40 -04:00
|
|
|
ModAction.log(:user_feedback_destroy, { user_id: user_id, reason: body, type: category, record_id: id })
|
2024-07-21 13:58:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-10 17:45:52 -05:00
|
|
|
module SearchMethods
|
|
|
|
def positive
|
|
|
|
where("category = ?", "positive")
|
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2013-01-10 17:45:52 -05:00
|
|
|
def neutral
|
|
|
|
where("category = ?", "neutral")
|
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2013-01-10 17:45:52 -05:00
|
|
|
def negative
|
|
|
|
where("category = ?", "negative")
|
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2013-01-10 17:45:52 -05:00
|
|
|
def for_user(user_id)
|
|
|
|
where("user_id = ?", user_id)
|
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2018-02-04 17:43:00 -05:00
|
|
|
def default_order
|
|
|
|
order(created_at: :desc)
|
|
|
|
end
|
|
|
|
|
2024-07-21 13:58:40 -04:00
|
|
|
def visible(user)
|
2024-11-25 09:50:12 -05:00
|
|
|
if user.is_staff?
|
2024-07-21 13:58:40 -04:00
|
|
|
all
|
|
|
|
else
|
|
|
|
active
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-10 17:45:52 -05:00
|
|
|
def search(params)
|
2017-12-17 17:58:34 -05:00
|
|
|
q = super
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2024-07-21 13:58:40 -04:00
|
|
|
deleted = (params[:deleted].presence || "excluded").downcase
|
|
|
|
q = q.active if deleted == "excluded"
|
|
|
|
q = q.deleted if deleted == "only"
|
2018-08-31 20:23:25 -04:00
|
|
|
|
2024-07-21 13:58:40 -04:00
|
|
|
q = q.attribute_matches(:body, params[:body_matches])
|
2023-08-03 16:01:53 -04:00
|
|
|
q = q.where_user(:user_id, :user, params)
|
|
|
|
q = q.where_user(:creator_id, :creator, params)
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2013-05-11 20:53:11 -04:00
|
|
|
if params[:category].present?
|
|
|
|
q = q.where("category = ?", params[:category])
|
|
|
|
end
|
|
|
|
|
2023-07-07 08:32:57 -04:00
|
|
|
q.apply_basic_order(params)
|
2013-01-10 17:45:52 -05:00
|
|
|
end
|
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2024-07-21 13:58:40 -04:00
|
|
|
include LogMethods
|
2013-01-10 17:45:52 -05:00
|
|
|
extend SearchMethods
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2011-07-17 18:40:24 -04:00
|
|
|
def user_name
|
2013-02-19 14:10:13 -05:00
|
|
|
User.id_to_name(user_id)
|
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2011-02-01 18:08:01 -05:00
|
|
|
def user_name=(name)
|
|
|
|
self.user_id = User.name_to_id(name)
|
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2013-03-01 09:27:48 -05:00
|
|
|
def create_dmail
|
2023-04-10 06:51:03 -04:00
|
|
|
should_send = saved_change_to_id? || (send_update_dmail.to_s.truthy? && saved_change_to_body?)
|
|
|
|
return unless should_send
|
|
|
|
|
|
|
|
action = saved_change_to_id? ? "created" : "updated"
|
2024-01-04 15:31:54 -05:00
|
|
|
body = %(#{updater_name} #{action} a "#{category} record":/user_feedbacks?search[user_id]=#{user_id} for your account:\n\n#{self.body})
|
2023-04-10 06:51:03 -04:00
|
|
|
Dmail.create_automated(to_id: user_id, title: "Your user record has been updated", body: body)
|
2013-03-01 09:27:48 -05:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2019-06-29 11:45:58 -04:00
|
|
|
def creator_is_moderator
|
2023-04-10 06:51:03 -04:00
|
|
|
errors.add(:creator, "must be moderator") unless creator.is_moderator?
|
2013-03-22 20:36:34 -04:00
|
|
|
end
|
2019-04-13 03:22:27 -04:00
|
|
|
|
2013-03-22 20:36:34 -04:00
|
|
|
def user_is_not_creator
|
2023-04-10 06:51:03 -04:00
|
|
|
errors.add(:creator, "cannot submit feedback for yourself") if user_id == creator_id
|
2010-02-19 18:04:43 -05:00
|
|
|
end
|
2016-11-28 04:48:24 -05:00
|
|
|
|
|
|
|
def editable_by?(editor)
|
2023-04-10 06:51:03 -04:00
|
|
|
editor.is_moderator? && editor != user
|
2016-11-28 04:48:24 -05:00
|
|
|
end
|
2024-07-21 13:58:40 -04:00
|
|
|
|
|
|
|
def deletable_by?(deleter)
|
|
|
|
editable_by?(deleter)
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroyable_by?(destroyer)
|
|
|
|
deletable_by?(destroyer) && (destroyer.is_admin? || destroyer == creator)
|
|
|
|
end
|
2010-02-19 17:30:11 -05:00
|
|
|
end
|