<commit_before>class Queries::UsersByVolumeQuery
  def self.normal_or_loud(model)
    base_query(model).where(volume_where(mode: :greater_than), volume: DiscussionReader.volumes[:normal])
  end

  %w(mute quiet normal loud).map(&:to_sym).each do |volume|
    define_singleton_method volume, ->(model) {
      base_query(model).where(volume_where, volume: DiscussionReader.volumes[volume])
    }
  end

  private

  def self.base_query(model)
    User.active
        .distinct
        .joins(membership_join(model))
        .joins(reader_join(model))
        .where.not('m.id': nil)
        .where('m.archived_at': nil)
  end

  def self.volume_where(mode: :equal_to)
    operator = mode == :equal_to ? '=' : '>='
    "dr.volume #{operator} :volume OR (dr.volume IS NULL AND m.volume #{operator} :volume)"
  end

  def self.reader_join(model)
    clause = model.respond_to?(:discussion) ? "= #{model.discussion.id.to_i}" : " IS NULL"
    "LEFT OUTER JOIN discussion_readers dr ON (dr.user_id = users.id AND dr.discussion_id #{clause})"
  end

  def self.membership_join(model)
    "LEFT OUTER JOIN memberships m ON (m.user_id = users.id AND m.group_id = #{model.group.id.to_i})"
  end
end<commit_msg>return User.none for users by volume when poll is standalone<commit_after>class Queries::UsersByVolumeQuery
  def self.normal_or_loud(model)
    base_query(model).where(volume_where(mode: :greater_than), volume: DiscussionReader.volumes[:normal])
  end

  %w(mute quiet normal loud).map(&:to_sym).each do |volume|
    define_singleton_method volume, ->(model) {
      base_query(model).where(volume_where, volume: DiscussionReader.volumes[volume])
    }
  end

  private

  def self.base_query(model)
    return User.none unless model
    User.active
        .distinct
        .joins(membership_join(model))
        .joins(reader_join(model))
        .where.not('m.id': nil)
        .where('m.archived_at': nil)
  end

  def self.volume_where(mode: :equal_to)
    operator = mode == :equal_to ? '=' : '>='
    "dr.volume #{operator} :volume OR (dr.volume IS NULL AND m.volume #{operator} :volume)"
  end

  def self.reader_join(model)
    clause = model.respond_to?(:discussion) ? "= #{model.discussion.id.to_i}" : " IS NULL"
    "LEFT OUTER JOIN discussion_readers dr ON (dr.user_id = users.id AND dr.discussion_id #{clause})"
  end

  def self.membership_join(model)
    "LEFT OUTER JOIN memberships m ON (m.user_id = users.id AND m.group_id = #{model.group.id.to_i})"
  end
end
{'fraction_non_alphanumeric': 0.13392496974586526, 'fraction_numerical': 0.0, 'mean_word_length': 3.732824427480916, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '3620145', 'n_tokens_mistral': 873, 'n_tokens_neox': 845, 'n_words': 184}