Add grouped_types parameter to allow clients to restrict which notifications types get grouped (#31594)

This commit is contained in:
Claire 2024-08-29 14:39:07 +02:00 committed by GitHub
parent 662f87dbe9
commit ad0a28a8bf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 90 additions and 19 deletions

View file

@ -6,8 +6,11 @@ class NotificationGroup < ActiveModelSerializers::Model
# Try to keep this consistent with `app/javascript/mastodon/models/notification_group.ts`
SAMPLE_ACCOUNTS_SIZE = 8
def self.from_notification(notification, max_id: nil)
if notification.group_key.present?
def self.from_notification(notification, max_id: nil, grouped_types: nil)
grouped_types = grouped_types.presence&.map(&:to_sym) || Notification::GROUPABLE_NOTIFICATION_TYPES
groupable = notification.group_key.present? && grouped_types.include?(notification.type)
if groupable
# TODO: caching, and, if caching, preloading
scope = notification.account.notifications.where(group_key: notification.group_key)
scope = scope.where(id: ..max_id) if max_id.present?
@ -25,7 +28,7 @@ class NotificationGroup < ActiveModelSerializers::Model
NotificationGroup.new(
notification: notification,
group_key: notification.group_key || "ungrouped-#{notification.id}",
group_key: groupable ? notification.group_key : "ungrouped-#{notification.id}",
sample_accounts: sample_accounts,
notifications_count: notifications_count,
most_recent_notification_id: most_recent_id