<commit_before># frozen_string_literal: true

class Feedback < ApplicationRecord
  include Websocket
  delegate :url_helpers, to: 'Rails.application.routes'

  default_scope { where(is_invalidated: false) }
  scope(:invalid, -> { unscoped.where(is_invalidated: true) })
  scope(:via_api, -> { unscoped.where.not(api_key: nil) })
  scope(:today, -> { where('created_at > ?', Date.today) })

  belongs_to :post, counter_cache: true
  belongs_to :user
  belongs_to :invalidated_by, class_name: 'User', foreign_key: 'invalidated_by'
  belongs_to :api_key

  before_save :check_for_user_assoc
  before_save :check_for_dupe_feedback

  after_create :send_to_chat
  after_create :send_blacklist_request

  after_save do
    if update_post_feedback_cache # if post feedback cache was changed
      if post.flagged? && !post.is_tp
        flags = post.flag_logs.successful.includes(:user)
        names = flags.map { |flag| '@' + flag.user.username.tr(' ', '') }

        flags.each do |flag|
          next unless flag.user.present?
          Thread.new do
            FlagCondition.validate_for_user(flag.user, User.find(-1))
          end
        end

        message = "fp feedback on autoflagged post: [#{post.title}](#{post.link}) [MS]" \
                  "(//metasmoke.erwaysoftware.com/post/#{post_id}) (#{names.join ' '})"
        SmokeDetector.send_message_to_charcoal message
        ActionCable.server.broadcast 'smokedetector_messages', autoflag_fp: { message: message, site: post.site.site_domain }
      end

      post.stack_exchange_user&.unblacklist_user if post.is_fp
    end
  end

  after_create do
    ActionCable.server.broadcast "posts_#{post_id}", feedback: FeedbacksController.render(locals: { feedback: self }, partial: 'feedback').html_safe
    feedback = FeedbacksController.render(locals: { feedback: self }, partial: 'feedback.json')
    ActionCable.server.broadcast 'api_feedback', feedback: JSON.parse(feedback)

    update(user_name: user.username) if user_id.present? && user_name.nil?
  end
<commit_msg>don't send autoflag FPs twice, strip out System<commit_after># frozen_string_literal: true

class Feedback < ApplicationRecord
  include Websocket
  delegate :url_helpers, to: 'Rails.application.routes'

  default_scope { where(is_invalidated: false) }
  scope(:invalid, -> { unscoped.where(is_invalidated: true) })
  scope(:via_api, -> { unscoped.where.not(api_key: nil) })
  scope(:today, -> { where('created_at > ?', Date.today) })

  belongs_to :post, counter_cache: true
  belongs_to :user
  belongs_to :invalidated_by, class_name: 'User', foreign_key: 'invalidated_by'
  belongs_to :api_key

  before_save :check_for_user_assoc
  before_save :check_for_dupe_feedback

  after_create :send_to_chat
  after_create :send_blacklist_request

  after_save do
    if update_post_feedback_cache # if post feedback cache was changed
      if post.flagged? && !post.is_tp
        flags = post.flag_logs.successful.where.not(user_id: -1).includes(:user)
        names = flags.map { |flag| '@' + flag.user.username.tr(' ', '') }

        flags.each do |flag|
          next unless flag.user.present?
          Thread.new do
            FlagCondition.validate_for_user(flag.user, User.find(-1))
          end
        end

        message = "fp feedback on autoflagged post: [#{post.title}](#{post.link}) [MS]" \
                  "(//metasmoke.erwaysoftware.com/post/#{post_id}) (#{names.join ' '})"
        ActionCable.server.broadcast 'smokedetector_messages', autoflag_fp: { message: message, site: post.site.site_domain }
      end

      post.stack_exchange_user&.unblacklist_user if post.is_fp
    end
  end

  after_create do
    ActionCable.server.broadcast "posts_#{post_id}", feedback: FeedbacksController.render(locals: { feedback: self }, partial: 'feedback').html_safe
    feedback = FeedbacksController.render(locals: { feedback: self }, partial: 'feedback.json')
    ActionCable.server.broadcast 'api_feedback', feedback: JSON.parse(feedback)

    update(user_name: user.username) if user_id.present? && user_name.nil?
  end

{'fraction_non_alphanumeric': 0.11884842519685039, 'fraction_numerical': 0.0007381889763779527, 'mean_word_length': 3.7767332549941246, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '78206', 'n_tokens_mistral': 1346, 'n_tokens_neox': 1309, 'n_words': 262}