models: set more creator names explicitly.

Set creators explicitly for bans, BURs, comment votes, and posts.
This commit is contained in:
evazion
2020-02-23 02:42:12 -06:00
parent 3a018ee9f7
commit e47d0e0d05
16 changed files with 18 additions and 64 deletions

View File

@@ -24,7 +24,7 @@ class BansController < ApplicationController
end end
def create def create
@ban = Ban.create(ban_params(:create)) @ban = Ban.create(banner: CurrentUser.user, **ban_params(:create))
if @ban.errors.any? if @ban.errors.any?
render :action => "new" render :action => "new"

View File

@@ -59,7 +59,7 @@ class UsersController < ApplicationController
end end
def create def create
@user = User.new(user_params(:create)) @user = User.new(last_ip_addr: CurrentUser.ip_addr, **user_params(:create))
if !Danbooru.config.enable_recaptcha? || verify_recaptcha(model: @user) if !Danbooru.config.enable_recaptcha? || verify_recaptcha(model: @user)
@user.save @user.save
if @user.errors.empty? if @user.errors.empty?

View File

@@ -8,7 +8,6 @@ class Ban < ApplicationRecord
belongs_to :banner, :class_name => "User" belongs_to :banner, :class_name => "User"
validate :user_is_inferior validate :user_is_inferior
validates_presence_of :reason, :duration validates_presence_of :reason, :duration
before_validation :initialize_banner_id, :on => :create
scope :unexpired, -> { where("bans.expires_at > ?", Time.now) } scope :unexpired, -> { where("bans.expires_at > ?", Time.now) }
scope :expired, -> { where("bans.expires_at <= ?", Time.now) } scope :expired, -> { where("bans.expires_at <= ?", Time.now) }
@@ -50,10 +49,6 @@ class Ban < ApplicationRecord
end end
end end
def initialize_banner_id
self.banner_id = CurrentUser.id if self.banner_id.blank?
end
def user_is_inferior def user_is_inferior
if user if user
if user.is_admin? if user.is_admin?

View File

@@ -13,7 +13,6 @@ class BulkUpdateRequest < ApplicationRecord
validate :script_formatted_correctly validate :script_formatted_correctly
validate :forum_topic_id_not_invalid validate :forum_topic_id_not_invalid
validate :validate_script, :on => :create validate :validate_script, :on => :create
before_validation :initialize_attributes, :on => :create
before_validation :normalize_text before_validation :normalize_text
after_create :create_forum_topic after_create :create_forum_topic
after_save :update_notice after_save :update_notice
@@ -173,11 +172,6 @@ class BulkUpdateRequest < ApplicationRecord
lines.join("\n") lines.join("\n")
end end
def initialize_attributes
self.user_id = CurrentUser.user.id unless self.user_id
self.status = "pending"
end
def normalize_text def normalize_text
self.script = script.downcase self.script = script.downcase
end end

View File

@@ -48,9 +48,9 @@ class Comment < ApplicationRecord
end end
module VoteMethods module VoteMethods
def vote!(val) def vote!(val, voter = CurrentUser.user)
numerical_score = (val == "up") ? 1 : -1 numerical_score = (val == "up") ? 1 : -1
vote = votes.create!(:score => numerical_score) vote = votes.create!(user: voter, score: numerical_score)
if vote.is_positive? if vote.is_positive?
update_column(:score, score + 1) update_column(:score, score + 1)

View File

@@ -3,7 +3,6 @@ class CommentVote < ApplicationRecord
belongs_to :comment belongs_to :comment
belongs_to :user belongs_to :user
before_validation :initialize_user, :on => :create
validates_presence_of :score validates_presence_of :score
validates_uniqueness_of :user_id, :scope => :comment_id, :message => "have already voted for this comment" validates_uniqueness_of :user_id, :scope => :comment_id, :message => "have already voted for this comment"
validate :validate_user_can_vote validate :validate_user_can_vote
@@ -52,10 +51,6 @@ class CommentVote < ApplicationRecord
score == -1 score == -1
end end
def initialize_user
self.user_id = CurrentUser.user.id
end
def self.available_includes def self.available_includes
[:comment, :user] [:comment, :user]
end end

View File

@@ -10,7 +10,6 @@ class ForumPost < ApplicationRecord
has_one :tag_implication has_one :tag_implication
has_one :bulk_update_request has_one :bulk_update_request
before_validation :initialize_is_deleted, :on => :create
before_save :update_dtext_links, if: :dtext_links_changed? before_save :update_dtext_links, if: :dtext_links_changed?
before_create :autoreport_spam before_create :autoreport_spam
after_create :update_topic_updated_at_on_create after_create :update_topic_updated_at_on_create
@@ -183,10 +182,6 @@ class ForumPost < ApplicationRecord
topic.response_count -= 1 topic.response_count -= 1
end end
def initialize_is_deleted
self.is_deleted = false if is_deleted.nil?
end
def quoted_response def quoted_response
DText.quote(body, creator.name) DText.quote(body, creator.name)
end end

View File

@@ -19,7 +19,6 @@ class ForumTopic < ApplicationRecord
has_many :moderation_reports, through: :posts has_many :moderation_reports, through: :posts
has_one :original_post, -> {order("forum_posts.id asc")}, class_name: "ForumPost", foreign_key: "topic_id", inverse_of: :topic has_one :original_post, -> {order("forum_posts.id asc")}, class_name: "ForumPost", foreign_key: "topic_id", inverse_of: :topic
before_validation :initialize_is_deleted, :on => :create
validates_presence_of :title validates_presence_of :title
validates_associated :original_post validates_associated :original_post
validates_inclusion_of :category_id, :in => CATEGORIES.keys validates_inclusion_of :category_id, :in => CATEGORIES.keys
@@ -151,10 +150,6 @@ class ForumTopic < ApplicationRecord
ModAction.log("undeleted forum topic ##{id} (title: #{title})", :forum_topic_undelete) ModAction.log("undeleted forum topic ##{id} (title: #{title})", :forum_topic_undelete)
end end
def initialize_is_deleted
self.is_deleted = false if is_deleted.nil?
end
def page_for(post_id) def page_for(post_id)
(posts.where("id < ?", post_id).count / Danbooru.config.posts_per_page.to_f).ceil (posts.where("id < ?", post_id).count / Danbooru.config.posts_per_page.to_f).ceil
end end

View File

@@ -1,6 +1,5 @@
class ModAction < ApplicationRecord class ModAction < ApplicationRecord
belongs_to :creator, :class_name => "User" belongs_to :creator, :class_name => "User"
before_validation :initialize_creator, :on => :create
api_attributes including: [:category_id] api_attributes including: [:category_id]
@@ -75,12 +74,8 @@ class ModAction < ApplicationRecord
self.class.categories[category] self.class.categories[category]
end end
def self.log(desc, cat = :other) def self.log(desc, cat = :other, user = CurrentUser.user)
create(:description => desc, :category => categories[cat]) create(creator: user, description: desc, category: categories[cat])
end
def initialize_creator
self.creator_id = CurrentUser.id
end end
def self.available_includes def self.available_includes

View File

@@ -9,7 +9,6 @@ class Post < ApplicationRecord
# Tags to copy when copying notes. # Tags to copy when copying notes.
NOTE_COPY_TAGS = %w[translated partially_translated check_translation translation_request reverse_translation] NOTE_COPY_TAGS = %w[translated partially_translated check_translation translation_request reverse_translation]
before_validation :initialize_uploader, :on => :create
before_validation :merge_old_changes before_validation :merge_old_changes
before_validation :normalize_tags before_validation :normalize_tags
before_validation :strip_source before_validation :strip_source
@@ -984,15 +983,6 @@ class Post < ApplicationRecord
end end
end end
module UploaderMethods
def initialize_uploader
if uploader_id.blank?
self.uploader_id = CurrentUser.id
self.uploader_ip_addr = CurrentUser.ip_addr
end
end
end
module PoolMethods module PoolMethods
def pools def pools
Pool.where("pools.post_ids && array[?]", id) Pool.where("pools.post_ids && array[?]", id)
@@ -1691,7 +1681,6 @@ class Post < ApplicationRecord
include PresenterMethods include PresenterMethods
include TagMethods include TagMethods
include FavoriteMethods include FavoriteMethods
include UploaderMethods
include PoolMethods include PoolMethods
include VoteMethods include VoteMethods
extend CountMethods extend CountMethods

View File

@@ -729,7 +729,6 @@ class User < ApplicationRecord
end end
def initialize_attributes def initialize_attributes
self.last_ip_addr ||= CurrentUser.ip_addr
self.enable_post_navigation = true self.enable_post_navigation = true
self.new_post_navigation_layout = true self.new_post_navigation_layout = true
self.enable_sequential_post_navigation = true self.enable_sequential_post_navigation = true

View File

@@ -1,5 +1,6 @@
FactoryBot.define do FactoryBot.define do
factory(:comment_vote) do factory(:comment_vote) do
user
score {1} score {1}
end end
end end

View File

@@ -26,7 +26,7 @@ class CommentVotesControllerTest < ActionDispatch::IntegrationTest
end end
should "fail silently on errors" do should "fail silently on errors" do
create(:comment_vote, comment: @comment, score: -1) create(:comment_vote, user: @user, comment: @comment, score: -1)
assert_difference("CommentVote.count", 0) do assert_difference("CommentVote.count", 0) do
post_auth comment_comment_votes_path(comment_id: @comment.id, score: "down", format: "json"), @user post_auth comment_comment_votes_path(comment_id: @comment.id, score: "down", format: "json"), @user
assert_response 422 assert_response 422
@@ -47,7 +47,7 @@ class CommentVotesControllerTest < ActionDispatch::IntegrationTest
end end
should "fail on errors" do should "fail on errors" do
create(:comment_vote, :comment => @comment, :score => -1) create(:comment_vote, user: @user, comment: @comment, score: -1)
assert_difference("CommentVote.count", 0) do assert_difference("CommentVote.count", 0) do
post_auth comment_comment_votes_path(comment_id: @comment.id, :score => "down", format: "js"), @user post_auth comment_comment_votes_path(comment_id: @comment.id, :score => "down", format: "js"), @user
assert_response 422 assert_response 422

View File

@@ -115,12 +115,6 @@ class CommentTest < ActiveSupport::TestCase
end end
end end
should "be created" do
comment = FactoryBot.build(:comment)
comment.save
assert(comment.errors.empty?, comment.errors.full_messages.join(", "))
end
should "not validate if the post does not exist" do should "not validate if the post does not exist" do
comment = FactoryBot.build(:comment, :post_id => -1) comment = FactoryBot.build(:comment, :post_id => -1)

View File

@@ -10,8 +10,8 @@ class ModActionTest < ActiveSupport::TestCase
should "hide ip addresses from non-moderators in ip ban modactions" do should "hide ip addresses from non-moderators in ip ban modactions" do
as(@mod) { create(:ip_ban, ip_addr: "1.1.1.1", reason: "test") } as(@mod) { create(:ip_ban, ip_addr: "1.1.1.1", reason: "test") }
as(@user) { assert_equal(0, ModAction.search({}).count) } assert_equal(0, ModAction.visible(@user).count)
as(@mod) { assert_equal(1, ModAction.search({}).count) } assert_equal(1, ModAction.visible(@mod).count)
end end
end end
end end

View File

@@ -2066,7 +2066,7 @@ class PostTest < ActiveSupport::TestCase
end end
should "return posts for the user:<name> metatag" do should "return posts for the user:<name> metatag" do
users = FactoryBot.create_list(:user, 2) users = FactoryBot.create_list(:user, 2, created_at: 2.weeks.ago)
posts = users.map { |u| FactoryBot.create(:post, uploader: u) } posts = users.map { |u| FactoryBot.create(:post, uploader: u) }
assert_tag_match([posts[0]], "user:#{users[0].name}") assert_tag_match([posts[0]], "user:#{users[0].name}")
@@ -2105,7 +2105,9 @@ class PostTest < ActiveSupport::TestCase
should "return posts for the noter:<name> metatag" do should "return posts for the noter:<name> metatag" do
users = FactoryBot.create_list(:user, 2) users = FactoryBot.create_list(:user, 2)
posts = FactoryBot.create_list(:post, 2) posts = FactoryBot.create_list(:post, 2)
notes = users.zip(posts).map { |u, p| FactoryBot.create(:note, creator: u, post: p) } notes = users.zip(posts).map do |u, p|
as(u) { create(:note, post: p) }
end
assert_tag_match([posts[0]], "noter:#{users[0].name}") assert_tag_match([posts[0]], "noter:#{users[0].name}")
assert_tag_match([posts[1]], "noter:#{users[1].name}") assert_tag_match([posts[1]], "noter:#{users[1].name}")
@@ -2201,7 +2203,7 @@ class PostTest < ActiveSupport::TestCase
disapproved = FactoryBot.create(:post, is_pending: true) disapproved = FactoryBot.create(:post, is_pending: true)
create(:post_flag, post: flagged, creator: create(:user, created_at: 2.weeks.ago)) create(:post_flag, post: flagged, creator: create(:user, created_at: 2.weeks.ago))
FactoryBot.create(:post_disapproval, post: disapproved, reason: "disinterest") create(:post_disapproval, user: CurrentUser.user, post: disapproved, reason: "disinterest")
assert_tag_match([pending, flagged], "status:unmoderated") assert_tag_match([pending, flagged], "status:unmoderated")
end end
@@ -2380,7 +2382,7 @@ class PostTest < ActiveSupport::TestCase
CurrentUser.scoped(FactoryBot.create(:mod_user)) do CurrentUser.scoped(FactoryBot.create(:mod_user)) do
pending = FactoryBot.create(:post, is_pending: true) pending = FactoryBot.create(:post, is_pending: true)
disapproved = FactoryBot.create(:post, is_pending: true) disapproved = FactoryBot.create(:post, is_pending: true)
disapproval = FactoryBot.create(:post_disapproval, post: disapproved, reason: "disinterest") disapproval = FactoryBot.create(:post_disapproval, user: CurrentUser.user, post: disapproved, reason: "disinterest")
assert_tag_match([pending], "disapproval:none") assert_tag_match([pending], "disapproval:none")
assert_tag_match([disapproved], "disapproval:any") assert_tag_match([disapproved], "disapproval:any")
@@ -2412,7 +2414,7 @@ class PostTest < ActiveSupport::TestCase
u = create(:user, created_at: 2.weeks.ago) u = create(:user, created_at: 2.weeks.ago)
create(:artist_commentary, post: p) create(:artist_commentary, post: p)
create(:comment, post: p, creator: u, do_not_bump_post: false) create(:comment, post: p, creator: u, do_not_bump_post: false)
create(:note, post: p, creator: u) create(:note, post: p)
p p
end end