application_helper.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # frozen_string_literal: true
  2. module ApplicationHelper
  3. DANGEROUS_SCOPES = %w(
  4. read
  5. write
  6. follow
  7. ).freeze
  8. def active_nav_class(*paths)
  9. paths.any? { |path| current_page?(path) } ? 'active' : ''
  10. end
  11. def active_link_to(label, path, **options)
  12. link_to label, path, options.merge(class: active_nav_class(path))
  13. end
  14. def show_landing_strip?
  15. !user_signed_in? && !single_user_mode?
  16. end
  17. def open_registrations?
  18. Setting.registrations_mode == 'open'
  19. end
  20. def approved_registrations?
  21. Setting.registrations_mode == 'approved'
  22. end
  23. def closed_registrations?
  24. Setting.registrations_mode == 'none'
  25. end
  26. def available_sign_up_path
  27. if closed_registrations?
  28. 'https://joinmastodon.org/#getting-started'
  29. else
  30. new_user_registration_path
  31. end
  32. end
  33. def open_deletion?
  34. Setting.open_deletion
  35. end
  36. def locale_direction
  37. if [:ar, :fa, :he].include?(I18n.locale)
  38. 'rtl'
  39. else
  40. 'ltr'
  41. end
  42. end
  43. def favicon_path
  44. env_suffix = Rails.env.production? ? '' : '-dev'
  45. "/favicon#{env_suffix}.ico"
  46. end
  47. def title
  48. Rails.env.production? ? site_title : "#{site_title} (Dev)"
  49. end
  50. def class_for_scope(scope)
  51. 'scope-danger' if DANGEROUS_SCOPES.include?(scope.to_s)
  52. end
  53. def can?(action, record)
  54. return false if record.nil?
  55. policy(record).public_send("#{action}?")
  56. end
  57. def fa_icon(icon, attributes = {})
  58. class_names = attributes[:class]&.split(' ') || []
  59. class_names << 'fa'
  60. class_names += icon.split(' ').map { |cl| "fa-#{cl}" }
  61. content_tag(:i, nil, attributes.merge(class: class_names.join(' ')))
  62. end
  63. def custom_emoji_tag(custom_emoji, animate = true)
  64. if animate
  65. image_tag(custom_emoji.image.url, class: 'emojione', alt: ":#{custom_emoji.shortcode}:")
  66. else
  67. image_tag(custom_emoji.image.url(:static), class: 'emojione custom-emoji', alt: ":#{custom_emoji.shortcode}", 'data-original' => full_asset_url(custom_emoji.image.url), 'data-static' => full_asset_url(custom_emoji.image.url(:static)))
  68. end
  69. end
  70. def opengraph(property, content)
  71. tag(:meta, content: content, property: property)
  72. end
  73. def react_component(name, props = {}, &block)
  74. if block.nil?
  75. content_tag(:div, nil, data: { component: name.to_s.camelcase, props: Oj.dump(props) })
  76. else
  77. content_tag(:div, data: { component: name.to_s.camelcase, props: Oj.dump(props) }, &block)
  78. end
  79. end
  80. def body_classes
  81. output = (@body_classes || '').split(' ')
  82. output << "theme-#{current_theme.parameterize}"
  83. output << 'system-font' if current_account&.user&.setting_system_font_ui
  84. output << (current_account&.user&.setting_reduce_motion ? 'reduce-motion' : 'no-reduce-motion')
  85. output << 'rtl' if locale_direction == 'rtl'
  86. output.reject(&:blank?).join(' ')
  87. end
  88. def cdn_host
  89. Rails.configuration.action_controller.asset_host
  90. end
  91. def cdn_host?
  92. cdn_host.present?
  93. end
  94. def storage_host
  95. "https://#{ENV['S3_ALIAS_HOST'].presence || ENV['S3_CLOUDFRONT_HOST']}"
  96. end
  97. def storage_host?
  98. ENV['S3_ALIAS_HOST'].present? || ENV['S3_CLOUDFRONT_HOST'].present?
  99. end
  100. def quote_wrap(text, line_width: 80, break_sequence: "\n")
  101. text = word_wrap(text, line_width: line_width - 2, break_sequence: break_sequence)
  102. text.split("\n").map { |line| '> ' + line }.join("\n")
  103. end
  104. def render_initial_state
  105. state_params = {
  106. settings: {
  107. known_fediverse: Setting.show_known_fediverse_at_about_page,
  108. },
  109. text: [params[:title], params[:text], params[:url]].compact.join(' '),
  110. }
  111. if user_signed_in?
  112. state_params[:settings] = state_params[:settings].merge(Web::Setting.find_by(user: current_user)&.data || {})
  113. state_params[:push_subscription] = current_account.user.web_push_subscription(current_session)
  114. state_params[:current_account] = current_account
  115. state_params[:token] = current_session.token
  116. state_params[:admin] = Account.find_local(Setting.site_contact_username.strip.gsub(/\A@/, ''))
  117. end
  118. json = ActiveModelSerializers::SerializableResource.new(InitialStatePresenter.new(state_params), serializer: InitialStateSerializer).to_json
  119. content_tag(:script, json_escape(json).html_safe, id: 'initial-state', type: 'application/json')
  120. end
  121. end