initial_state_serializer.rb 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # frozen_string_literal: true
  2. class InitialStateSerializer < ActiveModel::Serializer
  3. attributes :meta, :compose, :accounts,
  4. :media_attachments, :settings,
  5. :languages
  6. has_one :push_subscription, serializer: REST::WebPushSubscriptionSerializer
  7. def meta
  8. store = {
  9. streaming_api_base_url: Rails.configuration.x.streaming_api_base_url,
  10. access_token: object.token,
  11. locale: I18n.locale,
  12. domain: Rails.configuration.x.local_domain,
  13. title: instance_presenter.site_title,
  14. admin: object.admin&.id&.to_s,
  15. search_enabled: Chewy.enabled?,
  16. repository: Mastodon::Version.repository,
  17. source_url: Mastodon::Version.source_url,
  18. version: Mastodon::Version.to_s,
  19. invites_enabled: Setting.min_invite_role == 'user',
  20. limited_federation_mode: Rails.configuration.x.whitelist_mode,
  21. mascot: instance_presenter.mascot&.file&.url,
  22. profile_directory: Setting.profile_directory,
  23. trends: Setting.trends,
  24. }
  25. if object.current_account
  26. store[:me] = object.current_account.id.to_s
  27. store[:unfollow_modal] = object.current_account.user.setting_unfollow_modal
  28. store[:boost_modal] = object.current_account.user.setting_boost_modal
  29. store[:delete_modal] = object.current_account.user.setting_delete_modal
  30. store[:auto_play_gif] = object.current_account.user.setting_auto_play_gif
  31. store[:display_media] = object.current_account.user.setting_display_media
  32. store[:expand_spoilers] = object.current_account.user.setting_expand_spoilers
  33. store[:reduce_motion] = object.current_account.user.setting_reduce_motion
  34. store[:disable_swiping] = object.current_account.user.setting_disable_swiping
  35. store[:advanced_layout] = object.current_account.user.setting_advanced_layout
  36. store[:use_blurhash] = object.current_account.user.setting_use_blurhash
  37. store[:use_pending_items] = object.current_account.user.setting_use_pending_items
  38. store[:is_staff] = object.current_account.user.staff?
  39. store[:trends] = Setting.trends && object.current_account.user.setting_trends
  40. store[:crop_images] = object.current_account.user.setting_crop_images
  41. else
  42. store[:auto_play_gif] = Setting.auto_play_gif
  43. store[:display_media] = Setting.display_media
  44. store[:reduce_motion] = Setting.reduce_motion
  45. store[:use_blurhash] = Setting.use_blurhash
  46. store[:crop_images] = Setting.crop_images
  47. end
  48. store
  49. end
  50. def compose
  51. store = {}
  52. if object.current_account
  53. store[:me] = object.current_account.id.to_s
  54. store[:default_privacy] = object.visibility || object.current_account.user.setting_default_privacy
  55. store[:default_sensitive] = object.current_account.user.setting_default_sensitive
  56. store[:default_language] = object.current_account.user.preferred_posting_language
  57. end
  58. store[:text] = object.text if object.text
  59. store
  60. end
  61. def accounts
  62. store = {}
  63. store[object.current_account.id.to_s] = ActiveModelSerializers::SerializableResource.new(object.current_account, serializer: REST::AccountSerializer) if object.current_account
  64. store[object.admin.id.to_s] = ActiveModelSerializers::SerializableResource.new(object.admin, serializer: REST::AccountSerializer) if object.admin
  65. store
  66. end
  67. def media_attachments
  68. { accept_content_types: MediaAttachment.supported_file_extensions + MediaAttachment.supported_mime_types }
  69. end
  70. def languages
  71. LanguagesHelper::SUPPORTED_LOCALES.map { |(key, value)| [key, value[0], value[1]] }
  72. end
  73. private
  74. def instance_presenter
  75. @instance_presenter ||= InstancePresenter.new
  76. end
  77. end