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.

103 lines
2.3 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.
7 years ago
  1. # frozen_string_literal: true
  2. class REST::AccountSerializer < ActiveModel::Serializer
  3. include RoutingHelper
  4. attributes :id, :username, :acct, :display_name, :locked, :bot, :discoverable, :group, :created_at,
  5. :note, :url, :avatar, :avatar_static, :header, :header_static,
  6. :followers_count, :following_count, :statuses_count, :last_status_at
  7. has_one :moved_to_account, key: :moved, serializer: REST::AccountSerializer, if: :moved_and_not_nested?
  8. has_many :emojis, serializer: REST::CustomEmojiSerializer
  9. attribute :suspended, if: :suspended?
  10. class FieldSerializer < ActiveModel::Serializer
  11. attributes :name, :value, :verified_at
  12. def value
  13. Formatter.instance.format_field(object.account, object.value)
  14. end
  15. end
  16. has_many :fields
  17. def id
  18. object.id.to_s
  19. end
  20. def acct
  21. object.pretty_acct
  22. end
  23. def note
  24. object.suspended? ? '' : Formatter.instance.simplified_format(object)
  25. end
  26. def url
  27. ActivityPub::TagManager.instance.url_for(object)
  28. end
  29. def avatar
  30. full_asset_url(object.suspended? ? object.avatar.default_url : object.avatar_original_url)
  31. end
  32. def avatar_static
  33. full_asset_url(object.suspended? ? object.avatar.default_url : object.avatar_static_url)
  34. end
  35. def header
  36. full_asset_url(object.suspended? ? object.header.default_url : object.header_original_url)
  37. end
  38. def header_static
  39. full_asset_url(object.suspended? ? object.header.default_url : object.header_static_url)
  40. end
  41. def created_at
  42. object.created_at.midnight.as_json
  43. end
  44. def last_status_at
  45. object.last_status_at&.to_date&.iso8601
  46. end
  47. def display_name
  48. object.suspended? ? '' : object.display_name
  49. end
  50. def locked
  51. object.suspended? ? false : object.locked
  52. end
  53. def bot
  54. object.suspended? ? false : object.bot
  55. end
  56. def discoverable
  57. object.suspended? ? false : object.discoverable
  58. end
  59. def moved_to_account
  60. object.suspended? ? nil : object.moved_to_account
  61. end
  62. def emojis
  63. object.suspended? ? [] : object.emojis
  64. end
  65. def fields
  66. object.suspended? ? [] : object.fields
  67. end
  68. def suspended
  69. object.suspended?
  70. end
  71. delegate :suspended?, to: :object
  72. def moved_and_not_nested?
  73. object.moved? && object.moved_to_account.moved_to_account_id.nil?
  74. end
  75. end