<commit_before># frozen_string_literal: true

require "mastodon_ext"
require "uri"

class MastodonUserProcessor
  def self.stats
    @@stats ||= Stats.new
  end

  class TootError < StandardError
    def initialize(error)
      @error = error
    end
    attr_reader :error
  end

  def self.process_user(user)
    get_last_toots_for_user(user) if user.posting_from_mastodon
  rescue HTTP::ConnectionError, Oj::ParseError => ex
    Rails.logger.warn { "Domain #{user.mastodon.mastodon_client.domain} seems offline" }
    stats.increment("domain.offline")
    raise ex
  rescue OpenSSL::SSL::SSLError => ex
    Rails.logger.warn { "Domain #{user.mastodon.mastodon_client.domain} has SSL issues" }
    stats.increment("domain.ssl_error")
    raise ex
  rescue HTTP::Error => ex
    if ex.message == "Unknown MIME type: text/html"
      Rails.logger.warn { "Domain #{user.mastodon.mastodon_client.domain} seems offline" }
      stats.increment("domain.offline")
      raise ex
    else
      Rails.logger.error { "Issue connecting to user #{user.mastodon.uid}. -- #{ex} -- Bailing out" }
      stats.increment("user.http_error")
      raise ex
    end
  rescue TootError => ex
    raise ex.error
  rescue => ex
    Rails.logger.error { "Could not process user #{user.mastodon.uid}. -- #{ex} -- Bailing out" }
    stats.increment("user.processing_error")
    raise ex
  ensure
    user.mastodon_last_check = Time.now
    user.twitter_last_check = Time.now unless user.posting_from_twitter
    user.save
  end

  def self.statuses_options(user)
    opts = {}
    opts[:since_id] = user.last_toot unless user.last_toot.nil?
    opts
  end

  TWITTER_CANNOT_PERFORM_WRITE_ACTIONS = 261

  def self.stoplight_wrap_request(domain, &)
    if domain.present?
      Stoplight("source:#{domain}", &)
        .with_threshold(3)
        .with_cool_off_time(5.minutes.seconds)
        .with_error_handler { |error, handle| error.is_a?(HTTP::Error) || error.is_a?(OpenSSL::SSL::SSLError) ? handle.call(error) : raise(error) }
        .run
    else
      yield
    end
  end<commit_msg>Add server down to possible Stoplight errors<commit_after># frozen_string_literal: true

require "mastodon_ext"
require "uri"

class MastodonUserProcessor
  def self.stats
    @@stats ||= Stats.new
  end

  class TootError < StandardError
    def initialize(error)
      @error = error
    end
    attr_reader :error
  end

  def self.process_user(user)
    get_last_toots_for_user(user) if user.posting_from_mastodon
  rescue HTTP::ConnectionError, Oj::ParseError => ex
    Rails.logger.warn { "Domain #{user.mastodon.mastodon_client.domain} seems offline" }
    stats.increment("domain.offline")
    raise ex
  rescue OpenSSL::SSL::SSLError => ex
    Rails.logger.warn { "Domain #{user.mastodon.mastodon_client.domain} has SSL issues" }
    stats.increment("domain.ssl_error")
    raise ex
  rescue HTTP::Error => ex
    if ex.message == "Unknown MIME type: text/html"
      Rails.logger.warn { "Domain #{user.mastodon.mastodon_client.domain} seems offline" }
      stats.increment("domain.offline")
      raise ex
    else
      Rails.logger.error { "Issue connecting to user #{user.mastodon.uid}. -- #{ex} -- Bailing out" }
      stats.increment("user.http_error")
      raise ex
    end
  rescue TootError => ex
    raise ex.error
  rescue => ex
    Rails.logger.error { "Could not process user #{user.mastodon.uid}. -- #{ex} -- Bailing out" }
    stats.increment("user.processing_error")
    raise ex
  ensure
    user.mastodon_last_check = Time.now
    user.twitter_last_check = Time.now unless user.posting_from_twitter
    user.save
  end

  def self.statuses_options(user)
    opts = {}
    opts[:since_id] = user.last_toot unless user.last_toot.nil?
    opts
  end

  TWITTER_CANNOT_PERFORM_WRITE_ACTIONS = 261

  def self.stoplight_wrap_request(domain, &)
    if domain.present?
      Stoplight("source:#{domain}", &)
        .with_threshold(3)
        .with_cool_off_time(5.minutes.seconds)
        .with_error_handler { |error, handle| error.is_a?(HTTP::Error) || error.is_a?(OpenSSL::SSL::SSLError) || error.is_a?(Oj::ParseError) ? handle.call(error) : raise(error) }
        .run
    else
      yield
    end
  end
{'fraction_non_alphanumeric': 0.11576941400666985, 'fraction_numerical': 0.0023820867079561697, 'mean_word_length': 3.2934560327198366, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, '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': '1338557', 'n_tokens_mistral': 1463, 'n_tokens_neox': 1417, 'n_words': 292}