<commit_before>require 'exercism/rikki'

module ExercismAPI
  module Routes
    class Comments < Core
      # Submit a rikki- comment.
      # Only the rikki- daemon uses this endpoint.
      post '/submissions/:key/comments' do |key|
        unless Rikki.validate(params[:shared_key])
          halt 401, {error: "access denied"}
        end

        submission = Submission.find_by_key(key)
        if submission.nil?
          halt 404, {error: "unknown submission #{key}"}.to_json
        end

        user = User.find_by_username('rikki-')
        if user.nil?
          halt 400, {error: "cannot find rikki- the robot (submission: #{key})"}.to_json
        end

        body = request.body.read.to_s
        comment = JSON.parse(body)["comment"].to_s
        if comment.empty?
          halt 400, {error: "no comment submitted (submission: #{key})"}.to_json
        end

        if submission.comments.where(user_id: user.id).count > 0
          halt 204
        end

        submission.comments.create(user: user, body: comment)
        Notification.on(submission, to: submission.user, regarding: 'nitpick', creator: user)
        halt 204
      end
    end
  end
end<commit_msg>Ignore rikki- comment on earlier submissions<commit_after>require 'exercism/rikki'

module ExercismAPI
  module Routes
    class Comments < Core
      # Submit a rikki- comment.
      # Only the rikki- daemon uses this endpoint.
      post '/submissions/:key/comments' do |key|
        unless Rikki.validate(params[:shared_key])
          halt 401, {error: "access denied"}
        end

        submission = Submission.find_by_key(key)
        if submission.nil?
          halt 404, {error: "unknown submission #{key}"}.to_json
        end

        user = User.find_by_username('rikki-')
        if user.nil?
          halt 400, {error: "cannot find rikki- the robot (submission: #{key})"}.to_json
        end

        body = request.body.read.to_s
        comment = JSON.parse(body)["comment"].to_s
        if comment.empty?
          halt 400, {error: "no comment submitted (submission: #{key})"}.to_json
        end

        if submission.comments.where(user_id: user.id).count > 0
          halt 204
        end

        uuid = Submission.where(user_exercise_id: submission.user_exercise_id).order('created_at DESC').limit(1).pluck('key').first
        if uuid != submission.key
          halt 204
        end

        submission.comments.create(user: user, body: comment)
        Notification.on(submission, to: submission.user, regarding: 'nitpick', creator: user)
        halt 204
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.10688196847366398, 'fraction_numerical': 0.016147635524798153, 'mean_word_length': 2.2978453738910014, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1275763', 'n_tokens_mistral': 828, 'n_tokens_neox': 769, 'n_words': 210}