initial_state_serializer.rb 5.4 KB

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