<commit_before>class PmTopicsController < ApplicationController
  before_action :ensure_logged_in
  before_action :ensure_topic_post_associated, only: [:new, :create]
  before_action :ensure_contact,               only: [:new, :create]
  before_action :ensure_distinct_users,        only: [:new, :create]
  before_action :ensure_exists,                only: [:show, :update]
  before_action :ensure_valid_user,            only: [:show, :update]
  before_action :mark_as_read,                 only: :show

  def index
    @pm_topics = current_user.pm_topics
                             .order(last_posted: :desc)
                             .paginate(page: params[:page], per_page: 10)
  end

  def show
    @pm_topic = pm_topic
    @pm_posts = @pm_topic.pm_posts.order(created_at: :asc)
    @pm_post  = @pm_topic.pm_posts.build
    @user_handshake = user_handshake.to_s
    @user_sent_handshake = @pm_topic.send(user_handshake)
  end

  def new
    @pm_topic = PmTopic.new
    @pm_topic.pm_posts.build
    @topic = original_topic 
    @post  = original_post
    redirect_to root_url unless @post.visible
  end

  def create
    @pm_topic = PmTopic.new(pm_topic_params)
    if @pm_topic.save
      @pm_post = @pm_topic.pm_posts.first
      @pm_post.update_attributes(user_id:    current_user.id,
                                 ip_address: request.remote_ip)
      @pm_topic.update_attributes(sender_id:     current_user.id,
                                  recipient_id:  original_post.user_id,
                                  last_posted:   @pm_post.created_at,
                                  sender_unread: false)
      @pm_topic.update_attributes(sender_handshake: true) if handshake_sent
      redirect_to @pm_topic
    else
      render 'new'
    end
  end

  def update
    pm_topic.update_attributes(patch_params) if valid_patch
    redirect_to pm_topic
  end

  private

    def pm_topic_params
      params.require(:pm_topic)
            .permit(:topic_id, :post_id, :title,
                    pm_posts_attributes: [:content, :handshake_sent])
    end

    def patch_params
      params.require(:pm_topic)
            .permit(:id, :handshake_declined, user_handshake)
    end

    def pm_topic
      id = params[:id] || patch_params[:id]
      PmTopic.find_by(id: id)
    end

    def original_topic
      id = params[:topic_id] || pm_topic_params[:topic_id]
      Topic.find_by(id: id)
    end

    def original_post
      id = params[:post_id] || pm_topic_params[:post_id]
      Post.find_by(id: id)
    end

    def ensure_topic_post_associated
      associated_id = original_post.topic_id
      redirect_to root_url unless associated_id == original_topic.id
    end

    def ensure_contact
      redirect_to root_url unless original_post.contact
    end

    def ensure_distinct_users
      recipient_id = original_post.user_id
      if recipient_id == current_user.id
        flash[:warning] = "You can't send yourself a private message!"
        redirect_to :back
      end
    end

    def ensure_exists
      redirect_to root_url if pm_topic.nil?
    end

    def ensure_valid_user
      redirect_to root_url unless pm_topic.valid_users.include?(current_user.id)
    end

    def handshake_sent
      pm_topic_params[:pm_posts_attributes]["0"][:handshake_sent]
    end

    def pm_user
      pm_topic.sender_id == current_user.id ? 'sender' : 'recipient'
    end

    def user_handshake
      "#{pm_user}_handshake".to_sym
    end

    def mark_as_read
      user_unread = "#{pm_user}_unread".to_sym
      pm_topic.update_attributes(user_unread => false)
    end

    def valid_patch
      handshake = patch_params[user_handshake] || pm_topic.send(user_handshake)
      patch_params[:handshake_declined] && handshake_patch
    end
end
<commit_msg>Fix conditional error in PmTopics controller<commit_after>class PmTopicsController < ApplicationController
  before_action :ensure_logged_in
  before_action :ensure_topic_post_associated, only: [:new, :create]
  before_action :ensure_contact,               only: [:new, :create]
  before_action :ensure_distinct_users,        only: [:new, :create]
  before_action :ensure_exists,                only: [:show, :update]
  before_action :ensure_valid_user,            only: [:show, :update]
  before_action :mark_as_read,                 only: :show

  def index
    @pm_topics = current_user.pm_topics
                             .order(last_posted: :desc)
                             .paginate(page: params[:page], per_page: 10)
  end

  def show
    @pm_topic = pm_topic
    @pm_posts = @pm_topic.pm_posts.order(created_at: :asc)
    @pm_post  = @pm_topic.pm_posts.build
    @user_handshake = user_handshake.to_s
    @user_sent_handshake = @pm_topic.send(user_handshake)
  end

  def new
    @pm_topic = PmTopic.new
    @pm_topic.pm_posts.build
    @topic = original_topic 
    @post  = original_post
    redirect_to root_url unless @post.visible
  end

  def create
    @pm_topic = PmTopic.new(pm_topic_params)
    if @pm_topic.save
      @pm_post = @pm_topic.pm_posts.first
      @pm_post.update_attributes(user_id:    current_user.id,
                                 ip_address: request.remote_ip)
      @pm_topic.update_attributes(sender_id:     current_user.id,
                                  recipient_id:  original_post.user_id,
                                  last_posted:   @pm_post.created_at,
                                  sender_unread: false)
      @pm_topic.update_attributes(sender_handshake: true) if handshake_sent
      redirect_to @pm_topic
    else
      render 'new'
    end
  end

  def update
    pm_topic.update_attributes(patch_params) unless invalid_patch
    redirect_to pm_topic
  end

  private

    def pm_topic_params
      params.require(:pm_topic)
            .permit(:topic_id, :post_id, :title,
                    pm_posts_attributes: [:content, :handshake_sent])
    end

    def patch_params
      params.require(:pm_topic)
            .permit(:id, :handshake_declined, user_handshake)
    end

    def pm_topic
      id = params[:id] || patch_params[:id]
      PmTopic.find_by(id: id)
    end

    def original_topic
      id = params[:topic_id] || pm_topic_params[:topic_id]
      Topic.find_by(id: id)
    end

    def original_post
      id = params[:post_id] || pm_topic_params[:post_id]
      Post.find_by(id: id)
    end

    def ensure_topic_post_associated
      associated_id = original_post.topic_id
      redirect_to root_url unless associated_id == original_topic.id
    end

    def ensure_contact
      redirect_to root_url unless original_post.contact
    end

    def ensure_distinct_users
      recipient_id = original_post.user_id
      if recipient_id == current_user.id
        flash[:warning] = "You can't send yourself a private message!"
        redirect_to :back
      end
    end

    def ensure_exists
      redirect_to root_url if pm_topic.nil?
    end

    def ensure_valid_user
      redirect_to root_url unless pm_topic.valid_users.include?(current_user.id)
    end

    def handshake_sent
      pm_topic_params[:pm_posts_attributes]["0"][:handshake_sent]
    end

    def pm_user
      pm_topic.sender_id == current_user.id ? 'sender' : 'recipient'
    end

    def user_handshake
      "#{pm_user}_handshake".to_sym
    end

    def mark_as_read
      user_unread = "#{pm_user}_unread".to_sym
      pm_topic.update_attributes(user_unread => false)
    end

    def invalid_patch
      handshake = patch_params[user_handshake] || pm_topic.send(user_handshake)
      patch_params[:handshake_declined] && handshake
    end
end

{'fraction_non_alphanumeric': 0.07861759662313679, 'fraction_numerical': 0.0007914523149980214, 'mean_word_length': 2.55962441314554, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1773193', 'n_tokens_mistral': 2482, 'n_tokens_neox': 2418, 'n_words': 475}