application_helper.rb 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 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? || omniauth_only?
  43. 'https://joinmastodon.org/#getting-started'
  44. else
  45. ENV.fetch('SSO_ACCOUNT_SIGN_UP', new_user_registration_path)
  46. end
  47. end
  48. def omniauth_only?
  49. ENV['OMNIAUTH_ONLY'] == 'true'
  50. end
  51. def link_to_login(name = nil, html_options = nil, &block)
  52. target = new_user_session_path
  53. html_options = name if block
  54. if omniauth_only? && Devise.mappings[:user].omniauthable? && User.omniauth_providers.size == 1
  55. target = omniauth_authorize_path(:user, User.omniauth_providers[0])
  56. html_options ||= {}
  57. html_options[:method] = :post
  58. end
  59. if block
  60. link_to(target, html_options, &block)
  61. else
  62. link_to(name, target, html_options)
  63. end
  64. end
  65. def provider_sign_in_link(provider)
  66. label = Devise.omniauth_configs[provider]&.strategy&.display_name.presence || I18n.t("auth.providers.#{provider}", default: provider.to_s.chomp('_oauth2').capitalize)
  67. link_to label, omniauth_authorize_path(:user, provider), class: "button button-#{provider}", method: :post
  68. end
  69. def locale_direction
  70. if RTL_LOCALES.include?(I18n.locale)
  71. 'rtl'
  72. else
  73. 'ltr'
  74. end
  75. end
  76. def html_title
  77. safe_join(
  78. [content_for(:page_title).to_s.chomp, title]
  79. .select(&:present?),
  80. ' - '
  81. )
  82. end
  83. def title
  84. Rails.env.production? ? site_title : "#{site_title} (Dev)"
  85. end
  86. def class_for_scope(scope)
  87. 'scope-danger' if DANGEROUS_SCOPES.include?(scope.to_s)
  88. end
  89. def can?(action, record)
  90. return false if record.nil?
  91. policy(record).public_send("#{action}?")
  92. end
  93. def fa_icon(icon, attributes = {})
  94. class_names = attributes[:class]&.split || []
  95. class_names << 'fa'
  96. class_names += icon.split.map { |cl| "fa-#{cl}" }
  97. content_tag(:i, nil, attributes.merge(class: class_names.join(' ')))
  98. end
  99. def check_icon
  100. content_tag(:svg, tag.path('fill-rule': 'evenodd', 'clip-rule': 'evenodd', d: 'M16.704 4.153a.75.75 0 01.143 1.052l-8 10.5a.75.75 0 01-1.127.075l-4.5-4.5a.75.75 0 011.06-1.06l3.894 3.893 7.48-9.817a.75.75 0 011.05-.143z'), xmlns: 'http://www.w3.org/2000/svg', viewBox: '0 0 20 20', fill: 'currentColor')
  101. end
  102. def visibility_icon(status)
  103. if status.public_visibility?
  104. fa_icon('globe', title: I18n.t('statuses.visibilities.public'))
  105. elsif status.unlisted_visibility?
  106. fa_icon('unlock', title: I18n.t('statuses.visibilities.unlisted'))
  107. elsif status.private_visibility? || status.limited_visibility?
  108. fa_icon('lock', title: I18n.t('statuses.visibilities.private'))
  109. elsif status.direct_visibility?
  110. fa_icon('at', title: I18n.t('statuses.visibilities.direct'))
  111. end
  112. end
  113. def interrelationships_icon(relationships, account_id)
  114. if relationships.following[account_id] && relationships.followed_by[account_id]
  115. fa_icon('exchange', title: I18n.t('relationships.mutual'), class: 'fa-fw active passive')
  116. elsif relationships.following[account_id]
  117. fa_icon(locale_direction == 'ltr' ? 'arrow-right' : 'arrow-left', title: I18n.t('relationships.following'), class: 'fa-fw active')
  118. elsif relationships.followed_by[account_id]
  119. fa_icon(locale_direction == 'ltr' ? 'arrow-left' : 'arrow-right', title: I18n.t('relationships.followers'), class: 'fa-fw passive')
  120. end
  121. end
  122. def custom_emoji_tag(custom_emoji)
  123. if prefers_autoplay?
  124. image_tag(custom_emoji.image.url, class: 'emojione', alt: ":#{custom_emoji.shortcode}:")
  125. else
  126. 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)))
  127. end
  128. end
  129. def opengraph(property, content)
  130. tag.meta(content: content, property: property)
  131. end
  132. def body_classes
  133. output = body_class_string.split
  134. output << "theme-#{current_theme.parameterize}"
  135. output << 'system-font' if current_account&.user&.setting_system_font_ui
  136. output << (current_account&.user&.setting_reduce_motion ? 'reduce-motion' : 'no-reduce-motion')
  137. output << 'rtl' if locale_direction == 'rtl'
  138. output.compact_blank.join(' ')
  139. end
  140. def cdn_host
  141. Rails.configuration.action_controller.asset_host
  142. end
  143. def cdn_host?
  144. cdn_host.present?
  145. end
  146. def storage_host
  147. "https://#{storage_host_var}"
  148. end
  149. def storage_host?
  150. storage_host_var.present?
  151. end
  152. def quote_wrap(text, line_width: 80, break_sequence: "\n")
  153. text = word_wrap(text, line_width: line_width - 2, break_sequence: break_sequence)
  154. text.split("\n").map { |line| "> #{line}" }.join("\n")
  155. end
  156. def render_initial_state
  157. state_params = {
  158. settings: {},
  159. text: [params[:title], params[:text], params[:url]].compact.join(' '),
  160. }
  161. permit_visibilities = %w(public unlisted private direct)
  162. default_privacy = current_account&.user&.setting_default_privacy
  163. permit_visibilities.shift(permit_visibilities.index(default_privacy) + 1) if default_privacy.present?
  164. state_params[:visibility] = params[:visibility] if permit_visibilities.include? params[:visibility]
  165. if user_signed_in? && current_user.functional?
  166. state_params[:settings] = state_params[:settings].merge(Web::Setting.find_by(user: current_user)&.data || {})
  167. state_params[:push_subscription] = current_account.user.web_push_subscription(current_session)
  168. state_params[:current_account] = current_account
  169. state_params[:token] = current_session.token
  170. state_params[:admin] = Account.find_local(Setting.site_contact_username.strip.gsub(/\A@/, ''))
  171. end
  172. if user_signed_in? && !current_user.functional?
  173. state_params[:disabled_account] = current_account
  174. state_params[:moved_to_account] = current_account.moved_to_account
  175. end
  176. state_params[:owner] = Account.local.without_suspended.where('id > 0').first if single_user_mode?
  177. json = ActiveModelSerializers::SerializableResource.new(InitialStatePresenter.new(state_params), serializer: InitialStateSerializer).to_json
  178. # rubocop:disable Rails/OutputSafety
  179. content_tag(:script, json_escape(json).html_safe, id: 'initial-state', type: 'application/json')
  180. # rubocop:enable Rails/OutputSafety
  181. end
  182. def grouped_scopes(scopes)
  183. scope_parser = ScopeParser.new
  184. scope_transformer = ScopeTransformer.new
  185. scopes.each_with_object({}) do |str, h|
  186. scope = scope_transformer.apply(scope_parser.parse(str))
  187. if h[scope.key]
  188. h[scope.key].merge!(scope)
  189. else
  190. h[scope.key] = scope
  191. end
  192. end.values
  193. end
  194. def prerender_custom_emojis(html, custom_emojis, other_options = {})
  195. EmojiFormatter.new(html, custom_emojis, other_options.merge(animate: prefers_autoplay?)).to_s
  196. end
  197. private
  198. def storage_host_var
  199. ENV.fetch('S3_ALIAS_HOST', nil) || ENV.fetch('S3_CLOUDFRONT_HOST', nil) || ENV.fetch('AZURE_ALIAS_HOST', nil)
  200. end
  201. end