Files
danbooru/app/controllers/comment_votes_controller.rb
evazion ab6d9bd0e8 post votes: fix exception when voting on posts using API.
Fix an `undefined method post_vote_url` exception when doing
`POST https://danbooru.donmai.us/posts/1/votes.json`.

Also add the following API endpoints:

* https://danbooru.donmai.us/post_votes/:id.json
* https://danbooru.donmai.us/comment_votes/:id.json
* https://danbooru.donmai.us/forum_post_votes/:id.json

where `:id` is the vote ID, not the post ID.
2021-11-14 20:11:38 -06:00

40 lines
1.2 KiB
Ruby

class CommentVotesController < ApplicationController
respond_to :js, :json, :xml, :html
def index
@comment_votes = authorize CommentVote.visible(CurrentUser.user).paginated_search(params, count_pages: true)
@comment_votes = @comment_votes.includes(:user, comment: [:creator, { post: [:uploader, :media_asset] }]) if request.format.html?
respond_with(@comment_votes)
end
def show
@comment_vote = authorize CommentVote.find(params[:id])
respond_with(@comment_vote)
end
def create
@comment = Comment.find(params[:comment_id])
@comment.with_lock do
@comment_vote = authorize CommentVote.new(comment: @comment, score: params[:score], user: CurrentUser.user)
CommentVote.active.where(comment: @comment, user: CurrentUser.user).each do |vote|
vote.soft_delete!(updater: CurrentUser.user)
end
@comment_vote.save
end
flash.now[:notice] = @comment_vote.errors.full_messages.join("; ") if @comment_vote.errors.present?
respond_with(@comment_vote)
end
def destroy
@comment_vote = authorize CommentVote.find(params[:id])
@comment_vote.soft_delete(updater: CurrentUser.user)
respond_with(@comment_vote)
end
end