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.

150 lines
3.0 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
  1. # frozen_string_literal: true
  2. class REST::AccountSerializer < ActiveModel::Serializer
  3. include RoutingHelper
  4. include FormattingHelper
  5. attributes :id, :username, :acct, :display_name, :locked, :bot, :discoverable, :group, :created_at,
  6. :note, :url, :avatar, :avatar_static, :header, :header_static,
  7. :followers_count, :following_count, :statuses_count, :last_status_at
  8. has_one :moved_to_account, key: :moved, serializer: REST::AccountSerializer, if: :moved_and_not_nested?
  9. has_many :emojis, serializer: REST::CustomEmojiSerializer
  10. attribute :suspended, if: :suspended?
  11. attribute :silenced, key: :limited, if: :silenced?
  12. attribute :noindex, if: :local?
  13. attribute :memorial, if: :memorial?
  14. class AccountDecorator < SimpleDelegator
  15. def self.model_name
  16. Account.model_name
  17. end
  18. def moved?
  19. false
  20. end
  21. end
  22. class RoleSerializer < ActiveModel::Serializer
  23. attributes :id, :name, :color
  24. def id
  25. object.id.to_s
  26. end
  27. end
  28. has_many :roles, serializer: RoleSerializer, if: :local?
  29. class FieldSerializer < ActiveModel::Serializer
  30. include FormattingHelper
  31. attributes :name, :value, :verified_at
  32. def value
  33. account_field_value_format(object)
  34. end
  35. end
  36. has_many :fields
  37. def id
  38. object.id.to_s
  39. end
  40. def acct
  41. object.pretty_acct
  42. end
  43. def note
  44. object.suspended? ? '' : account_bio_format(object)
  45. end
  46. def url
  47. ActivityPub::TagManager.instance.url_for(object)
  48. end
  49. def avatar
  50. full_asset_url(object.suspended? ? object.avatar.default_url : object.avatar_original_url)
  51. end
  52. def avatar_static
  53. full_asset_url(object.suspended? ? object.avatar.default_url : object.avatar_static_url)
  54. end
  55. def header
  56. full_asset_url(object.suspended? ? object.header.default_url : object.header_original_url)
  57. end
  58. def header_static
  59. full_asset_url(object.suspended? ? object.header.default_url : object.header_static_url)
  60. end
  61. def created_at
  62. object.created_at.midnight.as_json
  63. end
  64. def last_status_at
  65. object.last_status_at&.to_date&.iso8601
  66. end
  67. def display_name
  68. object.suspended? ? '' : object.display_name
  69. end
  70. def locked
  71. object.suspended? ? false : object.locked
  72. end
  73. def bot
  74. object.suspended? ? false : object.bot
  75. end
  76. def discoverable
  77. object.suspended? ? false : object.discoverable
  78. end
  79. def moved_to_account
  80. object.suspended? ? nil : AccountDecorator.new(object.moved_to_account)
  81. end
  82. def emojis
  83. object.suspended? ? [] : object.emojis
  84. end
  85. def fields
  86. object.suspended? ? [] : object.fields
  87. end
  88. def suspended
  89. object.suspended?
  90. end
  91. def silenced
  92. object.silenced?
  93. end
  94. def memorial
  95. object.memorial?
  96. end
  97. def roles
  98. if object.suspended? || object.user.nil?
  99. []
  100. else
  101. [object.user.role].compact.filter(&:highlighted?)
  102. end
  103. end
  104. def noindex
  105. object.user_prefers_noindex?
  106. end
  107. delegate :suspended?, :silenced?, :local?, :memorial?, to: :object
  108. def moved_and_not_nested?
  109. object.moved?
  110. end
  111. end