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

132 lines
3.5 KiB

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