Implement forum topic voting and tag change pruning (#3580)

This commit is contained in:
Albert Yi
2018-04-16 16:09:39 -07:00
parent 45fad069d7
commit f2b525a6d2
182 changed files with 558 additions and 554 deletions

View File

@@ -18,6 +18,9 @@ class BulkUpdateRequest < ApplicationRecord
after_create :create_forum_topic
scope :pending_first, lambda { order("(case status when 'pending' then 0 when 'approved' then 1 else 2 end)") }
scope :pending, ->{where(status: "pending")}
scope :expired, ->{where("created_at < ?", TagRelationship::EXPIRY.days.ago)}
scope :old, ->{where("created_at between ? and ?", TagRelationship::EXPIRY.days.ago, TagRelationship::EXPIRY_WARNING.days.ago)}
module SearchMethods
def default_order
@@ -121,7 +124,7 @@ class BulkUpdateRequest < ApplicationRecord
end
end
def reject!(rejector)
def reject!(rejector = User.system)
transaction do
update(status: "rejected")
forum_updater.update("The #{bulk_update_request_link} (forum ##{forum_post.id}) has been rejected by @#{rejector.name}.", "REJECTED")

View File

@@ -5,6 +5,7 @@ class ForumPost < ApplicationRecord
belongs_to_creator
belongs_to_updater
belongs_to :topic, :class_name => "ForumTopic"
has_many :votes, class_name: "ForumPostVote"
before_validation :initialize_is_deleted, :on => :create
after_create :update_topic_updated_at_on_create
after_update :update_topic_updated_at_on_update_for_original_posts
@@ -128,6 +129,10 @@ class ForumPost < ApplicationRecord
end
end
def voted?(user, score)
votes.where(creator_id: user.id, score: score).exists?
end
def validate_topic_is_unlocked
return if CurrentUser.is_moderator?
return if topic.nil?
@@ -228,8 +233,12 @@ class ForumPost < ApplicationRecord
((ForumPost.where("topic_id = ? and created_at <= ?", topic_id, created_at).count) / Danbooru.config.posts_per_page.to_f).ceil
end
def is_original_post?
ForumPost.exists?(["id = ? and id = (select _.id from forum_posts _ where _.topic_id = ? order by _.id asc limit 1)", id, topic_id])
def is_original_post?(original_post_id = nil)
if original_post_id
return id == original_post_id
else
ForumPost.exists?(["id = ? and id = (select _.id from forum_posts _ where _.topic_id = ? order by _.id asc limit 1)", id, topic_id])
end
end
def delete_topic_if_original_post

View File

@@ -0,0 +1,44 @@
class ForumPostVote < ApplicationRecord
belongs_to_creator
belongs_to :forum_post
validates :creator_id, uniqueness: {scope: :forum_post_id}
validates :score, inclusion: {in: [-1, 0, 1]}
scope :up, -> {where(score: 1)}
scope :down, -> {where(score: -1)}
scope :by, ->(user_id) {where(creator_id: user_id)}
scope :excluding, ->(user_id) {where("creator_id <> ?", user_id)}
def up?
score == 1
end
def down?
score == -1
end
def meh?
score == 0
end
def fa_class
if score == 1
return "fa-thumbs-up"
elsif score == -1
return "fa-thumbs-down"
else
return "fa-meh"
end
end
def vote_type
if score == 1
return "up"
elsif score == -1
return "down"
elsif score == 0
return "meh"
else
raise
end
end
end

View File

@@ -10,6 +10,9 @@ class TagImplication < TagRelationship
validate :consequent_is_not_aliased
validate :antecedent_and_consequent_are_different
validate :wiki_pages_present, :on => :create
scope :expired, ->{where("created_at < ?", 2.months.ago)}
scope :old, ->{where("created_at between ? and ?", 2.months.ago, 1.month.ago)}
scope :pending, ->{where(status: "pending")}
module DescendantMethods
extend ActiveSupport::Concern

View File

@@ -1,6 +1,9 @@
class TagRelationship < ApplicationRecord
self.abstract_class = true
EXPIRY = 60
EXPIRY_WARNING = 55
attr_accessor :skip_secondary_validations
belongs_to_creator
@@ -10,6 +13,10 @@ class TagRelationship < ApplicationRecord
has_one :antecedent_tag, :class_name => "Tag", :foreign_key => "name", :primary_key => "antecedent_name"
has_one :consequent_tag, :class_name => "Tag", :foreign_key => "name", :primary_key => "consequent_name"
scope :expired, ->{where("created_at < ?", EXPIRY.days.ago)}
scope :old, ->{where("created_at >= ? and created_at < ?", EXPIRY.days.ago, EXPIRY_WARNING.days.ago)}
scope :pending, ->{where(status: "pending")}
before_validation :initialize_creator, :on => :create
before_validation :normalize_names
validates_format_of :status, :with => /\A(active|deleted|pending|processing|queued|error: .*)\Z/