<commit_before>class DatajamChat::ChatMessagesController < DatajamChat::EngineController

  before_filter :authenticate_user!, :except => [:create]

  def index
    @chat = Chat.find(params[:chat_id])
    @limit =  params[:limit] ? params[:limit].to_i : Datajam::Settings[:datajam_chat][:page_size].to_i
    unless params[:status]
      @incoming = @chat.incoming_messages.limit @limit
      @approved = @chat.approved_messages.limit @limit
      @rejected = @chat.rejected_messages.limit @limit
    else
      @messages = @chat.send("#{params[:status]}_messages".to_sym).limit(@limit)
    end

    respond_to do |format|
      format.json { render :json => locals, :callback => params[:callback] }
    end
  end

  def create
    @chat = Chat.find(params[:chat_id])
    @message = ChatMessage.new(JSON.parse(params[:model]))
    @message.chat_id = @chat.id

    if current_user
      @message.user = current_user
      @message.display_name = current_user.name
      @message.is_public = true
      @message.is_moderated = true
    elsif session[:display_name]
      @message.display_name = session[:display_name]
    else
      @message = nil
      response = {:errors => "You need to be identified to post"}
    end

    if @message && @message.save
      response = @message.as_json
    elsif @message
      response = {:errors => @message.errors}
    end

    respond_to do |format|
      format.json do
        if response.include? :errors
          render :json => response, :callback => params[:callback], :status => 409
        else
          render :json => response, :callback => params[:callback]
        end
      end
    end
  end

  def update
    @message = ChatMessage.find(params[:id])
    if @message.update_with(JSON.parse(params[:model]))
      @message.page.save
      response = @message
    else
      response = @message.errors
    end

    respond_to do |format|<commit_msg>don't save a nil page<commit_after>class DatajamChat::ChatMessagesController < DatajamChat::EngineController

  before_filter :authenticate_user!, :except => [:create]

  def index
    @chat = Chat.find(params[:chat_id])
    @limit =  params[:limit] ? params[:limit].to_i : Datajam::Settings[:datajam_chat][:page_size].to_i
    unless params[:status]
      @incoming = @chat.incoming_messages.limit @limit
      @approved = @chat.approved_messages.limit @limit
      @rejected = @chat.rejected_messages.limit @limit
    else
      @messages = @chat.send("#{params[:status]}_messages".to_sym).limit(@limit)
    end

    respond_to do |format|
      format.json { render :json => locals, :callback => params[:callback] }
    end
  end

  def create
    @chat = Chat.find(params[:chat_id])
    @message = ChatMessage.new(JSON.parse(params[:model]))
    @message.chat_id = @chat.id

    if current_user
      @message.user = current_user
      @message.display_name = current_user.name
      @message.is_public = true
      @message.is_moderated = true
    elsif session[:display_name]
      @message.display_name = session[:display_name]
    else
      @message = nil
      response = {:errors => "You need to be identified to post"}
    end

    if @message && @message.save
      response = @message.as_json
    elsif @message
      response = {:errors => @message.errors}
    end

    respond_to do |format|
      format.json do
        if response.include? :errors
          render :json => response, :callback => params[:callback], :status => 409
        else
          render :json => response, :callback => params[:callback]
        end
      end
    end
  end

  def update
    @message = ChatMessage.find(params[:id])
    if @message.update_with(JSON.parse(params[:model]))
      @message.page && @message.page.save
      response = @message
    else
      response = @message.errors
    end

    respond_to do |format|
{'fraction_non_alphanumeric': 0.12209150326797386, 'fraction_numerical': 0.001568627450980392, 'mean_word_length': 2.9937369519832986, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4437887', 'n_tokens_mistral': 1180, 'n_tokens_neox': 1131, 'n_words': 260}