1
0

application_helper.rb 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. # frozen_string_literal: true
  2. module ApplicationHelper
  3. RTL_LOCALES = %i(
  4. ar
  5. ckb
  6. fa
  7. he
  8. ).freeze
  9. def friendly_number_to_human(number, **options)
  10. # By default, the number of precision digits used by number_to_human
  11. # is looked up from the locales definition, and rails-i18n comes with
  12. # values that don't seem to make much sense for many languages, so
  13. # override these values with a default of 3 digits of precision.
  14. options = options.merge(
  15. precision: 3,
  16. strip_insignificant_zeros: true,
  17. significant: true
  18. )
  19. number_to_human(number, **options)
  20. end
  21. def open_registrations?
  22. Setting.registrations_mode == 'open'
  23. end
  24. def approved_registrations?
  25. Setting.registrations_mode == 'approved'
  26. end
  27. def closed_registrations?
  28. Setting.registrations_mode == 'none'
  29. end
  30. def available_sign_up_path
  31. if closed_registrations? || omniauth_only?
  32. 'https://joinmastodon.org/#getting-started'
  33. else
  34. ENV.fetch('SSO_ACCOUNT_SIGN_UP', new_user_registration_path)
  35. end
  36. end
  37. def omniauth_only?
  38. ENV['OMNIAUTH_ONLY'] == 'true'
  39. end
  40. def link_to_login(name = nil, html_options = nil, &block)
  41. target = new_user_session_path
  42. html_options = name if block
  43. if omniauth_only? && Devise.mappings[:user].omniauthable? && User.omniauth_providers.size == 1
  44. target = omniauth_authorize_path(:user, User.omniauth_providers[0])
  45. html_options ||= {}
  46. html_options[:method] = :post
  47. end
  48. if block
  49. link_to(target, html_options, &block)
  50. else
  51. link_to(name, target, html_options)
  52. end
  53. end
  54. def provider_sign_in_link(provider)
  55. label = Devise.omniauth_configs[provider]&.strategy&.display_name.presence || I18n.t("auth.providers.#{provider}", default: provider.to_s.chomp('_oauth2').capitalize)
  56. link_to label, omniauth_authorize_path(:user, provider), class: "button button-#{provider}", method: :post
  57. end
  58. def locale_direction
  59. if RTL_LOCALES.include?(I18n.locale)
  60. 'rtl'
  61. else
  62. 'ltr'
  63. end
  64. end
  65. def html_title
  66. safe_join(
  67. [content_for(:page_title), title]
  68. .compact_blank,
  69. ' - '
  70. )
  71. end
  72. def title
  73. Rails.env.production? ? site_title : "#{site_title} (Dev)"
  74. end
  75. def label_for_scope(scope)
  76. safe_join [
  77. tag.samp(scope, class: { 'scope-danger' => SessionActivation::DEFAULT_SCOPES.include?(scope.to_s) }),
  78. tag.span(t("doorkeeper.scopes.#{scope}"), class: :hint),
  79. ]
  80. end
  81. def can?(action, record)
  82. return false if record.nil?
  83. policy(record).public_send(:"#{action}?")
  84. end
  85. def material_symbol(icon, attributes = {})
  86. safe_join(
  87. [
  88. inline_svg_tag(
  89. "400-24px/#{icon}.svg",
  90. class: ['icon', "material-#{icon}"].concat(attributes[:class].to_s.split),
  91. role: :img,
  92. data: attributes[:data]
  93. ),
  94. ' ',
  95. ]
  96. )
  97. end
  98. def check_icon
  99. inline_svg_tag 'check.svg'
  100. end
  101. def interrelationships_icon(relationships, account_id)
  102. if relationships.following[account_id] && relationships.followed_by[account_id]
  103. material_symbol('sync_alt', title: I18n.t('relationships.mutual'), class: 'active passive')
  104. elsif relationships.following[account_id]
  105. material_symbol(locale_direction == 'ltr' ? 'arrow_right_alt' : 'arrow_left_alt', title: I18n.t('relationships.following'), class: 'active')
  106. elsif relationships.followed_by[account_id]
  107. material_symbol(locale_direction == 'ltr' ? 'arrow_left_alt' : 'arrow_right_alt', title: I18n.t('relationships.followers'), class: 'passive')
  108. end
  109. end
  110. def custom_emoji_tag(custom_emoji)
  111. if prefers_autoplay?
  112. image_tag(custom_emoji.image.url, class: 'emojione', alt: ":#{custom_emoji.shortcode}:")
  113. else
  114. 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)))
  115. end
  116. end
  117. def opengraph(property, content)
  118. tag.meta(content: content, property: property)
  119. end
  120. def body_classes
  121. output = body_class_string.split
  122. output << content_for(:body_classes)
  123. output << "theme-#{current_theme.parameterize}"
  124. output << 'system-font' if current_account&.user&.setting_system_font_ui
  125. output << (current_account&.user&.setting_reduce_motion ? 'reduce-motion' : 'no-reduce-motion')
  126. output << 'rtl' if locale_direction == 'rtl'
  127. output.compact_blank.join(' ')
  128. end
  129. def cdn_host
  130. Rails.configuration.action_controller.asset_host
  131. end
  132. def cdn_host?
  133. cdn_host.present?
  134. end
  135. def storage_host
  136. "https://#{storage_host_var}"
  137. end
  138. def storage_host?
  139. storage_host_var.present?
  140. end
  141. def quote_wrap(text, line_width: 80, break_sequence: "\n")
  142. text = word_wrap(text, line_width: line_width - 2, break_sequence: break_sequence)
  143. text.split("\n").map { |line| "> #{line}" }.join("\n")
  144. end
  145. def render_initial_state
  146. state_params = {
  147. settings: {},
  148. text: [params[:title], params[:text], params[:url]].compact.join(' '),
  149. }
  150. permit_visibilities = %w(public unlisted private direct)
  151. default_privacy = current_account&.user&.setting_default_privacy
  152. permit_visibilities.shift(permit_visibilities.index(default_privacy) + 1) if default_privacy.present?
  153. state_params[:visibility] = params[:visibility] if permit_visibilities.include? params[:visibility]
  154. if user_signed_in? && current_user.functional?
  155. state_params[:settings] = state_params[:settings].merge(Web::Setting.find_by(user: current_user)&.data || {})
  156. state_params[:push_subscription] = current_account.user.web_push_subscription(current_session)
  157. state_params[:current_account] = current_account
  158. state_params[:token] = current_session.token
  159. state_params[:admin] = Account.find_local(Setting.site_contact_username.strip.gsub(/\A@/, ''))
  160. end
  161. if user_signed_in? && !current_user.functional?
  162. state_params[:disabled_account] = current_account
  163. state_params[:moved_to_account] = current_account.moved_to_account
  164. end
  165. state_params[:owner] = Account.local.without_suspended.without_internal.first if single_user_mode?
  166. json = ActiveModelSerializers::SerializableResource.new(InitialStatePresenter.new(state_params), serializer: InitialStateSerializer).to_json
  167. # rubocop:disable Rails/OutputSafety
  168. content_tag(:script, json_escape(json).html_safe, id: 'initial-state', type: 'application/json')
  169. # rubocop:enable Rails/OutputSafety
  170. end
  171. def grouped_scopes(scopes)
  172. scope_parser = ScopeParser.new
  173. scope_transformer = ScopeTransformer.new
  174. scopes.each_with_object({}) do |str, h|
  175. scope = scope_transformer.apply(scope_parser.parse(str))
  176. if h[scope.key]
  177. h[scope.key].merge!(scope)
  178. else
  179. h[scope.key] = scope
  180. end
  181. end.values
  182. end
  183. def prerender_custom_emojis(html, custom_emojis, other_options = {})
  184. EmojiFormatter.new(html, custom_emojis, other_options.merge(animate: prefers_autoplay?)).to_s
  185. end
  186. def mascot_url
  187. full_asset_url(instance_presenter.mascot&.file&.url || frontend_asset_path('images/elephant_ui_plane.svg'))
  188. end
  189. def copyable_input(options = {})
  190. tag.input(type: :text, maxlength: 999, spellcheck: false, readonly: true, **options)
  191. end
  192. def recent_tag_usage(tag)
  193. people = tag.history.aggregate(2.days.ago.to_date..Time.zone.today).accounts
  194. I18n.t 'user_mailer.welcome.hashtags_recent_count', people: number_with_delimiter(people), count: people
  195. end
  196. private
  197. def storage_host_var
  198. ENV.fetch('S3_ALIAS_HOST', nil) || ENV.fetch('S3_CLOUDFRONT_HOST', nil) || ENV.fetch('AZURE_ALIAS_HOST', nil)
  199. end
  200. end