statuses_helper.rb 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. # frozen_string_literal: true
  2. module StatusesHelper
  3. EMBEDDED_CONTROLLER = 'statuses'
  4. EMBEDDED_ACTION = 'embed'
  5. def link_to_newer(url)
  6. link_to t('statuses.show_newer'), url, class: 'load-more load-gap'
  7. end
  8. def link_to_older(url)
  9. link_to t('statuses.show_older'), url, class: 'load-more load-gap'
  10. end
  11. def nothing_here(extra_classes = '')
  12. content_tag(:div, class: "nothing-here #{extra_classes}") do
  13. t('accounts.nothing_here')
  14. end
  15. end
  16. def media_summary(status)
  17. attachments = { image: 0, video: 0, audio: 0 }
  18. status.media_attachments.each do |media|
  19. if media.video?
  20. attachments[:video] += 1
  21. elsif media.audio?
  22. attachments[:audio] += 1
  23. else
  24. attachments[:image] += 1
  25. end
  26. end
  27. text = attachments.to_a.reject { |_, value| value.zero? }.map { |key, value| I18n.t("statuses.attached.#{key}", count: value) }.join(' · ')
  28. return if text.blank?
  29. I18n.t('statuses.attached.description', attached: text)
  30. end
  31. def status_text_summary(status)
  32. return if status.spoiler_text.blank?
  33. I18n.t('statuses.content_warning', warning: status.spoiler_text)
  34. end
  35. def poll_summary(status)
  36. return unless status.preloadable_poll
  37. status.preloadable_poll.options.map { |o| "[ ] #{o}" }.join("\n")
  38. end
  39. def status_description(status)
  40. components = [[media_summary(status), status_text_summary(status)].reject(&:blank?).join(' · ')]
  41. if status.spoiler_text.blank?
  42. components << status.text
  43. components << poll_summary(status)
  44. end
  45. components.reject(&:blank?).join("\n\n")
  46. end
  47. def stream_link_target
  48. embedded_view? ? '_blank' : nil
  49. end
  50. def style_classes(status, is_predecessor, is_successor, include_threads)
  51. classes = ['entry']
  52. classes << 'entry-predecessor' if is_predecessor
  53. classes << 'entry-reblog' if status.reblog?
  54. classes << 'entry-successor' if is_successor
  55. classes << 'entry-center' if include_threads
  56. classes.join(' ')
  57. end
  58. def microformats_classes(status, is_direct_parent, is_direct_child)
  59. classes = []
  60. classes << 'p-in-reply-to' if is_direct_parent
  61. classes << 'p-repost-of' if status.reblog? && is_direct_parent
  62. classes << 'p-comment' if is_direct_child
  63. classes.join(' ')
  64. end
  65. def microformats_h_class(status, is_predecessor, is_successor, include_threads)
  66. if is_predecessor || status.reblog? || is_successor
  67. 'h-cite'
  68. elsif include_threads
  69. ''
  70. else
  71. 'h-entry'
  72. end
  73. end
  74. def fa_visibility_icon(status)
  75. case status.visibility
  76. when 'public'
  77. fa_icon 'globe fw'
  78. when 'unlisted'
  79. fa_icon 'unlock fw'
  80. when 'private'
  81. fa_icon 'lock fw'
  82. when 'direct'
  83. fa_icon 'envelope fw'
  84. end
  85. end
  86. def sensitized?(status, account)
  87. if !account.nil? && account.id == status.account_id
  88. status.sensitive
  89. else
  90. status.account.sensitized? || status.sensitive
  91. end
  92. end
  93. private
  94. def simplified_text(text)
  95. text.dup.tap do |new_text|
  96. URI.extract(new_text).each do |url|
  97. new_text.gsub!(url, '')
  98. end
  99. new_text.gsub!(Account::MENTION_RE, '')
  100. new_text.gsub!(Tag::HASHTAG_RE, '')
  101. new_text.gsub!(/\s+/, '')
  102. end
  103. end
  104. def embedded_view?
  105. params[:controller] == EMBEDDED_CONTROLLER && params[:action] == EMBEDDED_ACTION
  106. end
  107. def render_video_component(status, **options)
  108. video = status.media_attachments.first
  109. meta = video.file.meta || {}
  110. component_params = {
  111. sensitive: sensitized?(status, current_account),
  112. src: full_asset_url(video.file.url(:original)),
  113. preview: full_asset_url(video.thumbnail.present? ? video.thumbnail.url : video.file.url(:small)),
  114. alt: video.description,
  115. blurhash: video.blurhash,
  116. frameRate: meta.dig('original', 'frame_rate'),
  117. inline: true,
  118. media: [
  119. ActiveModelSerializers::SerializableResource.new(video, serializer: REST::MediaAttachmentSerializer),
  120. ].as_json,
  121. }.merge(**options)
  122. react_component :video, component_params do
  123. render partial: 'statuses/attachment_list', locals: { attachments: status.media_attachments }
  124. end
  125. end
  126. def render_audio_component(status, **options)
  127. audio = status.media_attachments.first
  128. meta = audio.file.meta || {}
  129. component_params = {
  130. src: full_asset_url(audio.file.url(:original)),
  131. poster: full_asset_url(audio.thumbnail.present? ? audio.thumbnail.url : status.account.avatar_static_url),
  132. alt: audio.description,
  133. backgroundColor: meta.dig('colors', 'background'),
  134. foregroundColor: meta.dig('colors', 'foreground'),
  135. accentColor: meta.dig('colors', 'accent'),
  136. duration: meta.dig('original', 'duration'),
  137. }.merge(**options)
  138. react_component :audio, component_params do
  139. render partial: 'statuses/attachment_list', locals: { attachments: status.media_attachments }
  140. end
  141. end
  142. def render_media_gallery_component(status, **options)
  143. component_params = {
  144. sensitive: sensitized?(status, current_account),
  145. autoplay: prefers_autoplay?,
  146. media: status.media_attachments.map { |a| ActiveModelSerializers::SerializableResource.new(a, serializer: REST::MediaAttachmentSerializer).as_json },
  147. }.merge(**options)
  148. react_component :media_gallery, component_params do
  149. render partial: 'statuses/attachment_list', locals: { attachments: status.media_attachments }
  150. end
  151. end
  152. def render_card_component(status, **options)
  153. component_params = {
  154. sensitive: sensitized?(status, current_account),
  155. maxDescription: 160,
  156. card: ActiveModelSerializers::SerializableResource.new(status.preview_card, serializer: REST::PreviewCardSerializer).as_json,
  157. }.merge(**options)
  158. react_component :card, component_params
  159. end
  160. def render_poll_component(status, **options)
  161. component_params = {
  162. disabled: true,
  163. poll: ActiveModelSerializers::SerializableResource.new(status.preloadable_poll, serializer: REST::PollSerializer, scope: current_user, scope_name: :current_user).as_json,
  164. }.merge(**options)
  165. react_component :poll, component_params do
  166. render partial: 'statuses/poll', locals: { status: status, poll: status.preloadable_poll, autoplay: prefers_autoplay? }
  167. end
  168. end
  169. def prefers_autoplay?
  170. ActiveModel::Type::Boolean.new.cast(params[:autoplay]) || current_user&.setting_auto_play_gif
  171. end
  172. end