2024-06-03 08:35:59 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class NotificationGroup < ActiveModelSerializers::Model
|
2024-06-14 10:33:06 +00:00
|
|
|
attributes :group_key, :sample_accounts, :notifications_count, :notification, :most_recent_notification_id
|
2024-06-03 08:35:59 +00:00
|
|
|
|
2024-07-18 14:36:09 +00:00
|
|
|
# Try to keep this consistent with `app/javascript/mastodon/models/notification_group.ts`
|
|
|
|
SAMPLE_ACCOUNTS_SIZE = 8
|
|
|
|
|
2024-08-29 12:39:07 +00:00
|
|
|
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
|
2024-07-18 14:36:09 +00:00
|
|
|
# TODO: caching, and, if caching, preloading
|
2024-06-20 15:54:50 +00:00
|
|
|
scope = notification.account.notifications.where(group_key: notification.group_key)
|
|
|
|
scope = scope.where(id: ..max_id) if max_id.present?
|
|
|
|
|
2024-07-18 14:36:09 +00:00
|
|
|
# Ideally, we would not load accounts for each notification group
|
|
|
|
most_recent_notifications = scope.order(id: :desc).includes(:from_account).take(SAMPLE_ACCOUNTS_SIZE)
|
2024-06-14 10:33:06 +00:00
|
|
|
most_recent_id = most_recent_notifications.first.id
|
|
|
|
sample_accounts = most_recent_notifications.map(&:from_account)
|
2024-06-20 15:54:50 +00:00
|
|
|
notifications_count = scope.count
|
2024-06-03 08:35:59 +00:00
|
|
|
else
|
2024-06-14 10:33:06 +00:00
|
|
|
most_recent_id = notification.id
|
2024-06-03 08:35:59 +00:00
|
|
|
sample_accounts = [notification.from_account]
|
|
|
|
notifications_count = 1
|
|
|
|
end
|
|
|
|
|
|
|
|
NotificationGroup.new(
|
|
|
|
notification: notification,
|
2024-08-29 12:39:07 +00:00
|
|
|
group_key: groupable ? notification.group_key : "ungrouped-#{notification.id}",
|
2024-06-03 08:35:59 +00:00
|
|
|
sample_accounts: sample_accounts,
|
2024-06-14 10:33:06 +00:00
|
|
|
notifications_count: notifications_count,
|
|
|
|
most_recent_notification_id: most_recent_id
|
2024-06-03 08:35:59 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
delegate :type,
|
|
|
|
:target_status,
|
|
|
|
:report,
|
|
|
|
:account_relationship_severance_event,
|
2024-06-20 13:44:49 +00:00
|
|
|
:account_warning,
|
2024-06-03 08:35:59 +00:00
|
|
|
to: :notification, prefix: false
|
|
|
|
end
|