statuses_helper.rb 3.7 KB

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