Files
danbooru/app/models/user_feedback.rb
evazion 34057b25e1 mod actions: record the subject of the mod action.
Add a polymorphic `subject` field that records the subject of the mod
action. The subject is the post, user, comment, artist, etc the mod
action is for.

* The subject for the user ban and unban actions is the user, not the ban itself.
* The subject for the user feedback update and deletion actions is the user,
  not the feedback itself.
* The subject for the post undeletion action is the post, not the approval itself.
* The subject for the move favorites action is the source post where the
  favorites were moved from, not the destination post where the favorites
  were moved to.
* The subject for the post permanent delete action is nil, because the
  post itself is hard deleted.
* When a post is permanently deleted, all mod actions related to the
  post are deleted as well.
2022-09-25 04:04:28 -05:00

65 lines
2.7 KiB
Ruby

# frozen_string_literal: true
class UserFeedback < ApplicationRecord
self.table_name = "user_feedback"
attr_accessor :disable_dmail_notification
belongs_to :user
belongs_to :creator, class_name: "User"
validates :body, presence: true
validates :category, presence: true, inclusion: { in: %w[positive negative neutral] }
after_create :create_dmail, unless: :disable_dmail_notification
after_update(:if => ->(rec) { CurrentUser.id != rec.creator_id}) do |rec|
ModAction.log(%{updated user feedback for "#{rec.user.name}":#{Routes.user_path(rec.user)}}, :user_feedback_update, subject: user, user: CurrentUser.user)
end
after_destroy(:if => ->(rec) { CurrentUser.id != rec.creator_id}) do |rec|
ModAction.log(%{deleted user feedback for "#{rec.user.name}":#{Routes.user_path(rec.user)}}, :user_feedback_delete, subject: user, user: CurrentUser.user)
end
deletable
scope :positive, -> { where(category: "positive") }
scope :neutral, -> { where(category: "neutral") }
scope :negative, -> { where(category: "negative") }
module SearchMethods
def visible(viewer)
viewer.is_moderator? ? all : undeleted
end
def default_order
order(created_at: :desc)
end
def search(params, current_user)
q = search_attributes(params, [:id, :created_at, :updated_at, :category, :body, :is_deleted, :creator, :user], current_user: current_user)
q.apply_default_order(params)
end
end
extend SearchMethods
def user_name=(name)
self.user = User.find_by_name(name)
end
def disclaimer
if category != "negative"
return nil
end
"The purpose of feedback is to help you become a valuable member of the site by highlighting adverse behaviors. The author, #{creator.name}, should have sent you a message in the recent past as a warning. The fact that you're receiving this feedback now implies you've ignored their advice.\n\nYou can protest this feedback by petitioning the mods and admins in the forum. If #{creator.name} fails to provide sufficient evidence, you can have the feedback removed. However, if you fail to defend yourself against the accusations, you will likely earn yourself another negative feedback.\n\nNegative feedback generally doesn't affect your usability of the site. But it does mean other users may trust you less and give you less benefit of the doubt.\n\n"
end
def create_dmail
body = %{#{disclaimer}@#{creator.name} created a "#{category} record":#{Routes.user_feedbacks_path(search: { user_id: user_id })} for your account:\n\n#{self.body}}
Dmail.create_automated(:to_id => user_id, :title => "Your user record has been updated", :body => body)
end
def self.available_includes
[:creator, :user]
end
end