From 43c9f0b5f28546c72a6c7f12fa704e51221bdef1 Mon Sep 17 00:00:00 2001 From: Claire Date: Tue, 30 Jul 2024 14:27:24 +0200 Subject: [PATCH] [WiP] Add presenter and serializer to make use of shallow serializers --- .../grouped_notifications_presenter.rb | 34 +++++++++++++++++++ .../dedup_notification_group_serializer.rb | 7 ++++ 2 files changed, 41 insertions(+) create mode 100644 app/presenters/grouped_notifications_presenter.rb create mode 100644 app/serializers/rest/dedup_notification_group_serializer.rb diff --git a/app/presenters/grouped_notifications_presenter.rb b/app/presenters/grouped_notifications_presenter.rb new file mode 100644 index 0000000000..f64d5df59b --- /dev/null +++ b/app/presenters/grouped_notifications_presenter.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +class GroupedNotificationsPresenter < ActiveModelSerializers::Model + def initialize(grouped_notifications) + super() + + @grouped_notifications = grouped_notifications + end + + def notification_groups + @grouped_notifications + end + + def statuses + @grouped_notifications.filter_map(&:target_status) + end + + def accounts + @grouped_notifications.flat_map do |group| + accounts = group.sample_accounts.dup + + case group.type + when :favourite, :reblog, :status, :mention, :poll, :update + accounts << group.target_status&.account + when :'admin.report' + accounts << group.report&.target_account + when :moderation_warning + accounts << group.account_warning&.target_account + end + + accounts.compact + end.uniq(&:id) + end +end diff --git a/app/serializers/rest/dedup_notification_group_serializer.rb b/app/serializers/rest/dedup_notification_group_serializer.rb new file mode 100644 index 0000000000..5491950c25 --- /dev/null +++ b/app/serializers/rest/dedup_notification_group_serializer.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class REST::DedupNotificationGroupSerializer < ActiveModel::Serializer + has_many :accounts, serializer: REST::Shallow::AccountSerializer + has_many :statuses, serializer: REST::Shallow::StatusSerializer + has_many :notification_groups, serializer: REST::Shallow::NotificationGroupSerializer +end