application_helper.rb 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. # frozen_string_literal: true
  2. module ApplicationHelper
  3. DANGEROUS_SCOPES = %w(
  4. read
  5. write
  6. follow
  7. ).freeze
  8. RTL_LOCALES = %i(
  9. ar
  10. fa
  11. he
  12. ku
  13. ).freeze
  14. def friendly_number_to_human(number, **options)
  15. # By default, the number of precision digits used by number_to_human
  16. # is looked up from the locales definition, and rails-i18n comes with
  17. # values that don't seem to make much sense for many languages, so
  18. # override these values with a default of 3 digits of precision.
  19. options[:precision] = 3
  20. options[:strip_insignificant_zeros] = true
  21. number_to_human(number, **options)
  22. end
  23. def active_nav_class(*paths)
  24. paths.any? { |path| current_page?(path) } ? 'active' : ''
  25. end
  26. def active_link_to(label, path, **options)
  27. link_to label, path, options.merge(class: active_nav_class(path))
  28. end
  29. def show_landing_strip?
  30. !user_signed_in? && !single_user_mode?
  31. end
  32. def open_registrations?
  33. Setting.registrations_mode == 'open'
  34. end
  35. def approved_registrations?
  36. Setting.registrations_mode == 'approved'
  37. end
  38. def closed_registrations?
  39. Setting.registrations_mode == 'none'
  40. end
  41. def available_sign_up_path
  42. if closed_registrations?
  43. 'https://joinmastodon.org/#getting-started'
  44. else
  45. new_user_registration_path
  46. end
  47. end
  48. def open_deletion?
  49. Setting.open_deletion
  50. end
  51. def locale_direction
  52. if RTL_LOCALES.include?(I18n.locale)
  53. 'rtl'
  54. else
  55. 'ltr'
  56. end
  57. end
  58. def favicon_path
  59. env_suffix = Rails.env.production? ? '' : '-dev'
  60. "/favicon#{env_suffix}.ico"
  61. end
  62. def title
  63. Rails.env.production? ? site_title : "#{site_title} (Dev)"
  64. end
  65. def class_for_scope(scope)
  66. 'scope-danger' if DANGEROUS_SCOPES.include?(scope.to_s)
  67. end
  68. def can?(action, record)
  69. return false if record.nil?
  70. policy(record).public_send("#{action}?")
  71. end
  72. def fa_icon(icon, attributes = {})
  73. class_names = attributes[:class]&.split(' ') || []
  74. class_names << 'fa'
  75. class_names += icon.split(' ').map { |cl| "fa-#{cl}" }
  76. content_tag(:i, nil, attributes.merge(class: class_names.join(' ')))
  77. end
  78. def visibility_icon(status)
  79. if status.public_visibility?
  80. fa_icon('globe', title: I18n.t('statuses.visibilities.public'))
  81. elsif status.unlisted_visibility?
  82. fa_icon('unlock', title: I18n.t('statuses.visibilities.unlisted'))
  83. elsif status.private_visibility? || status.limited_visibility?
  84. fa_icon('lock', title: I18n.t('statuses.visibilities.private'))
  85. elsif status.direct_visibility?
  86. fa_icon('envelope', title: I18n.t('statuses.visibilities.direct'))
  87. end
  88. end
  89. def interrelationships_icon(relationships, account_id)
  90. if relationships.following[account_id] && relationships.followed_by[account_id]
  91. fa_icon('exchange', title: I18n.t('relationships.mutual'), class: 'fa-fw active passive')
  92. elsif relationships.following[account_id]
  93. fa_icon(locale_direction == 'ltr' ? 'arrow-right' : 'arrow-left', title: I18n.t('relationships.following'), class: 'fa-fw active')
  94. elsif relationships.followed_by[account_id]
  95. fa_icon(locale_direction == 'ltr' ? 'arrow-left' : 'arrow-right', title: I18n.t('relationships.followers'), class: 'fa-fw passive')
  96. end
  97. end
  98. def custom_emoji_tag(custom_emoji, animate = true)
  99. if animate
  100. image_tag(custom_emoji.image.url, class: 'emojione', alt: ":#{custom_emoji.shortcode}:")
  101. else
  102. 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)))
  103. end
  104. end
  105. def opengraph(property, content)
  106. tag(:meta, content: content, property: property)
  107. end
  108. def react_component(name, props = {}, &block)
  109. if block.nil?
  110. content_tag(:div, nil, data: { component: name.to_s.camelcase, props: Oj.dump(props) })
  111. else
  112. content_tag(:div, data: { component: name.to_s.camelcase, props: Oj.dump(props) }, &block)
  113. end
  114. end
  115. def react_admin_component(name, props = {})
  116. content_tag(:div, nil, data: { 'admin-component': name.to_s.camelcase, props: Oj.dump({ locale: I18n.locale }.merge(props)) })
  117. end
  118. def body_classes
  119. output = (@body_classes || '').split(' ')
  120. output << "theme-#{current_theme.parameterize}"
  121. output << 'system-font' if current_account&.user&.setting_system_font_ui
  122. output << (current_account&.user&.setting_reduce_motion ? 'reduce-motion' : 'no-reduce-motion')
  123. output << 'rtl' if locale_direction == 'rtl'
  124. output.reject(&:blank?).join(' ')
  125. end
  126. def cdn_host
  127. Rails.configuration.action_controller.asset_host
  128. end
  129. def cdn_host?
  130. cdn_host.present?
  131. end
  132. def storage_host
  133. "https://#{ENV['S3_ALIAS_HOST'].presence || ENV['S3_CLOUDFRONT_HOST']}"
  134. end
  135. def storage_host?
  136. ENV['S3_ALIAS_HOST'].present? || ENV['S3_CLOUDFRONT_HOST'].present?
  137. end
  138. def quote_wrap(text, line_width: 80, break_sequence: "\n")
  139. text = word_wrap(text, line_width: line_width - 2, break_sequence: break_sequence)
  140. text.split("\n").map { |line| '> ' + line }.join("\n")
  141. end
  142. def render_initial_state
  143. state_params = {
  144. settings: {
  145. known_fediverse: Setting.show_known_fediverse_at_about_page,
  146. },
  147. text: [params[:title], params[:text], params[:url]].compact.join(' '),
  148. }
  149. permit_visibilities = %w(public unlisted private direct)
  150. default_privacy = current_account&.user&.setting_default_privacy
  151. permit_visibilities.shift(permit_visibilities.index(default_privacy) + 1) if default_privacy.present?
  152. state_params[:visibility] = params[:visibility] if permit_visibilities.include? params[:visibility]
  153. if user_signed_in?
  154. state_params[:settings] = state_params[:settings].merge(Web::Setting.find_by(user: current_user)&.data || {})
  155. state_params[:push_subscription] = current_account.user.web_push_subscription(current_session)
  156. state_params[:current_account] = current_account
  157. state_params[:token] = current_session.token
  158. state_params[:admin] = Account.find_local(Setting.site_contact_username.strip.gsub(/\A@/, ''))
  159. end
  160. json = ActiveModelSerializers::SerializableResource.new(InitialStatePresenter.new(state_params), serializer: InitialStateSerializer).to_json
  161. # rubocop:disable Rails/OutputSafety
  162. content_tag(:script, json_escape(json).html_safe, id: 'initial-state', type: 'application/json')
  163. # rubocop:enable Rails/OutputSafety
  164. end
  165. end