You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

247 lines
8.0 KiB

8 years ago
8 years ago
  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
  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
  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 << "flavour-#{current_flavour.parameterize}"
  138. output << "skin-#{current_skin.parameterize}"
  139. output << 'system-font' if current_account&.user&.setting_system_font_ui
  140. output << (current_account&.user&.setting_reduce_motion ? 'reduce-motion' : 'no-reduce-motion')
  141. output << 'rtl' if locale_direction == 'rtl'
  142. output.reject(&:blank?).join(' ')
  143. end
  144. def cdn_host
  145. Rails.configuration.action_controller.asset_host
  146. end
  147. def cdn_host?
  148. cdn_host.present?
  149. end
  150. def storage_host
  151. "https://#{ENV['S3_ALIAS_HOST'].presence || ENV['S3_CLOUDFRONT_HOST']}"
  152. end
  153. def storage_host?
  154. ENV['S3_ALIAS_HOST'].present? || ENV['S3_CLOUDFRONT_HOST'].present?
  155. end
  156. def quote_wrap(text, line_width: 80, break_sequence: "\n")
  157. text = word_wrap(text, line_width: line_width - 2, break_sequence: break_sequence)
  158. text.split("\n").map { |line| "> #{line}" }.join("\n")
  159. end
  160. def render_initial_state
  161. state_params = {
  162. settings: {},
  163. text: [params[:title], params[:text], params[:url]].compact.join(' '),
  164. }
  165. permit_visibilities = %w(public unlisted private direct)
  166. default_privacy = current_account&.user&.setting_default_privacy
  167. permit_visibilities.shift(permit_visibilities.index(default_privacy) + 1) if default_privacy.present?
  168. state_params[:visibility] = params[:visibility] if permit_visibilities.include? params[:visibility]
  169. if user_signed_in? && current_user.functional?
  170. state_params[:settings] = state_params[:settings].merge(Web::Setting.find_by(user: current_user)&.data || {})
  171. state_params[:push_subscription] = current_account.user.web_push_subscription(current_session)
  172. state_params[:current_account] = current_account
  173. state_params[:token] = current_session.token
  174. state_params[:admin] = Account.find_local(Setting.site_contact_username.strip.gsub(/\A@/, ''))
  175. end
  176. if user_signed_in? && !current_user.functional?
  177. state_params[:disabled_account] = current_account
  178. state_params[:moved_to_account] = current_account.moved_to_account
  179. end
  180. state_params[:owner] = Account.local.without_suspended.where('id > 0').first if single_user_mode?
  181. json = ActiveModelSerializers::SerializableResource.new(InitialStatePresenter.new(state_params), serializer: InitialStateSerializer).to_json
  182. # rubocop:disable Rails/OutputSafety
  183. content_tag(:script, json_escape(json).html_safe, id: 'initial-state', type: 'application/json')
  184. # rubocop:enable Rails/OutputSafety
  185. end
  186. def grouped_scopes(scopes)
  187. scope_parser = ScopeParser.new
  188. scope_transformer = ScopeTransformer.new
  189. scopes.each_with_object({}) do |str, h|
  190. scope = scope_transformer.apply(scope_parser.parse(str))
  191. if h[scope.key]
  192. h[scope.key].merge!(scope)
  193. else
  194. h[scope.key] = scope
  195. end
  196. end.values
  197. end
  198. def prerender_custom_emojis(html, custom_emojis, other_options = {})
  199. EmojiFormatter.new(html, custom_emojis, other_options.merge(animate: prefers_autoplay?)).to_s
  200. end
  201. end