initial_state_serializer.rb 5.3 KB

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