instance_serializer.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 RoutingHelper
  8. attributes :domain, :title, :version, :source_url, :description,
  9. :usage, :thumbnail, :languages, :configuration,
  10. :registrations
  11. has_one :contact, serializer: ContactSerializer
  12. has_many :rules, serializer: REST::RuleSerializer
  13. def thumbnail
  14. if object.thumbnail
  15. {
  16. url: full_asset_url(object.thumbnail.file.url(:'@1x')),
  17. blurhash: object.thumbnail.blurhash,
  18. versions: {
  19. '@1x': full_asset_url(object.thumbnail.file.url(:'@1x')),
  20. '@2x': full_asset_url(object.thumbnail.file.url(:'@2x')),
  21. },
  22. }
  23. else
  24. {
  25. url: full_pack_url('media/images/preview.png'),
  26. }
  27. end
  28. end
  29. def usage
  30. {
  31. users: {
  32. active_month: object.active_user_count(4),
  33. },
  34. }
  35. end
  36. def configuration
  37. {
  38. urls: {
  39. streaming: Rails.configuration.x.streaming_api_base_url,
  40. status: object.status_page_url,
  41. },
  42. accounts: {
  43. max_featured_tags: FeaturedTag::LIMIT,
  44. },
  45. statuses: {
  46. max_characters: StatusLengthValidator::MAX_CHARS,
  47. max_media_attachments: 4,
  48. characters_reserved_per_url: StatusLengthValidator::URL_PLACEHOLDER_CHARS,
  49. },
  50. media_attachments: {
  51. supported_mime_types: MediaAttachment::IMAGE_MIME_TYPES + MediaAttachment::VIDEO_MIME_TYPES + MediaAttachment::AUDIO_MIME_TYPES,
  52. image_size_limit: MediaAttachment::IMAGE_LIMIT,
  53. image_matrix_limit: Attachmentable::MAX_MATRIX_LIMIT,
  54. video_size_limit: MediaAttachment::VIDEO_LIMIT,
  55. video_frame_rate_limit: MediaAttachment::MAX_VIDEO_FRAME_RATE,
  56. video_matrix_limit: MediaAttachment::MAX_VIDEO_MATRIX_LIMIT,
  57. },
  58. polls: {
  59. max_options: PollValidator::MAX_OPTIONS,
  60. max_characters_per_option: PollValidator::MAX_OPTION_CHARS,
  61. min_expiration: PollValidator::MIN_EXPIRATION,
  62. max_expiration: PollValidator::MAX_EXPIRATION,
  63. },
  64. translation: {
  65. enabled: TranslationService.configured?,
  66. },
  67. }
  68. end
  69. def registrations
  70. {
  71. enabled: registrations_enabled?,
  72. approval_required: Setting.registrations_mode == 'approved',
  73. message: registrations_enabled? ? nil : registrations_message,
  74. }
  75. end
  76. private
  77. def registrations_enabled?
  78. Setting.registrations_mode != 'none' && !Rails.configuration.x.single_user_mode
  79. end
  80. def registrations_message
  81. if Setting.closed_registrations_message.present?
  82. markdown.render(Setting.closed_registrations_message)
  83. end
  84. end
  85. def markdown
  86. @markdown ||= Redcarpet::Markdown.new(Redcarpet::Render::HTML, no_images: true)
  87. end
  88. end