fixes #2822: Post vote API: returns 200 for both success and failure; fixes bug with x-api-limit header
This commit is contained in:
@@ -47,7 +47,7 @@ class ApplicationController < ActionController::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
throttled = CurrentUser.user.token_bucket.throttled?
|
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
|
if throttled
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
|
|||||||
@@ -1,11 +1,14 @@
|
|||||||
class CommentVotesController < ApplicationController
|
class CommentVotesController < ApplicationController
|
||||||
respond_to :js, :json, :xml
|
respond_to :js, :json, :xml
|
||||||
before_filter :member_only
|
before_filter :member_only
|
||||||
|
skip_before_filter :api_check
|
||||||
|
|
||||||
def create
|
def create
|
||||||
@comment = Comment.find(params[:comment_id])
|
@comment = Comment.find(params[:comment_id])
|
||||||
@comment_vote = @comment.vote!(params[:score])
|
@comment_vote = @comment.vote!(params[:score])
|
||||||
respond_with(@comment_vote)
|
rescue CommentVote::Error, ActiveRecord::RecordInvalid => x
|
||||||
|
@error = x
|
||||||
|
render status: 500
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
@@ -13,5 +16,6 @@ class CommentVotesController < ApplicationController
|
|||||||
@comment.unvote!(params[:score])
|
@comment.unvote!(params[:score])
|
||||||
rescue CommentVote::Error => x
|
rescue CommentVote::Error => x
|
||||||
@error = x
|
@error = x
|
||||||
|
render status: 500
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -5,8 +5,9 @@ class PostVotesController < ApplicationController
|
|||||||
def create
|
def create
|
||||||
@post = Post.find(params[:post_id])
|
@post = Post.find(params[:post_id])
|
||||||
@post.vote!(params[:score])
|
@post.vote!(params[:score])
|
||||||
rescue PostVote::Error => x
|
rescue PostVote::Error, ActiveRecord::RecordInvalid => x
|
||||||
@error = x
|
@error = x
|
||||||
|
render status: 500
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
@@ -14,5 +15,6 @@ class PostVotesController < ApplicationController
|
|||||||
@post.unvote!
|
@post.unvote!
|
||||||
rescue PostVote::Error => x
|
rescue PostVote::Error => x
|
||||||
@error = x
|
@error = x
|
||||||
|
render status: 500
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -109,14 +109,12 @@ class Comment < ActiveRecord::Base
|
|||||||
module VoteMethods
|
module VoteMethods
|
||||||
def vote!(val)
|
def vote!(val)
|
||||||
numerical_score = val == "up" ? 1 : -1
|
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?
|
||||||
if vote.is_positive?
|
update_column(:score, score + 1)
|
||||||
update_column(:score, score + 1)
|
elsif vote.is_negative?
|
||||||
elsif vote.is_negative?
|
update_column(:score, score - 1)
|
||||||
update_column(:score, score - 1)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return vote
|
return vote
|
||||||
|
|||||||
@@ -1075,7 +1075,7 @@ class Post < ActiveRecord::Base
|
|||||||
raise PostVote::Error.new("You have already voted for this post")
|
raise PostVote::Error.new("You have already voted for this post")
|
||||||
end
|
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.
|
reload # PostVote.create modifies our score. Reload to get the new score.
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -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('; ') %>");
|
Danbooru.error("<%= j @comment_vote.errors.full_messages.join('; ') %>");
|
||||||
<% else %>
|
<% else %>
|
||||||
<% if @comment_vote.is_negative? %>
|
<% if @comment_vote.is_negative? %>
|
||||||
$(".comment[data-comment-id=<%= @comment.id %>]").remove();
|
$(".comment[data-comment-id=<%= @comment.id %>]").remove();
|
||||||
<% end %>
|
<% 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 %>
|
<% end %>
|
||||||
$("#comment-vote-up-link-for-<%= @comment.id %>").hide();
|
|
||||||
$("#comment-vote-down-link-for-<%= @comment.id %>").hide();
|
|
||||||
$("#comment-unvote-link-for-<%= @comment.id %>").show();
|
|
||||||
7
app/views/comment_votes/destroy.js.erb
Normal file
7
app/views/comment_votes/destroy.js.erb
Normal 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 %>
|
||||||
@@ -3,6 +3,6 @@
|
|||||||
<% else %>
|
<% else %>
|
||||||
Danbooru.notice("Vote saved");
|
Danbooru.notice("Vote saved");
|
||||||
$("#score-for-post-<%= @post.id %>").html(<%= @post.score %>);
|
$("#score-for-post-<%= @post.id %>").html(<%= @post.score %>);
|
||||||
|
$("#vote-links-for-post-<%= @post.id %>").hide();
|
||||||
|
$("#unvote-link-for-post-<%= @post.id %>").show();
|
||||||
<% end %>
|
<% end %>
|
||||||
$("#vote-links-for-post-<%= @post.id %>").hide();
|
|
||||||
$("#unvote-link-for-post-<%= @post.id %>").show();
|
|
||||||
Reference in New Issue
Block a user