fixes #2822: Post vote API: returns 200 for both success and failure; fixes bug with x-api-limit header

This commit is contained in:
Albert Yi
2017-01-09 17:28:34 -08:00
parent f2a5d45db0
commit 5445b341bc
8 changed files with 31 additions and 17 deletions

View File

@@ -47,7 +47,7 @@ class ApplicationController < ActionController::Base
end
throttled = CurrentUser.user.token_bucket.throttled?
headers["X-Api-Limit"] = CurrentUser.user.token_bucket.token_count
headers["X-Api-Limit"] = CurrentUser.user.token_bucket.token_count.to_s
if throttled
respond_to do |format|

View File

@@ -1,11 +1,14 @@
class CommentVotesController < ApplicationController
respond_to :js, :json, :xml
before_filter :member_only
skip_before_filter :api_check
def create
@comment = Comment.find(params[:comment_id])
@comment_vote = @comment.vote!(params[:score])
respond_with(@comment_vote)
rescue CommentVote::Error, ActiveRecord::RecordInvalid => x
@error = x
render status: 500
end
def destroy
@@ -13,5 +16,6 @@ class CommentVotesController < ApplicationController
@comment.unvote!(params[:score])
rescue CommentVote::Error => x
@error = x
render status: 500
end
end

View File

@@ -5,8 +5,9 @@ class PostVotesController < ApplicationController
def create
@post = Post.find(params[:post_id])
@post.vote!(params[:score])
rescue PostVote::Error => x
rescue PostVote::Error, ActiveRecord::RecordInvalid => x
@error = x
render status: 500
end
def destroy
@@ -14,5 +15,6 @@ class PostVotesController < ApplicationController
@post.unvote!
rescue PostVote::Error => x
@error = x
render status: 500
end
end

View File

@@ -109,14 +109,12 @@ class Comment < ActiveRecord::Base
module VoteMethods
def vote!(val)
numerical_score = val == "up" ? 1 : -1
vote = votes.create(:score => numerical_score)
vote = votes.create!(:score => numerical_score)
if vote.errors.empty?
if vote.is_positive?
update_column(:score, score + 1)
elsif vote.is_negative?
update_column(:score, score - 1)
end
if vote.is_positive?
update_column(:score, score + 1)
elsif vote.is_negative?
update_column(:score, score - 1)
end
return vote

View File

@@ -1075,7 +1075,7 @@ class Post < ActiveRecord::Base
raise PostVote::Error.new("You have already voted for this post")
end
PostVote.create(:post_id => id, :score => score)
PostVote.create!(:post_id => id, :score => score)
reload # PostVote.create modifies our score. Reload to get the new score.
end

View File

@@ -1,10 +1,13 @@
<% if @comment_vote.errors.any? %>
<% if @error %>
Danbooru.error("<%= j @error.to_s %>");
<% elsif @comment_vote.errors.any? %>
Danbooru.error("<%= j @comment_vote.errors.full_messages.join('; ') %>");
<% else %>
<% if @comment_vote.is_negative? %>
$(".comment[data-comment-id=<%= @comment.id %>]").remove();
<% end %>
$("#comment-vote-up-link-for-<%= @comment.id %>").hide();
$("#comment-vote-down-link-for-<%= @comment.id %>").hide();
$("#comment-unvote-link-for-<%= @comment.id %>").show();
<% end %>
$("#comment-vote-up-link-for-<%= @comment.id %>").hide();
$("#comment-vote-down-link-for-<%= @comment.id %>").hide();
$("#comment-unvote-link-for-<%= @comment.id %>").show();

View File

@@ -0,0 +1,7 @@
<% if @error %>
Danbooru.error("<%= j @error.to_s %>");
<% else %>
<% if @comment_vote.is_negative? %>
$(".comment[data-comment-id=<%= @comment.id %>]").remove();
<% end %>
<% end %>

View File

@@ -3,6 +3,6 @@
<% else %>
Danbooru.notice("Vote saved");
$("#score-for-post-<%= @post.id %>").html(<%= @post.score %>);
$("#vote-links-for-post-<%= @post.id %>").hide();
$("#unvote-link-for-post-<%= @post.id %>").show();
<% end %>
$("#vote-links-for-post-<%= @post.id %>").hide();
$("#unvote-link-for-post-<%= @post.id %>").show();