implement super voters

This commit is contained in:
r888888888
2016-02-22 14:02:52 -08:00
parent caf4a28b02
commit 2a87aad34e
13 changed files with 189 additions and 12 deletions

View File

@@ -6,8 +6,8 @@ class DailyMaintenance
Upload.delete_all(['created_at < ?', 1.day.ago])
ModAction.delete_all(['created_at < ?', 3.days.ago])
Delayed::Job.delete_all(['created_at < ?', 7.days.ago])
PostVote.delete_all(['created_at < ?', 1.month.ago])
CommentVote.delete_all(['created_at < ?', 1.month.ago])
PostVote.prune!
CommentVote.prune!
TagSubscription.process_all
ApiCacheGenerator.new.generate_tag_cache
PostDisapproval.prune!
@@ -15,5 +15,7 @@ class DailyMaintenance
TagAlias.update_cached_post_counts_for_all
PostDisapproval.dmail_messages!
Tag.clean_up_negative_post_counts!
SuperVoter.prune!
SuperVoter.init!
end
end

View File

@@ -1,5 +1,7 @@
module Reports
class UserSimilarity
NOT_READY_STRING = "not ready"
attr_reader :user_id
def initialize(user_id)
@@ -10,6 +12,16 @@ module Reports
User.find(user_id)
end
def prime
10.times do
if fetch_similar_user_ids == NOT_READY_STRING
sleep(60)
else
break
end
end
end
def fetch_similar_user_ids
return NotImplementedError unless Danbooru.config.report_server

View File

@@ -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)

View File

@@ -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

View File

@@ -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
View 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

View File

@@ -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"

View File

@@ -17,7 +17,7 @@ class UserSimilarityPresenter
def fetch
data = report.fetch_similar_user_ids
if data == "not ready"
if data == Reports::UserSimilarity::NOT_READY_STRING
@not_ready = true
else
@user_ids_with_scores = data.scan(/\S+/).in_groups_of(2)