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.

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