1
0

statuses_helper.rb 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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.ordered_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 'at 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. def embedded_view?
  94. params[:controller] == EMBEDDED_CONTROLLER && params[:action] == EMBEDDED_ACTION
  95. end
  96. def render_video_component(status, **options)
  97. video = status.ordered_media_attachments.first
  98. meta = video.file.meta || {}
  99. component_params = {
  100. sensitive: sensitized?(status, current_account),
  101. src: full_asset_url(video.file.url(:original)),
  102. preview: full_asset_url(video.thumbnail.present? ? video.thumbnail.url : video.file.url(:small)),
  103. alt: video.description,
  104. blurhash: video.blurhash,
  105. frameRate: meta.dig('original', 'frame_rate'),
  106. inline: true,
  107. media: [
  108. ActiveModelSerializers::SerializableResource.new(video, serializer: REST::MediaAttachmentSerializer),
  109. ].as_json,
  110. }.merge(**options)
  111. react_component :video, component_params do
  112. render partial: 'statuses/attachment_list', locals: { attachments: status.ordered_media_attachments }
  113. end
  114. end
  115. def render_audio_component(status, **options)
  116. audio = status.ordered_media_attachments.first
  117. meta = audio.file.meta || {}
  118. component_params = {
  119. src: full_asset_url(audio.file.url(:original)),
  120. poster: full_asset_url(audio.thumbnail.present? ? audio.thumbnail.url : status.account.avatar_static_url),
  121. alt: audio.description,
  122. backgroundColor: meta.dig('colors', 'background'),
  123. foregroundColor: meta.dig('colors', 'foreground'),
  124. accentColor: meta.dig('colors', 'accent'),
  125. duration: meta.dig('original', 'duration'),
  126. }.merge(**options)
  127. react_component :audio, component_params do
  128. render partial: 'statuses/attachment_list', locals: { attachments: status.ordered_media_attachments }
  129. end
  130. end
  131. def render_media_gallery_component(status, **options)
  132. component_params = {
  133. sensitive: sensitized?(status, current_account),
  134. autoplay: prefers_autoplay?,
  135. media: status.ordered_media_attachments.map { |a| ActiveModelSerializers::SerializableResource.new(a, serializer: REST::MediaAttachmentSerializer).as_json },
  136. }.merge(**options)
  137. react_component :media_gallery, component_params do
  138. render partial: 'statuses/attachment_list', locals: { attachments: status.ordered_media_attachments }
  139. end
  140. end
  141. def render_card_component(status, **options)
  142. component_params = {
  143. sensitive: sensitized?(status, current_account),
  144. maxDescription: 160,
  145. card: ActiveModelSerializers::SerializableResource.new(status.preview_card, serializer: REST::PreviewCardSerializer).as_json,
  146. }.merge(**options)
  147. react_component :card, component_params
  148. end
  149. def render_poll_component(status, **options)
  150. component_params = {
  151. disabled: true,
  152. poll: ActiveModelSerializers::SerializableResource.new(status.preloadable_poll, serializer: REST::PollSerializer, scope: current_user, scope_name: :current_user).as_json,
  153. }.merge(**options)
  154. react_component :poll, component_params do
  155. render partial: 'statuses/poll', locals: { status: status, poll: status.preloadable_poll, autoplay: prefers_autoplay? }
  156. end
  157. end
  158. def prefers_autoplay?
  159. ActiveModel::Type::Boolean.new.cast(params[:autoplay]) || current_user&.setting_auto_play_gif
  160. end
  161. end