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.

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