<commit_before># frozen_string_literal: true

class MessagesController < ApplicationController
  filter_access_to :approve, :reject, attribute_check: true

  def create
    @message ||= thread.messages.build permitted_params.merge(created_by: current_user)
    return redirect_to(:back) if thread.private_message? && !(permitted_to? :send_private_message, thread.other_user(current_user))

    message.check_reason = check_reason unless thread.private_message?

    if message.save
      if message.check_reason
        flash[:alert] = t(message.check_reason)
      else
        message.skip_mod_queue!
        set_flash_message :success
      end
    else
      flash[:alert] = message.errors.to_a.to_sentence
    end

    ThreadRecorder.thread_viewed thread, current_user

    respond_to do |format|
      format.html { redirect_to thread_path(thread) }
      format.js { render "messages/created" }
    end
  end

  def censor
    if message.censor!
      set_flash_message :success
    else
      set_flash_message :failure
    end
    redirect_to thread_path thread
  end

  def approve
    message.approve!
  end

  def reject
    message.reject!
  end

  def vote_up
    current_user.vote_exclusively_for message unless current_user.voted_for? message
    render partial: "shared/vote_detail", locals: { resource: message }
  end

  def vote_clear
    current_user.clear_votes message
    render partial: "shared/vote_detail", locals: { resource: message }
  end

  protected

  def check_reason
    # spam? check needs to be done in the controller
    if message.spam?
      "possible_spam"
    elsif !current_user.approved?
      "not_approved"
    end
  end

  def permitted_params
    params.require(:message).permit :body, :component, action_message_ids: []
  end

  def message
    @message ||= Message.includes(:thread).find(params[:id])
  end

  def thread
    @thread ||= MessageThread.find(params[:thread_id])
  end
end
<commit_msg>Filter access to censor for non committee members<commit_after># frozen_string_literal: true

class MessagesController < ApplicationController
  filter_access_to :approve, :reject, :censor, attribute_check: true

  def create
    @message ||= thread.messages.build permitted_params.merge(created_by: current_user)
    return redirect_to(:back) if thread.private_message? && !(permitted_to? :send_private_message, thread.other_user(current_user))

    message.check_reason = check_reason unless thread.private_message?

    if message.save
      if message.check_reason
        flash[:alert] = t(message.check_reason)
      else
        message.skip_mod_queue!
        set_flash_message :success
      end
    else
      flash[:alert] = message.errors.to_a.to_sentence
    end

    ThreadRecorder.thread_viewed thread, current_user

    respond_to do |format|
      format.html { redirect_to thread_path(thread) }
      format.js { render "messages/created" }
    end
  end

  def censor
    if message.censor!
      set_flash_message :success
    else
      set_flash_message :failure
    end
    redirect_to thread_path thread
  end

  def approve
    message.approve!
  end

  def reject
    message.reject!
  end

  def vote_up
    current_user.vote_exclusively_for message unless current_user.voted_for? message
    render partial: "shared/vote_detail", locals: { resource: message }
  end

  def vote_clear
    current_user.clear_votes message
    render partial: "shared/vote_detail", locals: { resource: message }
  end

  protected

  def check_reason
    # spam? check needs to be done in the controller
    if message.spam?
      "possible_spam"
    elsif !current_user.approved?
      "not_approved"
    end
  end

  def permitted_params
    params.require(:message).permit :body, :component, action_message_ids: []
  end

  def message
    @message ||= Message.includes(:thread).find(params[:id])
  end

  def thread
    @thread ||= MessageThread.find(params[:thread_id])
  end
end

{'fraction_non_alphanumeric': 0.07760588384478823, 'fraction_numerical': 0.0, 'mean_word_length': 3.4264870931537597, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '7644262', 'n_tokens_mistral': 1219, 'n_tokens_neox': 1176, 'n_words': 292}