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.

242 lines
7.2 KiB

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