application_helper.rb 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. ckb
  11. fa
  12. he
  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 = options.merge(
  20. precision: 3,
  21. strip_insignificant_zeros: true,
  22. significant: true
  23. )
  24. number_to_human(number, **options)
  25. end
  26. def active_nav_class(*paths)
  27. paths.any? { |path| current_page?(path) } ? 'active' : ''
  28. end
  29. def active_link_to(label, path, **options)
  30. link_to label, path, options.merge(class: active_nav_class(path))
  31. end
  32. def show_landing_strip?
  33. !user_signed_in? && !single_user_mode?
  34. end
  35. def open_registrations?
  36. Setting.registrations_mode == 'open'
  37. end
  38. def approved_registrations?
  39. Setting.registrations_mode == 'approved'
  40. end
  41. def closed_registrations?
  42. Setting.registrations_mode == 'none'
  43. end
  44. def available_sign_up_path
  45. if closed_registrations? || omniauth_only?
  46. 'https://joinmastodon.org/#getting-started'
  47. else
  48. new_user_registration_path
  49. end
  50. end
  51. def omniauth_only?
  52. ENV['OMNIAUTH_ONLY'] == 'true'
  53. end
  54. def link_to_login(name = nil, html_options = nil, &block)
  55. target = new_user_session_path
  56. html_options = name if block_given?
  57. if omniauth_only? && Devise.mappings[:user].omniauthable? && User.omniauth_providers.size == 1
  58. target = omniauth_authorize_path(:user, User.omniauth_providers[0])
  59. html_options ||= {}
  60. html_options[:method] = :post
  61. end
  62. if block_given?
  63. link_to(target, html_options, &block)
  64. else
  65. link_to(name, target, html_options)
  66. end
  67. end
  68. def provider_sign_in_link(provider)
  69. label = Devise.omniauth_configs[provider]&.strategy&.display_name.presence || I18n.t("auth.providers.#{provider}", default: provider.to_s.chomp('_oauth2').capitalize)
  70. link_to label, omniauth_authorize_path(:user, provider), class: "button button-#{provider}", method: :post
  71. end
  72. def open_deletion?
  73. Setting.open_deletion
  74. end
  75. def locale_direction
  76. if RTL_LOCALES.include?(I18n.locale)
  77. 'rtl'
  78. else
  79. 'ltr'
  80. end
  81. end
  82. def title
  83. Rails.env.production? ? site_title : "#{site_title} (Dev)"
  84. end
  85. def class_for_scope(scope)
  86. 'scope-danger' if DANGEROUS_SCOPES.include?(scope.to_s)
  87. end
  88. def can?(action, record)
  89. return false if record.nil?
  90. policy(record).public_send("#{action}?")
  91. end
  92. def fa_icon(icon, attributes = {})
  93. class_names = attributes[:class]&.split(' ') || []
  94. class_names << 'fa'
  95. class_names += icon.split(' ').map { |cl| "fa-#{cl}" }
  96. content_tag(:i, nil, attributes.merge(class: class_names.join(' ')))
  97. end
  98. def visibility_icon(status)
  99. if status.public_visibility?
  100. fa_icon('globe', title: I18n.t('statuses.visibilities.public'))
  101. elsif status.unlisted_visibility?
  102. fa_icon('unlock', title: I18n.t('statuses.visibilities.unlisted'))
  103. elsif status.private_visibility? || status.limited_visibility?
  104. fa_icon('lock', title: I18n.t('statuses.visibilities.private'))
  105. elsif status.direct_visibility?
  106. fa_icon('at', title: I18n.t('statuses.visibilities.direct'))
  107. end
  108. end
  109. def interrelationships_icon(relationships, account_id)
  110. if relationships.following[account_id] && relationships.followed_by[account_id]
  111. fa_icon('exchange', title: I18n.t('relationships.mutual'), class: 'fa-fw active passive')
  112. elsif relationships.following[account_id]
  113. fa_icon(locale_direction == 'ltr' ? 'arrow-right' : 'arrow-left', title: I18n.t('relationships.following'), class: 'fa-fw active')
  114. elsif relationships.followed_by[account_id]
  115. fa_icon(locale_direction == 'ltr' ? 'arrow-left' : 'arrow-right', title: I18n.t('relationships.followers'), class: 'fa-fw passive')
  116. end
  117. end
  118. def custom_emoji_tag(custom_emoji)
  119. if prefers_autoplay?
  120. image_tag(custom_emoji.image.url, class: 'emojione', alt: ":#{custom_emoji.shortcode}:")
  121. else
  122. 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)))
  123. end
  124. end
  125. def opengraph(property, content)
  126. tag(:meta, content: content, property: property)
  127. end
  128. def react_component(name, props = {}, &block)
  129. if block.nil?
  130. content_tag(:div, nil, data: { component: name.to_s.camelcase, props: Oj.dump(props) })
  131. else
  132. content_tag(:div, data: { component: name.to_s.camelcase, props: Oj.dump(props) }, &block)
  133. end
  134. end
  135. def react_admin_component(name, props = {})
  136. content_tag(:div, nil, data: { 'admin-component': name.to_s.camelcase, props: Oj.dump({ locale: I18n.locale }.merge(props)) })
  137. end
  138. def body_classes
  139. output = (@body_classes || '').split(' ')
  140. output << "theme-#{current_theme.parameterize}"
  141. output << 'system-font' if current_account&.user&.setting_system_font_ui
  142. output << (current_account&.user&.setting_reduce_motion ? 'reduce-motion' : 'no-reduce-motion')
  143. output << 'rtl' if locale_direction == 'rtl'
  144. output.reject(&:blank?).join(' ')
  145. end
  146. def cdn_host
  147. Rails.configuration.action_controller.asset_host
  148. end
  149. def cdn_host?
  150. cdn_host.present?
  151. end
  152. def storage_host
  153. "https://#{ENV['S3_ALIAS_HOST'].presence || ENV['S3_CLOUDFRONT_HOST']}"
  154. end
  155. def storage_host?
  156. ENV['S3_ALIAS_HOST'].present? || ENV['S3_CLOUDFRONT_HOST'].present?
  157. end
  158. def quote_wrap(text, line_width: 80, break_sequence: "\n")
  159. text = word_wrap(text, line_width: line_width - 2, break_sequence: break_sequence)
  160. text.split("\n").map { |line| "> #{line}" }.join("\n")
  161. end
  162. def render_initial_state
  163. state_params = {
  164. settings: {},
  165. text: [params[:title], params[:text], params[:url]].compact.join(' '),
  166. }
  167. permit_visibilities = %w(public unlisted private direct)
  168. default_privacy = current_account&.user&.setting_default_privacy
  169. permit_visibilities.shift(permit_visibilities.index(default_privacy) + 1) if default_privacy.present?
  170. state_params[:visibility] = params[:visibility] if permit_visibilities.include? params[:visibility]
  171. if user_signed_in?
  172. state_params[:settings] = state_params[:settings].merge(Web::Setting.find_by(user: current_user)&.data || {})
  173. state_params[:push_subscription] = current_account.user.web_push_subscription(current_session)
  174. state_params[:current_account] = current_account
  175. state_params[:token] = current_session.token
  176. state_params[:admin] = Account.find_local(Setting.site_contact_username.strip.gsub(/\A@/, ''))
  177. end
  178. json = ActiveModelSerializers::SerializableResource.new(InitialStatePresenter.new(state_params), serializer: InitialStateSerializer).to_json
  179. # rubocop:disable Rails/OutputSafety
  180. content_tag(:script, json_escape(json).html_safe, id: 'initial-state', type: 'application/json')
  181. # rubocop:enable Rails/OutputSafety
  182. end
  183. def grouped_scopes(scopes)
  184. scope_parser = ScopeParser.new
  185. scope_transformer = ScopeTransformer.new
  186. scopes.each_with_object({}) do |str, h|
  187. scope = scope_transformer.apply(scope_parser.parse(str))
  188. if h[scope.key]
  189. h[scope.key].merge!(scope)
  190. else
  191. h[scope.key] = scope
  192. end
  193. end.values
  194. end
  195. def prerender_custom_emojis(html, custom_emojis, other_options = {})
  196. EmojiFormatter.new(html, custom_emojis, other_options.merge(animate: prefers_autoplay?)).to_s
  197. end
  198. end