闭社主体 forked from https://github.com/tootsuite/mastodon
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.

87 lines
2.1 KiB

  1. # frozen_string_literal: true
  2. module StreamEntriesHelper
  3. EMBEDDED_CONTROLLER = 'stream_entries'
  4. EMBEDDED_ACTION = 'embed'
  5. def display_name(account)
  6. account.display_name.presence || account.username
  7. end
  8. def stream_link_target
  9. embedded_view? ? '_blank' : nil
  10. end
  11. def acct(account)
  12. if embedded_view? && account.local?
  13. "@#{account.acct}@#{Rails.configuration.x.local_domain}"
  14. else
  15. "@#{account.acct}"
  16. end
  17. end
  18. def style_classes(status, is_predecessor, is_successor, include_threads)
  19. classes = ['entry']
  20. classes << 'entry-predecessor' if is_predecessor
  21. classes << 'entry-reblog' if status.reblog?
  22. classes << 'entry-successor' if is_successor
  23. classes << 'entry-center' if include_threads
  24. classes.join(' ')
  25. end
  26. def microformats_classes(status, is_direct_parent, is_direct_child)
  27. classes = []
  28. classes << 'p-in-reply-to' if is_direct_parent
  29. classes << 'p-repost-of' if status.reblog? && is_direct_parent
  30. classes << 'p-comment' if is_direct_child
  31. classes.join(' ')
  32. end
  33. def microformats_h_class(status, is_predecessor, is_successor, include_threads)
  34. if is_predecessor || status.reblog? || is_successor
  35. 'h-cite'
  36. elsif include_threads
  37. ''
  38. else
  39. 'h-entry'
  40. end
  41. end
  42. def rtl_status?(status)
  43. status.local? ? rtl?(status.text) : rtl?(strip_tags(status.text))
  44. end
  45. def rtl?(text)
  46. text = simplified_text(text)
  47. rtl_words = text.scan(/[\p{Hebrew}\p{Arabic}\p{Syriac}\p{Thaana}\p{Nko}]+/m)
  48. if rtl_words.present?
  49. total_size = text.size.to_f
  50. rtl_size(rtl_words) / total_size > 0.3
  51. else
  52. false
  53. end
  54. end
  55. private
  56. def simplified_text(text)
  57. text.dup.tap do |new_text|
  58. URI.extract(new_text).each do |url|
  59. new_text.gsub!(url, '')
  60. end
  61. new_text.gsub!(Account::MENTION_RE, '')
  62. new_text.gsub!(Tag::HASHTAG_RE, '')
  63. new_text.gsub!(/\s+/, '')
  64. end
  65. end
  66. def rtl_size(words)
  67. words.reduce(0) { |acc, elem| acc + elem.size }.to_f
  68. end
  69. def embedded_view?
  70. params[:controller] == EMBEDDED_CONTROLLER && params[:action] == EMBEDDED_ACTION
  71. end
  72. end