initial_state_serializer.rb 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # frozen_string_literal: true
  2. class InitialStateSerializer < ActiveModel::Serializer
  3. include RoutingHelper
  4. attributes :meta, :compose, :accounts,
  5. :media_attachments, :settings,
  6. :max_toot_chars, :languages
  7. attribute :critical_updates_pending, if: -> { object&.role&.can?(:view_devops) && SoftwareUpdate.check_enabled? }
  8. has_one :push_subscription, serializer: REST::WebPushSubscriptionSerializer
  9. has_one :role, serializer: REST::RoleSerializer
  10. def max_toot_chars
  11. StatusLengthValidator::MAX_CHARS
  12. end
  13. def meta
  14. store = {
  15. streaming_api_base_url: Rails.configuration.x.streaming_api_base_url,
  16. access_token: object.token,
  17. locale: I18n.locale,
  18. domain: Addressable::IDNA.to_unicode(instance_presenter.domain),
  19. title: instance_presenter.title,
  20. admin: object.admin&.id&.to_s,
  21. search_enabled: Chewy.enabled?,
  22. repository: Mastodon::Version.repository,
  23. source_url: instance_presenter.source_url,
  24. version: instance_presenter.version,
  25. limited_federation_mode: Rails.configuration.x.limited_federation_mode,
  26. mascot: instance_presenter.mascot&.file&.url,
  27. profile_directory: Setting.profile_directory,
  28. trends_enabled: Setting.trends,
  29. registrations_open: Setting.registrations_mode != 'none' && !Rails.configuration.x.single_user_mode,
  30. timeline_preview: Setting.timeline_preview,
  31. activity_api_enabled: Setting.activity_api_enabled,
  32. single_user_mode: Rails.configuration.x.single_user_mode,
  33. trends_as_landing_page: Setting.trends_as_landing_page,
  34. status_page_url: Setting.status_page_url,
  35. sso_redirect: sso_redirect,
  36. }
  37. if object.current_account
  38. store[:me] = object.current_account.id.to_s
  39. store[:unfollow_modal] = object.current_account.user.setting_unfollow_modal
  40. store[:boost_modal] = object.current_account.user.setting_boost_modal
  41. store[:delete_modal] = object.current_account.user.setting_delete_modal
  42. store[:auto_play_gif] = object.current_account.user.setting_auto_play_gif
  43. store[:expand_usernames] = object.current_account.user.setting_expand_usernames
  44. store[:display_media] = object.current_account.user.setting_display_media
  45. store[:expand_spoilers] = object.current_account.user.setting_expand_spoilers
  46. store[:reduce_motion] = object.current_account.user.setting_reduce_motion
  47. store[:disable_swiping] = object.current_account.user.setting_disable_swiping
  48. store[:advanced_layout] = object.current_account.user.setting_advanced_layout
  49. store[:use_blurhash] = object.current_account.user.setting_use_blurhash
  50. store[:use_pending_items] = object.current_account.user.setting_use_pending_items
  51. store[:show_trends] = Setting.trends && object.current_account.user.setting_trends
  52. else
  53. store[:auto_play_gif] = Setting.auto_play_gif
  54. store[:display_media] = Setting.display_media
  55. store[:reduce_motion] = Setting.reduce_motion
  56. store[:use_blurhash] = Setting.use_blurhash
  57. end
  58. store[:disabled_account_id] = object.disabled_account.id.to_s if object.disabled_account
  59. store[:moved_to_account_id] = object.moved_to_account.id.to_s if object.moved_to_account
  60. store[:owner] = object.owner&.id&.to_s if Rails.configuration.x.single_user_mode
  61. store
  62. end
  63. def compose
  64. store = {}
  65. if object.current_account
  66. store[:me] = object.current_account.id.to_s
  67. store[:default_privacy] = object.visibility || object.current_account.user.setting_default_privacy
  68. store[:default_sensitive] = object.current_account.user.setting_default_sensitive
  69. store[:default_federation] = object.current_account.user.setting_default_federation
  70. store[:default_language] = object.current_account.user.preferred_posting_language
  71. end
  72. store[:text] = object.text if object.text
  73. store
  74. end
  75. def accounts
  76. store = {}
  77. ActiveRecord::Associations::Preloader.new(
  78. records: [object.current_account, object.admin, object.owner, object.disabled_account, object.moved_to_account].compact,
  79. associations: [:account_stat, { user: :role, moved_to_account: [:account_stat, { user: :role }] }]
  80. ).call
  81. store[object.current_account.id.to_s] = ActiveModelSerializers::SerializableResource.new(object.current_account, serializer: REST::AccountSerializer) if object.current_account
  82. store[object.admin.id.to_s] = ActiveModelSerializers::SerializableResource.new(object.admin, serializer: REST::AccountSerializer) if object.admin
  83. store[object.owner.id.to_s] = ActiveModelSerializers::SerializableResource.new(object.owner, serializer: REST::AccountSerializer) if object.owner
  84. store[object.disabled_account.id.to_s] = ActiveModelSerializers::SerializableResource.new(object.disabled_account, serializer: REST::AccountSerializer) if object.disabled_account
  85. store[object.moved_to_account.id.to_s] = ActiveModelSerializers::SerializableResource.new(object.moved_to_account, serializer: REST::AccountSerializer) if object.moved_to_account
  86. store
  87. end
  88. def media_attachments
  89. { accept_content_types: MediaAttachment.supported_file_extensions + MediaAttachment.supported_mime_types }
  90. end
  91. def languages
  92. LanguagesHelper::SUPPORTED_LOCALES.map { |(key, value)| [key, value[0], value[1]] }
  93. end
  94. private
  95. def instance_presenter
  96. @instance_presenter ||= InstancePresenter.new
  97. end
  98. def sso_redirect
  99. "/auth/auth/#{Devise.omniauth_providers[0]}" if ENV['ONE_CLICK_SSO_LOGIN'] == 'true' && ENV['OMNIAUTH_ONLY'] == 'true' && Devise.omniauth_providers.length == 1
  100. end
  101. end