application_helper.rb 5.8 KB

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