闭社主体 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.

157 lines
4.3 KiB

  1. # frozen_string_literal: true
  2. class AccountsController < ApplicationController
  3. PAGE_SIZE = 20
  4. include AccountControllerConcern
  5. include SignatureAuthentication
  6. before_action :set_cache_headers
  7. before_action :set_body_classes
  8. skip_around_action :set_locale, if: -> { request.format == :json }
  9. def show
  10. respond_to do |format|
  11. format.html do
  12. expires_in 0, public: true unless user_signed_in?
  13. @pinned_statuses = []
  14. @endorsed_accounts = @account.endorsed_accounts.to_a.sample(4)
  15. @featured_hashtags = @account.featured_tags.order(statuses_count: :desc)
  16. if current_account && @account.blocking?(current_account)
  17. @statuses = []
  18. return
  19. end
  20. @pinned_statuses = cache_collection(@account.pinned_statuses, Status) if show_pinned_statuses?
  21. @statuses = filtered_status_page(params)
  22. @statuses = cache_collection(@statuses, Status)
  23. @rss_url = rss_url
  24. unless @statuses.empty?
  25. @older_url = older_url if @statuses.last.id > filtered_statuses.last.id
  26. @newer_url = newer_url if @statuses.first.id < filtered_statuses.first.id
  27. end
  28. end
  29. format.rss do
  30. expires_in 0, public: true
  31. @statuses = filtered_statuses.without_reblogs.without_replies.limit(PAGE_SIZE)
  32. @statuses = cache_collection(@statuses, Status)
  33. render xml: RSS::AccountSerializer.render(@account, @statuses, params[:tag])
  34. end
  35. format.json do
  36. expires_in 3.minutes, public: !(authorized_fetch_mode? && signed_request_account.present?)
  37. render_with_cache json: @account, content_type: 'application/activity+json', serializer: ActivityPub::ActorSerializer, adapter: ActivityPub::Adapter, fields: restrict_fields_to
  38. end
  39. end
  40. end
  41. private
  42. def set_body_classes
  43. @body_classes = 'with-modals'
  44. end
  45. def show_pinned_statuses?
  46. [replies_requested?, media_requested?, tag_requested?, params[:max_id].present?, params[:min_id].present?].none?
  47. end
  48. def filtered_statuses
  49. default_statuses.tap do |statuses|
  50. statuses.merge!(hashtag_scope) if tag_requested?
  51. statuses.merge!(only_media_scope) if media_requested?
  52. statuses.merge!(no_replies_scope) unless replies_requested?
  53. end
  54. end
  55. def default_statuses
  56. @account.statuses.where(visibility: [:public, :unlisted])
  57. end
  58. def only_media_scope
  59. Status.where(id: account_media_status_ids)
  60. end
  61. def account_media_status_ids
  62. @account.media_attachments.attached.reorder(nil).select(:status_id).distinct
  63. end
  64. def no_replies_scope
  65. Status.without_replies
  66. end
  67. def hashtag_scope
  68. tag = Tag.find_normalized(params[:tag])
  69. if tag
  70. Status.tagged_with(tag.id)
  71. else
  72. Status.none
  73. end
  74. end
  75. def username_param
  76. params[:username]
  77. end
  78. def rss_url
  79. if tag_requested?
  80. short_account_tag_url(@account, params[:tag], format: 'rss')
  81. else
  82. short_account_url(@account, format: 'rss')
  83. end
  84. end
  85. def older_url
  86. pagination_url(max_id: @statuses.last.id)
  87. end
  88. def newer_url
  89. pagination_url(min_id: @statuses.first.id)
  90. end
  91. def pagination_url(max_id: nil, min_id: nil)
  92. if tag_requested?
  93. short_account_tag_url(@account, params[:tag], max_id: max_id, min_id: min_id)
  94. elsif media_requested?
  95. short_account_media_url(@account, max_id: max_id, min_id: min_id)
  96. elsif replies_requested?
  97. short_account_with_replies_url(@account, max_id: max_id, min_id: min_id)
  98. else
  99. short_account_url(@account, max_id: max_id, min_id: min_id)
  100. end
  101. end
  102. def media_requested?
  103. request.path.ends_with?('/media')
  104. end
  105. def replies_requested?
  106. request.path.ends_with?('/with_replies')
  107. end
  108. def tag_requested?
  109. request.path.split('.').first.ends_with?(Addressable::URI.parse("/tagged/#{params[:tag]}").normalize)
  110. end
  111. def filtered_status_page(params)
  112. if params[:min_id].present?
  113. filtered_statuses.paginate_by_min_id(PAGE_SIZE, params[:min_id]).reverse
  114. else
  115. filtered_statuses.paginate_by_max_id(PAGE_SIZE, params[:max_id], params[:since_id]).to_a
  116. end
  117. end
  118. def restrict_fields_to
  119. if signed_request_account.present? || public_fetch_mode?
  120. # Return all fields
  121. else
  122. %i(id type preferred_username inbox public_key endpoints)
  123. end
  124. end
  125. end