initial_state_serializer.rb 5.2 KB

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