Add emoji reactions bar in embedded status

This commit is contained in:
KMY 2023-04-04 16:12:37 +09:00
parent 9cee344e99
commit 67bee91eed
5 changed files with 35 additions and 4 deletions

View file

@ -245,4 +245,8 @@ module ApplicationHelper
def prerender_custom_emojis(html, custom_emojis, other_options = {}) def prerender_custom_emojis(html, custom_emojis, other_options = {})
EmojiFormatter.new(html, custom_emojis, other_options.merge(animate: prefers_autoplay?)).to_s EmojiFormatter.new(html, custom_emojis, other_options.merge(animate: prefers_autoplay?)).to_s
end end
def prerender_custom_emojis_from_hash(html, custom_emojis_hash)
prerender_custom_emojis(html, JSON.parse([custom_emojis_hash].to_json, object_class: OpenStruct))
end
end end

View file

@ -18,6 +18,10 @@ module FormattingHelper
html_aware_format(status.text, status.local?, preloaded_accounts: [status.account] + (status.respond_to?(:active_mentions) ? status.active_mentions.map(&:account) : [])) html_aware_format(status.text, status.local?, preloaded_accounts: [status.account] + (status.respond_to?(:active_mentions) ? status.active_mentions.map(&:account) : []))
end end
def emoji_name_format(emoji_reaction, status)
html_aware_format(emoji_reaction['domain'].nil? ? (emoji_reaction['url'].present? ? ":#{emoji_reaction['name']}:" : emoji_reaction['name']) : ":#{emoji_reaction['name']}@#{emoji_reaction['domain']}:", status.local?)
end
def rss_status_content_format(status) def rss_status_content_format(status)
html = status_content_format(status) html = status_content_format(status)
@ -45,7 +49,7 @@ module FormattingHelper
prerender_custom_emojis( prerender_custom_emojis(
safe_join([before_html, html, after_html]), safe_join([before_html, html, after_html]),
status.emojis, status.emojis,
style: 'width: 1.1em; height: 1.1em; object-fit: contain; vertical-align: middle; margin: -.2ex .15em .2ex' style: 'min-width: 1.1em; height: 1.1em; object-fit: contain; vertical-align: middle; margin: -.2ex .15em .2ex'
).to_str ).to_str
end end

View file

@ -62,7 +62,12 @@ class EmojiFormatter
private private
def emoji_map def emoji_map
@emoji_map ||= custom_emojis.each_with_object({}) { |e, h| h[e.shortcode] = [full_asset_url(e.image.url), full_asset_url(e.image.url(:static))] } # from emoji_reactions_grouped_by_name (status_stat)
if !custom_emojis.first&.image.present?
return @emoji_map ||= custom_emojis.each_with_object({}) { |e, h| h[e.name] = [e.url, e.static_url] }
end
return @emoji_map ||= custom_emojis.each_with_object({}) { |e, h| h[e.shortcode] = [full_asset_url(e.image.url), full_asset_url(e.image.url(:static))] }
end end
def count_tag_nesting(tag) def count_tag_nesting(tag)
@ -85,7 +90,7 @@ class EmojiFormatter
end end
def image_attributes def image_attributes
{ rel: 'emoji', draggable: false, width: 16, height: 16, class: image_class_names, style: image_style } { rel: 'emoji', draggable: false, height: 16, class: image_class_names, style: image_style }
end end
def image_data_attributes(original_url, static_url) def image_data_attributes(original_url, static_url)
@ -97,7 +102,7 @@ class EmojiFormatter
end end
def image_style def image_style
@options[:style] 'min-width:16px;' + (@options[:style] || '')
end end
def animate? def animate?

View file

@ -1,3 +1,6 @@
:ruby
grouped_emoji_reactions ||= Oj.load(status.status_stat&.emoji_reactions || '', mode: :strict) || []
.detailed-status.detailed-status--flex{ class: "detailed-status-#{status.visibility}" } .detailed-status.detailed-status--flex{ class: "detailed-status-#{status.visibility}" }
.p-author.h-card .p-author.h-card
= link_to ActivityPub::TagManager.instance.url_for(status.account), class: 'detailed-status__display-name u-url', target: stream_link_target, rel: 'noopener' do = link_to ActivityPub::TagManager.instance.url_for(status.account), class: 'detailed-status__display-name u-url', target: stream_link_target, rel: 'noopener' do
@ -36,6 +39,13 @@
- elsif status.preview_card - elsif status.preview_card
= render_card_component(status) = render_card_component(status)
- if grouped_emoji_reactions.size > 0
.status__emoji-reactions-bar
- grouped_emoji_reactions.each do |reaction|
%button.emoji-reactions-bar__button
%span.emojify= prerender_custom_emojis_from_hash(emoji_name_format(reaction, status), reaction)
%span.count= reaction.count
.detailed-status__meta .detailed-status__meta
%data.dt-published{ value: status.created_at.to_time.iso8601 } %data.dt-published{ value: status.created_at.to_time.iso8601 }
- if status.edited? - if status.edited?

View file

@ -1,5 +1,6 @@
:ruby :ruby
hide_show_thread ||= false hide_show_thread ||= false
grouped_emoji_reactions ||= Oj.load(status.status_stat&.emoji_reactions || '', mode: :strict) || []
.status{ class: "status-#{status.visibility}" } .status{ class: "status-#{status.visibility}" }
.status__info .status__info
@ -52,6 +53,13 @@
= link_to ActivityPub::TagManager.instance.url_for(status), class: 'status__content__read-more-button', target: stream_link_target, rel: 'noopener noreferrer' do = link_to ActivityPub::TagManager.instance.url_for(status), class: 'status__content__read-more-button', target: stream_link_target, rel: 'noopener noreferrer' do
= t 'statuses.show_thread' = t 'statuses.show_thread'
- if grouped_emoji_reactions.size > 0
.status__emoji-reactions-bar
- grouped_emoji_reactions.each do |reaction|
%button.emoji-reactions-bar__button
%span.emojify= prerender_custom_emojis_from_hash(emoji_name_format(reaction, status), reaction)
%span.count= reaction.count
.status__action-bar .status__action-bar
%span.status__action-bar-button.icon-button.icon-button--with-counter %span.status__action-bar-button.icon-button.icon-button--with-counter
- if status.in_reply_to_id.nil? - if status.in_reply_to_id.nil?