node_info_presenter.rb 2.4 KB
Newer Older
Jonne Haß's avatar
Jonne Haß committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
class NodeInfoPresenter
  delegate :as_json, :content_type, to: :document

  def initialize(version)
    @version = version
  end

  def document
    @document ||= NodeInfo.build do |doc|
      doc.version = @version

      add_static_data doc
      add_configuration doc
      add_user_counts doc.usage.users
      add_usage doc.usage
    end
  end

  def add_configuration(doc)
20 21 22 23 24 25
    doc.software.version         = version
    doc.services.outbound        = available_services
    doc.open_registrations       = open_registrations?
    doc.metadata["nodeName"]     = name
    doc.metadata["xmppChat"]     = chat_enabled?
    doc.metadata["adminAccount"] = admin_account
Jonne Haß's avatar
Jonne Haß committed
26 27 28 29
  end

  def add_static_data(doc)
    doc.software.name = "diaspora"
Benjamin Neff's avatar
Benjamin Neff committed
30
    doc.protocols.protocols << "diaspora"
Jonne Haß's avatar
Jonne Haß committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
  end

  def add_user_counts(doc)
    return unless expose_user_counts?

    doc.total           = total_users
    doc.active_halfyear = halfyear_users
    doc.active_month    = monthly_users
  end

  def add_usage(doc)
    doc.local_posts    = local_posts    if expose_posts_counts?
    doc.local_comments = local_comments if expose_comment_counts?
  end

  def expose_user_counts?
    AppConfig.privacy.statistics.user_counts?
  end

  def expose_posts_counts?
    AppConfig.privacy.statistics.post_counts?
  end

  def expose_comment_counts?
    AppConfig.privacy.statistics.comment_counts?
  end

  def name
    AppConfig.settings.pod_name
  end

  def version
    AppConfig.version_string
  end

  def open_registrations?
    AppConfig.settings.enable_registrations?
  end

70 71 72 73
  def chat_enabled?
    AppConfig.chat.enabled?
  end

74 75 76 77
  def admin_account
    AppConfig.admins.account if AppConfig.admins.account?
  end

Jonne Haß's avatar
Jonne Haß committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
  def available_services
    Configuration::KNOWN_SERVICES.select {|service|
      AppConfig.show_service?(service, nil)
    }.map(&:to_s)
  end

  def total_users
    @total_users ||= User.active.count
  end

  def monthly_users
    @monthly_users ||= User.monthly_actives.count
  end

  def halfyear_users
    @halfyear_users ||= User.halfyear_actives.count
  end

  def local_posts
    @local_posts ||= Post.where(type: "StatusMessage")
                         .joins(:author)
                         .where("owner_id IS NOT null")
                         .count
  end

  def local_comments
    @local_comments ||= Comment.joins(:author)
                               .where("owner_id IS NOT null")
                               .count
  end
end