instance_serializer.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # frozen_string_literal: true
  2. class REST::InstanceSerializer < ActiveModel::Serializer
  3. class ContactSerializer < ActiveModel::Serializer
  4. attributes :email
  5. has_one :account, serializer: REST::AccountSerializer
  6. end
  7. include InstanceHelper
  8. include RoutingHelper
  9. attributes :domain, :title, :version, :source_url, :description,
  10. :usage, :thumbnail, :icon, :languages, :configuration,
  11. :registrations, :api_versions
  12. has_one :contact, serializer: ContactSerializer
  13. has_many :rules, serializer: REST::RuleSerializer
  14. def thumbnail
  15. if object.thumbnail
  16. {
  17. url: full_asset_url(object.thumbnail.file.url(:'@1x')),
  18. blurhash: object.thumbnail.blurhash,
  19. versions: {
  20. '@1x': full_asset_url(object.thumbnail.file.url(:'@1x')),
  21. '@2x': full_asset_url(object.thumbnail.file.url(:'@2x')),
  22. },
  23. }
  24. else
  25. {
  26. url: frontend_asset_url('images/preview.png'),
  27. }
  28. end
  29. end
  30. def icon
  31. SiteUpload::ANDROID_ICON_SIZES.map do |size|
  32. src = app_icon_path(size.to_i)
  33. src = URI.join(root_url, src).to_s if src.present?
  34. {
  35. src: src || frontend_asset_url("icons/android-chrome-#{size}x#{size}.png"),
  36. size: "#{size}x#{size}",
  37. }
  38. end
  39. end
  40. def usage
  41. {
  42. users: {
  43. active_month: object.active_user_count(4),
  44. },
  45. }
  46. end
  47. def configuration
  48. {
  49. urls: {
  50. streaming: Rails.configuration.x.streaming_api_base_url,
  51. status: object.status_page_url,
  52. },
  53. vapid: {
  54. public_key: Rails.configuration.x.vapid_public_key,
  55. },
  56. accounts: {
  57. max_featured_tags: FeaturedTag::LIMIT,
  58. max_pinned_statuses: StatusPinValidator::PIN_LIMIT,
  59. },
  60. statuses: {
  61. max_characters: StatusLengthValidator::MAX_CHARS,
  62. max_media_attachments: Status::MEDIA_ATTACHMENTS_LIMIT,
  63. characters_reserved_per_url: StatusLengthValidator::URL_PLACEHOLDER_CHARS,
  64. },
  65. media_attachments: {
  66. supported_mime_types: MediaAttachment.supported_mime_types,
  67. image_size_limit: MediaAttachment::IMAGE_LIMIT,
  68. image_matrix_limit: Attachmentable::MAX_MATRIX_LIMIT,
  69. video_size_limit: MediaAttachment::VIDEO_LIMIT,
  70. video_frame_rate_limit: MediaAttachment::MAX_VIDEO_FRAME_RATE,
  71. video_matrix_limit: MediaAttachment::MAX_VIDEO_MATRIX_LIMIT,
  72. },
  73. polls: {
  74. max_options: PollValidator::MAX_OPTIONS,
  75. max_characters_per_option: PollValidator::MAX_OPTION_CHARS,
  76. min_expiration: PollValidator::MIN_EXPIRATION,
  77. max_expiration: PollValidator::MAX_EXPIRATION,
  78. },
  79. translation: {
  80. enabled: TranslationService.configured?,
  81. },
  82. }
  83. end
  84. def registrations
  85. {
  86. enabled: registrations_enabled?,
  87. approval_required: Setting.registrations_mode == 'approved',
  88. message: registrations_enabled? ? nil : registrations_message,
  89. url: ENV.fetch('SSO_ACCOUNT_SIGN_UP', nil),
  90. }
  91. end
  92. def api_versions
  93. Mastodon::Version.api_versions
  94. end
  95. private
  96. def registrations_enabled?
  97. Setting.registrations_mode != 'none' && !Rails.configuration.x.single_user_mode
  98. end
  99. def registrations_message
  100. markdown.render(Setting.closed_registrations_message) if Setting.closed_registrations_message.present?
  101. end
  102. def markdown
  103. @markdown ||= Redcarpet::Markdown.new(Redcarpet::Render::HTML, no_images: true)
  104. end
  105. end