diff options
Diffstat (limited to 'app/serializers/rest')
-rw-r--r-- | app/serializers/rest/account_serializer.rb | 7 | ||||
-rw-r--r-- | app/serializers/rest/admin/account_serializer.rb | 8 | ||||
-rw-r--r-- | app/serializers/rest/admin/canonical_email_block_serializer.rb | 9 | ||||
-rw-r--r-- | app/serializers/rest/admin/email_domain_block_serializer.rb | 9 | ||||
-rw-r--r-- | app/serializers/rest/admin/ip_block_serializer.rb | 14 | ||||
-rw-r--r-- | app/serializers/rest/domain_block_serializer.rb | 17 | ||||
-rw-r--r-- | app/serializers/rest/extended_description_serializer.rb | 23 | ||||
-rw-r--r-- | app/serializers/rest/featured_tag_serializer.rb | 8 | ||||
-rw-r--r-- | app/serializers/rest/instance_serializer.rb | 124 | ||||
-rw-r--r-- | app/serializers/rest/privacy_policy_serializer.rb | 19 | ||||
-rw-r--r-- | app/serializers/rest/relationship_serializer.rb | 7 | ||||
-rw-r--r-- | app/serializers/rest/report_serializer.rb | 8 | ||||
-rw-r--r-- | app/serializers/rest/translation_serializer.rb | 9 | ||||
-rw-r--r-- | app/serializers/rest/v1/instance_serializer.rb | 119 |
14 files changed, 310 insertions, 71 deletions
diff --git a/app/serializers/rest/account_serializer.rb b/app/serializers/rest/account_serializer.rb index e644a3f91..9a3ca75dc 100644 --- a/app/serializers/rest/account_serializer.rb +++ b/app/serializers/rest/account_serializer.rb @@ -14,6 +14,7 @@ class REST::AccountSerializer < ActiveModel::Serializer attribute :suspended, if: :suspended? attribute :silenced, key: :limited, if: :silenced? + attribute :noindex, if: :local? class FieldSerializer < ActiveModel::Serializer include FormattingHelper @@ -107,7 +108,11 @@ class REST::AccountSerializer < ActiveModel::Serializer object.silenced? end - delegate :suspended?, :silenced?, to: :object + def noindex + object.user_prefers_noindex? + end + + delegate :suspended?, :silenced?, :local?, to: :object def moved_and_not_nested? object.moved? && object.moved_to_account.moved_to_account_id.nil? diff --git a/app/serializers/rest/admin/account_serializer.rb b/app/serializers/rest/admin/account_serializer.rb index 3480e8c5a..ad98a53e8 100644 --- a/app/serializers/rest/admin/account_serializer.rb +++ b/app/serializers/rest/admin/account_serializer.rb @@ -3,7 +3,7 @@ class REST::Admin::AccountSerializer < ActiveModel::Serializer attributes :id, :username, :domain, :created_at, :email, :ip, :role, :confirmed, :suspended, - :silenced, :disabled, :approved, :locale, + :silenced, :sensitized, :disabled, :approved, :locale, :invite_request attribute :created_by_application_id, if: :created_by_application? @@ -32,6 +32,10 @@ class REST::Admin::AccountSerializer < ActiveModel::Serializer object.silenced? end + def sensitized + object.sensitized? + end + def confirmed object.user_confirmed? end @@ -77,6 +81,6 @@ class REST::Admin::AccountSerializer < ActiveModel::Serializer end def ip - ips&.first + ips&.first&.ip end end diff --git a/app/serializers/rest/admin/canonical_email_block_serializer.rb b/app/serializers/rest/admin/canonical_email_block_serializer.rb new file mode 100644 index 000000000..fe385940a --- /dev/null +++ b/app/serializers/rest/admin/canonical_email_block_serializer.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class REST::Admin::CanonicalEmailBlockSerializer < ActiveModel::Serializer + attributes :id, :canonical_email_hash + + def id + object.id.to_s + end +end diff --git a/app/serializers/rest/admin/email_domain_block_serializer.rb b/app/serializers/rest/admin/email_domain_block_serializer.rb new file mode 100644 index 000000000..a026ff680 --- /dev/null +++ b/app/serializers/rest/admin/email_domain_block_serializer.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class REST::Admin::EmailDomainBlockSerializer < ActiveModel::Serializer + attributes :id, :domain, :created_at, :history + + def id + object.id.to_s + end +end diff --git a/app/serializers/rest/admin/ip_block_serializer.rb b/app/serializers/rest/admin/ip_block_serializer.rb new file mode 100644 index 000000000..6a38f8b56 --- /dev/null +++ b/app/serializers/rest/admin/ip_block_serializer.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +class REST::Admin::IpBlockSerializer < ActiveModel::Serializer + attributes :id, :ip, :severity, :comment, + :created_at, :expires_at + + def id + object.id.to_s + end + + def ip + "#{object.ip}/#{object.ip.prefix}" + end +end diff --git a/app/serializers/rest/domain_block_serializer.rb b/app/serializers/rest/domain_block_serializer.rb new file mode 100644 index 000000000..678463e13 --- /dev/null +++ b/app/serializers/rest/domain_block_serializer.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class REST::DomainBlockSerializer < ActiveModel::Serializer + attributes :domain, :digest, :severity, :comment + + def domain + object.public_domain + end + + def digest + object.domain_digest + end + + def comment + object.public_comment if instance_options[:with_comment] + end +end diff --git a/app/serializers/rest/extended_description_serializer.rb b/app/serializers/rest/extended_description_serializer.rb new file mode 100644 index 000000000..c0fa3450d --- /dev/null +++ b/app/serializers/rest/extended_description_serializer.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +class REST::ExtendedDescriptionSerializer < ActiveModel::Serializer + attributes :updated_at, :content + + def updated_at + object.updated_at&.iso8601 + end + + def content + if object.text.present? + markdown.render(object.text) + else + '' + end + end + + private + + def markdown + @markdown ||= Redcarpet::Markdown.new(Redcarpet::Render::HTML) + end +end diff --git a/app/serializers/rest/featured_tag_serializer.rb b/app/serializers/rest/featured_tag_serializer.rb index 8abcd9b90..c4b35ab03 100644 --- a/app/serializers/rest/featured_tag_serializer.rb +++ b/app/serializers/rest/featured_tag_serializer.rb @@ -16,4 +16,12 @@ class REST::FeaturedTagSerializer < ActiveModel::Serializer def name object.display_name end + + def statuses_count + object.statuses_count.to_s + end + + def last_status_at + object.last_status_at&.to_date&.iso8601 + end end diff --git a/app/serializers/rest/instance_serializer.rb b/app/serializers/rest/instance_serializer.rb index 575c6214e..5ae1099d0 100644 --- a/app/serializers/rest/instance_serializer.rb +++ b/app/serializers/rest/instance_serializer.rb @@ -1,74 +1,56 @@ # frozen_string_literal: true class REST::InstanceSerializer < ActiveModel::Serializer - include RoutingHelper - - attributes :uri, :title, :short_description, :description, :email, - :version, :urls, :stats, :thumbnail, :max_toot_chars, :poll_limits, - :languages, :registrations, :approval_required, :invites_enabled, - :configuration - - has_one :contact_account, serializer: REST::AccountSerializer - - has_many :rules, serializer: REST::RuleSerializer - - delegate :contact_account, :rules, to: :instance_presenter - - def uri - Rails.configuration.x.local_domain - end - - def title - Setting.site_title - end + class ContactSerializer < ActiveModel::Serializer + attributes :email - def short_description - Setting.site_short_description + has_one :account, serializer: REST::AccountSerializer end - def description - Setting.site_description - end + include RoutingHelper - def email - Setting.site_contact_email - end + attributes :domain, :title, :version, :source_url, :description, + :usage, :thumbnail, :languages, :configuration, + :registrations - def version - Mastodon::Version.to_s - end + has_one :contact, serializer: ContactSerializer + has_many :rules, serializer: REST::RuleSerializer def thumbnail - instance_presenter.thumbnail ? full_asset_url(instance_presenter.thumbnail.file.url) : full_pack_url('media/images/preview.png') - end - - def max_toot_chars - StatusLengthValidator::MAX_CHARS - end - - def poll_limits + if object.thumbnail + { + url: full_asset_url(object.thumbnail.file.url(:'@1x')), + blurhash: object.thumbnail.blurhash, + versions: { + '@1x': full_asset_url(object.thumbnail.file.url(:'@1x')), + '@2x': full_asset_url(object.thumbnail.file.url(:'@2x')), + }, + } + else + { + url: full_pack_url('media/images/preview.png'), + } + end + end + + def usage { - max_options: PollValidator::MAX_OPTIONS, - max_option_chars: PollValidator::MAX_OPTION_CHARS, - min_expiration: PollValidator::MIN_EXPIRATION, - max_expiration: PollValidator::MAX_EXPIRATION, + users: { + active_month: object.active_user_count(4), + }, } end - def stats + def configuration { - user_count: instance_presenter.user_count, - status_count: instance_presenter.status_count, - domain_count: instance_presenter.domain_count, - } - end + urls: { + streaming: Rails.configuration.x.streaming_api_base_url, + }, - def urls - { streaming_api: Rails.configuration.x.streaming_api_base_url } - end + accounts: { + max_featured_tags: FeaturedTag::LIMIT, + }, - def configuration - { statuses: { max_characters: StatusLengthValidator::MAX_CHARS, max_media_attachments: 4, @@ -90,28 +72,36 @@ class REST::InstanceSerializer < ActiveModel::Serializer min_expiration: PollValidator::MIN_EXPIRATION, max_expiration: PollValidator::MAX_EXPIRATION, }, - } - end - def languages - [I18n.default_locale] + translation: { + enabled: TranslationService.configured?, + }, + } end def registrations - Setting.registrations_mode != 'none' && !Rails.configuration.x.single_user_mode + { + enabled: registrations_enabled?, + approval_required: Setting.registrations_mode == 'approved', + message: registrations_enabled? ? nil : registrations_message, + } end - def approval_required - Setting.registrations_mode == 'approved' - end + private - def invites_enabled - UserRole.everyone.can?(:invite_users) + def registrations_enabled? + Setting.registrations_mode != 'none' && !Rails.configuration.x.single_user_mode end - private + def registrations_message + if Setting.closed_registrations_message.present? + markdown.render(Setting.closed_registrations_message) + else + nil + end + end - def instance_presenter - @instance_presenter ||= InstancePresenter.new + def markdown + @markdown ||= Redcarpet::Markdown.new(Redcarpet::Render::HTML, no_images: true) end end diff --git a/app/serializers/rest/privacy_policy_serializer.rb b/app/serializers/rest/privacy_policy_serializer.rb new file mode 100644 index 000000000..f0572e714 --- /dev/null +++ b/app/serializers/rest/privacy_policy_serializer.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class REST::PrivacyPolicySerializer < ActiveModel::Serializer + attributes :updated_at, :content + + def updated_at + object.updated_at.iso8601 + end + + def content + markdown.render(object.text % { domain: Rails.configuration.x.local_domain }) + end + + private + + def markdown + @markdown ||= Redcarpet::Markdown.new(Redcarpet::Render::HTML, escape_html: true, no_images: true) + end +end diff --git a/app/serializers/rest/relationship_serializer.rb b/app/serializers/rest/relationship_serializer.rb index afd4cddf9..31fc60eb2 100644 --- a/app/serializers/rest/relationship_serializer.rb +++ b/app/serializers/rest/relationship_serializer.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class REST::RelationshipSerializer < ActiveModel::Serializer - attributes :id, :following, :showing_reblogs, :notifying, :followed_by, + attributes :id, :following, :showing_reblogs, :notifying, :languages, :followed_by, :blocking, :blocked_by, :muting, :muting_notifications, :requested, :domain_blocking, :endorsed, :note @@ -25,6 +25,11 @@ class REST::RelationshipSerializer < ActiveModel::Serializer false end + def languages + (instance_options[:relationships].following[object.id] || {})[:languages] || + (instance_options[:relationships].requested[object.id] || {})[:languages] + end + def followed_by instance_options[:relationships].followed_by[object.id] || false end diff --git a/app/serializers/rest/report_serializer.rb b/app/serializers/rest/report_serializer.rb index de68dfc6d..f4e9af249 100644 --- a/app/serializers/rest/report_serializer.rb +++ b/app/serializers/rest/report_serializer.rb @@ -9,4 +9,12 @@ class REST::ReportSerializer < ActiveModel::Serializer def id object.id.to_s end + + def status_ids + object&.status_ids&.map(&:to_s) + end + + def rule_ids + object&.rule_ids&.map(&:to_s) + end end diff --git a/app/serializers/rest/translation_serializer.rb b/app/serializers/rest/translation_serializer.rb new file mode 100644 index 000000000..05ededc95 --- /dev/null +++ b/app/serializers/rest/translation_serializer.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class REST::TranslationSerializer < ActiveModel::Serializer + attributes :content, :detected_source_language, :provider + + def content + object.text + end +end diff --git a/app/serializers/rest/v1/instance_serializer.rb b/app/serializers/rest/v1/instance_serializer.rb new file mode 100644 index 000000000..389ec7dff --- /dev/null +++ b/app/serializers/rest/v1/instance_serializer.rb @@ -0,0 +1,119 @@ +# frozen_string_literal: true + +class REST::V1::InstanceSerializer < ActiveModel::Serializer + include RoutingHelper + + attributes :uri, :title, :short_description, :description, :email, + :version, :urls, :stats, :thumbnail, :max_toot_chars, :poll_limits, + :languages, :registrations, :approval_required, :invites_enabled, + :configuration + + has_one :contact_account, serializer: REST::AccountSerializer + + has_many :rules, serializer: REST::RuleSerializer + + def uri + object.domain + end + + def short_description + object.description + end + + def description + Setting.site_description # Legacy + end + + def email + object.contact.email + end + + def contact_account + object.contact.account + end + + def thumbnail + instance_presenter.thumbnail ? full_asset_url(instance_presenter.thumbnail.file.url(:'@1x')) : full_pack_url('media/images/preview.png') + end + + def max_toot_chars + StatusLengthValidator::MAX_CHARS + end + + def poll_limits + { + max_options: PollValidator::MAX_OPTIONS, + max_option_chars: PollValidator::MAX_OPTION_CHARS, + min_expiration: PollValidator::MIN_EXPIRATION, + max_expiration: PollValidator::MAX_EXPIRATION, + } + end + + def stats + { + user_count: instance_presenter.user_count, + status_count: instance_presenter.status_count, + domain_count: instance_presenter.domain_count, + } + end + + def urls + { streaming_api: Rails.configuration.x.streaming_api_base_url } + end + + def usage + { + users: { + active_month: instance_presenter.active_user_count(4), + }, + } + end + + def configuration + { + accounts: { + max_featured_tags: FeaturedTag::LIMIT, + }, + + statuses: { + max_characters: StatusLengthValidator::MAX_CHARS, + max_media_attachments: 4, + characters_reserved_per_url: StatusLengthValidator::URL_PLACEHOLDER_CHARS, + }, + + media_attachments: { + supported_mime_types: MediaAttachment::IMAGE_MIME_TYPES + MediaAttachment::VIDEO_MIME_TYPES + MediaAttachment::AUDIO_MIME_TYPES, + image_size_limit: MediaAttachment::IMAGE_LIMIT, + image_matrix_limit: Attachmentable::MAX_MATRIX_LIMIT, + video_size_limit: MediaAttachment::VIDEO_LIMIT, + video_frame_rate_limit: MediaAttachment::MAX_VIDEO_FRAME_RATE, + video_matrix_limit: MediaAttachment::MAX_VIDEO_MATRIX_LIMIT, + }, + + polls: { + max_options: PollValidator::MAX_OPTIONS, + max_characters_per_option: PollValidator::MAX_OPTION_CHARS, + min_expiration: PollValidator::MIN_EXPIRATION, + max_expiration: PollValidator::MAX_EXPIRATION, + }, + } + end + + def registrations + Setting.registrations_mode != 'none' && !Rails.configuration.x.single_user_mode + end + + def approval_required + Setting.registrations_mode == 'approved' + end + + def invites_enabled + UserRole.everyone.can?(:invite_users) + end + + private + + def instance_presenter + @instance_presenter ||= InstancePresenter.new + end +end |