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.

149 lines
3.6 KiB

  1. # frozen_string_literal: true
  2. module StatusesHelper
  3. EMBEDDED_CONTROLLER = 'statuses'
  4. EMBEDDED_ACTION = 'embed'
  5. def link_to_more(url)
  6. link_to t('statuses.show_more'), url, class: 'load-more load-gap'
  7. end
  8. def nothing_here(extra_classes = '')
  9. content_tag(:div, class: "nothing-here #{extra_classes}") do
  10. t('accounts.nothing_here')
  11. end
  12. end
  13. def media_summary(status)
  14. attachments = { image: 0, video: 0, audio: 0 }
  15. status.media_attachments.each do |media|
  16. if media.video?
  17. attachments[:video] += 1
  18. elsif media.audio?
  19. attachments[:audio] += 1
  20. else
  21. attachments[:image] += 1
  22. end
  23. end
  24. text = attachments.to_a.reject { |_, value| value.zero? }.map { |key, value| I18n.t("statuses.attached.#{key}", count: value) }.join(' · ')
  25. return if text.blank?
  26. I18n.t('statuses.attached.description', attached: text)
  27. end
  28. def status_text_summary(status)
  29. return if status.spoiler_text.blank?
  30. I18n.t('statuses.content_warning', warning: status.spoiler_text)
  31. end
  32. def poll_summary(status)
  33. return unless status.preloadable_poll
  34. status.preloadable_poll.options.map { |o| "[ ] #{o}" }.join("\n")
  35. end
  36. def status_description(status)
  37. components = [[media_summary(status), status_text_summary(status)].reject(&:blank?).join(' · ')]
  38. if status.spoiler_text.blank?
  39. components << status.text
  40. components << poll_summary(status)
  41. end
  42. components.reject(&:blank?).join("\n\n")
  43. end
  44. def stream_link_target
  45. embedded_view? ? '_blank' : nil
  46. end
  47. def style_classes(status, is_predecessor, is_successor, include_threads)
  48. classes = ['entry']
  49. classes << 'entry-predecessor' if is_predecessor
  50. classes << 'entry-reblog' if status.reblog?
  51. classes << 'entry-successor' if is_successor
  52. classes << 'entry-center' if include_threads
  53. classes.join(' ')
  54. end
  55. def microformats_classes(status, is_direct_parent, is_direct_child)
  56. classes = []
  57. classes << 'p-in-reply-to' if is_direct_parent
  58. classes << 'p-repost-of' if status.reblog? && is_direct_parent
  59. classes << 'p-comment' if is_direct_child
  60. classes.join(' ')
  61. end
  62. def microformats_h_class(status, is_predecessor, is_successor, include_threads)
  63. if is_predecessor || status.reblog? || is_successor
  64. 'h-cite'
  65. elsif include_threads
  66. ''
  67. else
  68. 'h-entry'
  69. end
  70. end
  71. def rtl_status?(status)
  72. status.local? ? rtl?(status.text) : rtl?(strip_tags(status.text))
  73. end
  74. def rtl?(text)
  75. text = simplified_text(text)
  76. rtl_words = text.scan(/[\p{Hebrew}\p{Arabic}\p{Syriac}\p{Thaana}\p{Nko}]+/m)
  77. if rtl_words.present?
  78. total_size = text.size.to_f
  79. rtl_size(rtl_words) / total_size > 0.3
  80. else
  81. false
  82. end
  83. end
  84. def fa_visibility_icon(status)
  85. case status.visibility
  86. when 'public'
  87. fa_icon 'globe fw'
  88. when 'unlisted'
  89. fa_icon 'unlock fw'
  90. when 'private'
  91. fa_icon 'lock fw'
  92. when 'direct'
  93. fa_icon 'envelope fw'
  94. end
  95. end
  96. def sensitized?(status, account)
  97. if !account.nil? && account.id == status.account_id
  98. status.sensitive
  99. else
  100. status.account.sensitized? || status.sensitive
  101. end
  102. end
  103. private
  104. def simplified_text(text)
  105. text.dup.tap do |new_text|
  106. URI.extract(new_text).each do |url|
  107. new_text.gsub!(url, '')
  108. end
  109. new_text.gsub!(Account::MENTION_RE, '')
  110. new_text.gsub!(Tag::HASHTAG_RE, '')
  111. new_text.gsub!(/\s+/, '')
  112. end
  113. end
  114. def rtl_size(words)
  115. words.reduce(0) { |acc, elem| acc + elem.size }.to_f
  116. end
  117. def embedded_view?
  118. params[:controller] == EMBEDDED_CONTROLLER && params[:action] == EMBEDDED_ACTION
  119. end
  120. end