Change: 絵文字リアクションの通知のグループ化で、アカウントを絵文字の種類ごとに表示 (#796)
* Change: 絵文字リアクションの通知のグループ化で、アカウントを絵文字の種類ごとに表示 * Fix lint * アカウントの一括取得数を制限 * ストリーミング対応 * Fix * Fix * Fix * Fix some problems * Fix
This commit is contained in:
parent
5dec110dec
commit
f14c2d3ada
10 changed files with 258 additions and 7 deletions
|
@ -1,10 +1,15 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class NotificationGroup < ActiveModelSerializers::Model
|
||||
attributes :group_key, :sample_accounts, :notifications_count, :notification, :most_recent_notification_id
|
||||
attributes :group_key, :sample_accounts, :notifications_count, :notification, :most_recent_notification_id, :emoji_reaction_groups
|
||||
|
||||
# Try to keep this consistent with `app/javascript/mastodon/models/notification_group.ts`
|
||||
SAMPLE_ACCOUNTS_SIZE = 8
|
||||
SAMPLE_ACCOUNTS_SIZE_FOR_EMOJI_REACTION = 40
|
||||
|
||||
class NotificationEmojiReactionGroup < ActiveModelSerializers::Model
|
||||
attributes :emoji_reaction, :sample_accounts
|
||||
end
|
||||
|
||||
def self.from_notification(notification, max_id: nil)
|
||||
if notification.group_key.present?
|
||||
|
@ -16,10 +21,14 @@ class NotificationGroup < ActiveModelSerializers::Model
|
|||
most_recent_notifications = scope.order(id: :desc).includes(:from_account).take(SAMPLE_ACCOUNTS_SIZE)
|
||||
most_recent_id = most_recent_notifications.first.id
|
||||
sample_accounts = most_recent_notifications.map(&:from_account)
|
||||
emoji_reaction_groups = extract_emoji_reaction_pair(
|
||||
scope.order(id: :desc).includes(emoji_reaction: :account).take(SAMPLE_ACCOUNTS_SIZE_FOR_EMOJI_REACTION)
|
||||
)
|
||||
notifications_count = scope.count
|
||||
else
|
||||
most_recent_id = notification.id
|
||||
sample_accounts = [notification.from_account]
|
||||
emoji_reaction_groups = extract_emoji_reaction_pair([notification])
|
||||
notifications_count = 1
|
||||
end
|
||||
|
||||
|
@ -27,6 +36,7 @@ class NotificationGroup < ActiveModelSerializers::Model
|
|||
notification: notification,
|
||||
group_key: notification.group_key || "ungrouped-#{notification.id}",
|
||||
sample_accounts: sample_accounts,
|
||||
emoji_reaction_groups: emoji_reaction_groups,
|
||||
notifications_count: notifications_count,
|
||||
most_recent_notification_id: most_recent_id
|
||||
)
|
||||
|
@ -38,4 +48,16 @@ class NotificationGroup < ActiveModelSerializers::Model
|
|||
:account_relationship_severance_event,
|
||||
:account_warning,
|
||||
to: :notification, prefix: false
|
||||
|
||||
def self.extract_emoji_reaction_pair(scope)
|
||||
scope = scope.filter { |g| g.emoji_reaction.present? }
|
||||
|
||||
return [] if scope.empty?
|
||||
return [] unless scope.first.type == :emoji_reaction
|
||||
|
||||
scope
|
||||
.each_with_object({}) { |e, h| h[e.emoji_reaction.name] = (h[e.emoji_reaction.name] || []).push(e.emoji_reaction) }
|
||||
.to_a
|
||||
.map { |pair| NotificationEmojiReactionGroup.new(emoji_reaction: pair[1].first, sample_accounts: pair[1].take(SAMPLE_ACCOUNTS_SIZE).map(&:account)) }
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue