implement super voters
This commit is contained in:
@@ -12,7 +12,7 @@ class CommentVote < ActiveRecord::Base
|
||||
attr_accessible :comment_id, :user_id, :score
|
||||
|
||||
def self.prune!
|
||||
destroy_all("created_at < ?", 14.days.ago)
|
||||
where("created_at < ?", 14.days.ago).delete_all
|
||||
end
|
||||
|
||||
def self.search(params)
|
||||
|
||||
@@ -952,14 +952,22 @@ class Post < ActiveRecord::Base
|
||||
!PostVote.exists?(:user_id => user.id, :post_id => id)
|
||||
end
|
||||
|
||||
def vote_magnitude
|
||||
if CurrentUser.is_super_voter?
|
||||
SuperVoter::MAGNITUDE
|
||||
else
|
||||
1
|
||||
end
|
||||
end
|
||||
|
||||
def vote!(score)
|
||||
if can_be_voted_by?(CurrentUser.user)
|
||||
if score == "up"
|
||||
Post.where(:id => id).update_all("score = score + 1, up_score = up_score + 1")
|
||||
self.score += 1
|
||||
Post.where(:id => id).update_all("score = score + #{vote_magnitude}, up_score = up_score + #{vote_magnitude}")
|
||||
self.score += vote_magnitude
|
||||
elsif score == "down"
|
||||
Post.where(:id => id).update_all("score = score - 1, down_score = down_score - 1")
|
||||
self.score -= 1
|
||||
Post.where(:id => id).update_all("score = score - #{vote_magnitude}, down_score = down_score - #{vote_magnitude}")
|
||||
self.score -= vote_magnitude
|
||||
end
|
||||
|
||||
votes.create(:score => score)
|
||||
@@ -975,11 +983,11 @@ class Post < ActiveRecord::Base
|
||||
vote = votes.where("user_id = ?", CurrentUser.user.id).first
|
||||
|
||||
if vote.score == 1
|
||||
Post.where(:id => id).update_all("score = score - 1, up_score = up_score - 1")
|
||||
self.score -= 1
|
||||
Post.where(:id => id).update_all("score = score - #{vote_magnitude}, up_score = up_score - #{vote_magnitude}")
|
||||
self.score -= vote_magnitude
|
||||
else
|
||||
Post.where(:id => id).update_all("score = score + 1, down_score = down_score + 1")
|
||||
self.score += 1
|
||||
Post.where(:id => id).update_all("score = score + #{vote_magnitude}, down_score = down_score + #{vote_magnitude}")
|
||||
self.score += vote_magnitude
|
||||
end
|
||||
|
||||
vote.destroy
|
||||
|
||||
@@ -7,6 +7,10 @@ class PostVote < ActiveRecord::Base
|
||||
validates_inclusion_of :score, :in => [1, -1]
|
||||
attr_accessible :post_id, :user_id, :score
|
||||
|
||||
def self.prune!
|
||||
where("created_at < ?", 30.days.ago).delete_all
|
||||
end
|
||||
|
||||
def score=(x)
|
||||
if x == "up"
|
||||
write_attribute(:score, 1)
|
||||
|
||||
33
app/models/super_voter.rb
Normal file
33
app/models/super_voter.rb
Normal file
@@ -0,0 +1,33 @@
|
||||
class SuperVoter < ActiveRecord::Base
|
||||
MAGNITUDE = 5
|
||||
DURATION = 1.week
|
||||
|
||||
belongs_to :user
|
||||
validates_uniqueness_of :user_id
|
||||
after_create :update_user_on_create
|
||||
after_destroy :update_user_on_destroy
|
||||
|
||||
def self.prune!
|
||||
where("created_at < ?", DURATION.ago).destroy_all
|
||||
end
|
||||
|
||||
def self.init!
|
||||
report = Reports::UserSimilarity.new(User.admins.first.id)
|
||||
report.prime
|
||||
report.fetch_similar_user_ids.scan(/\S+/).in_groups_of(2).each do |user_id, score|
|
||||
unless where("user_id = ?", user_id.to_i).exists?
|
||||
create(:user_id => user_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def update_user_on_create
|
||||
user.is_super_voter = true
|
||||
user.save
|
||||
end
|
||||
|
||||
def update_user_on_destroy
|
||||
user.is_super_voter = false
|
||||
user.save
|
||||
end
|
||||
end
|
||||
@@ -33,6 +33,7 @@ class User < ActiveRecord::Base
|
||||
can_approve_posts
|
||||
can_upload_free
|
||||
disable_categorized_saved_searches
|
||||
is_super_voter
|
||||
)
|
||||
|
||||
include Danbooru::HasBitFlags
|
||||
@@ -67,6 +68,7 @@ class User < ActiveRecord::Base
|
||||
has_one :recent_ban, lambda {order("bans.id desc")}, :class_name => "Ban"
|
||||
has_one :api_key
|
||||
has_one :dmail_filter
|
||||
has_one :super_voter
|
||||
has_many :subscriptions, lambda {order("tag_subscriptions.name")}, :class_name => "TagSubscription", :foreign_key => "creator_id"
|
||||
has_many :note_versions, :foreign_key => "updater_id"
|
||||
has_many :dmails, lambda {order("dmails.id desc")}, :foreign_key => "owner_id"
|
||||
|
||||
Reference in New Issue
Block a user