instance_presenter.rb 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # frozen_string_literal: true
  2. class InstancePresenter < ActiveModelSerializers::Model
  3. attributes :domain, :title, :version, :source_url,
  4. :description, :languages, :rules, :contact
  5. class ContactPresenter < ActiveModelSerializers::Model
  6. attributes :email, :account
  7. def email
  8. Setting.site_contact_email
  9. end
  10. def account
  11. username, domain = Setting.site_contact_username.strip.gsub(/\A@/, '').split('@', 2)
  12. domain = nil if TagManager.instance.local_domain?(domain)
  13. Account.find_remote(username, domain) if username.present?
  14. end
  15. end
  16. def contact
  17. ContactPresenter.new
  18. end
  19. def closed_registrations_message
  20. Setting.closed_registrations_message
  21. end
  22. def description
  23. Setting.site_short_description
  24. end
  25. def extended_description
  26. Setting.site_extended_description
  27. end
  28. def privacy_policy
  29. Setting.site_terms
  30. end
  31. def domain
  32. Rails.configuration.x.local_domain
  33. end
  34. def title
  35. Setting.site_title
  36. end
  37. def languages
  38. [I18n.default_locale]
  39. end
  40. def rules
  41. Rule.ordered
  42. end
  43. def user_count
  44. Rails.cache.fetch('user_count') { User.confirmed.joins(:account).merge(Account.without_suspended).count }
  45. end
  46. def active_user_count(num_weeks = 4)
  47. Rails.cache.fetch("active_user_count/#{num_weeks}") { ActivityTracker.new('activity:logins', :unique).sum(num_weeks.weeks.ago) }
  48. end
  49. def status_count
  50. Rails.cache.fetch('local_status_count') { Account.local.joins(:account_stat).sum('account_stats.statuses_count') }.to_i
  51. end
  52. def domain_count
  53. Rails.cache.fetch('distinct_domain_count') { Instance.count }
  54. end
  55. def sample_accounts
  56. Rails.cache.fetch('sample_accounts', expires_in: 12.hours) { Account.local.discoverable.popular.limit(3) }
  57. end
  58. def version
  59. Mastodon::Version.to_s
  60. end
  61. def source_url
  62. Mastodon::Version.source_url
  63. end
  64. def thumbnail
  65. @thumbnail ||= Rails.cache.fetch('site_uploads/thumbnail') { SiteUpload.find_by(var: 'thumbnail') }
  66. end
  67. def mascot
  68. @mascot ||= Rails.cache.fetch('site_uploads/mascot') { SiteUpload.find_by(var: 'mascot') }
  69. end
  70. end