eBooru/app/models/user_feedback.rb

132 lines
3.5 KiB
Ruby
Raw Permalink Normal View History

# frozen_string_literal: true
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
Raise error on unpermitted params. Fail loudly if we forget to whitelist a param instead of silently ignoring it. misc models: convert to strong params. artist commentaries: convert to strong params. * Disallow changing or setting post_id to a nonexistent post. artists: convert to strong params. * Disallow setting `is_banned` in create/update actions. Changing it this way instead of with the ban/unban actions would leave the artist in a partially banned state. bans: convert to strong params. * Disallow changing the user_id after the ban has been created. comments: convert to strong params. favorite groups: convert to strong params. news updates: convert to strong params. post appeals: convert to strong params. post flags: convert to strong params. * Disallow users from setting the `is_deleted` / `is_resolved` flags. ip bans: convert to strong params. user feedbacks: convert to strong params. * Disallow users from setting `disable_dmail_notification` when creating feedbacks. * Disallow changing the user_id after the feedback has been created. notes: convert to strong params. wiki pages: convert to strong params. * Also fix non-Builders being able to delete wiki pages. saved searches: convert to strong params. pools: convert to strong params. * Disallow setting `post_count` or `is_deleted` in create/update actions. janitor trials: convert to strong params. post disapprovals: convert to strong params. * Factor out quick-mod bar to shared partial. * Fix quick-mod bar to use `Post#is_approvable?` to determine visibility of Approve button. dmail filters: convert to strong params. password resets: convert to strong params. user name change requests: convert to strong params. posts: convert to strong params. users: convert to strong params. * Disallow setting password_hash, last_logged_in_at, last_forum_read_at, has_mail, and dmail_filter_attributes[user_id]. * Remove initialize_default_image_size (dead code). uploads: convert to strong params. * Remove `initialize_status` because status already defaults to pending in the database. tag aliases/implications: convert to strong params. tags: convert to strong params. forum posts: convert to strong params. * Disallow changing the topic_id after creating the post. * Disallow setting is_deleted (destroy/undelete actions should be used instead). * Remove is_sticky / is_locked (nonexistent attributes). forum topics: convert to strong params. * merges https://github.com/evazion/danbooru/tree/wip-rails-5.1 * lock pg gem to 0.21 (1.0.0 is incompatible with rails 5.1.4) * switch to factorybot and change all references Co-authored-by: r888888888 <r888888888@gmail.com> Co-authored-by: evazion <noizave@gmail.com> add diffs
2018-04-02 13:51:26 -04:00
belongs_to_creator
2024-01-04 15:31:54 -05:00
belongs_to_updater
normalizes :body, with: ->(body) { body.gsub("\r\n", "\n") }
validates :body, :category, presence: true
validates :category, inclusion: { in: %w[positive negative neutral] }
validates :body, length: { minimum: 1, maximum: Danbooru.config.user_feedback_max_size }
validate :creator_is_moderator, on: :create
2013-03-22 20:36:34 -04:00
validate :user_is_not_creator
after_create :log_create
after_update :log_update
after_destroy :log_destroy
after_save :create_dmail
2013-01-10 17:45:52 -05:00
attr_accessor :send_update_dmail
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
ModAction.log(:user_feedback_destroy, { user_id: user_id, reason: body, type: category, record_id: id })
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
def default_order
order(created_at: :desc)
end
def visible(user)
if user.is_staff?
all
else
active
end
end
2013-01-10 17:45:52 -05:00
def search(params)
q = super
2013-03-19 08:10:10 -04:00
deleted = (params[:deleted].presence || "excluded").downcase
q = q.active if deleted == "excluded"
q = q.deleted if deleted == "only"
q = q.attribute_matches(:body, params[:body_matches])
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
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
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})
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
def creator_is_moderator
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
errors.add(:creator, "cannot submit feedback for yourself") if user_id == creator_id
2010-02-19 18:04:43 -05:00
end
def editable_by?(editor)
editor.is_moderator? && editor != user
end
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