闭社主体 forked from https://github.com/tootsuite/mastodon
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.

187 lines
5.8 KiB

8 years ago
3 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. fa
  11. he
  12. ku
  13. ).freeze
  14. def active_nav_class(*paths)
  15. paths.any? { |path| current_page?(path) } ? 'active' : ''
  16. end
  17. def active_link_to(label, path, **options)
  18. link_to label, path, options.merge(class: active_nav_class(path))
  19. end
  20. def show_landing_strip?
  21. !user_signed_in? && !single_user_mode?
  22. end
  23. def open_registrations?
  24. Setting.registrations_mode == 'open'
  25. end
  26. def approved_registrations?
  27. Setting.registrations_mode == 'approved'
  28. end
  29. def closed_registrations?
  30. Setting.registrations_mode == 'none'
  31. end
  32. def available_sign_up_path
  33. if closed_registrations?
  34. #'https://joinmastodon.org/#getting-started'
  35. 'https://closed.social'
  36. else
  37. new_user_registration_path
  38. end
  39. end
  40. def open_deletion?
  41. Setting.open_deletion
  42. end
  43. def locale_direction
  44. if RTL_LOCALES.include?(I18n.locale)
  45. 'rtl'
  46. else
  47. 'ltr'
  48. end
  49. end
  50. def favicon_path
  51. env_suffix = Rails.env.production? ? '' : '-dev'
  52. "/favicon#{env_suffix}.ico"
  53. end
  54. def title
  55. Rails.env.production? ? site_title : "#{site_title} (Dev)"
  56. end
  57. def class_for_scope(scope)
  58. 'scope-danger' if DANGEROUS_SCOPES.include?(scope.to_s)
  59. end
  60. def can?(action, record)
  61. return false if record.nil?
  62. policy(record).public_send("#{action}?")
  63. end
  64. def fa_icon(icon, attributes = {})
  65. class_names = attributes[:class]&.split(' ') || []
  66. class_names << 'fa'
  67. class_names += icon.split(' ').map { |cl| "fa-#{cl}" }
  68. content_tag(:i, nil, attributes.merge(class: class_names.join(' ')))
  69. end
  70. def visibility_icon(status)
  71. if status.public_visibility?
  72. fa_icon('globe', title: I18n.t('statuses.visibilities.public'))
  73. elsif status.unlisted_visibility?
  74. fa_icon('unlock', title: I18n.t('statuses.visibilities.unlisted'))
  75. elsif status.private_visibility? || status.limited_visibility?
  76. fa_icon('lock', title: I18n.t('statuses.visibilities.private'))
  77. elsif status.direct_visibility?
  78. fa_icon('envelope', title: I18n.t('statuses.visibilities.direct'))
  79. end
  80. end
  81. def interrelationships_icon(relationships, account_id)
  82. if relationships.following[account_id] && relationships.followed_by[account_id]
  83. fa_icon('exchange', title: I18n.t('relationships.mutual'), class: 'fa-fw active passive')
  84. elsif relationships.following[account_id]
  85. fa_icon(locale_direction == 'ltr' ? 'arrow-right' : 'arrow-left', title: I18n.t('relationships.following'), class: 'fa-fw active')
  86. elsif relationships.followed_by[account_id]
  87. fa_icon(locale_direction == 'ltr' ? 'arrow-left' : 'arrow-right', title: I18n.t('relationships.followers'), class: 'fa-fw passive')
  88. end
  89. end
  90. def custom_emoji_tag(custom_emoji, animate = true)
  91. if animate
  92. image_tag(custom_emoji.image.url, class: 'emojione', alt: ":#{custom_emoji.shortcode}:")
  93. else
  94. 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)))
  95. end
  96. end
  97. def opengraph(property, content)
  98. tag(:meta, content: content, property: property)
  99. end
  100. def react_component(name, props = {}, &block)
  101. if block.nil?
  102. content_tag(:div, nil, data: { component: name.to_s.camelcase, props: Oj.dump(props) })
  103. else
  104. content_tag(:div, data: { component: name.to_s.camelcase, props: Oj.dump(props) }, &block)
  105. end
  106. end
  107. def body_classes
  108. output = (@body_classes || '').split(' ')
  109. output << "theme-#{current_theme.parameterize}"
  110. output << 'system-font' if current_account&.user&.setting_system_font_ui
  111. output << (current_account&.user&.setting_reduce_motion ? 'reduce-motion' : 'no-reduce-motion')
  112. output << 'rtl' if locale_direction == 'rtl'
  113. output.reject(&:blank?).join(' ')
  114. end
  115. def cdn_host
  116. Rails.configuration.action_controller.asset_host
  117. end
  118. def cdn_host?
  119. cdn_host.present?
  120. end
  121. def storage_host
  122. "https://#{ENV['S3_ALIAS_HOST'].presence || ENV['S3_CLOUDFRONT_HOST']}"
  123. end
  124. def storage_host?
  125. ENV['S3_ALIAS_HOST'].present? || ENV['S3_CLOUDFRONT_HOST'].present?
  126. end
  127. def quote_wrap(text, line_width: 80, break_sequence: "\n")
  128. text = word_wrap(text, line_width: line_width - 2, break_sequence: break_sequence)
  129. text.split("\n").map { |line| '> ' + line }.join("\n")
  130. end
  131. def render_initial_state
  132. state_params = {
  133. settings: {
  134. known_fediverse: Setting.show_known_fediverse_at_about_page,
  135. },
  136. text: [params[:title], params[:text], params[:url]].compact.join(' '),
  137. }
  138. permit_visibilities = %w(public unlisted private direct)
  139. default_privacy = current_account&.user&.setting_default_privacy
  140. permit_visibilities.shift(permit_visibilities.index(default_privacy) + 1) if default_privacy.present?
  141. state_params[:visibility] = params[:visibility] if permit_visibilities.include? params[:visibility]
  142. if user_signed_in?
  143. state_params[:settings] = state_params[:settings].merge(Web::Setting.find_by(user: current_user)&.data || {})
  144. state_params[:push_subscription] = current_account.user.web_push_subscription(current_session)
  145. state_params[:current_account] = current_account
  146. state_params[:token] = current_session.token
  147. state_params[:admin] = Account.find_local(Setting.site_contact_username.strip.gsub(/\A@/, ''))
  148. end
  149. json = ActiveModelSerializers::SerializableResource.new(InitialStatePresenter.new(state_params), serializer: InitialStateSerializer).to_json
  150. # rubocop:disable Rails/OutputSafety
  151. content_tag(:script, json_escape(json).html_safe, id: 'initial-state', type: 'application/json')
  152. # rubocop:enable Rails/OutputSafety
  153. end
  154. end