initial_state_serializer.rb 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. }
  32. if object.current_account
  33. store[:me] = object.current_account.id.to_s
  34. store[:unfollow_modal] = object.current_account.user.setting_unfollow_modal
  35. store[:boost_modal] = object.current_account.user.setting_boost_modal
  36. store[:delete_modal] = object.current_account.user.setting_delete_modal
  37. store[:auto_play_gif] = object.current_account.user.setting_auto_play_gif
  38. store[:display_media] = object.current_account.user.setting_display_media
  39. store[:expand_spoilers] = object.current_account.user.setting_expand_spoilers
  40. store[:reduce_motion] = object.current_account.user.setting_reduce_motion
  41. store[:disable_swiping] = object.current_account.user.setting_disable_swiping
  42. store[:advanced_layout] = object.current_account.user.setting_advanced_layout
  43. store[:use_blurhash] = object.current_account.user.setting_use_blurhash
  44. store[:use_pending_items] = object.current_account.user.setting_use_pending_items
  45. store[:trends] = Setting.trends && object.current_account.user.setting_trends
  46. store[:crop_images] = object.current_account.user.setting_crop_images
  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. store[:crop_images] = Setting.crop_images
  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. if Rails.configuration.x.single_user_mode
  57. store[:owner] = object.owner&.id&.to_s
  58. end
  59. store
  60. end
  61. # rubocop:enable Metrics/AbcSize
  62. def compose
  63. store = {}
  64. if object.current_account
  65. store[:me] = object.current_account.id.to_s
  66. store[:default_privacy] = object.visibility || object.current_account.user.setting_default_privacy
  67. store[:default_sensitive] = object.current_account.user.setting_default_sensitive
  68. store[:default_language] = object.current_account.user.preferred_posting_language
  69. end
  70. store[:text] = object.text if object.text
  71. store
  72. end
  73. def accounts
  74. store = {}
  75. 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] }])
  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. end