initial_state_serializer.rb 5.2 KB

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