From 5445b341bcd77dee941623ed7e36e751fa2b64a1 Mon Sep 17 00:00:00 2001 From: Albert Yi Date: Mon, 9 Jan 2017 17:28:34 -0800 Subject: [PATCH] fixes #2822: Post vote API: returns 200 for both success and failure; fixes bug with x-api-limit header --- app/controllers/application_controller.rb | 2 +- app/controllers/comment_votes_controller.rb | 6 +++++- app/controllers/post_votes_controller.rb | 4 +++- app/models/comment.rb | 12 +++++------- app/models/post.rb | 2 +- app/views/comment_votes/create.js.erb | 11 +++++++---- app/views/comment_votes/destroy.js.erb | 7 +++++++ app/views/post_votes/create.js.erb | 4 ++-- 8 files changed, 31 insertions(+), 17 deletions(-) create mode 100644 app/views/comment_votes/destroy.js.erb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 28a036818..09a16203d 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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| diff --git a/app/controllers/comment_votes_controller.rb b/app/controllers/comment_votes_controller.rb index 101a5ab02..17efbf86e 100644 --- a/app/controllers/comment_votes_controller.rb +++ b/app/controllers/comment_votes_controller.rb @@ -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 diff --git a/app/controllers/post_votes_controller.rb b/app/controllers/post_votes_controller.rb index e0c00b16a..07c2dd0c4 100644 --- a/app/controllers/post_votes_controller.rb +++ b/app/controllers/post_votes_controller.rb @@ -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 diff --git a/app/models/comment.rb b/app/models/comment.rb index ca28446f3..472326064 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -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 diff --git a/app/models/post.rb b/app/models/post.rb index a679c0fce..062682889 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -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 diff --git a/app/views/comment_votes/create.js.erb b/app/views/comment_votes/create.js.erb index 13f89d0dc..ea87d20ea 100644 --- a/app/views/comment_votes/create.js.erb +++ b/app/views/comment_votes/create.js.erb @@ -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(); \ No newline at end of file diff --git a/app/views/comment_votes/destroy.js.erb b/app/views/comment_votes/destroy.js.erb new file mode 100644 index 000000000..e99eb4a71 --- /dev/null +++ b/app/views/comment_votes/destroy.js.erb @@ -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 %> \ No newline at end of file diff --git a/app/views/post_votes/create.js.erb b/app/views/post_votes/create.js.erb index bfeb6a784..c61179241 100644 --- a/app/views/post_votes/create.js.erb +++ b/app/views/post_votes/create.js.erb @@ -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(); \ No newline at end of file