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.

207 lines
6.4 KiB

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