statuses_helper.rb 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. # frozen_string_literal: true
  2. module StatusesHelper
  3. EMBEDDED_CONTROLLER = 'statuses'
  4. EMBEDDED_ACTION = 'embed'
  5. def display_name(account, **options)
  6. if options[:custom_emojify]
  7. Formatter.instance.format_display_name(account, options)
  8. else
  9. account.display_name.presence || account.username
  10. end
  11. end
  12. def account_action_button(account)
  13. if user_signed_in?
  14. if account.id == current_user.account_id
  15. link_to settings_profile_url, class: 'button logo-button' do
  16. safe_join([svg_logo, t('settings.edit_profile')])
  17. end
  18. elsif current_account.following?(account) || current_account.requested?(account)
  19. link_to account_unfollow_path(account), class: 'button logo-button button--destructive', data: { method: :post } do
  20. safe_join([svg_logo, t('accounts.unfollow')])
  21. end
  22. elsif !(account.memorial? || account.moved?)
  23. link_to account_follow_path(account), class: "button logo-button#{account.blocking?(current_account) ? ' disabled' : ''}", data: { method: :post } do
  24. safe_join([svg_logo, t('accounts.follow')])
  25. end
  26. end
  27. elsif !(account.memorial? || account.moved?)
  28. link_to account_remote_follow_path(account), class: 'button logo-button modal-button', target: '_new' do
  29. safe_join([svg_logo, t('accounts.follow')])
  30. end
  31. end
  32. end
  33. def svg_logo
  34. content_tag(:svg, tag(:use, 'xlink:href' => '#mastodon-svg-logo'), 'viewBox' => '0 0 216.4144 232.00976')
  35. end
  36. def svg_logo_full
  37. content_tag(:svg, tag(:use, 'xlink:href' => '#mastodon-svg-logo-full'), 'viewBox' => '0 0 713.35878 175.8678')
  38. end
  39. def account_badge(account, all: false)
  40. if account.bot?
  41. content_tag(:div, content_tag(:div, t('accounts.roles.bot'), class: 'account-role bot'), class: 'roles')
  42. elsif (Setting.show_staff_badge && account.user_staff?) || all
  43. content_tag(:div, class: 'roles') do
  44. if all && !account.user_staff?
  45. content_tag(:div, t('admin.accounts.roles.user'), class: 'account-role')
  46. elsif account.user_admin?
  47. content_tag(:div, t('accounts.roles.admin'), class: 'account-role admin')
  48. elsif account.user_moderator?
  49. content_tag(:div, t('accounts.roles.moderator'), class: 'account-role moderator')
  50. end
  51. end
  52. end
  53. end
  54. def link_to_more(url)
  55. link_to t('statuses.show_more'), url, class: 'load-more load-gap'
  56. end
  57. def nothing_here(extra_classes = '')
  58. content_tag(:div, class: "nothing-here #{extra_classes}") do
  59. t('accounts.nothing_here')
  60. end
  61. end
  62. def account_description(account)
  63. prepend_str = [
  64. [
  65. number_to_human(account.statuses_count, strip_insignificant_zeros: true),
  66. I18n.t('accounts.posts', count: account.statuses_count),
  67. ].join(' '),
  68. [
  69. number_to_human(account.following_count, strip_insignificant_zeros: true),
  70. I18n.t('accounts.following', count: account.following_count),
  71. ].join(' '),
  72. [
  73. number_to_human(account.followers_count, strip_insignificant_zeros: true),
  74. I18n.t('accounts.followers', count: account.followers_count),
  75. ].join(' '),
  76. ].join(', ')
  77. [prepend_str, account.note].join(' · ')
  78. end
  79. def media_summary(status)
  80. attachments = { image: 0, video: 0 }
  81. status.media_attachments.each do |media|
  82. if media.video?
  83. attachments[:video] += 1
  84. else
  85. attachments[:image] += 1
  86. end
  87. end
  88. text = attachments.to_a.reject { |_, value| value.zero? }.map { |key, value| I18n.t("statuses.attached.#{key}", count: value) }.join(' · ')
  89. return if text.blank?
  90. I18n.t('statuses.attached.description', attached: text)
  91. end
  92. def status_text_summary(status)
  93. return if status.spoiler_text.blank?
  94. I18n.t('statuses.content_warning', warning: status.spoiler_text)
  95. end
  96. def poll_summary(status)
  97. return unless status.preloadable_poll
  98. status.preloadable_poll.options.map { |o| "[ ] #{o}" }.join("\n")
  99. end
  100. def status_description(status)
  101. components = [[media_summary(status), status_text_summary(status)].reject(&:blank?).join(' · ')]
  102. if status.spoiler_text.blank?
  103. components << status.text
  104. components << poll_summary(status)
  105. end
  106. components.reject(&:blank?).join("\n\n")
  107. end
  108. def stream_link_target
  109. embedded_view? ? '_blank' : nil
  110. end
  111. def acct(account)
  112. if account.local?
  113. "@#{account.acct}@#{Rails.configuration.x.local_domain}"
  114. else
  115. "@#{account.acct}"
  116. end
  117. end
  118. def style_classes(status, is_predecessor, is_successor, include_threads)
  119. classes = ['entry']
  120. classes << 'entry-predecessor' if is_predecessor
  121. classes << 'entry-reblog' if status.reblog?
  122. classes << 'entry-successor' if is_successor
  123. classes << 'entry-center' if include_threads
  124. classes.join(' ')
  125. end
  126. def microformats_classes(status, is_direct_parent, is_direct_child)
  127. classes = []
  128. classes << 'p-in-reply-to' if is_direct_parent
  129. classes << 'p-repost-of' if status.reblog? && is_direct_parent
  130. classes << 'p-comment' if is_direct_child
  131. classes.join(' ')
  132. end
  133. def microformats_h_class(status, is_predecessor, is_successor, include_threads)
  134. if is_predecessor || status.reblog? || is_successor
  135. 'h-cite'
  136. elsif include_threads
  137. ''
  138. else
  139. 'h-entry'
  140. end
  141. end
  142. def rtl_status?(status)
  143. status.local? ? rtl?(status.text) : rtl?(strip_tags(status.text))
  144. end
  145. def rtl?(text)
  146. text = simplified_text(text)
  147. rtl_words = text.scan(/[\p{Hebrew}\p{Arabic}\p{Syriac}\p{Thaana}\p{Nko}]+/m)
  148. if rtl_words.present?
  149. total_size = text.size.to_f
  150. rtl_size(rtl_words) / total_size > 0.3
  151. else
  152. false
  153. end
  154. end
  155. def fa_visibility_icon(status)
  156. case status.visibility
  157. when 'public'
  158. fa_icon 'globe fw'
  159. when 'unlisted'
  160. fa_icon 'unlock fw'
  161. when 'private'
  162. fa_icon 'lock fw'
  163. when 'direct'
  164. fa_icon 'envelope fw'
  165. end
  166. end
  167. private
  168. def simplified_text(text)
  169. text.dup.tap do |new_text|
  170. URI.extract(new_text).each do |url|
  171. new_text.gsub!(url, '')
  172. end
  173. new_text.gsub!(Account::MENTION_RE, '')
  174. new_text.gsub!(Tag::HASHTAG_RE, '')
  175. new_text.gsub!(/\s+/, '')
  176. end
  177. end
  178. def rtl_size(words)
  179. words.reduce(0) { |acc, elem| acc + elem.size }.to_f
  180. end
  181. def embedded_view?
  182. params[:controller] == EMBEDDED_CONTROLLER && params[:action] == EMBEDDED_ACTION
  183. end
  184. end