initial_state_serializer.rb 3.3 KB

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