[UserWarnable] Fix warning_user_id never being set (#541)

* [UserWarnable] fix `warning_user_id` never being set
I know `update` & `update_columns` function differently, but I feel the difference isn't needed

* [UserWarnable] Add inline user and timestamp

* lint

* Move warning info into partial

Also removes the user/timestamp as that is redundant for now

* Fix unmarking

* Tweak fixer script

---------

Co-authored-by: Earlopain <14981592+Earlopain@users.noreply.github.com>
This commit is contained in:
Donovan Daniels 2023-12-04 13:19:49 -06:00 committed by GitHub
parent 6eaf435002
commit fd5b491694
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 37 additions and 28 deletions

View File

@ -12,11 +12,11 @@ module UserWarnable
end
def user_warned!(type, user)
update({ warning_type: type, warning_user_id: user })
update(warning_type: type, warning_user_id: user.id)
end
def remove_user_warning!
update_columns({ warning_type: nil, warning_user_id: nil })
update(warning_type: nil, warning_user_id: nil)
end
def was_warned?
@ -26,11 +26,11 @@ module UserWarnable
def warning_type_string
case warning_type
when "warning"
"User received a warning for the contents of this message."
"User received a warning for the contents of this message"
when "record"
"User received a record for the contents of this message."
"User received a record for the contents of this message"
when "ban"
"User was banned for the contents of this message."
"User was banned for the contents of this message"
else
"[This is a bug with the website. Woo!]"
end

View File

@ -2,13 +2,15 @@ class Blip < ApplicationRecord
include UserWarnable
simple_versioning
belongs_to_creator
user_status_counter :blip_count
validates :body, presence: true
belongs_to :parent, class_name: "Blip", foreign_key: "response_to", optional: true
has_many :responses, class_name: "Blip", foreign_key: "response_to"
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
validate :validate_parent_exists, on: :create
validate :validate_creator_is_not_limited, on: :create
user_status_counter :blip_count
belongs_to :parent, class_name: "Blip", foreign_key: "response_to", optional: true
belongs_to :warning_user, class_name: "User", optional: true
has_many :responses, class_name: "Blip", foreign_key: "response_to"
def response?
parent.present?

View File

@ -26,6 +26,7 @@ class Comment < ApplicationRecord
user_status_counter :comment_count
belongs_to :post, counter_cache: :comment_count
belongs_to :warning_user, class_name: "User", optional: true
has_many :votes, :class_name => "CommentVote", :dependent => :destroy
module SearchMethods

View File

@ -6,6 +6,7 @@ class ForumPost < ApplicationRecord
belongs_to_updater
user_status_counter :forum_post_count
belongs_to :topic, :class_name => "ForumTopic"
belongs_to :warning_user, class_name: "User", optional: true
has_many :votes, class_name: "ForumPostVote"
has_one :tag_alias
has_one :tag_implication

View File

@ -0,0 +1,6 @@
<% if record.was_warned? %>
<div class="user-warning">
<hr>
<em><%= record.warning_type_string %>.</em>
</div>
<% end %>

View File

@ -23,12 +23,7 @@
<div class="body dtext-container">
<%= format_text(blip.body, allow_color: blip.creator.is_privileged?) %>
</div>
<% if blip.was_warned? %>
<div class="user-warning">
<hr>
<em><%= blip.warning_type_string %></em>
</div>
<% end %>
<%= render "application/warned_notice", record: blip %>
<div class="content-menu">
<menu>
<% if CurrentUser.is_member? %>

View File

@ -23,12 +23,7 @@
<%= format_text(comment.body, allow_color: comment.creator.is_privileged?) %>
</div>
<%= render "application/update_notice", record: comment %>
<% if comment.was_warned? %>
<div class="user-warning">
<hr>
<em><%= comment.warning_type_string %></em>
</div>
<% end %>
<%= render "application/warned_notice", record: comment %>
<div class="content-menu">
<menu>
<% if post && comment.can_reply?(CurrentUser.user) %>

View File

@ -23,12 +23,7 @@
<%= format_text(parse_embedded_tag_request_text(forum_post.body), allow_color: forum_post.creator.is_privileged?) %>
</div>
<%= render "application/update_notice", record: forum_post %>
<% if forum_post.was_warned? %>
<div class="user-warning">
<hr>
<em><%= forum_post.warning_type_string %></em>
</div>
<% end %>
<%= render "application/warned_notice", record: forum_post %>
<div class="content-menu">
<menu>
<% if CurrentUser.is_member? && @forum_topic && params[:controller] != "forum_posts" %>

View File

@ -0,0 +1,14 @@
#!/usr/bin/env ruby
require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "config", "environment"))
# warning_user_id has never been set - we're updating it with the current updater_id, as they are very likely to be the one that added the warning
def update(model)
model.where(warning_user_id: nil).where.not(warning_type: nil).find_each do |record|
if record.was_warned? && record.warning_user_id != record.updater_id
record.update_columns(warning_user_id: record.updater_id)
end
end
end
[Comment, ForumPost].each { |model| update(model) }