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.

154 lines
3.9 KiB

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