logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

stream_entries_helper.rb (3561B)


  1. # frozen_string_literal: true
  2. module StreamEntriesHelper
  3. EMBEDDED_CONTROLLER = 'statuses'
  4. EMBEDDED_ACTION = 'embed'
  5. def display_name(account)
  6. account.display_name.presence || account.username
  7. end
  8. def account_description(account)
  9. prepend_str = [
  10. [
  11. number_to_human(account.statuses_count, strip_insignificant_zeros: true),
  12. t('accounts.posts'),
  13. ].join(' '),
  14. [
  15. number_to_human(account.following_count, strip_insignificant_zeros: true),
  16. t('accounts.following'),
  17. ].join(' '),
  18. [
  19. number_to_human(account.followers_count, strip_insignificant_zeros: true),
  20. t('accounts.followers'),
  21. ].join(' '),
  22. ].join(', ')
  23. [prepend_str, account.note].join(' · ')
  24. end
  25. def media_summary(status)
  26. attachments = { image: 0, video: 0 }
  27. status.media_attachments.each do |media|
  28. if media.video?
  29. attachments[:video] += 1
  30. else
  31. attachments[:image] += 1
  32. end
  33. end
  34. text = attachments.to_a.reject { |_, value| value.zero? }.map { |key, value| t("statuses.attached.#{key}", count: value) }.join(' · ')
  35. return if text.blank?
  36. t('statuses.attached.description', attached: text)
  37. end
  38. def status_text_summary(status)
  39. return if status.spoiler_text.blank?
  40. t('statuses.content_warning', warning: status.spoiler_text)
  41. end
  42. def status_description(status)
  43. components = [[media_summary(status), status_text_summary(status)].reject(&:blank?).join(' · ')]
  44. components << status.text if status.spoiler_text.blank?
  45. components.reject(&:blank?).join("\n\n")
  46. end
  47. def stream_link_target
  48. embedded_view? ? '_blank' : nil
  49. end
  50. def acct(account)
  51. if embedded_view? && account.local?
  52. "@#{account.acct}@#{Rails.configuration.x.local_domain}"
  53. else
  54. "@#{account.acct}"
  55. end
  56. end
  57. def style_classes(status, is_predecessor, is_successor, include_threads)
  58. classes = ['entry']
  59. classes << 'entry-predecessor' if is_predecessor
  60. classes << 'entry-reblog' if status.reblog?
  61. classes << 'entry-successor' if is_successor
  62. classes << 'entry-center' if include_threads
  63. classes.join(' ')
  64. end
  65. def microformats_classes(status, is_direct_parent, is_direct_child)
  66. classes = []
  67. classes << 'p-in-reply-to' if is_direct_parent
  68. classes << 'p-repost-of' if status.reblog? && is_direct_parent
  69. classes << 'p-comment' if is_direct_child
  70. classes.join(' ')
  71. end
  72. def microformats_h_class(status, is_predecessor, is_successor, include_threads)
  73. if is_predecessor || status.reblog? || is_successor
  74. 'h-cite'
  75. elsif include_threads
  76. ''
  77. else
  78. 'h-entry'
  79. end
  80. end
  81. def rtl_status?(status)
  82. status.local? ? rtl?(status.text) : rtl?(strip_tags(status.text))
  83. end
  84. def rtl?(text)
  85. text = simplified_text(text)
  86. rtl_words = text.scan(/[\p{Hebrew}\p{Arabic}\p{Syriac}\p{Thaana}\p{Nko}]+/m)
  87. if rtl_words.present?
  88. total_size = text.size.to_f
  89. rtl_size(rtl_words) / total_size > 0.3
  90. else
  91. false
  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