<commit_before>module Thredded
  class ApplicationController < ::ApplicationController
    layout Thredded.layout

    helper Thredded::Engine.helpers
    helper_method \
      :active_users,
      :messageboard,
      :preferences,
      :unread_private_topics_count

    rescue_from \
      CanCan::AccessDenied,
      Thredded::Errors::MessageboardNotFound,
      Thredded::Errors::MessageboardReadDenied,
      Thredded::Errors::TopicCreateDenied,
      Thredded::Errors::PrivateTopicCreateDenied do |exception|
      redirect_to thredded.root_path, alert: exception.message
    end

    rescue_from \
      Thredded::Errors::EmptySearchResults,
      Thredded::Errors::TopicNotFound do |exception|
      redirect_to messageboard_topics_path(messageboard), alert: exception.message
    end

    def signed_in?
      !current_user.anonymous?
    end

    private

    def unread_private_topics_count
      Rails.cache.fetch("private_topics_count_#{messageboard.id}_#{current_user.id}") do
        Thredded::PrivateTopic
          .joins(:private_users)
          .where(
            messageboard: messageboard,
            thredded_private_users: {
              user_id: current_user.id,
              read: false,
            }
          )
          .count
      end
    end

    def authorize_reading(obj)
      return if can? :read, obj

      class_name = obj.class.to_s
      error = class_name
        .gsub(/Thredded::/, 'Thredded::Errors::') + 'ReadDenied'

      fail error.constantize
    end

    def authorize_creating(obj)
      return if can? :create, obj

      class_name = obj.class.to_s
      error = class_name
        .gsub(/Thredded::/, 'Thredded::Errors::') + 'CreateDenied'

      fail error.constantize
    end

    def update_user_activity
      Thredded::ActivityUpdaterJob.queue.update_user_activity(
        'messageboard_id' => messageboard.id,
        'user_id' => current_user.id
      )
    end

    def current_ability
      @current_ability ||= Ability.new(current_user)
    end

    def messageboard
      @messageboard ||= Messageboard.find_by_slug(params[:messageboard_id])
    end

    def preferences
      @preferences ||= current_user.thredded_user_preference
    end

    def current_user
      super || NullUser.new
    end

    def active_users
      users = messageboard.try(:active_users) || []
      users.push(current_user).uniq unless current_user.is_a?(NullUser)
      users
    end
  end
end<commit_msg>[JRO] Ensure list of active users is uniq<commit_after>module Thredded
  class ApplicationController < ::ApplicationController
    layout Thredded.layout

    helper Thredded::Engine.helpers
    helper_method \
      :active_users,
      :messageboard,
      :preferences,
      :unread_private_topics_count

    rescue_from \
      CanCan::AccessDenied,
      Thredded::Errors::MessageboardNotFound,
      Thredded::Errors::MessageboardReadDenied,
      Thredded::Errors::TopicCreateDenied,
      Thredded::Errors::PrivateTopicCreateDenied do |exception|
      redirect_to thredded.root_path, alert: exception.message
    end

    rescue_from \
      Thredded::Errors::EmptySearchResults,
      Thredded::Errors::TopicNotFound do |exception|
      redirect_to messageboard_topics_path(messageboard), alert: exception.message
    end

    def signed_in?
      !current_user.anonymous?
    end

    private

    def unread_private_topics_count
      Rails.cache.fetch("private_topics_count_#{messageboard.id}_#{current_user.id}") do
        Thredded::PrivateTopic
          .joins(:private_users)
          .where(
            messageboard: messageboard,
            thredded_private_users: {
              user_id: current_user.id,
              read: false,
            }
          )
          .count
      end
    end

    def authorize_reading(obj)
      return if can? :read, obj

      class_name = obj.class.to_s
      error = class_name
        .gsub(/Thredded::/, 'Thredded::Errors::') + 'ReadDenied'

      fail error.constantize
    end

    def authorize_creating(obj)
      return if can? :create, obj

      class_name = obj.class.to_s
      error = class_name
        .gsub(/Thredded::/, 'Thredded::Errors::') + 'CreateDenied'

      fail error.constantize
    end

    def update_user_activity
      Thredded::ActivityUpdaterJob.queue.update_user_activity(
        'messageboard_id' => messageboard.id,
        'user_id' => current_user.id
      )
    end

    def current_ability
      @current_ability ||= Ability.new(current_user)
    end

    def messageboard
      @messageboard ||= Messageboard.find_by_slug(params[:messageboard_id])
    end

    def preferences
      @preferences ||= current_user.thredded_user_preference
    end

    def current_user
      super || NullUser.new
    end

    def active_users
      users = messageboard.try(:active_users) || []
      users.push(current_user) unless current_user.is_a?(NullUser)
      users.uniq
    end
  end
end
{'fraction_non_alphanumeric': 0.08737864077669903, 'fraction_numerical': 0.0, 'mean_word_length': 2.662962962962963, 'pattern_counts': {'":': 0, '<': 5, '<?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': '3515077', 'n_tokens_mistral': 1515, 'n_tokens_neox': 1490, 'n_words': 270}