<commit_before>class VotesController < ApplicationController
  before_action :auth_for_voting

  def create
    post = (params[:post_type] == "a" ? Answer.find(params[:post_id]) : Question.find(params[:post_id]))
    existing = Vote.where(:post => post, :user => current_user)

    if post.user == current_user
      render :plain => "You may not vote on your own posts.", :status => 403 and return
    end

    if existing.count > 0
      if existing.first.vote_type == params[:vote_type].to_i
        render :plain => "You have already voted.", :status => 409 and return
      else
        # There's already a vote by this user on this post, so we may as well update that instead of removing it.
        render :plain => "Vote preconditions failed; contact administration.", :status => 412 unless existing.count == 1
        vote = existing.first
        vote.vote_type = params[:vote_type].to_i
        vote.save!

        state = { :status => "modified", :vote_id => vote.id }
      end
    else
      vote = Vote.new
      vote.user = current_user
      vote.post = post
      vote.vote_type = params[:vote_type]
      vote.save!

      state = { :status => "OK", :vote_id => vote.id }
    end

    if vote.vote_type == 0
      post.score += 1
      post.save!

      if params[:post_type] == "a"
        post.user.reputation += get_setting('AnswerUpVoteRep').to_i or 0
      else
        post.user.reputation += get_setting('QuestionUpVoteRep').to_i or 0
      end
      post.user.save!
    else
      post.score -= 1
      post.save!

      if params[:post_type] == "a"
        post.user.reputation += get_setting('AnswerDownVoteRep').to_i or 0
      else
        post.user.reputation += get_setting('QuestionDownVoteRep').to_i or 0
      end
      post.user.save!
    end

    render :json => state
  end

  def destroy
    vote = Vote.find params[:id]

    if vote.user != current_user
      render :plain => "You are not authorized to remove this vote.", :status => 403 and return
    end

    vote.destroy
    render :plain => "OK"
  end

  private
    def auth_for_voting
      if !user_signed_in?
        render :plain => "You must be logged in to vote.", :status => 403 and return
      end
    end
end
<commit_msg>update score & rep on vote destroy<commit_after>class VotesController < ApplicationController
  before_action :auth_for_voting

  def create
    post = (params[:post_type] == "a" ? Answer.find(params[:post_id]) : Question.find(params[:post_id]))
    existing = Vote.where(:post => post, :user => current_user)

    if post.user == current_user
      render :plain => "You may not vote on your own posts.", :status => 403 and return
    end

    if existing.count > 0
      if existing.first.vote_type == params[:vote_type].to_i
        render :plain => "You have already voted.", :status => 409 and return
      else
        # There's already a vote by this user on this post, so we may as well update that instead of removing it.
        render :plain => "Vote preconditions failed; contact administration.", :status => 412 unless existing.count == 1
        vote = existing.first
        vote.vote_type = params[:vote_type].to_i
        vote.save!

        state = { :status => "modified", :vote_id => vote.id }
      end
    else
      vote = Vote.new
      vote.user = current_user
      vote.post = post
      vote.vote_type = params[:vote_type]
      vote.save!

      state = { :status => "OK", :vote_id => vote.id }
    end

    if vote.vote_type == 0
      post.score += 1
      post.save!

      if params[:post_type] == "a"
        post.user.reputation += get_setting('AnswerUpVoteRep').to_i or 0
      else
        post.user.reputation += get_setting('QuestionUpVoteRep').to_i or 0
      end
      post.user.save!
    else
      post.score -= 1
      post.save!

      if params[:post_type] == "a"
        post.user.reputation += get_setting('AnswerDownVoteRep').to_i or 0
      else
        post.user.reputation += get_setting('QuestionDownVoteRep').to_i or 0
      end
      post.user.save!
    end

    render :json => state
  end

  def destroy
    vote = Vote.find params[:id]

    if vote.user != current_user
      render :plain => "You are not authorized to remove this vote.", :status => 403 and return
    end

    if vote.vote_type == 0
      vote.post.score -= 1
      if vote.post_type == 'Answer'
        vote.post.user.reputation -= get_setting('AnswerUpVoteRep').to_i or 0
      else
        vote.post.user.reputation -= get_setting('QuestionUpVoteRep').to_i or 0
      end
    else
      vote.post.score += 1
      if vote.post_type == 'Answer'
        vote.post.user.reputation -= get_setting('AnswerDownVoteRep').to_i or 0
      else
        vote.post.user.reputation -= get_setting('QuestionDownVoteRep').to_i or 0
      end
    end
    vote.post.save!
    vote.post.user.save!

    vote.destroy
    render :plain => "OK"
  end

  private
    def auth_for_voting
      if !user_signed_in?
        render :plain => "You must be logged in to vote.", :status => 403 and return
      end
    end
end

{'fraction_non_alphanumeric': 0.11172631995254104, 'fraction_numerical': 0.010876013446707534, 'mean_word_length': 2.401479488903833, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3812538', 'n_tokens_mistral': 1641, 'n_tokens_neox': 1532, 'n_words': 450}