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.

169 lines
5.2 KiB

8 years ago
8 years ago
  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 visibility_icon(status)
  64. if status.public_visibility?
  65. fa_icon('globe', title: I18n.t('statuses.visibilities.public'))
  66. elsif status.unlisted_visibility?
  67. fa_icon('unlock', title: I18n.t('statuses.visibilities.unlisted'))
  68. elsif status.private_visibility? || status.limited_visibility?
  69. fa_icon('lock', title: I18n.t('statuses.visibilities.private'))
  70. elsif status.direct_visibility?
  71. fa_icon('envelope', title: I18n.t('statuses.visibilities.direct'))
  72. end
  73. end
  74. def custom_emoji_tag(custom_emoji, animate = true)
  75. if animate
  76. image_tag(custom_emoji.image.url, class: 'emojione', alt: ":#{custom_emoji.shortcode}:")
  77. else
  78. 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)))
  79. end
  80. end
  81. def opengraph(property, content)
  82. tag(:meta, content: content, property: property)
  83. end
  84. def react_component(name, props = {}, &block)
  85. if block.nil?
  86. content_tag(:div, nil, data: { component: name.to_s.camelcase, props: Oj.dump(props) })
  87. else
  88. content_tag(:div, data: { component: name.to_s.camelcase, props: Oj.dump(props) }, &block)
  89. end
  90. end
  91. def body_classes
  92. output = (@body_classes || '').split(' ')
  93. output << "theme-#{current_theme.parameterize}"
  94. output << 'system-font' if current_account&.user&.setting_system_font_ui
  95. output << (current_account&.user&.setting_reduce_motion ? 'reduce-motion' : 'no-reduce-motion')
  96. output << 'rtl' if locale_direction == 'rtl'
  97. output.reject(&:blank?).join(' ')
  98. end
  99. def cdn_host
  100. Rails.configuration.action_controller.asset_host
  101. end
  102. def cdn_host?
  103. cdn_host.present?
  104. end
  105. def storage_host
  106. "https://#{ENV['S3_ALIAS_HOST'].presence || ENV['S3_CLOUDFRONT_HOST']}"
  107. end
  108. def storage_host?
  109. ENV['S3_ALIAS_HOST'].present? || ENV['S3_CLOUDFRONT_HOST'].present?
  110. end
  111. def quote_wrap(text, line_width: 80, break_sequence: "\n")
  112. text = word_wrap(text, line_width: line_width - 2, break_sequence: break_sequence)
  113. text.split("\n").map { |line| '> ' + line }.join("\n")
  114. end
  115. def render_initial_state
  116. state_params = {
  117. settings: {
  118. known_fediverse: Setting.show_known_fediverse_at_about_page,
  119. },
  120. text: [params[:title], params[:text], params[:url]].compact.join(' '),
  121. }
  122. permit_visibilities = %w(public unlisted private direct)
  123. default_privacy = current_account&.user&.setting_default_privacy
  124. permit_visibilities.shift(permit_visibilities.index(default_privacy) + 1) if default_privacy.present?
  125. state_params[:visibility] = params[:visibility] if permit_visibilities.include? params[:visibility]
  126. if user_signed_in?
  127. state_params[:settings] = state_params[:settings].merge(Web::Setting.find_by(user: current_user)&.data || {})
  128. state_params[:push_subscription] = current_account.user.web_push_subscription(current_session)
  129. state_params[:current_account] = current_account
  130. state_params[:token] = current_session.token
  131. state_params[:admin] = Account.find_local(Setting.site_contact_username.strip.gsub(/\A@/, ''))
  132. end
  133. json = ActiveModelSerializers::SerializableResource.new(InitialStatePresenter.new(state_params), serializer: InitialStateSerializer).to_json
  134. # rubocop:disable Rails/OutputSafety
  135. content_tag(:script, json_escape(json).html_safe, id: 'initial-state', type: 'application/json')
  136. # rubocop:enable Rails/OutputSafety
  137. end
  138. end