initial_state_serializer.rb 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. # rubocop:disable Metrics/AbcSize
  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.whitelist_mode,
  23. mascot: instance_presenter.mascot&.file&.url,
  24. profile_directory: Setting.profile_directory,
  25. trends: 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. translation_enabled: TranslationService.configured?,
  31. trends_as_landing_page: Setting.trends_as_landing_page,
  32. status_page_url: Setting.status_page_url,
  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[:trends] = Setting.trends && object.current_account.user.setting_trends
  48. store[:crop_images] = object.current_account.user.setting_crop_images
  49. else
  50. store[:auto_play_gif] = Setting.auto_play_gif
  51. store[:display_media] = Setting.display_media
  52. store[:reduce_motion] = Setting.reduce_motion
  53. store[:use_blurhash] = Setting.use_blurhash
  54. store[:crop_images] = Setting.crop_images
  55. end
  56. store[:disabled_account_id] = object.disabled_account.id.to_s if object.disabled_account
  57. store[:moved_to_account_id] = object.moved_to_account.id.to_s if object.moved_to_account
  58. if Rails.configuration.x.single_user_mode
  59. store[:owner] = object.owner&.id&.to_s
  60. end
  61. store
  62. end
  63. # rubocop:enable Metrics/AbcSize
  64. def compose
  65. store = {}
  66. if object.current_account
  67. store[:me] = object.current_account.id.to_s
  68. store[:default_privacy] = object.visibility || object.current_account.user.setting_default_privacy
  69. store[:default_sensitive] = object.current_account.user.setting_default_sensitive
  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.preload([object.current_account, object.admin, object.owner, object.disabled_account, object.moved_to_account].compact, [:account_stat, :user, { moved_to_account: [:account_stat, :user] }])
  78. store[object.current_account.id.to_s] = ActiveModelSerializers::SerializableResource.new(object.current_account, serializer: REST::AccountSerializer) if object.current_account
  79. store[object.admin.id.to_s] = ActiveModelSerializers::SerializableResource.new(object.admin, serializer: REST::AccountSerializer) if object.admin
  80. store[object.owner.id.to_s] = ActiveModelSerializers::SerializableResource.new(object.owner, serializer: REST::AccountSerializer) if object.owner
  81. store[object.disabled_account.id.to_s] = ActiveModelSerializers::SerializableResource.new(object.disabled_account, serializer: REST::AccountSerializer) if object.disabled_account
  82. store[object.moved_to_account.id.to_s] = ActiveModelSerializers::SerializableResource.new(object.moved_to_account, serializer: REST::AccountSerializer) if object.moved_to_account
  83. store
  84. end
  85. def media_attachments
  86. { accept_content_types: MediaAttachment.supported_file_extensions + MediaAttachment.supported_mime_types }
  87. end
  88. def languages
  89. LanguagesHelper::SUPPORTED_LOCALES.map { |(key, value)| [key, value[0], value[1]] }
  90. end
  91. private
  92. def instance_presenter
  93. @instance_presenter ||= InstancePresenter.new
  94. end
  95. end