2017-07-07 11:02:06 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class REST::AccountSerializer < ActiveModel::Serializer
|
|
|
|
include RoutingHelper
|
2022-03-26 10:53:34 +09:00
|
|
|
include FormattingHelper
|
2017-07-07 11:02:06 +09:00
|
|
|
|
2023-09-26 18:25:01 +09:00
|
|
|
# Please update `app/javascript/mastodon/api_types/accounts.ts` when making changes to the attributes
|
|
|
|
|
2023-11-15 20:13:53 +09:00
|
|
|
attributes :id, :username, :acct, :display_name, :locked, :bot, :discoverable, :indexable, :group, :created_at,
|
2023-07-27 23:11:17 +09:00
|
|
|
:note, :url, :uri, :avatar, :avatar_static, :header, :header_static,
|
2023-11-09 21:58:02 +09:00
|
|
|
:followers_count, :following_count, :statuses_count, :last_status_at, :hide_collections
|
2017-07-07 11:02:06 +09:00
|
|
|
|
2017-12-15 05:35:30 +09:00
|
|
|
has_one :moved_to_account, key: :moved, serializer: REST::AccountSerializer, if: :moved_and_not_nested?
|
2020-09-11 22:16:29 +09:00
|
|
|
|
2018-05-06 18:48:51 +09:00
|
|
|
has_many :emojis, serializer: REST::CustomEmojiSerializer
|
2017-11-19 03:39:02 +09:00
|
|
|
|
2020-09-11 22:16:29 +09:00
|
|
|
attribute :suspended, if: :suspended?
|
2022-05-10 16:44:35 +09:00
|
|
|
attribute :silenced, key: :limited, if: :silenced?
|
2022-10-20 21:35:29 +09:00
|
|
|
attribute :noindex, if: :local?
|
2020-09-11 22:16:29 +09:00
|
|
|
|
2023-02-14 21:50:55 +09:00
|
|
|
attribute :memorial, if: :memorial?
|
|
|
|
|
2023-01-19 00:25:31 +09:00
|
|
|
class AccountDecorator < SimpleDelegator
|
|
|
|
def self.model_name
|
|
|
|
Account.model_name
|
|
|
|
end
|
|
|
|
|
|
|
|
def moved?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-26 03:55:40 +09:00
|
|
|
class RoleSerializer < ActiveModel::Serializer
|
|
|
|
attributes :id, :name, :color
|
|
|
|
|
|
|
|
def id
|
|
|
|
object.id.to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
has_many :roles, serializer: RoleSerializer, if: :local?
|
|
|
|
|
2018-04-14 19:41:08 +09:00
|
|
|
class FieldSerializer < ActiveModel::Serializer
|
2022-03-26 10:53:34 +09:00
|
|
|
include FormattingHelper
|
|
|
|
|
2018-10-04 22:47:03 +09:00
|
|
|
attributes :name, :value, :verified_at
|
2018-09-18 23:45:58 +09:00
|
|
|
|
2018-04-14 19:41:08 +09:00
|
|
|
def value
|
2022-03-28 08:17:17 +09:00
|
|
|
account_field_value_format(object)
|
2018-04-14 19:41:08 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
has_many :fields
|
|
|
|
|
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.
2017-09-20 21:53:48 +09:00
|
|
|
def id
|
|
|
|
object.id.to_s
|
|
|
|
end
|
|
|
|
|
2019-12-31 03:20:43 +09:00
|
|
|
def acct
|
|
|
|
object.pretty_acct
|
|
|
|
end
|
|
|
|
|
2017-07-07 11:02:06 +09:00
|
|
|
def note
|
2023-12-01 00:43:26 +09:00
|
|
|
object.unavailable? ? '' : account_bio_format(object)
|
2017-07-07 11:02:06 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
def url
|
2019-07-07 23:16:51 +09:00
|
|
|
ActivityPub::TagManager.instance.url_for(object)
|
2017-07-07 11:02:06 +09:00
|
|
|
end
|
|
|
|
|
2023-07-27 23:11:17 +09:00
|
|
|
def uri
|
|
|
|
ActivityPub::TagManager.instance.uri_for(object)
|
|
|
|
end
|
|
|
|
|
2017-07-07 11:02:06 +09:00
|
|
|
def avatar
|
2023-12-01 00:43:26 +09:00
|
|
|
full_asset_url(object.unavailable? ? object.avatar.default_url : object.avatar_original_url)
|
2017-07-07 11:02:06 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
def avatar_static
|
2023-12-01 00:43:26 +09:00
|
|
|
full_asset_url(object.unavailable? ? object.avatar.default_url : object.avatar_static_url)
|
2017-07-07 11:02:06 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
def header
|
2023-12-01 00:43:26 +09:00
|
|
|
full_asset_url(object.unavailable? ? object.header.default_url : object.header_original_url)
|
2017-07-07 11:02:06 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
def header_static
|
2023-12-01 00:43:26 +09:00
|
|
|
full_asset_url(object.unavailable? ? object.header.default_url : object.header_static_url)
|
2017-12-15 05:35:30 +09:00
|
|
|
end
|
2018-12-18 04:36:30 +09:00
|
|
|
|
2021-05-07 21:33:19 +09:00
|
|
|
def created_at
|
2021-05-09 17:39:13 +09:00
|
|
|
object.created_at.midnight.as_json
|
2021-05-07 21:33:19 +09:00
|
|
|
end
|
|
|
|
|
2020-01-27 07:13:48 +09:00
|
|
|
def last_status_at
|
|
|
|
object.last_status_at&.to_date&.iso8601
|
|
|
|
end
|
2020-01-27 23:46:50 +09:00
|
|
|
|
2018-12-18 04:36:30 +09:00
|
|
|
def followers_count
|
2023-04-09 18:25:30 +09:00
|
|
|
Setting.hide_followers_count || object.user&.setting_hide_followers_count ? -1 : object.followers_count
|
2018-12-18 04:36:30 +09:00
|
|
|
end
|
2020-09-28 21:13:30 +09:00
|
|
|
|
2020-09-11 22:16:29 +09:00
|
|
|
def display_name
|
2023-12-01 00:43:26 +09:00
|
|
|
object.unavailable? ? '' : object.display_name
|
2020-09-11 22:16:29 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
def locked
|
2023-12-01 00:43:26 +09:00
|
|
|
object.unavailable? ? false : object.locked
|
2020-09-11 22:16:29 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
def bot
|
2023-12-01 00:43:26 +09:00
|
|
|
object.unavailable? ? false : object.bot
|
2020-09-11 22:16:29 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
def discoverable
|
2023-12-01 00:43:26 +09:00
|
|
|
object.unavailable? ? false : object.discoverable
|
2020-09-11 22:16:29 +09:00
|
|
|
end
|
|
|
|
|
2023-11-15 20:13:53 +09:00
|
|
|
def indexable
|
2023-12-01 00:43:26 +09:00
|
|
|
object.unavailable? ? false : object.indexable
|
2023-11-15 20:13:53 +09:00
|
|
|
end
|
|
|
|
|
2020-09-11 22:16:29 +09:00
|
|
|
def moved_to_account
|
2023-12-01 00:43:26 +09:00
|
|
|
object.unavailable? ? nil : AccountDecorator.new(object.moved_to_account)
|
2020-09-11 22:16:29 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
def emojis
|
2023-12-01 00:43:26 +09:00
|
|
|
object.unavailable? ? [] : object.emojis
|
2020-09-11 22:16:29 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
def fields
|
2023-12-01 00:43:26 +09:00
|
|
|
object.unavailable? ? [] : object.fields
|
2020-09-11 22:16:29 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
def suspended
|
2023-12-01 00:43:26 +09:00
|
|
|
object.unavailable?
|
2020-09-11 22:16:29 +09:00
|
|
|
end
|
|
|
|
|
2022-05-10 16:44:35 +09:00
|
|
|
def silenced
|
|
|
|
object.silenced?
|
|
|
|
end
|
|
|
|
|
2023-02-14 21:50:55 +09:00
|
|
|
def memorial
|
|
|
|
object.memorial?
|
|
|
|
end
|
|
|
|
|
2023-01-26 03:55:40 +09:00
|
|
|
def roles
|
2023-12-01 00:43:26 +09:00
|
|
|
if object.unavailable? || object.user.nil?
|
2023-01-26 03:55:40 +09:00
|
|
|
[]
|
|
|
|
else
|
2023-02-07 09:03:26 +09:00
|
|
|
[object.user.role].compact.filter(&:highlighted?)
|
2023-01-26 03:55:40 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-10-20 21:35:29 +09:00
|
|
|
def noindex
|
|
|
|
object.user_prefers_noindex?
|
|
|
|
end
|
|
|
|
|
2023-02-14 21:50:55 +09:00
|
|
|
delegate :suspended?, :silenced?, :local?, :memorial?, to: :object
|
2020-09-11 22:16:29 +09:00
|
|
|
|
|
|
|
def moved_and_not_nested?
|
2023-01-19 00:25:31 +09:00
|
|
|
object.moved?
|
2020-09-11 22:16:29 +09:00
|
|
|
end
|
2017-07-07 11:02:06 +09:00
|
|
|
end
|