statuses_helper.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 fa_visibility_icon(status)
  75. case status.visibility
  76. when 'public'
  77. fa_icon 'globe fw'
  78. when 'unlisted'
  79. fa_icon 'unlock fw'
  80. when 'private'
  81. fa_icon 'lock fw'
  82. when 'direct'
  83. fa_icon 'envelope fw'
  84. end
  85. end
  86. def sensitized?(status, account)
  87. if !account.nil? && account.id == status.account_id
  88. status.sensitive
  89. else
  90. status.account.sensitized? || status.sensitive
  91. end
  92. end
  93. private
  94. def simplified_text(text)
  95. text.dup.tap do |new_text|
  96. URI.extract(new_text).each do |url|
  97. new_text.gsub!(url, '')
  98. end
  99. new_text.gsub!(Account::MENTION_RE, '')
  100. new_text.gsub!(Tag::HASHTAG_RE, '')
  101. new_text.gsub!(/\s+/, '')
  102. end
  103. end
  104. def embedded_view?
  105. params[:controller] == EMBEDDED_CONTROLLER && params[:action] == EMBEDDED_ACTION
  106. end
  107. end