<commit_before>class CommentController < ApplicationController

  needs_profile

  before_filter :can_update?, :only => [:edit, :update]

  def create
    begin
      @page = profile.articles.find(params[:id])
    rescue
      @page = nil
    end

    # page not found, give error
    if @page.nil?
      respond_to do |format|
        format.js do
           render :json => { :msg => _('Page not found.')}
         end
       end
      return
    end

    unless @page.accept_comments?
      respond_to do |format|
        format.js do
           render :json => { :msg => _('Comment not allowed in this article')}
         end
       end
      return
    end

    @comment = Comment.new(params[:comment])
    @comment.author = user if logged_in?
    @comment.article = @page
    @comment.ip_address = request.remote_ip
    @comment.user_agent = request.user_agent
    @comment.referrer = request.referrer
    @plugins.dispatch(:filter_comment, @comment)

    if @comment.rejected?
      respond_to do |format|
        format.js do
           render :json => { :msg => _('Comment was rejected')}
         end
       end
      return
    end

    if !@comment.valid? || (not pass_without_comment_captcha? and not captcha_verify(:model => @comment, :message => _('Please type the words correctly')))
      respond_to do |format|
        format.js do
          render :json => {
             :render_target => 'form',
             :html => render_to_string(:partial => 'comment_form', :object => @comment, :locals => {:comment => @comment, :display_link => true, :show_form => true})
          }
        end
      end
      return
    end

    if @comment.need_moderation?
      @comment.created_at = Time.now
      ApproveComment.create!(:requestor => @comment.author, :target => profile, :comment_attributes => @comment.attributes.to_json)

      respond_to do |format|
        format.js do
          render :json => { :render_target => nil, :msg => _('Your comment is waiting for approval.') }
        end
      end
      return
    end

    @saved = @comment.save
    url = "#{url_for only_path: true, controller: :content_viewer, action: :view_page, profile: profile.identifier, page: @page.path.split('/')}/new_comment"
    comment_to_render = @comment.comment_root
    comment_html = render_to_string partial: 'comment', locals: {comment: comment_to_render, display_link: true}
    comment_json = {
      user_login: current_user.login,
      saved: @saved,
      render_target: comment_to_render.anchor,
      html: comment_html,
    }

    if @saved
      @plugins.dispatch(:process_extra_comment_params, [@comment,params]) if @saved
      MessageBus.publish url, comment_json
    end

    comment_json[:msg] = _('Comment successfully created.')
    render json: comment_json
  end

  def destroy
    comment = profile.comments_received.find(params[:id])

    if comment && comment.can_be_destroyed_by?(user) && comment.destroy
      render :text => {'ok' => true}.to_json, :content_type => 'application/json'
    else
      session[:notice] = _("The comment was not removed.")
      render :text => {'ok' => false}.to_json, :content_type => 'application/json'
    end
  end

  def mark_as_spam
    comment = profile.comments_received.find(params[:id])
    if comment.can_be_marked_as_spam_by?(user)
      comment.spam!
      render :text => {'ok' => true}.to_json, :content_type => 'application/json'
    else
      session[:notice] = _("You couldn't mark this comment as spam.")
      render :text => {'ok' => false}.to_json, :content_type => 'application/json'
    end
  end

  def edit
    render :partial => "comment_form", :locals => {:comment => @comment, :display_link => params[:reply_of_id].present?, :edition_mode => true, :show_form => true}
  end

  def update
    if @comment.update(params[:comment])
      @plugins.dispatch(:process_extra_comment_params, [@comment,params])

      respond_to do |format|
        format.js do
          comment_to_render = @comment.comment_root
          render :json => {
            :ok => true,
            :render_target => comment_to_render.anchor,
            :html => render_to_string(:partial => 'comment', :locals => {:comment => comment_to_render})
          }
        end
      end
    else
     respond_to do |format|
       format.js do
         render :json => {
           :ok => false,
           :render_target => 'form',
           :html => render_to_string(:partial => 'comment_form', :object => @comment, :locals => {:comment => @comment, :display_link => false, :edition_mode => true, :show_form => true})
         }
       end
     end
   end
  end

  def check_actions
    comment = profile.comments_received.find(params[:id])
    ids = @plugins.dispatch(:check_comment_actions, comment).collect do |action|
      action.kind_of?(Proc) ? self.instance_eval(&action) : action
    end.flatten.compact
    render :json => {:ids => ids}
  end

  protected

  def pass_without_comment_captcha?
    logged_in? && !environment.enabled?('captcha_for_logged_users')
  end
  helper_method :pass_without_comment_captcha?

  def can_update?
    begin
      @comment = profile.comments_received.find(params[:id])
      raise ActiveRecord::RecordNotFound unless @comment.can_be_updated_by?(user) # Not reveal that the comment exists
    rescue ActiveRecord::RecordNotFound
      render_not_found
      return
    end
  end
<commit_msg>Fix UnknownFormat error on comment rejection<commit_after>class CommentController < ApplicationController

  needs_profile

  before_filter :can_update?, :only => [:edit, :update]

  def create
    begin
      @page = profile.articles.find(params[:id])
    rescue
      @page = nil
    end

    # page not found, give error
    if @page.nil?
      respond_to do |format|
        format.js do
           render :json => { :msg => _('Page not found.')}
         end
       end
      return
    end

    unless @page.accept_comments?
      respond_to do |format|
        format.js do
           render :json => { :msg => _('Comment not allowed in this article')}
         end
       end
      return
    end

    @comment = Comment.new(params[:comment])
    @comment.author = user if logged_in?
    @comment.article = @page
    @comment.ip_address = request.remote_ip
    @comment.user_agent = request.user_agent
    @comment.referrer = request.referrer
    @plugins.dispatch(:filter_comment, @comment)

    return render :json => { :msg => _('Comment was rejected')} if @comment.rejected?

    if !@comment.valid? || (not pass_without_comment_captcha? and not captcha_verify(:model => @comment, :message => _('Please type the words correctly')))
      respond_to do |format|
        format.js do
          render :json => {
             :render_target => 'form',
             :html => render_to_string(:partial => 'comment_form', :object => @comment, :locals => {:comment => @comment, :display_link => true, :show_form => true})
          }
        end
      end
      return
    end

    if @comment.need_moderation?
      @comment.created_at = Time.now
      ApproveComment.create!(:requestor => @comment.author, :target => profile, :comment_attributes => @comment.attributes.to_json)

      respond_to do |format|
        format.js do
          render :json => { :render_target => nil, :msg => _('Your comment is waiting for approval.') }
        end
      end
      return
    end

    @saved = @comment.save
    url = "#{url_for only_path: true, controller: :content_viewer, action: :view_page, profile: profile.identifier, page: @page.path.split('/')}/new_comment"
    comment_to_render = @comment.comment_root
    comment_html = render_to_string partial: 'comment', locals: {comment: comment_to_render, display_link: true}
    comment_json = {
      user_login: current_user.login,
      saved: @saved,
      render_target: comment_to_render.anchor,
      html: comment_html,
    }

    if @saved
      @plugins.dispatch(:process_extra_comment_params, [@comment,params]) if @saved
      MessageBus.publish url, comment_json
    end

    comment_json[:msg] = _('Comment successfully created.')
    render json: comment_json
  end

  def destroy
    comment = profile.comments_received.find(params[:id])

    if comment && comment.can_be_destroyed_by?(user) && comment.destroy
      render :text => {'ok' => true}.to_json, :content_type => 'application/json'
    else
      session[:notice] = _("The comment was not removed.")
      render :text => {'ok' => false}.to_json, :content_type => 'application/json'
    end
  end

  def mark_as_spam
    comment = profile.comments_received.find(params[:id])
    if comment.can_be_marked_as_spam_by?(user)
      comment.spam!
      render :text => {'ok' => true}.to_json, :content_type => 'application/json'
    else
      session[:notice] = _("You couldn't mark this comment as spam.")
      render :text => {'ok' => false}.to_json, :content_type => 'application/json'
    end
  end

  def edit
    render :partial => "comment_form", :locals => {:comment => @comment, :display_link => params[:reply_of_id].present?, :edition_mode => true, :show_form => true}
  end

  def update
    if @comment.update(params[:comment])
      @plugins.dispatch(:process_extra_comment_params, [@comment,params])

      respond_to do |format|
        format.js do
          comment_to_render = @comment.comment_root
          render :json => {
            :ok => true,
            :render_target => comment_to_render.anchor,
            :html => render_to_string(:partial => 'comment', :locals => {:comment => comment_to_render})
          }
        end
      end
    else
      respond_to do |format|
        format.js do
          render :json => {
            :ok => false,
            :render_target => 'form',
            :html => render_to_string(:partial => 'comment_form', :object => @comment, :locals => {:comment => @comment, :display_link => false, :edition_mode => true, :show_form => true})
          }
        end
      end
    end
  end

  def check_actions
    comment = profile.comments_received.find(params[:id])
    ids = @plugins.dispatch(:check_comment_actions, comment).collect do |action|
      action.kind_of?(Proc) ? self.instance_eval(&action) : action
    end.flatten.compact
    render :json => {:ids => ids}
  end

  protected

  def pass_without_comment_captcha?
    logged_in? && !environment.enabled?('captcha_for_logged_users')
  end
  helper_method :pass_without_comment_captcha?

  def can_update?
    begin
      @comment = profile.comments_received.find(params[:id])
      raise ActiveRecord::RecordNotFound unless @comment.can_be_updated_by?(user) # Not reveal that the comment exists
    rescue ActiveRecord::RecordNotFound
      render_not_found
      return
    end
  end

{'fraction_non_alphanumeric': 0.1207635009310987, 'fraction_numerical': 0.0, 'mean_word_length': 2.8525824964131994, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 127, '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': '2548408', 'n_tokens_mistral': 3251, 'n_tokens_neox': 3178, 'n_words': 781}