nas/app/serializers/rest/notification_group_serializer.rb
KMY(雪あすか) f14c2d3ada
Change: 絵文字リアクションの通知のグループ化で、アカウントを絵文字の種類ごとに表示 (#796)
* Change: 絵文字リアクションの通知のグループ化で、アカウントを絵文字の種類ごとに表示

* Fix lint

* アカウントの一括取得数を制限

* ストリーミング対応

* Fix

* Fix

* Fix

* Fix some problems

* Fix
2024-08-17 08:16:27 +09:00

74 lines
2.4 KiB
Ruby

# frozen_string_literal: true
class REST::NotificationGroupSerializer < ActiveModel::Serializer
# Please update app/javascript/api_types/notification.ts when making changes to the attributes
attributes :group_key, :notifications_count, :type, :most_recent_notification_id
attribute :page_min_id, if: :paginated?
attribute :page_max_id, if: :paginated?
attribute :latest_page_notification_at, if: :paginated?
attribute :sample_account_ids
attribute :status_id, if: :status_type?
belongs_to :report, if: :report_type?, serializer: REST::ReportSerializer
belongs_to :account_relationship_severance_event, key: :event, if: :relationship_severance_event?, serializer: REST::AccountRelationshipSeveranceEventSerializer
belongs_to :account_warning, key: :moderation_warning, if: :moderation_warning_event?, serializer: REST::AccountWarningSerializer
def sample_account_ids
object.sample_accounts.pluck(:id).map(&:to_s)
end
def status_id
object.target_status&.id&.to_s
end
def status_type?
[:favourite, :emoji_reaction, :reblog, :status, :mention, :status_reference, :poll, :update].include?(object.type)
end
def report_type?
object.type == :'admin.report'
end
def emoji_reaction_type?
object.type == :emoji_reaction
end
def relationship_severance_event?
object.type == :severed_relationships
end
def moderation_warning_event?
object.type == :moderation_warning
end
def page_min_id
range = instance_options[:group_metadata][object.group_key]
range.present? ? range[:min_id].to_s : object.notification.id.to_s
end
def page_max_id
range = instance_options[:group_metadata][object.group_key]
range.present? ? range[:max_id].to_s : object.notification.id.to_s
end
def latest_page_notification_at
range = instance_options[:group_metadata][object.group_key]
range.present? ? range[:latest_notification_at] : object.notification.created_at
end
def paginated?
!instance_options[:group_metadata].nil?
end
class NotificationEmojiReactionGroupSerializer < ActiveModel::Serializer
has_one :emoji_reaction, serializer: REST::NotifyEmojiReactionSerializer
attribute :sample_account_ids
def sample_account_ids
object.sample_accounts.pluck(:id).map(&:to_s)
end
end
has_many :emoji_reaction_groups, each_serializer: NotificationEmojiReactionGroupSerializer, if: :emoji_reaction_type?
end