instance_serializer.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. vapid: {
  43. public_key: Rails.configuration.x.vapid_public_key,
  44. },
  45. accounts: {
  46. max_featured_tags: FeaturedTag::LIMIT,
  47. },
  48. statuses: {
  49. max_characters: StatusLengthValidator::MAX_CHARS,
  50. max_media_attachments: 4,
  51. characters_reserved_per_url: StatusLengthValidator::URL_PLACEHOLDER_CHARS,
  52. },
  53. media_attachments: {
  54. supported_mime_types: MediaAttachment::IMAGE_MIME_TYPES + MediaAttachment::VIDEO_MIME_TYPES + MediaAttachment::AUDIO_MIME_TYPES,
  55. image_size_limit: MediaAttachment::IMAGE_LIMIT,
  56. image_matrix_limit: Attachmentable::MAX_MATRIX_LIMIT,
  57. video_size_limit: MediaAttachment::VIDEO_LIMIT,
  58. video_frame_rate_limit: MediaAttachment::MAX_VIDEO_FRAME_RATE,
  59. video_matrix_limit: MediaAttachment::MAX_VIDEO_MATRIX_LIMIT,
  60. },
  61. polls: {
  62. max_options: PollValidator::MAX_OPTIONS,
  63. max_characters_per_option: PollValidator::MAX_OPTION_CHARS,
  64. min_expiration: PollValidator::MIN_EXPIRATION,
  65. max_expiration: PollValidator::MAX_EXPIRATION,
  66. },
  67. translation: {
  68. enabled: TranslationService.configured?,
  69. },
  70. }
  71. end
  72. def registrations
  73. {
  74. enabled: registrations_enabled?,
  75. approval_required: Setting.registrations_mode == 'approved',
  76. message: registrations_enabled? ? nil : registrations_message,
  77. url: ENV.fetch('SSO_ACCOUNT_SIGN_UP', nil),
  78. }
  79. end
  80. private
  81. def registrations_enabled?
  82. Setting.registrations_mode != 'none' && !Rails.configuration.x.single_user_mode
  83. end
  84. def registrations_message
  85. markdown.render(Setting.closed_registrations_message) if Setting.closed_registrations_message.present?
  86. end
  87. def markdown
  88. @markdown ||= Redcarpet::Markdown.new(Redcarpet::Render::HTML, no_images: true)
  89. end
  90. end