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.

170 lines
5.2 KiB

8 years ago
4 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. 'https://closed.social'
  30. else
  31. new_user_registration_path
  32. end
  33. end
  34. def open_deletion?
  35. Setting.open_deletion
  36. end
  37. def locale_direction
  38. if [:ar, :fa, :he].include?(I18n.locale)
  39. 'rtl'
  40. else
  41. 'ltr'
  42. end
  43. end
  44. def favicon_path
  45. env_suffix = Rails.env.production? ? '' : '-dev'
  46. "/favicon#{env_suffix}.ico"
  47. end
  48. def title
  49. Rails.env.production? ? site_title : "#{site_title} (Dev)"
  50. end
  51. def class_for_scope(scope)
  52. 'scope-danger' if DANGEROUS_SCOPES.include?(scope.to_s)
  53. end
  54. def can?(action, record)
  55. return false if record.nil?
  56. policy(record).public_send("#{action}?")
  57. end
  58. def fa_icon(icon, attributes = {})
  59. class_names = attributes[:class]&.split(' ') || []
  60. class_names << 'fa'
  61. class_names += icon.split(' ').map { |cl| "fa-#{cl}" }
  62. content_tag(:i, nil, attributes.merge(class: class_names.join(' ')))
  63. end
  64. def visibility_icon(status)
  65. if status.public_visibility?
  66. fa_icon('globe', title: I18n.t('statuses.visibilities.public'))
  67. elsif status.unlisted_visibility?
  68. fa_icon('unlock', title: I18n.t('statuses.visibilities.unlisted'))
  69. elsif status.private_visibility? || status.limited_visibility?
  70. fa_icon('lock', title: I18n.t('statuses.visibilities.private'))
  71. elsif status.direct_visibility?
  72. fa_icon('envelope', title: I18n.t('statuses.visibilities.direct'))
  73. end
  74. end
  75. def custom_emoji_tag(custom_emoji, animate = true)
  76. if animate
  77. image_tag(custom_emoji.image.url, class: 'emojione', alt: ":#{custom_emoji.shortcode}:")
  78. else
  79. 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)))
  80. end
  81. end
  82. def opengraph(property, content)
  83. tag(:meta, content: content, property: property)
  84. end
  85. def react_component(name, props = {}, &block)
  86. if block.nil?
  87. content_tag(:div, nil, data: { component: name.to_s.camelcase, props: Oj.dump(props) })
  88. else
  89. content_tag(:div, data: { component: name.to_s.camelcase, props: Oj.dump(props) }, &block)
  90. end
  91. end
  92. def body_classes
  93. output = (@body_classes || '').split(' ')
  94. output << "theme-#{current_theme.parameterize}"
  95. output << 'system-font' if current_account&.user&.setting_system_font_ui
  96. output << (current_account&.user&.setting_reduce_motion ? 'reduce-motion' : 'no-reduce-motion')
  97. output << 'rtl' if locale_direction == 'rtl'
  98. output.reject(&:blank?).join(' ')
  99. end
  100. def cdn_host
  101. Rails.configuration.action_controller.asset_host
  102. end
  103. def cdn_host?
  104. cdn_host.present?
  105. end
  106. def storage_host
  107. "https://#{ENV['S3_ALIAS_HOST'].presence || ENV['S3_CLOUDFRONT_HOST']}"
  108. end
  109. def storage_host?
  110. ENV['S3_ALIAS_HOST'].present? || ENV['S3_CLOUDFRONT_HOST'].present?
  111. end
  112. def quote_wrap(text, line_width: 80, break_sequence: "\n")
  113. text = word_wrap(text, line_width: line_width - 2, break_sequence: break_sequence)
  114. text.split("\n").map { |line| '> ' + line }.join("\n")
  115. end
  116. def render_initial_state
  117. state_params = {
  118. settings: {
  119. known_fediverse: Setting.show_known_fediverse_at_about_page,
  120. },
  121. text: [params[:title], params[:text], params[:url]].compact.join(' '),
  122. }
  123. permit_visibilities = %w(public unlisted private direct)
  124. default_privacy = current_account&.user&.setting_default_privacy
  125. permit_visibilities.shift(permit_visibilities.index(default_privacy) + 1) if default_privacy.present?
  126. state_params[:visibility] = params[:visibility] if permit_visibilities.include? params[:visibility]
  127. if user_signed_in?
  128. state_params[:settings] = state_params[:settings].merge(Web::Setting.find_by(user: current_user)&.data || {})
  129. state_params[:push_subscription] = current_account.user.web_push_subscription(current_session)
  130. state_params[:current_account] = current_account
  131. state_params[:token] = current_session.token
  132. state_params[:admin] = Account.find_local(Setting.site_contact_username.strip.gsub(/\A@/, ''))
  133. end
  134. json = ActiveModelSerializers::SerializableResource.new(InitialStatePresenter.new(state_params), serializer: InitialStateSerializer).to_json
  135. # rubocop:disable Rails/OutputSafety
  136. content_tag(:script, json_escape(json).html_safe, id: 'initial-state', type: 'application/json')
  137. # rubocop:enable Rails/OutputSafety
  138. end
  139. end