<commit_before>require_dependency 'pinned_check'
require_dependency 'new_post_manager'

class TopicViewSerializer < ApplicationSerializer
  include PostStreamSerializerMixin

  def self.attributes_from_topic(*list)
    [list].flatten.each do |attribute|
      attributes(attribute)
      class_eval %{def #{attribute}
        object.topic.#{attribute}
      end}
    end
  end

  attributes_from_topic :id,
                        :title,
                        :fancy_title,
                        :posts_count,
                        :created_at,
                        :views,
                        :reply_count,
                        :participant_count,
                        :like_count,
                        :last_posted_at,
                        :visible,
                        :closed,
                        :archived,
                        :has_summary,
                        :archetype,
                        :slug,
                        :category_id,
                        :word_count,
                        :deleted_at,
                        :pending_posts_count,
                        :user_id

  attributes :draft,
             :draft_key,
             :draft_sequence,
             :posted,
             :unpinned,
             :pinned_globally,
             :pinned,    # Is topic pinned and viewer hasn't cleared the pin?
             :pinned_at, # Ignores clear pin
             :pinned_until,
             :details,
             :highest_post_number,
             :last_read_post_number,
             :deleted_by,
             :has_deleted,
             :actions_summary,
             :expandable_first_post,
             :is_warning,
             :chunk_size,
             :bookmarked

  # TODO: Split off into proper object / serializer
  def details
    result = {
      auto_close_at: object.topic.auto_close_at,
      auto_close_hours: object.topic.auto_close_hours,
      auto_close_based_on_last_post: object.topic.auto_close_based_on_last_post,
      created_by: BasicUserSerializer.new(object.topic.user, scope: scope, root: false),
      last_poster: BasicUserSerializer.new(object.topic.last_poster, scope: scope, root: false)
    }

    if object.topic.allowed_users.present?
      result[:allowed_users] = object.topic.allowed_users.map do |user|
        BasicUserSerializer.new(user, scope: scope, root: false)
      end
    end

    if object.topic.allowed_groups.present?
      result[:allowed_groups] = object.topic.allowed_groups.map do |ag|
        BasicGroupSerializer.new(ag, scope: scope, root: false)
      end
    end

    if object.post_counts_by_user.present?
      result[:participants] = object.post_counts_by_user.map do |pc|
        TopicPostCountSerializer.new({user: object.participants[pc[0]], post_count: pc[1]}, scope: scope, root: false)
      end
    end


    if object.suggested_topics.try(:topics).present?
      result[:suggested_topics] = object.suggested_topics.topics.map do |user|
        SuggestedTopicSerializer.new(user, scope: scope, root: false)
      end
    end

    if object.links.present?
      result[:links] = object.links.map do |user|
        TopicLinkSerializer.new(user, scope: scope, root: false)
      end
    end<commit_msg>PERF: avoid needless queries on non private messages<commit_after>require_dependency 'pinned_check'
require_dependency 'new_post_manager'

class TopicViewSerializer < ApplicationSerializer
  include PostStreamSerializerMixin

  def self.attributes_from_topic(*list)
    [list].flatten.each do |attribute|
      attributes(attribute)
      class_eval %{def #{attribute}
        object.topic.#{attribute}
      end}
    end
  end

  attributes_from_topic :id,
                        :title,
                        :fancy_title,
                        :posts_count,
                        :created_at,
                        :views,
                        :reply_count,
                        :participant_count,
                        :like_count,
                        :last_posted_at,
                        :visible,
                        :closed,
                        :archived,
                        :has_summary,
                        :archetype,
                        :slug,
                        :category_id,
                        :word_count,
                        :deleted_at,
                        :pending_posts_count,
                        :user_id

  attributes :draft,
             :draft_key,
             :draft_sequence,
             :posted,
             :unpinned,
             :pinned_globally,
             :pinned,    # Is topic pinned and viewer hasn't cleared the pin?
             :pinned_at, # Ignores clear pin
             :pinned_until,
             :details,
             :highest_post_number,
             :last_read_post_number,
             :deleted_by,
             :has_deleted,
             :actions_summary,
             :expandable_first_post,
             :is_warning,
             :chunk_size,
             :bookmarked

  # TODO: Split off into proper object / serializer
  def details
    result = {
      auto_close_at: object.topic.auto_close_at,
      auto_close_hours: object.topic.auto_close_hours,
      auto_close_based_on_last_post: object.topic.auto_close_based_on_last_post,
      created_by: BasicUserSerializer.new(object.topic.user, scope: scope, root: false),
      last_poster: BasicUserSerializer.new(object.topic.last_poster, scope: scope, root: false)
    }

    if object.topic.private_message?
      result[:allowed_users] = object.topic.allowed_users.map do |user|
        BasicUserSerializer.new(user, scope: scope, root: false)
      end
    end

    if object.topic.private_message?
      result[:allowed_groups] = object.topic.allowed_groups.map do |ag|
        BasicGroupSerializer.new(ag, scope: scope, root: false)
      end
    end

    if object.post_counts_by_user.present?
      result[:participants] = object.post_counts_by_user.map do |pc|
        TopicPostCountSerializer.new({user: object.participants[pc[0]], post_count: pc[1]}, scope: scope, root: false)
      end
    end


    if object.suggested_topics.try(:topics).present?
      result[:suggested_topics] = object.suggested_topics.topics.map do |user|
        SuggestedTopicSerializer.new(user, scope: scope, root: false)
      end
    end

    if object.links.present?
      result[:links] = object.links.map do |user|
        TopicLinkSerializer.new(user, scope: scope, root: false)
      end
    end
{'fraction_non_alphanumeric': 0.08064516129032258, 'fraction_numerical': 0.0006144393241167435, 'mean_word_length': 1.7334172963895886, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '3506354', 'n_tokens_mistral': 1865, 'n_tokens_neox': 1764, 'n_words': 350}