Add emoji_reactions property to status api object

This commit is contained in:
KMY 2023-02-23 20:32:27 +09:00
parent 0ebf8b302d
commit 092f9916b0
7 changed files with 73 additions and 2 deletions

View file

@ -62,6 +62,7 @@ class Status < ApplicationRecord
belongs_to :reblog, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblogs, optional: true
has_many :favourites, inverse_of: :status, dependent: :destroy
has_many :emoji_reactions, inverse_of: :status, dependent: :destroy
has_many :bookmarks, inverse_of: :status, dependent: :destroy
has_many :reblogs, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblog, dependent: :destroy
has_many :reblogged_by_accounts, through: :reblogs, class_name: 'Account', source: :account
@ -288,6 +289,29 @@ class Status < ApplicationRecord
update_status_stat!(key => [public_send(key) - 1, 0].max)
end
def emoji_reactions_grouped_by_name(account = nil)
(Oj.load(status_stat&.emoji_reactions || '', mode: :strict) || []).tap do |emoji_reactions|
if account.present?
emoji_reactions.each do |emoji_reaction|
emoji_reaction['me'] = emoji_reaction['account_ids'].include?(account.id.to_s)
emoji_reaction['count'] = emoji_reaction['account_ids'].size
emoji_reaction['account_ids'] -= account.excluded_from_timeline_account_ids.map(&:to_s)
end
end
end
end
def generate_emoji_reactions_grouped_by_name
records = emoji_reactions.group(:name).order(Arel.sql('MIN(created_at) ASC')).select('name, min(custom_emoji_id) as custom_emoji_id, count(*) as count, array_agg(account_id::text order by created_at) as account_ids')
Oj.dump(ActiveModelSerializers::SerializableResource.new(records, each_serializer: REST::EmojiReactionsGroupedByNameSerializer, scope: nil, scope_name: :current_user))
end
def refresh_emoji_reactions_grouped_by_name!
generate_emoji_reactions_grouped_by_name.tap do |emoji_reactions|
update_status_stat!(emoji_reactions: emoji_reactions)
end
end
def trendable?
if attributes['trendable'].nil?
account.trendable?

View file

@ -9,6 +9,7 @@
# replies_count :bigint(8) default(0), not null
# reblogs_count :bigint(8) default(0), not null
# favourites_count :bigint(8) default(0), not null
# emoji_reactions :string
# created_at :datetime not null
# updated_at :datetime not null
#
@ -30,6 +31,10 @@ class StatusStat < ApplicationRecord
[attributes['favourites_count'], 0].max
end
def emoji_reactions
attributes['emoji_reactions'] || ''
end
private
def reset_parent_cache