comments: allow swapping votes.
Allow users to upvote a comment, then downvote it, without raising an error or having to manually remove the upvote first. The upvote is automatically removed and replaced by the downvote. Changes to the /comment_votes API: * `POST /comment_votes` and `DELETE /comment_votes` now return a comment vote instead of a comment. * The `score` param in `POST /comment_votes` is now 1 or -1, not `up` or `down.`
This commit is contained in:
@@ -51,7 +51,7 @@
|
||||
<% elsif upvoted? %>
|
||||
<%= link_to "🡹", comment_comment_votes_path(comment_id: comment.id), class: "comment-upvote-link comment-unvote-link", method: :delete, remote: true %>
|
||||
<% else %>
|
||||
<%= link_to "🡹", comment_comment_votes_path(comment_id: comment.id, score: "up"), class: "comment-upvote-link", method: :post, remote: true %>
|
||||
<%= link_to "🡹", comment_comment_votes_path(comment_id: comment.id, score: "1"), class: "comment-upvote-link", method: :post, remote: true %>
|
||||
<% end %>
|
||||
|
||||
<span class="comment-score"><%= comment.score %></span>
|
||||
@@ -61,7 +61,7 @@
|
||||
<% elsif downvoted? %>
|
||||
<%= link_to "🡻", comment_comment_votes_path(comment_id: comment.id), class: "comment-downvote-link comment-unvote-link", method: :delete, remote: true %>
|
||||
<% else %>
|
||||
<%= link_to "🡻", comment_comment_votes_path(comment_id: comment.id, score: "down"), class: "comment-downvote-link", method: :post, remote: true %>
|
||||
<%= link_to "🡻", comment_comment_votes_path(comment_id: comment.id, score: "-1"), class: "comment-downvote-link", method: :post, remote: true %>
|
||||
<% end %>
|
||||
</li>
|
||||
<% end %>
|
||||
|
||||
@@ -1,23 +1,31 @@
|
||||
class CommentVotesController < ApplicationController
|
||||
skip_before_action :api_check
|
||||
respond_to :js, :json, :xml, :html
|
||||
rescue_with CommentVote::Error, ActiveRecord::RecordInvalid, status: 422
|
||||
|
||||
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]]) if request.format.html?
|
||||
|
||||
respond_with(@comment_votes)
|
||||
end
|
||||
|
||||
def create
|
||||
@comment = authorize Comment.find(params[:comment_id]), policy_class: CommentVotePolicy
|
||||
@comment_vote = @comment.vote!(params[:score])
|
||||
respond_with(@comment)
|
||||
@comment = Comment.find(params[:comment_id])
|
||||
|
||||
@comment.with_lock do
|
||||
@comment_vote = authorize CommentVote.new(comment: @comment, score: params[:score], user: CurrentUser.user)
|
||||
CommentVote.where(comment: @comment, user: CurrentUser.user).destroy_all
|
||||
@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 = authorize Comment.find(params[:comment_id]), policy_class: CommentVotePolicy
|
||||
@comment.unvote!
|
||||
respond_with(@comment)
|
||||
@comment_vote = authorize CommentVote.find_by!(comment_id: params[:comment_id], user: CurrentUser.user)
|
||||
@comment_vote.destroy
|
||||
|
||||
respond_with(@comment_vote)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -44,39 +44,7 @@ class Comment < ApplicationRecord
|
||||
end
|
||||
end
|
||||
|
||||
module VoteMethods
|
||||
def vote!(val, voter = CurrentUser.user)
|
||||
numerical_score = (val == "up") ? 1 : -1
|
||||
vote = votes.create!(user: voter, score: numerical_score)
|
||||
|
||||
if vote.is_positive?
|
||||
update_column(:score, score + 1)
|
||||
elsif vote.is_negative?
|
||||
update_column(:score, score - 1)
|
||||
end
|
||||
|
||||
return vote
|
||||
end
|
||||
|
||||
def unvote!
|
||||
vote = votes.where("user_id = ?", CurrentUser.user.id).first
|
||||
|
||||
if vote
|
||||
if vote.is_positive?
|
||||
update_column(:score, score - 1)
|
||||
else
|
||||
update_column(:score, score + 1)
|
||||
end
|
||||
|
||||
vote.destroy
|
||||
else
|
||||
raise CommentVote::Error.new("You have not voted for this comment")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
extend SearchMethods
|
||||
include VoteMethods
|
||||
|
||||
def validate_creator_is_not_limited
|
||||
if creator.is_comment_limited? && !do_not_bump_post?
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
class CommentVote < ApplicationRecord
|
||||
class Error < StandardError; end
|
||||
|
||||
belongs_to :comment
|
||||
belongs_to :user
|
||||
|
||||
validates_presence_of :score
|
||||
validates_uniqueness_of :user_id, :scope => :comment_id, :message => "have already voted for this comment"
|
||||
validate :validate_comment_can_be_down_voted
|
||||
validates_inclusion_of :score, :in => [-1, 1], :message => "must be 1 or -1"
|
||||
|
||||
after_create :update_score_after_create
|
||||
after_destroy :update_score_after_destroy
|
||||
|
||||
def self.visible(user)
|
||||
if user.is_moderator?
|
||||
all
|
||||
@@ -37,6 +39,18 @@ class CommentVote < ApplicationRecord
|
||||
score == -1
|
||||
end
|
||||
|
||||
def update_score_after_create
|
||||
comment.with_lock do
|
||||
comment.update_columns(score: comment.score + score)
|
||||
end
|
||||
end
|
||||
|
||||
def update_score_after_destroy
|
||||
comment.with_lock do
|
||||
comment.update_columns(score: comment.score - score)
|
||||
end
|
||||
end
|
||||
|
||||
def self.available_includes
|
||||
[:comment, :user]
|
||||
end
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
class CommentVotePolicy < ApplicationPolicy
|
||||
def create?
|
||||
unbanned? && !record.comment.is_deleted?
|
||||
end
|
||||
|
||||
def destroy?
|
||||
# XXX permissions are checked in Comment#unvote!
|
||||
true
|
||||
record.user_id == user.id
|
||||
end
|
||||
|
||||
def can_see_votes?
|
||||
|
||||
@@ -1,2 +1,6 @@
|
||||
var $comment = $("article#comment_<%= @comment.id %>");
|
||||
$comment.replaceWith("<%= j render_comment(@comment, current_user: CurrentUser.user) %>");
|
||||
<% if flash[:notice] %>
|
||||
Danbooru.Utility.notice("<%= j flash[:notice] %>");
|
||||
<% end %>
|
||||
|
||||
var $comment = $("article#comment_<%= @comment_vote.comment_id %>");
|
||||
$comment.replaceWith("<%= j render_comment(@comment_vote.comment, current_user: CurrentUser.user) %>");
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
var $comment = $("article#comment_<%= @comment.id %>");
|
||||
$comment.replaceWith("<%= j render_comment(@comment, current_user: CurrentUser.user) %>");
|
||||
1
app/views/comment_votes/destroy.js.erb
Symbolic link
1
app/views/comment_votes/destroy.js.erb
Symbolic link
@@ -0,0 +1 @@
|
||||
create.js.erb
|
||||
Reference in New Issue
Block a user