2012-06-17 03:15:46 +02:00
|
|
|
class Vote < ActiveRecord::Base
|
2012-07-01 00:43:45 +02:00
|
|
|
belongs_to :user
|
2012-06-17 03:15:46 +02:00
|
|
|
belongs_to :story
|
2015-02-17 18:46:04 +01:00
|
|
|
belongs_to :comment
|
2012-06-17 03:15:46 +02:00
|
|
|
|
|
|
|
COMMENT_REASONS = {
|
2016-11-24 17:22:56 +01:00
|
|
|
"O" => I18n.t('models.vote.offtopicvote'),
|
|
|
|
"I" => I18n.t('models.vote.incorrectvote'),
|
|
|
|
"M" => I18n.t('models.vote.metoovote'),
|
|
|
|
"T" => I18n.t('models.vote.trollvote'),
|
|
|
|
"S" => I18n.t('models.vote.spamvote'),
|
|
|
|
"" => I18n.t('models.vote.cancel')
|
2012-07-01 00:43:45 +02:00
|
|
|
}
|
2012-06-17 03:15:46 +02:00
|
|
|
|
2014-03-18 06:06:43 +01:00
|
|
|
STORY_REASONS = {
|
2016-11-24 17:22:56 +01:00
|
|
|
"O" => I18n.t('models.vote.offtopicvote'),
|
|
|
|
"A" => I18n.t('models.vote.alreadypostedvote'),
|
|
|
|
"S" => I18n.t('models.vote.spamvote'),
|
|
|
|
"" => I18n.t('models.vote.cancel')
|
2014-03-18 06:06:43 +01:00
|
|
|
}
|
|
|
|
OLD_STORY_REASONS = {
|
|
|
|
"Q" => "Low Quality",
|
|
|
|
}
|
|
|
|
|
2012-07-01 00:43:45 +02:00
|
|
|
def self.votes_by_user_for_stories_hash(user, stories)
|
2012-07-12 00:20:43 +02:00
|
|
|
votes = {}
|
|
|
|
|
2012-06-30 18:18:36 +02:00
|
|
|
Vote.where(:user_id => user, :story_id => stories,
|
|
|
|
:comment_id => nil).each do |v|
|
2015-02-11 18:37:03 +01:00
|
|
|
votes[v.story_id] = { :vote => v.vote, :reason => v.reason }
|
2012-06-17 03:15:46 +02:00
|
|
|
end
|
|
|
|
|
2012-07-01 00:43:45 +02:00
|
|
|
votes
|
|
|
|
end
|
2012-06-17 03:15:46 +02:00
|
|
|
|
2012-07-01 00:43:45 +02:00
|
|
|
def self.comment_votes_by_user_for_story_hash(user_id, story_id)
|
|
|
|
votes = {}
|
2012-06-17 03:15:46 +02:00
|
|
|
|
2013-12-24 04:20:06 +01:00
|
|
|
Vote.where(
|
|
|
|
:user_id => user_id, :story_id => story_id
|
|
|
|
).where(
|
|
|
|
"comment_id IS NOT NULL"
|
|
|
|
).each do |v|
|
2012-07-01 00:43:45 +02:00
|
|
|
votes[v.comment_id] = { :vote => v.vote, :reason => v.reason }
|
2012-06-17 03:15:46 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
votes
|
|
|
|
end
|
2013-02-14 01:50:51 +01:00
|
|
|
|
2012-07-12 00:20:43 +02:00
|
|
|
def self.story_votes_by_user_for_story_ids_hash(user_id, story_ids)
|
2013-12-30 19:48:57 +01:00
|
|
|
if story_ids.empty?
|
|
|
|
{}
|
|
|
|
else
|
|
|
|
votes = self.where(
|
|
|
|
:user_id => user_id,
|
|
|
|
:comment_id => nil,
|
|
|
|
:story_id => story_ids,
|
|
|
|
)
|
|
|
|
votes.inject({}) do |memo, v|
|
|
|
|
memo[v.story_id] = { :vote => v.vote, :reason => v.reason }
|
|
|
|
memo
|
|
|
|
end
|
2012-07-12 00:20:43 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.comment_votes_by_user_for_comment_ids_hash(user_id, comment_ids)
|
2013-12-30 19:48:57 +01:00
|
|
|
if comment_ids.empty?
|
|
|
|
{}
|
|
|
|
else
|
|
|
|
votes = self.where(
|
|
|
|
:user_id => user_id,
|
|
|
|
:comment_id => comment_ids,
|
|
|
|
)
|
|
|
|
votes.inject({}) do |memo, v|
|
|
|
|
memo[v.comment_id] = { :vote => v.vote, :reason => v.reason }
|
|
|
|
memo
|
|
|
|
end
|
2012-07-12 00:20:43 +02:00
|
|
|
end
|
|
|
|
end
|
2012-06-17 03:15:46 +02:00
|
|
|
|
2012-07-01 00:43:45 +02:00
|
|
|
def self.vote_thusly_on_story_or_comment_for_user_because(vote, story_id,
|
2012-06-30 18:18:36 +02:00
|
|
|
comment_id, user_id, reason, update_counters = true)
|
2013-12-24 04:20:06 +01:00
|
|
|
v = Vote.where(:user_id => user_id, :story_id => story_id,
|
|
|
|
:comment_id => comment_id).first_or_initialize
|
2012-06-17 03:15:46 +02:00
|
|
|
|
2015-02-11 18:37:03 +01:00
|
|
|
if !v.new_record? && v.vote == vote
|
2012-06-17 03:15:46 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
upvote = 0
|
|
|
|
downvote = 0
|
|
|
|
|
|
|
|
Vote.transaction do
|
|
|
|
# unvote
|
2015-02-11 18:37:03 +01:00
|
|
|
if vote == 0
|
2014-03-04 00:14:33 +01:00
|
|
|
# neutralize previous vote
|
|
|
|
upvote = (v.vote == 1 ? -1 : 0)
|
|
|
|
downvote = (v.vote == -1 ? -1 : 0)
|
|
|
|
v.destroy!
|
2012-06-17 03:15:46 +02:00
|
|
|
|
|
|
|
# new vote or change vote
|
|
|
|
else
|
2014-03-04 00:14:33 +01:00
|
|
|
if !v.new_record?
|
|
|
|
upvote = (v.vote == 1 ? -1 : 0)
|
|
|
|
downvote = (v.vote == -1 ? -1 : 0)
|
2012-06-17 03:15:46 +02:00
|
|
|
end
|
|
|
|
|
2014-03-04 00:14:33 +01:00
|
|
|
upvote += (vote == 1 ? 1 : 0)
|
|
|
|
downvote += (vote == -1 ? 1 : 0)
|
|
|
|
|
2012-06-17 03:15:46 +02:00
|
|
|
v.vote = vote
|
|
|
|
v.reason = reason
|
|
|
|
v.save!
|
|
|
|
end
|
|
|
|
|
2012-06-30 18:18:36 +02:00
|
|
|
if update_counters && (downvote != 0 || upvote != 0)
|
|
|
|
if v.comment_id
|
2012-07-03 21:29:00 +02:00
|
|
|
c = Comment.find(v.comment_id)
|
|
|
|
if c.user_id != user_id
|
2014-01-21 03:22:45 +01:00
|
|
|
User.update_counters c.user_id, :karma => upvote - downvote
|
2012-06-30 21:14:35 +02:00
|
|
|
end
|
|
|
|
|
2012-07-03 21:29:00 +02:00
|
|
|
c.give_upvote_or_downvote_and_recalculate_confidence!(upvote,
|
|
|
|
downvote)
|
|
|
|
else
|
|
|
|
s = Story.find(v.story_id)
|
|
|
|
if s.user_id != user_id
|
2014-01-01 22:02:19 +01:00
|
|
|
User.update_counters s.user_id, :karma => upvote - downvote
|
2012-06-30 21:14:35 +02:00
|
|
|
end
|
2012-07-03 21:29:00 +02:00
|
|
|
|
|
|
|
s.give_upvote_or_downvote_and_recalculate_hotness!(upvote, downvote)
|
2012-06-17 03:15:46 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|