From 2f013abc5991245bbab1f3665021267f4198780f Mon Sep 17 00:00:00 2001 From: Emelia Smith Date: Tue, 14 May 2024 20:12:11 +0200 Subject: [PATCH] WIP --- app/presenters/instance_presenter.rb | 9 ++++++++- app/serializers/rest/instance_serializer.rb | 1 + app/serializers/rest/report_category_serializer.rb | 9 +++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 app/serializers/rest/report_category_serializer.rb diff --git a/app/presenters/instance_presenter.rb b/app/presenters/instance_presenter.rb index 25df4d85aa4..d93a6d66c20 100644 --- a/app/presenters/instance_presenter.rb +++ b/app/presenters/instance_presenter.rb @@ -2,7 +2,7 @@ class InstancePresenter < ActiveModelSerializers::Model attributes :domain, :title, :version, :source_url, - :description, :languages, :rules, :contact + :description, :languages, :rules, :report_categories, :contact class ContactPresenter < ActiveModelSerializers::Model attributes :email, :account @@ -50,6 +50,13 @@ class InstancePresenter < ActiveModelSerializers::Model Rule.ordered end + def report_categories + # We only return the `violation` category if we have rules to violate: + Report.categories.filter_map do |category, _value| + { name: category } unless category == 'violation' && Rule.count.zero? + end + end + def user_count Rails.cache.fetch('user_count') { User.confirmed.joins(:account).merge(Account.without_suspended).count } end diff --git a/app/serializers/rest/instance_serializer.rb b/app/serializers/rest/instance_serializer.rb index 42b73f4387c..78761c77166 100644 --- a/app/serializers/rest/instance_serializer.rb +++ b/app/serializers/rest/instance_serializer.rb @@ -15,6 +15,7 @@ class REST::InstanceSerializer < ActiveModel::Serializer has_one :contact, serializer: ContactSerializer has_many :rules, serializer: REST::RuleSerializer + has_many :report_categories, serializer: REST::ReportCategorySerializer def thumbnail if object.thumbnail diff --git a/app/serializers/rest/report_category_serializer.rb b/app/serializers/rest/report_category_serializer.rb new file mode 100644 index 00000000000..9256b6e3382 --- /dev/null +++ b/app/serializers/rest/report_category_serializer.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class REST::ReportCategorySerializer < ActiveModel::Serializer + attributes :name + + def name + object[:name] + end +end