statuses_helper.rb 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # frozen_string_literal: true
  2. module StatusesHelper
  3. EMBEDDED_CONTROLLER = 'statuses'
  4. EMBEDDED_ACTION = 'embed'
  5. def nothing_here(extra_classes = '')
  6. content_tag(:div, class: "nothing-here #{extra_classes}") do
  7. t('accounts.nothing_here')
  8. end
  9. end
  10. def media_summary(status)
  11. attachments = { image: 0, video: 0, audio: 0 }
  12. status.ordered_media_attachments.each do |media|
  13. if media.video?
  14. attachments[:video] += 1
  15. elsif media.audio?
  16. attachments[:audio] += 1
  17. else
  18. attachments[:image] += 1
  19. end
  20. end
  21. text = attachments.to_a.reject { |_, value| value.zero? }.map { |key, value| I18n.t("statuses.attached.#{key}", count: value) }.join(' · ')
  22. return if text.blank?
  23. I18n.t('statuses.attached.description', attached: text)
  24. end
  25. def status_text_summary(status)
  26. return if status.spoiler_text.blank?
  27. I18n.t('statuses.content_warning', warning: status.spoiler_text)
  28. end
  29. def poll_summary(status)
  30. return unless status.preloadable_poll
  31. status.preloadable_poll.options.map { |o| "[ ] #{o}" }.join("\n")
  32. end
  33. def status_description(status)
  34. components = [[media_summary(status), status_text_summary(status)].compact_blank.join(' · ')]
  35. if status.spoiler_text.blank?
  36. components << status.text
  37. components << poll_summary(status)
  38. end
  39. components.compact_blank.join("\n\n")
  40. end
  41. def stream_link_target
  42. embedded_view? ? '_blank' : nil
  43. end
  44. def fa_visibility_icon(status)
  45. case status.visibility
  46. when 'public'
  47. fa_icon 'globe fw'
  48. when 'unlisted'
  49. fa_icon 'unlock fw'
  50. when 'private'
  51. fa_icon 'lock fw'
  52. when 'direct'
  53. fa_icon 'at fw'
  54. end
  55. end
  56. def embedded_view?
  57. params[:controller] == EMBEDDED_CONTROLLER && params[:action] == EMBEDDED_ACTION
  58. end
  59. def prefers_autoplay?
  60. ActiveModel::Type::Boolean.new.cast(params[:autoplay]) || current_user&.setting_auto_play_gif
  61. end
  62. end