set maximum time during which comments and stories can be downvoted

always allow a user to "unvote" if they're previously downvoted, but
after a certain number of days, don't accept new downvotes

there isn't really any benefit in downvoting old stuff that is
already off the front pages or on a dead comment thread, other than
to maliciously strip karma for particular users
This commit is contained in:
joshua stein 2014-02-17 10:13:08 -06:00
parent 20c870b78b
commit 67fc2cc75c
7 changed files with 60 additions and 10 deletions

View file

@ -150,7 +150,7 @@ class CommentsController < ApplicationController
return render :text => "invalid reason", :status => 400
end
if !@user.can_downvote?
if !@user.can_downvote?(comment)
return render :text => "not permitted to downvote", :status => 400
end
@ -260,6 +260,12 @@ private
end
def find_comment
Comment.where(:short_id => params[:id]).first
comment = Comment.where(:short_id => params[:id]).first
if @user && comment
comment.current_vote = Vote.where(:user_id => @user.id,
:story_id => comment.story_id, :comment_id => comment.id).first
end
comment
end
end

View file

@ -213,7 +213,7 @@ class StoriesController < ApplicationController
return render :text => "invalid reason", :status => 400
end
if !@user.can_downvote?
if !@user.can_downvote?(story)
return render :text => "not permitted to downvote", :status => 400
end
@ -239,7 +239,13 @@ private
end
def find_story
Story.where(:short_id => params[:story_id]).first
story = Story.where(:short_id => params[:story_id]).first
if @user && story
story.vote = Vote.where(:user_id => @user.id,
:story_id => story.id, :comment_id => nil).first.try(:vote)
end
story
end
def find_user_story

View file

@ -20,6 +20,9 @@ class Comment < ActiveRecord::Base
:deliver_mention_notifications, :log_to_countinual
after_destroy :unassign_votes
DOWNVOTABLE_DAYS = 7
# after this many minutes old, a comment cannot be edited
MAX_EDIT_MINS = 90
validate do
@ -278,6 +281,14 @@ class Comment < ActiveRecord::Base
end
end
def is_downvotable?
if self.created_at
Time.now - self.created_at <= DOWNVOTABLE_DAYS.days
else
false
end
end
def is_editable_by_user?(user)
if user && user.id == self.user_id
if self.is_moderated?

View file

@ -10,10 +10,12 @@ class Story < ActiveRecord::Base
validates_length_of :description, :maximum => (64 * 1024)
validates_presence_of :user_id
DOWNVOTABLE_DAYS = 14
# after this many minutes old, a story cannot be edited
MAX_EDIT_MINS = 30
# days a story is considered recent
# days a story is considered recent, for resubmitting
RECENT_DAYS = 30
attr_accessor :vote, :already_posted_story, :fetched_content, :previewing,
@ -220,6 +222,14 @@ class Story < ActiveRecord::Base
"hotness = '#{self.calculated_hotness}' WHERE id = #{self.id.to_i}")
end
def is_downvotable?
if self.created_at
Time.now - self.created_at <= DOWNVOTABLE_DAYS.days
else
false
end
end
def is_editable_by_user?(user)
if user && user.is_moderator?
return true

View file

@ -98,9 +98,26 @@ class User < ActiveRecord::Base
true
end
def can_downvote?
# TODO: maybe change this to require a certain level of karma
!is_new?
def can_downvote?(obj)
if is_new?
return false
elsif obj.is_a?(Story)
if obj.is_downvotable?
return true
elsif obj.vote == -1
# user can unvote
return true
end
elsif obj.is_a?(Comment)
if obj.is_downvotable?
return true
elsif obj.current_vote.try(:vote).to_i == -1
# user can unvote
return true
end
end
false
end
def check_session_token

View file

@ -14,7 +14,7 @@ class="comment <%= comment.current_vote ? (comment.current_vote[:vote] == 1 ?
<%= link_to "", login_url, :class => "upvoter" %>
<% end %>
<div class="score"><%= comment.score %></div>
<% if @user && @user.can_downvote? %>
<% if @user && @user.can_downvote?(comment) %>
<a class="downvoter"></a>
<% else %>
<span class="downvoter downvoter_stub"></span>

View file

@ -9,7 +9,7 @@ class="story <%= story.vote == 1 ? "upvoted" : (story.vote == -1 ?
<%= link_to "", login_url, :class => "upvoter" %>
<% end %>
<div class="score"><%= story.upvotes - story.downvotes %></div>
<% if @user && @user.can_downvote? %>
<% if @user && @user.can_downvote?(story) %>
<a class="downvoter"></a>
<% else %>
<span class="downvoter downvoter_stub"></span>