application_helper.rb 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 locale_direction
  73. if RTL_LOCALES.include?(I18n.locale)
  74. 'rtl'
  75. else
  76. 'ltr'
  77. end
  78. end
  79. def title
  80. Rails.env.production? ? site_title : "#{site_title} (Dev)"
  81. end
  82. def class_for_scope(scope)
  83. 'scope-danger' if DANGEROUS_SCOPES.include?(scope.to_s)
  84. end
  85. def can?(action, record)
  86. return false if record.nil?
  87. policy(record).public_send("#{action}?")
  88. end
  89. def fa_icon(icon, attributes = {})
  90. class_names = attributes[:class]&.split(' ') || []
  91. class_names << 'fa'
  92. class_names += icon.split(' ').map { |cl| "fa-#{cl}" }
  93. content_tag(:i, nil, attributes.merge(class: class_names.join(' ')))
  94. end
  95. def visibility_icon(status)
  96. if status.public_visibility?
  97. fa_icon('globe', title: I18n.t('statuses.visibilities.public'))
  98. elsif status.unlisted_visibility?
  99. fa_icon('unlock', title: I18n.t('statuses.visibilities.unlisted'))
  100. elsif status.private_visibility? || status.limited_visibility?
  101. fa_icon('lock', title: I18n.t('statuses.visibilities.private'))
  102. elsif status.direct_visibility?
  103. fa_icon('at', title: I18n.t('statuses.visibilities.direct'))
  104. end
  105. end
  106. def interrelationships_icon(relationships, account_id)
  107. if relationships.following[account_id] && relationships.followed_by[account_id]
  108. fa_icon('exchange', title: I18n.t('relationships.mutual'), class: 'fa-fw active passive')
  109. elsif relationships.following[account_id]
  110. fa_icon(locale_direction == 'ltr' ? 'arrow-right' : 'arrow-left', title: I18n.t('relationships.following'), class: 'fa-fw active')
  111. elsif relationships.followed_by[account_id]
  112. fa_icon(locale_direction == 'ltr' ? 'arrow-left' : 'arrow-right', title: I18n.t('relationships.followers'), class: 'fa-fw passive')
  113. end
  114. end
  115. def custom_emoji_tag(custom_emoji)
  116. if prefers_autoplay?
  117. image_tag(custom_emoji.image.url, class: 'emojione', alt: ":#{custom_emoji.shortcode}:")
  118. else
  119. 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)))
  120. end
  121. end
  122. def opengraph(property, content)
  123. tag(:meta, content: content, property: property)
  124. end
  125. def react_component(name, props = {}, &block)
  126. if block.nil?
  127. content_tag(:div, nil, data: { component: name.to_s.camelcase, props: Oj.dump(props) })
  128. else
  129. content_tag(:div, data: { component: name.to_s.camelcase, props: Oj.dump(props) }, &block)
  130. end
  131. end
  132. def react_admin_component(name, props = {})
  133. content_tag(:div, nil, data: { 'admin-component': name.to_s.camelcase, props: Oj.dump({ locale: I18n.locale }.merge(props)) })
  134. end
  135. def body_classes
  136. output = (@body_classes || '').split(' ')
  137. output << "theme-#{current_theme.parameterize}"
  138. output << 'system-font' if current_account&.user&.setting_system_font_ui
  139. output << (current_account&.user&.setting_reduce_motion ? 'reduce-motion' : 'no-reduce-motion')
  140. output << 'rtl' if locale_direction == 'rtl'
  141. output.reject(&:blank?).join(' ')
  142. end
  143. def cdn_host
  144. Rails.configuration.action_controller.asset_host
  145. end
  146. def cdn_host?
  147. cdn_host.present?
  148. end
  149. def storage_host
  150. "https://#{ENV['S3_ALIAS_HOST'].presence || ENV['S3_CLOUDFRONT_HOST']}"
  151. end
  152. def storage_host?
  153. ENV['S3_ALIAS_HOST'].present? || ENV['S3_CLOUDFRONT_HOST'].present?
  154. end
  155. def quote_wrap(text, line_width: 80, break_sequence: "\n")
  156. text = word_wrap(text, line_width: line_width - 2, break_sequence: break_sequence)
  157. text.split("\n").map { |line| "> #{line}" }.join("\n")
  158. end
  159. def render_initial_state
  160. state_params = {
  161. settings: {},
  162. text: [params[:title], params[:text], params[:url]].compact.join(' '),
  163. }
  164. permit_visibilities = %w(public unlisted private direct)
  165. default_privacy = current_account&.user&.setting_default_privacy
  166. permit_visibilities.shift(permit_visibilities.index(default_privacy) + 1) if default_privacy.present?
  167. state_params[:visibility] = params[:visibility] if permit_visibilities.include? params[:visibility]
  168. if user_signed_in? && current_user.functional?
  169. state_params[:settings] = state_params[:settings].merge(Web::Setting.find_by(user: current_user)&.data || {})
  170. state_params[:push_subscription] = current_account.user.web_push_subscription(current_session)
  171. state_params[:current_account] = current_account
  172. state_params[:token] = current_session.token
  173. state_params[:admin] = Account.find_local(Setting.site_contact_username.strip.gsub(/\A@/, ''))
  174. end
  175. if user_signed_in? && !current_user.functional?
  176. state_params[:disabled_account] = current_account
  177. state_params[:moved_to_account] = current_account.moved_to_account
  178. end
  179. if single_user_mode?
  180. state_params[:owner] = Account.local.without_suspended.where('id > 0').first
  181. end
  182. json = ActiveModelSerializers::SerializableResource.new(InitialStatePresenter.new(state_params), serializer: InitialStateSerializer).to_json
  183. # rubocop:disable Rails/OutputSafety
  184. content_tag(:script, json_escape(json).html_safe, id: 'initial-state', type: 'application/json')
  185. # rubocop:enable Rails/OutputSafety
  186. end
  187. def grouped_scopes(scopes)
  188. scope_parser = ScopeParser.new
  189. scope_transformer = ScopeTransformer.new
  190. scopes.each_with_object({}) do |str, h|
  191. scope = scope_transformer.apply(scope_parser.parse(str))
  192. if h[scope.key]
  193. h[scope.key].merge!(scope)
  194. else
  195. h[scope.key] = scope
  196. end
  197. end.values
  198. end
  199. def prerender_custom_emojis(html, custom_emojis, other_options = {})
  200. EmojiFormatter.new(html, custom_emojis, other_options.merge(animate: prefers_autoplay?)).to_s
  201. end
  202. end