You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
5.7 KiB

Change IDs to strings rather than numbers in API JSON output (#5019) * Fix JavaScript interface with long IDs Somewhat predictably, the JS interface handled IDs as numbers, which in JS are IEEE double-precision floats. This loses some precision when working with numbers as large as those generated by the new ID scheme, so we instead handle them here as strings. This is relatively simple, and doesn't appear to have caused any problems, but should definitely be tested more thoroughly than the built-in tests. Several days of use appear to support this working properly. BREAKING CHANGE: The major(!) change here is that IDs are now returned as strings by the REST endpoints, rather than as integers. In practice, relatively few changes were required to make the existing JS UI work with this change, but it will likely hit API clients pretty hard: it's an entirely different type to consume. (The one API client I tested, Tusky, handles this with no problems, however.) Twitter ran into this issue when introducing Snowflake IDs, and decided to instead introduce an `id_str` field in JSON responses. I have opted to *not* do that, and instead force all IDs to 64-bit integers represented by strings in one go. (I believe Twitter exacerbated their problem by rolling out the changes three times: once for statuses, once for DMs, and once for user IDs, as well as by leaving an integer ID value in JSON. As they said, "If you’re using the `id` field with JSON in a Javascript-related language, there is a very high likelihood that the integers will be silently munged by Javascript interpreters. In most cases, this will result in behavior such as being unable to load or delete a specific direct message, because the ID you're sending to the API is different than the actual identifier associated with the message." [1]) However, given that this is a significant change for API users, alternatives or a transition time may be appropriate. 1: https://blog.twitter.com/developer/en_us/a/2011/direct-messages-going-snowflake-on-sep-30-2011.html * Additional fixes for stringified IDs in JSON These should be the last two. These were identified using eslint to try to identify any plain casts to JavaScript numbers. (Some such casts are legitimate, but these were not.) Adding the following to .eslintrc.yml will identify casts to numbers: ~~~ no-restricted-syntax: - warn - selector: UnaryExpression[operator='+'] > :not(Literal) message: Avoid the use of unary + - selector: CallExpression[callee.name='Number'] message: Casting with Number() may coerce string IDs to numbers ~~~ The remaining three casts appear legitimate: two casts to array indices, one in a server to turn an environment variable into a number. * Back out RelationshipsController Change This was made to make a test a bit less flakey, but has nothing to do with this branch. * Change internal streaming payloads to stringified IDs as well Per https://github.com/tootsuite/mastodon/pull/5019#issuecomment-330736452 we need these changes to send deleted status IDs as strings, not integers.
6 years ago
Change IDs to strings rather than numbers in API JSON output (#5019) * Fix JavaScript interface with long IDs Somewhat predictably, the JS interface handled IDs as numbers, which in JS are IEEE double-precision floats. This loses some precision when working with numbers as large as those generated by the new ID scheme, so we instead handle them here as strings. This is relatively simple, and doesn't appear to have caused any problems, but should definitely be tested more thoroughly than the built-in tests. Several days of use appear to support this working properly. BREAKING CHANGE: The major(!) change here is that IDs are now returned as strings by the REST endpoints, rather than as integers. In practice, relatively few changes were required to make the existing JS UI work with this change, but it will likely hit API clients pretty hard: it's an entirely different type to consume. (The one API client I tested, Tusky, handles this with no problems, however.) Twitter ran into this issue when introducing Snowflake IDs, and decided to instead introduce an `id_str` field in JSON responses. I have opted to *not* do that, and instead force all IDs to 64-bit integers represented by strings in one go. (I believe Twitter exacerbated their problem by rolling out the changes three times: once for statuses, once for DMs, and once for user IDs, as well as by leaving an integer ID value in JSON. As they said, "If you’re using the `id` field with JSON in a Javascript-related language, there is a very high likelihood that the integers will be silently munged by Javascript interpreters. In most cases, this will result in behavior such as being unable to load or delete a specific direct message, because the ID you're sending to the API is different than the actual identifier associated with the message." [1]) However, given that this is a significant change for API users, alternatives or a transition time may be appropriate. 1: https://blog.twitter.com/developer/en_us/a/2011/direct-messages-going-snowflake-on-sep-30-2011.html * Additional fixes for stringified IDs in JSON These should be the last two. These were identified using eslint to try to identify any plain casts to JavaScript numbers. (Some such casts are legitimate, but these were not.) Adding the following to .eslintrc.yml will identify casts to numbers: ~~~ no-restricted-syntax: - warn - selector: UnaryExpression[operator='+'] > :not(Literal) message: Avoid the use of unary + - selector: CallExpression[callee.name='Number'] message: Casting with Number() may coerce string IDs to numbers ~~~ The remaining three casts appear legitimate: two casts to array indices, one in a server to turn an environment variable into a number. * Back out RelationshipsController Change This was made to make a test a bit less flakey, but has nothing to do with this branch. * Change internal streaming payloads to stringified IDs as well Per https://github.com/tootsuite/mastodon/pull/5019#issuecomment-330736452 we need these changes to send deleted status IDs as strings, not integers.
6 years ago
  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, :poll_limits,
  7. :languages
  8. has_one :push_subscription, serializer: REST::WebPushSubscriptionSerializer
  9. has_one :role, serializer: REST::RoleSerializer
  10. def max_toot_chars
  11. StatusLengthValidator::MAX_CHARS
  12. end
  13. def poll_limits
  14. {
  15. max_options: PollValidator::MAX_OPTIONS,
  16. max_option_chars: PollValidator::MAX_OPTION_CHARS,
  17. min_expiration: PollValidator::MIN_EXPIRATION,
  18. max_expiration: PollValidator::MAX_EXPIRATION,
  19. }
  20. end
  21. def meta
  22. store = {
  23. streaming_api_base_url: Rails.configuration.x.streaming_api_base_url,
  24. access_token: object.token,
  25. locale: I18n.locale,
  26. domain: Addressable::IDNA.to_unicode(instance_presenter.domain),
  27. title: instance_presenter.title,
  28. admin: object.admin&.id&.to_s,
  29. search_enabled: Chewy.enabled?,
  30. repository: Mastodon::Version.repository,
  31. source_url: instance_presenter.source_url,
  32. version: instance_presenter.version,
  33. limited_federation_mode: Rails.configuration.x.whitelist_mode,
  34. mascot: instance_presenter.mascot&.file&.url,
  35. profile_directory: Setting.profile_directory,
  36. trends: Setting.trends,
  37. registrations_open: Setting.registrations_mode != 'none' && !Rails.configuration.x.single_user_mode,
  38. timeline_preview: Setting.timeline_preview,
  39. activity_api_enabled: Setting.activity_api_enabled,
  40. single_user_mode: Rails.configuration.x.single_user_mode,
  41. trends_as_landing_page: Setting.trends_as_landing_page,
  42. status_page_url: Setting.status_page_url,
  43. news_bot_id: Rails.configuration.x.news_bot_id,
  44. }
  45. if object.current_account
  46. store[:me] = object.current_account.id.to_s
  47. store[:unfollow_modal] = object.current_account.user.setting_unfollow_modal
  48. store[:boost_modal] = object.current_account.user.setting_boost_modal
  49. store[:favourite_modal] = object.current_account.user.setting_favourite_modal
  50. store[:delete_modal] = object.current_account.user.setting_delete_modal
  51. store[:auto_play_gif] = object.current_account.user.setting_auto_play_gif
  52. store[:display_media] = object.current_account.user.setting_display_media
  53. store[:expand_spoilers] = object.current_account.user.setting_expand_spoilers
  54. store[:reduce_motion] = object.current_account.user.setting_reduce_motion
  55. store[:disable_swiping] = object.current_account.user.setting_disable_swiping
  56. store[:advanced_layout] = object.current_account.user.setting_advanced_layout
  57. store[:use_blurhash] = object.current_account.user.setting_use_blurhash
  58. store[:use_pending_items] = object.current_account.user.setting_use_pending_items
  59. store[:trends] = Setting.trends && object.current_account.user.setting_trends
  60. store[:default_content_type] = object.current_account.user.setting_default_content_type
  61. store[:system_emoji_font] = object.current_account.user.setting_system_emoji_font
  62. store[:crop_images] = object.current_account.user.setting_crop_images
  63. else
  64. store[:auto_play_gif] = Setting.auto_play_gif
  65. store[:display_media] = Setting.display_media
  66. store[:reduce_motion] = Setting.reduce_motion
  67. store[:use_blurhash] = Setting.use_blurhash
  68. store[:crop_images] = Setting.crop_images
  69. end
  70. store[:disabled_account_id] = object.disabled_account.id.to_s if object.disabled_account
  71. store[:moved_to_account_id] = object.moved_to_account.id.to_s if object.moved_to_account
  72. store[:owner] = object.owner&.id&.to_s if Rails.configuration.x.single_user_mode
  73. store
  74. end
  75. def compose
  76. store = {}
  77. if object.current_account
  78. store[:me] = object.current_account.id.to_s
  79. store[:default_privacy] = object.visibility || object.current_account.user.setting_default_privacy
  80. store[:default_sensitive] = object.current_account.user.setting_default_sensitive
  81. store[:default_language] = object.current_account.user.preferred_posting_language
  82. end
  83. store[:text] = object.text if object.text
  84. store
  85. end
  86. def accounts
  87. store = {}
  88. 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] }])
  89. store[object.current_account.id.to_s] = ActiveModelSerializers::SerializableResource.new(object.current_account, serializer: REST::AccountSerializer) if object.current_account
  90. store[object.admin.id.to_s] = ActiveModelSerializers::SerializableResource.new(object.admin, serializer: REST::AccountSerializer) if object.admin
  91. store[object.owner.id.to_s] = ActiveModelSerializers::SerializableResource.new(object.owner, serializer: REST::AccountSerializer) if object.owner
  92. store[object.disabled_account.id.to_s] = ActiveModelSerializers::SerializableResource.new(object.disabled_account, serializer: REST::AccountSerializer) if object.disabled_account
  93. store[object.moved_to_account.id.to_s] = ActiveModelSerializers::SerializableResource.new(object.moved_to_account, serializer: REST::AccountSerializer) if object.moved_to_account
  94. store
  95. end
  96. def media_attachments
  97. { accept_content_types: MediaAttachment.supported_file_extensions + MediaAttachment.supported_mime_types }
  98. end
  99. def languages
  100. LanguagesHelper::SUPPORTED_LOCALES.map { |(key, value)| [key, value[0], value[1]] }
  101. end
  102. private
  103. def instance_presenter
  104. @instance_presenter ||= InstancePresenter.new
  105. end
  106. end