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.

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