You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
6.2 KiB

  1. # frozen_string_literal: true
  2. module StreamEntriesHelper
  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 account_badge(account, all: false)
  37. if account.bot?
  38. content_tag(:div, content_tag(:div, t('accounts.roles.bot'), class: 'account-role bot'), class: 'roles')
  39. elsif (Setting.show_staff_badge && account.user_staff?) || all
  40. content_tag(:div, class: 'roles') do
  41. if all && !account.user_staff?
  42. content_tag(:div, t('admin.accounts.roles.user'), class: 'account-role')
  43. elsif account.user_admin?
  44. content_tag(:div, t('accounts.roles.admin'), class: 'account-role admin')
  45. elsif account.user_moderator?
  46. content_tag(:div, t('accounts.roles.moderator'), class: 'account-role moderator')
  47. end
  48. end
  49. end
  50. end
  51. def link_to_more(url)
  52. link_to t('statuses.show_more'), url, class: 'load-more load-gap'
  53. end
  54. def nothing_here(extra_classes = '')
  55. content_tag(:div, class: "nothing-here #{extra_classes}") do
  56. t('accounts.nothing_here')
  57. end
  58. end
  59. def account_description(account)
  60. prepend_str = [
  61. [
  62. number_to_human(account.statuses_count, strip_insignificant_zeros: true),
  63. I18n.t('accounts.posts', count: account.statuses_count),
  64. ].join(' '),
  65. [
  66. number_to_human(account.following_count, strip_insignificant_zeros: true),
  67. I18n.t('accounts.following', count: account.following_count),
  68. ].join(' '),
  69. [
  70. number_to_human(account.followers_count, strip_insignificant_zeros: true),
  71. I18n.t('accounts.followers', count: account.followers_count),
  72. ].join(' '),
  73. ].join(', ')
  74. [prepend_str, account.note].join(' · ')
  75. end
  76. def media_summary(status)
  77. attachments = { image: 0, video: 0 }
  78. status.media_attachments.each do |media|
  79. if media.video?
  80. attachments[:video] += 1
  81. else
  82. attachments[:image] += 1
  83. end
  84. end
  85. text = attachments.to_a.reject { |_, value| value.zero? }.map { |key, value| I18n.t("statuses.attached.#{key}", count: value) }.join(' · ')
  86. return if text.blank?
  87. I18n.t('statuses.attached.description', attached: text)
  88. end
  89. def status_text_summary(status)
  90. return if status.spoiler_text.blank?
  91. I18n.t('statuses.content_warning', warning: status.spoiler_text)
  92. end
  93. def poll_summary(status)
  94. return unless status.preloadable_poll
  95. status.preloadable_poll.options.map { |o| "[ ] #{o}" }.join("\n")
  96. end
  97. def status_description(status)
  98. components = [[media_summary(status), status_text_summary(status)].reject(&:blank?).join(' · ')]
  99. if status.spoiler_text.blank?
  100. components << status.text
  101. components << poll_summary(status)
  102. end
  103. components.reject(&:blank?).join("\n\n")
  104. end
  105. def stream_link_target
  106. embedded_view? ? '_blank' : nil
  107. end
  108. def acct(account)
  109. if account.local?
  110. "@#{account.acct}@#{Rails.configuration.x.local_domain}"
  111. else
  112. "@#{account.acct}"
  113. end
  114. end
  115. def style_classes(status, is_predecessor, is_successor, include_threads)
  116. classes = ['entry']
  117. classes << 'entry-predecessor' if is_predecessor
  118. classes << 'entry-reblog' if status.reblog?
  119. classes << 'entry-successor' if is_successor
  120. classes << 'entry-center' if include_threads
  121. classes.join(' ')
  122. end
  123. def microformats_classes(status, is_direct_parent, is_direct_child)
  124. classes = []
  125. classes << 'p-in-reply-to' if is_direct_parent
  126. classes << 'p-repost-of' if status.reblog? && is_direct_parent
  127. classes << 'p-comment' if is_direct_child
  128. classes.join(' ')
  129. end
  130. def microformats_h_class(status, is_predecessor, is_successor, include_threads)
  131. if is_predecessor || status.reblog? || is_successor
  132. 'h-cite'
  133. elsif include_threads
  134. ''
  135. else
  136. 'h-entry'
  137. end
  138. end
  139. def rtl_status?(status)
  140. status.local? ? rtl?(status.text) : rtl?(strip_tags(status.text))
  141. end
  142. def rtl?(text)
  143. text = simplified_text(text)
  144. rtl_words = text.scan(/[\p{Hebrew}\p{Arabic}\p{Syriac}\p{Thaana}\p{Nko}]+/m)
  145. if rtl_words.present?
  146. total_size = text.size.to_f
  147. rtl_size(rtl_words) / total_size > 0.3
  148. else
  149. false
  150. end
  151. end
  152. def fa_visibility_icon(status)
  153. case status.visibility
  154. when 'public'
  155. fa_icon 'globe fw'
  156. when 'unlisted'
  157. fa_icon 'unlock fw'
  158. when 'private'
  159. fa_icon 'lock fw'
  160. when 'direct'
  161. fa_icon 'envelope fw'
  162. end
  163. end
  164. private
  165. def simplified_text(text)
  166. text.dup.tap do |new_text|
  167. URI.extract(new_text).each do |url|
  168. new_text.gsub!(url, '')
  169. end
  170. new_text.gsub!(Account::MENTION_RE, '')
  171. new_text.gsub!(Tag::HASHTAG_RE, '')
  172. new_text.gsub!(/\s+/, '')
  173. end
  174. end
  175. def rtl_size(words)
  176. words.reduce(0) { |acc, elem| acc + elem.size }.to_f
  177. end
  178. def embedded_view?
  179. params[:controller] == EMBEDDED_CONTROLLER && params[:action] == EMBEDDED_ACTION
  180. end
  181. end